Keep selected time slot and expand state when device is rotate
Bug: 187772493 Test: make SettingsRoboTests Change-Id: If70abf06825a88f0ad0add37ef8d4f46a0aad1e9
This commit is contained in:
@@ -18,6 +18,7 @@ package com.android.settings.fuelgauge;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
@@ -36,8 +37,9 @@ 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.OnCreate;
|
||||
import com.android.settingslib.core.lifecycle.events.OnDestroy;
|
||||
import com.android.settingslib.core.lifecycle.events.OnPause;
|
||||
import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState;
|
||||
import com.android.settingslib.utils.StringUtil;
|
||||
|
||||
import java.time.Clock;
|
||||
@@ -50,8 +52,9 @@ import java.util.Map;
|
||||
|
||||
/** Controls the update for chart graph and the list items. */
|
||||
public class BatteryChartPreferenceController extends AbstractPreferenceController
|
||||
implements PreferenceControllerMixin, LifecycleObserver, OnPause, OnDestroy,
|
||||
BatteryChartView.OnSelectListener, ExpandDividerPreference.OnExpandListener {
|
||||
implements PreferenceControllerMixin, LifecycleObserver, OnCreate, OnDestroy,
|
||||
OnSaveInstanceState, BatteryChartView.OnSelectListener,
|
||||
ExpandDividerPreference.OnExpandListener {
|
||||
private static final String TAG = "BatteryChartPreferenceController";
|
||||
/** Desired battery history size for timestamp slots. */
|
||||
public static final int DESIRED_HISTORY_SIZE = 25;
|
||||
@@ -60,6 +63,10 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
||||
private static final long VALID_USAGE_TIME_DURATION = DateUtils.HOUR_IN_MILLIS * 2;
|
||||
private static final long VALID_DIFF_DURATION = DateUtils.MINUTE_IN_MILLIS * 3;
|
||||
|
||||
// Keys for bundle instance to restore configurations.
|
||||
private static final String KEY_EXPAND_SYSTEM_INFO = "expand_system_info";
|
||||
private static final String KEY_CURRENT_TIME_SLOT = "current_time_slot";
|
||||
|
||||
@VisibleForTesting
|
||||
Map<Integer, List<BatteryDiffEntry>> mBatteryIndexedMap;
|
||||
|
||||
@@ -69,6 +76,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
||||
@VisibleForTesting BatteryChartView mBatteryChartView;
|
||||
@VisibleForTesting ExpandDividerPreference mExpandDividerPreference;
|
||||
|
||||
@VisibleForTesting boolean mIsExpanded = false;
|
||||
@VisibleForTesting int[] mBatteryHistoryLevels;
|
||||
@VisibleForTesting long[] mBatteryHistoryKeys;
|
||||
@VisibleForTesting int mTrapezoidIndex = BatteryChartView.SELECTED_INDEX_INVALID;
|
||||
@@ -79,8 +87,6 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
||||
private final Handler mHandler = new Handler(Looper.getMainLooper());
|
||||
private final CharSequence[] mNotAllowShowSummaryPackages;
|
||||
|
||||
private boolean mIsExpanded = false;
|
||||
|
||||
// Preference cache to avoid create new instance each time.
|
||||
@VisibleForTesting
|
||||
final Map<String, Preference> mPreferenceCache = new HashMap<>();
|
||||
@@ -103,7 +109,27 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
if (savedInstanceState == null) {
|
||||
return;
|
||||
}
|
||||
mTrapezoidIndex =
|
||||
savedInstanceState.getInt(KEY_CURRENT_TIME_SLOT, mTrapezoidIndex);
|
||||
mIsExpanded =
|
||||
savedInstanceState.getBoolean(KEY_EXPAND_SYSTEM_INFO, mIsExpanded);
|
||||
Log.d(TAG, String.format("onCreate() slotIndex=%d isExpanded=%b",
|
||||
mTrapezoidIndex, mIsExpanded));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle savedInstance) {
|
||||
if (savedInstance == null) {
|
||||
return;
|
||||
}
|
||||
savedInstance.putInt(KEY_CURRENT_TIME_SLOT, mTrapezoidIndex);
|
||||
savedInstance.putBoolean(KEY_EXPAND_SYSTEM_INFO, mIsExpanded);
|
||||
Log.d(TAG, String.format("onSaveInstanceState() slotIndex=%d isExpanded=%b",
|
||||
mTrapezoidIndex, mIsExpanded));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -234,7 +260,9 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
||||
}
|
||||
|
||||
void setBatteryChartView(final BatteryChartView batteryChartView) {
|
||||
mHandler.post(() -> setBatteryChartViewInner(batteryChartView));
|
||||
if (mBatteryChartView != batteryChartView) {
|
||||
mHandler.post(() -> setBatteryChartViewInner(batteryChartView));
|
||||
}
|
||||
}
|
||||
|
||||
private void setBatteryChartViewInner(final BatteryChartView batteryChartView) {
|
||||
@@ -250,6 +278,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
||||
: mTrapezoidIndex;
|
||||
if (mBatteryChartView != null) {
|
||||
mBatteryChartView.setLevels(mBatteryHistoryLevels);
|
||||
mBatteryChartView.setSelectedIndex(refreshIndex);
|
||||
setTimestampLabel();
|
||||
}
|
||||
refreshUi(refreshIndex, /*isForce=*/ true);
|
||||
@@ -310,6 +339,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
||||
if (mExpandDividerPreference == null) {
|
||||
mExpandDividerPreference = new ExpandDividerPreference(mPrefContext);
|
||||
mExpandDividerPreference.setOnExpandListener(this);
|
||||
mExpandDividerPreference.setIsExpanded(mIsExpanded);
|
||||
}
|
||||
mExpandDividerPreference.setOrder(
|
||||
mAppListPrefGroup.getPreferenceCount());
|
||||
|
@@ -30,6 +30,7 @@ import android.content.Context;
|
||||
import android.content.ContentValues;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.text.format.DateUtils;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
@@ -559,6 +560,27 @@ public final class BatteryChartPreferenceControllerTest {
|
||||
.setTimestamps(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnSaveInstanceState_restoreSelectedIndexAndExpandState() {
|
||||
final int expectedIndex = 1;
|
||||
final boolean isExpanded = true;
|
||||
final Bundle bundle = new Bundle();
|
||||
mBatteryChartPreferenceController.mTrapezoidIndex = expectedIndex;
|
||||
mBatteryChartPreferenceController.mIsExpanded = isExpanded;
|
||||
mBatteryChartPreferenceController.onSaveInstanceState(bundle);
|
||||
// Replaces the original controller with other values.
|
||||
mBatteryChartPreferenceController.mTrapezoidIndex = -1;
|
||||
mBatteryChartPreferenceController.mIsExpanded = false;
|
||||
|
||||
mBatteryChartPreferenceController.onCreate(bundle);
|
||||
mBatteryChartPreferenceController.setBatteryHistoryMap(
|
||||
createBatteryHistoryMap());
|
||||
|
||||
assertThat(mBatteryChartPreferenceController.mTrapezoidIndex)
|
||||
.isEqualTo(expectedIndex);
|
||||
assertThat(mBatteryChartPreferenceController.mIsExpanded).isTrue();
|
||||
}
|
||||
|
||||
private static Map<Long, Map<String, BatteryHistEntry>> createBatteryHistoryMap() {
|
||||
final Map<Long, Map<String, BatteryHistEntry>> batteryHistoryMap = new HashMap<>();
|
||||
for (int index = 0; index < DESIRED_HISTORY_SIZE; index++) {
|
||||
|
Reference in New Issue
Block a user