Add kill-switch to distinguish chart or non-chart design
Bug: 184807417 Bug: 180607918 Test: make SettingsRoboTests Test: make SettingsGoogleRoboTests Change-Id: Iacbb012209d552b58b774f90f5b0aeb60ce6d33d
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (C) 2021 The Android Open Source Project
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package com.android.settings.fuelgauge;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnDestroy;
|
||||
import com.android.settingslib.core.lifecycle.events.OnPause;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** Cotrols the update for chart graph and the list items. */
|
||||
public class BatteryChartPreferenceController extends AbstractPreferenceController
|
||||
implements PreferenceControllerMixin, LifecycleObserver, OnPause, OnDestroy {
|
||||
private static final String TAG = "BatteryChartPreferenceController";
|
||||
|
||||
private final String mPreferenceKey;
|
||||
|
||||
public BatteryChartPreferenceController(
|
||||
Context context, String chartPreferenceKey, String listPreferenceKey,
|
||||
Lifecycle lifecycle, SettingsActivity activity,
|
||||
InstrumentedPreferenceFragment fragment) {
|
||||
super(context);
|
||||
mPreferenceKey = listPreferenceKey;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return mPreferenceKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void refreshUi(Map<Long, List<BatteryHistEntry>> batteryHistoryMap) {
|
||||
Log.d(TAG, "refreshUi:" + batteryHistoryMap.size());
|
||||
}
|
||||
}
|
@@ -74,7 +74,9 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
|
||||
public BatteryChartView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
initializeColors(context);
|
||||
// Registers the click event listener.
|
||||
setOnClickListener(this);
|
||||
setClickable(false);
|
||||
setSelectedIndex(SELECTED_INDEX_ALL);
|
||||
setTrapezoidCount(DEFAULT_TRAPEZOID_COUNT);
|
||||
}
|
||||
|
48
src/com/android/settings/fuelgauge/BatteryHistoryLoader.java
Normal file
48
src/com/android/settings/fuelgauge/BatteryHistoryLoader.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2021 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.android.settings.fuelgauge;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settingslib.utils.AsyncLoaderCompat;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** Loader that can be used to load battery history information. */
|
||||
public class BatteryHistoryLoader
|
||||
extends AsyncLoaderCompat<Map<Long, List<BatteryHistEntry>>> {
|
||||
private static final String TAG = "BatteryHistoryLoader";
|
||||
|
||||
private final Context mContext;
|
||||
|
||||
public BatteryHistoryLoader(Context context) {
|
||||
super(context);
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDiscardResult(Map<Long, List<BatteryHistEntry>> result) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, List<BatteryHistEntry>> loadInBackground() {
|
||||
final PowerUsageFeatureProvider powerUsageFeatureProvider =
|
||||
FeatureFactory.getFactory(mContext).getPowerUsageFeatureProvider(mContext);
|
||||
return powerUsageFeatureProvider.getBatteryHistory(mContext);
|
||||
}
|
||||
}
|
@@ -20,8 +20,12 @@ import android.content.Context;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.Bundle;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.loader.app.LoaderManager;
|
||||
import androidx.loader.content.Loader;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
@@ -33,26 +37,38 @@ import com.android.settingslib.search.SearchIndexable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
|
||||
public class PowerUsageAdvanced extends PowerUsageBase {
|
||||
private static final String TAG = "AdvancedBatteryUsage";
|
||||
private static final String KEY_REFRESH_TYPE = "refresh_type";
|
||||
private static final String KEY_BATTERY_GRAPH = "battery_graph";
|
||||
private static final String KEY_APP_LIST = "app_list";
|
||||
private static final int LOADER_BATTERY_USAGE_STATS = 2;
|
||||
|
||||
@VisibleForTesting
|
||||
BatteryHistoryPreference mHistPref;
|
||||
@VisibleForTesting
|
||||
Map<Long, List<BatteryHistEntry>> mBatteryHistoryMap;
|
||||
@VisibleForTesting
|
||||
final BatteryHistoryLoaderCallbacks mBatteryHistoryLoaderCallbacks =
|
||||
new BatteryHistoryLoaderCallbacks();
|
||||
|
||||
private boolean mIsChartGraphEnabled = false;
|
||||
private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
|
||||
private BatteryChartPreferenceController mBatteryChartPreferenceController;
|
||||
private BatteryAppListPreferenceController mBatteryAppListPreferenceController;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
final Context context = getContext();
|
||||
|
||||
refreshFeatureFlag(getContext());
|
||||
mHistPref = (BatteryHistoryPreference) findPreference(KEY_BATTERY_GRAPH);
|
||||
mPowerUsageFeatureProvider = FeatureFactory.getFactory(context)
|
||||
.getPowerUsageFeatureProvider(context);
|
||||
// Removes chart graph preference if the chart design is disabled.
|
||||
if (!mIsChartGraphEnabled) {
|
||||
removePreference(KEY_BATTERY_GRAPH);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -80,12 +96,21 @@ public class PowerUsageAdvanced extends PowerUsageBase {
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
|
||||
refreshFeatureFlag(context);
|
||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||
|
||||
mBatteryAppListPreferenceController = new BatteryAppListPreferenceController(context,
|
||||
KEY_APP_LIST, getSettingsLifecycle(), (SettingsActivity) getActivity(), this);
|
||||
// Creates based on the chart design is enabled or not.
|
||||
if (mIsChartGraphEnabled) {
|
||||
mBatteryChartPreferenceController =
|
||||
new BatteryChartPreferenceController(context,
|
||||
KEY_BATTERY_GRAPH, KEY_APP_LIST,
|
||||
getSettingsLifecycle(), (SettingsActivity) getActivity(), this);
|
||||
controllers.add(mBatteryChartPreferenceController);
|
||||
} else {
|
||||
mBatteryAppListPreferenceController =
|
||||
new BatteryAppListPreferenceController(context, KEY_APP_LIST,
|
||||
getSettingsLifecycle(), (SettingsActivity) getActivity(), this);
|
||||
controllers.add(mBatteryAppListPreferenceController);
|
||||
|
||||
}
|
||||
return controllers;
|
||||
}
|
||||
|
||||
@@ -101,7 +126,34 @@ public class PowerUsageAdvanced extends PowerUsageBase {
|
||||
return;
|
||||
}
|
||||
updatePreference(mHistPref);
|
||||
mBatteryAppListPreferenceController.refreshAppListGroup(mBatteryUsageStats, true);
|
||||
if (mBatteryAppListPreferenceController != null && mBatteryUsageStats != null) {
|
||||
mBatteryAppListPreferenceController.refreshAppListGroup(
|
||||
mBatteryUsageStats, /* showAllApps */true);
|
||||
}
|
||||
if (mBatteryChartPreferenceController != null && mBatteryHistoryMap != null) {
|
||||
mBatteryChartPreferenceController.refreshUi(mBatteryHistoryMap);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void restartBatteryStatsLoader(int refreshType) {
|
||||
final Bundle bundle = new Bundle();
|
||||
bundle.putInt(KEY_REFRESH_TYPE, refreshType);
|
||||
// Uses customized battery history loader if chart design is enabled.
|
||||
if (mIsChartGraphEnabled) {
|
||||
getLoaderManager().restartLoader(LOADER_BATTERY_USAGE_STATS, bundle,
|
||||
mBatteryHistoryLoaderCallbacks);
|
||||
} else {
|
||||
super.restartBatteryStatsLoader(refreshType);
|
||||
}
|
||||
}
|
||||
|
||||
private void refreshFeatureFlag(Context context) {
|
||||
if (mPowerUsageFeatureProvider == null) {
|
||||
mPowerUsageFeatureProvider = FeatureFactory.getFactory(context)
|
||||
.getPowerUsageFeatureProvider(context);
|
||||
mIsChartGraphEnabled = mPowerUsageFeatureProvider.isChartGraphEnabled(context);
|
||||
}
|
||||
}
|
||||
|
||||
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
@@ -125,4 +177,27 @@ public class PowerUsageAdvanced extends PowerUsageBase {
|
||||
}
|
||||
};
|
||||
|
||||
private class BatteryHistoryLoaderCallbacks
|
||||
implements LoaderManager.LoaderCallbacks<Map<Long, List<BatteryHistEntry>>> {
|
||||
private int mRefreshType;
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Loader<Map<Long, List<BatteryHistEntry>>> onCreateLoader(int id, Bundle bundle) {
|
||||
mRefreshType = bundle.getInt(KEY_REFRESH_TYPE);
|
||||
return new BatteryHistoryLoader(getContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<Map<Long, List<BatteryHistEntry>>> loader,
|
||||
Map<Long, List<BatteryHistEntry>> batteryHistoryMap) {
|
||||
mBatteryHistoryMap = batteryHistoryMap;
|
||||
PowerUsageAdvanced.this.onLoadFinished(mRefreshType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<Map<Long, List<BatteryHistEntry>>> loader) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -36,9 +36,6 @@ import com.android.settings.dashboard.DashboardFragment;
|
||||
*/
|
||||
public abstract class PowerUsageBase extends DashboardFragment {
|
||||
|
||||
// +1 to allow ordering for PowerUsageSummary.
|
||||
@VisibleForTesting
|
||||
static final int MENU_STATS_REFRESH = Menu.FIRST + 1;
|
||||
private static final String TAG = "PowerUsageBase";
|
||||
private static final String KEY_REFRESH_TYPE = "refresh_type";
|
||||
private static final String KEY_INCLUDE_HISTORY = "include_history";
|
||||
@@ -49,8 +46,8 @@ public abstract class PowerUsageBase extends DashboardFragment {
|
||||
BatteryUsageStats mBatteryUsageStats;
|
||||
|
||||
protected UserManager mUm;
|
||||
private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
|
||||
protected boolean mIsBatteryPresent = true;
|
||||
private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
|
||||
|
||||
@VisibleForTesting
|
||||
final BatteryUsageStatsLoaderCallbacks mBatteryUsageStatsLoaderCallbacks =
|
||||
@@ -65,7 +62,6 @@ public abstract class PowerUsageBase extends DashboardFragment {
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
setHasOptionsMenu(true);
|
||||
|
||||
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(getContext());
|
||||
mBatteryBroadcastReceiver.setBatteryChangedListener(type -> {
|
||||
@@ -96,7 +92,7 @@ public abstract class PowerUsageBase extends DashboardFragment {
|
||||
mBatteryUsageStatsLoaderCallbacks);
|
||||
}
|
||||
|
||||
private void onLoadFinished(@BatteryUpdateType int refreshType) {
|
||||
protected void onLoadFinished(@BatteryUpdateType int refreshType) {
|
||||
refreshUi(refreshType);
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (C) 2021 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.android.settings.fuelgauge;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public final class BatteryHistoryLoaderTest {
|
||||
|
||||
private Context mContext;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
private BatteryHistoryLoader mBatteryHistoryLoader;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
mBatteryHistoryLoader = new BatteryHistoryLoader(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadIBackground_returnsMapFromPowerFeatureProvider() {
|
||||
final Map<Long, List<BatteryHistEntry>> batteryHistoryMap = new HashMap<>();
|
||||
doReturn(batteryHistoryMap).when(mFeatureFactory.powerUsageFeatureProvider)
|
||||
.getBatteryHistory(mContext);
|
||||
|
||||
assertThat(mBatteryHistoryLoader.loadInBackground())
|
||||
.isSameInstanceAs(batteryHistoryMap);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user