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:
ykhung
2021-04-09 11:43:22 +08:00
committed by YUKAI HUNG
parent 57f27f7a30
commit cdd73e7fec
6 changed files with 284 additions and 16 deletions

View File

@@ -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());
}
}

View File

@@ -74,7 +74,9 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
public BatteryChartView(Context context, AttributeSet attrs) { public BatteryChartView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
initializeColors(context); initializeColors(context);
// Registers the click event listener.
setOnClickListener(this); setOnClickListener(this);
setClickable(false);
setSelectedIndex(SELECTED_INDEX_ALL); setSelectedIndex(SELECTED_INDEX_ALL);
setTrapezoidCount(DEFAULT_TRAPEZOID_COUNT); setTrapezoidCount(DEFAULT_TRAPEZOID_COUNT);
} }

View 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);
}
}

View File

@@ -20,8 +20,12 @@ import android.content.Context;
import android.os.BatteryManager; import android.os.BatteryManager;
import android.os.Bundle; import android.os.Bundle;
import android.provider.SearchIndexableResource; import android.provider.SearchIndexableResource;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsActivity; import com.android.settings.SettingsActivity;
@@ -33,26 +37,38 @@ import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC) @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class PowerUsageAdvanced extends PowerUsageBase { public class PowerUsageAdvanced extends PowerUsageBase {
private static final String TAG = "AdvancedBatteryUsage"; 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_BATTERY_GRAPH = "battery_graph";
private static final String KEY_APP_LIST = "app_list"; private static final String KEY_APP_LIST = "app_list";
private static final int LOADER_BATTERY_USAGE_STATS = 2;
@VisibleForTesting @VisibleForTesting
BatteryHistoryPreference mHistPref; BatteryHistoryPreference mHistPref;
@VisibleForTesting
Map<Long, List<BatteryHistEntry>> mBatteryHistoryMap;
@VisibleForTesting
final BatteryHistoryLoaderCallbacks mBatteryHistoryLoaderCallbacks =
new BatteryHistoryLoaderCallbacks();
private boolean mIsChartGraphEnabled = false;
private PowerUsageFeatureProvider mPowerUsageFeatureProvider; private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
private BatteryChartPreferenceController mBatteryChartPreferenceController;
private BatteryAppListPreferenceController mBatteryAppListPreferenceController; private BatteryAppListPreferenceController mBatteryAppListPreferenceController;
@Override @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
final Context context = getContext(); refreshFeatureFlag(getContext());
mHistPref = (BatteryHistoryPreference) findPreference(KEY_BATTERY_GRAPH); mHistPref = (BatteryHistoryPreference) findPreference(KEY_BATTERY_GRAPH);
mPowerUsageFeatureProvider = FeatureFactory.getFactory(context) // Removes chart graph preference if the chart design is disabled.
.getPowerUsageFeatureProvider(context); if (!mIsChartGraphEnabled) {
removePreference(KEY_BATTERY_GRAPH);
}
} }
@Override @Override
@@ -80,12 +96,21 @@ public class PowerUsageAdvanced extends PowerUsageBase {
@Override @Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
refreshFeatureFlag(context);
final List<AbstractPreferenceController> controllers = new ArrayList<>(); final List<AbstractPreferenceController> controllers = new ArrayList<>();
// Creates based on the chart design is enabled or not.
mBatteryAppListPreferenceController = new BatteryAppListPreferenceController(context, if (mIsChartGraphEnabled) {
KEY_APP_LIST, getSettingsLifecycle(), (SettingsActivity) getActivity(), this); mBatteryChartPreferenceController =
controllers.add(mBatteryAppListPreferenceController); 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; return controllers;
} }
@@ -101,7 +126,34 @@ public class PowerUsageAdvanced extends PowerUsageBase {
return; return;
} }
updatePreference(mHistPref); 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 = 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) {
}
}
} }

View File

@@ -36,9 +36,6 @@ import com.android.settings.dashboard.DashboardFragment;
*/ */
public abstract class PowerUsageBase extends 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 TAG = "PowerUsageBase";
private static final String KEY_REFRESH_TYPE = "refresh_type"; private static final String KEY_REFRESH_TYPE = "refresh_type";
private static final String KEY_INCLUDE_HISTORY = "include_history"; private static final String KEY_INCLUDE_HISTORY = "include_history";
@@ -49,8 +46,8 @@ public abstract class PowerUsageBase extends DashboardFragment {
BatteryUsageStats mBatteryUsageStats; BatteryUsageStats mBatteryUsageStats;
protected UserManager mUm; protected UserManager mUm;
private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
protected boolean mIsBatteryPresent = true; protected boolean mIsBatteryPresent = true;
private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
@VisibleForTesting @VisibleForTesting
final BatteryUsageStatsLoaderCallbacks mBatteryUsageStatsLoaderCallbacks = final BatteryUsageStatsLoaderCallbacks mBatteryUsageStatsLoaderCallbacks =
@@ -65,7 +62,6 @@ public abstract class PowerUsageBase extends DashboardFragment {
@Override @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
setHasOptionsMenu(true);
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(getContext()); mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(getContext());
mBatteryBroadcastReceiver.setBatteryChangedListener(type -> { mBatteryBroadcastReceiver.setBatteryChangedListener(type -> {
@@ -96,7 +92,7 @@ public abstract class PowerUsageBase extends DashboardFragment {
mBatteryUsageStatsLoaderCallbacks); mBatteryUsageStatsLoaderCallbacks);
} }
private void onLoadFinished(@BatteryUpdateType int refreshType) { protected void onLoadFinished(@BatteryUpdateType int refreshType) {
refreshUi(refreshType); refreshUi(refreshType);
} }

View File

@@ -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);
}
}