Display the chart levels data into BatteryHistoryPreference

Bug: 184807417
Bug: 180607918
Test: make SettingsRoboTests
Test: make SettingsGoogleRoboTests
Change-Id: I78718a59671ca6775abc725432fbfbec6c0993fe
This commit is contained in:
ykhung
2021-04-09 17:40:40 +08:00
committed by YUKAI HUNG
parent 9b226a4ff0
commit 740ac9a047
5 changed files with 136 additions and 15 deletions

View File

@@ -20,6 +20,7 @@ package com.android.settings.fuelgauge;
import android.content.Context;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
@@ -34,25 +35,49 @@ 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.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/** Cotrols the update for chart graph and the list items. */
/** Controls the update for chart graph and the list items. */
public class BatteryChartPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin, LifecycleObserver, OnPause, OnDestroy {
implements PreferenceControllerMixin, LifecycleObserver, OnPause, OnDestroy,
BatteryChartView.OnSelectListener {
private static final String TAG = "BatteryChartPreferenceController";
private static final int CHART_KEY_ARRAY_SIZE = 25;
private static final int CHART_LEVEL_ARRAY_SIZE = 13;
@VisibleForTesting
PreferenceGroup mAppListGroup;
private Context mPrefContext;
private BatteryChartView mBatteryChartView;
// Battery history relative data.
private int[] mBatteryHistoryLevels;
private long[] mBatteryHistoryKeys;
private Map<Long, List<BatteryHistEntry>> mBatteryHistoryMap;
private int mTrapezoidIndex = BatteryChartView.SELECTED_INDEX_INVALID;
private final String mPreferenceKey;
private final SettingsActivity mActivity;
private final InstrumentedPreferenceFragment mFragment;
public BatteryChartPreferenceController(
Context context, String chartPreferenceKey, String listPreferenceKey,
Context context, String preferenceKey,
Lifecycle lifecycle, SettingsActivity activity,
InstrumentedPreferenceFragment fragment) {
super(context);
mPreferenceKey = listPreferenceKey;
mActivity = activity;
mFragment = fragment;
mPreferenceKey = preferenceKey;
if (lifecycle != null) {
lifecycle.addObserver(this);
}
}
@Override
public void onPause() {
}
@@ -63,6 +88,9 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPrefContext = screen.getContext();
mAppListGroup = screen.findPreference(mPreferenceKey);
}
@Override
@@ -80,7 +108,73 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
return false;
}
void refreshUi(Map<Long, List<BatteryHistEntry>> batteryHistoryMap) {
Log.d(TAG, "refreshUi:" + batteryHistoryMap.size());
@Override
public void onSelect(int trapezoidIndex) {
Log.d(TAG, "onSelect:" + trapezoidIndex);
refreshUi(trapezoidIndex);
}
void setBatteryHistoryMap(Map<Long, List<BatteryHistEntry>> batteryHistoryMap) {
// Assumes all timestamp data is consecutive and aligns to hourly time slot.
mBatteryHistoryMap = batteryHistoryMap;
// Generates battery history keys.
final List<Long> batteryHistoryKeyList =
new ArrayList<Long>(mBatteryHistoryMap.keySet());
// Sorts all timestamp keys ordered by ASC from the map keys.
Collections.sort(batteryHistoryKeyList);
mBatteryHistoryKeys = new long[CHART_KEY_ARRAY_SIZE];
final int elementSize = Math.min(
batteryHistoryKeyList.size(), CHART_KEY_ARRAY_SIZE);
final int offset = CHART_KEY_ARRAY_SIZE - elementSize;
for (int index = 0; index < elementSize; index++) {
mBatteryHistoryKeys[index + offset] = batteryHistoryKeyList.get(index);
}
// Generates the battery history levels.
mBatteryHistoryLevels = new int[CHART_LEVEL_ARRAY_SIZE];
for (int index = 0; index < CHART_LEVEL_ARRAY_SIZE; index++) {
final Long timestamp = Long.valueOf(mBatteryHistoryKeys[index * 2]);
final List<BatteryHistEntry> entryList = mBatteryHistoryMap.get(timestamp);
if (entryList != null && !entryList.isEmpty()) {
// All battery levels are the same in the same timestamp snapshot.
mBatteryHistoryLevels[index] = entryList.get(0).mBatteryLevel;
} else {
Log.w(TAG, "abnormal entry list in the timestamp:" + timestamp);
}
}
if (mBatteryChartView != null) {
mBatteryChartView.setLevels(mBatteryHistoryLevels);
}
Log.d(TAG, String.format("setBatteryHistoryMap() size=%d\nkeys=%s\nlevels=%s",
batteryHistoryKeyList.size(),
utcToLocalTime(mBatteryHistoryKeys),
Arrays.toString(mBatteryHistoryLevels)));
}
void setBatteryChartView(BatteryChartView batteryChartView) {
mBatteryChartView = batteryChartView;
mBatteryChartView.setOnSelectListener(this);
if (mBatteryHistoryLevels != null) {
mBatteryChartView.setLevels(mBatteryHistoryLevels);
}
}
private void refreshUi(int trapezoidIndex) {
// Invalid refresh condition.
if (mBatteryHistoryMap == null || mBatteryChartView == null ||
mTrapezoidIndex == trapezoidIndex) {
return;
}
mTrapezoidIndex = trapezoidIndex;
Log.d(TAG, String.format("refreshUi: index=%d size=%d",
mTrapezoidIndex, mBatteryHistoryMap.size()));
}
private static String utcToLocalTime(long[] timestamps) {
final StringBuilder builder = new StringBuilder();
for (int index = 0; index < timestamps.length; index++) {
builder.append(String.format("%s| ",
ConvertUtils.utcToLocalTime(timestamps[index])));
}
return builder.toString();
}
}