|
|
|
|
@@ -18,6 +18,10 @@
|
|
|
|
|
package com.android.settings.fuelgauge;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.os.Looper;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.VisibleForTesting;
|
|
|
|
|
@@ -38,6 +42,7 @@ import com.android.settingslib.core.lifecycle.events.OnPause;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@@ -63,6 +68,11 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|
|
|
|
private final String mPreferenceKey;
|
|
|
|
|
private final SettingsActivity mActivity;
|
|
|
|
|
private final InstrumentedPreferenceFragment mFragment;
|
|
|
|
|
private final Handler mHandler = new Handler(Looper.getMainLooper());
|
|
|
|
|
|
|
|
|
|
// Preference cache to avoid create new instance each time.
|
|
|
|
|
@VisibleForTesting
|
|
|
|
|
final Map<String, Preference> mPreferenceCache = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
public BatteryChartPreferenceController(
|
|
|
|
|
Context context, String preferenceKey,
|
|
|
|
|
@@ -86,6 +96,8 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|
|
|
|
if (mActivity.isChangingConfigurations()) {
|
|
|
|
|
BatteryDiffEntry.clearCache();
|
|
|
|
|
}
|
|
|
|
|
mHandler.removeCallbacksAndMessages(/*token=*/ null);
|
|
|
|
|
mPreferenceCache.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@@ -93,6 +105,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|
|
|
|
super.displayPreference(screen);
|
|
|
|
|
mPrefContext = screen.getContext();
|
|
|
|
|
mAppListPrefGroup = screen.findPreference(mPreferenceKey);
|
|
|
|
|
mAppListPrefGroup.setOrderingAsAdded(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@@ -116,7 +129,13 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|
|
|
|
refreshUi(trapezoidIndex, /*isForce=*/ false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setBatteryHistoryMap(Map<Long, List<BatteryHistEntry>> batteryHistoryMap) {
|
|
|
|
|
void setBatteryHistoryMap(
|
|
|
|
|
final Map<Long, List<BatteryHistEntry>> batteryHistoryMap) {
|
|
|
|
|
mHandler.post(() -> setBatteryHistoryMapInner(batteryHistoryMap));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setBatteryHistoryMapInner(
|
|
|
|
|
final Map<Long, List<BatteryHistEntry>> batteryHistoryMap) {
|
|
|
|
|
// Resets all battery history data relative variables.
|
|
|
|
|
if (batteryHistoryMap == null) {
|
|
|
|
|
mBatteryIndexedMap = null;
|
|
|
|
|
@@ -163,7 +182,11 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|
|
|
|
Arrays.toString(mBatteryHistoryLevels)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setBatteryChartView(BatteryChartView batteryChartView) {
|
|
|
|
|
void setBatteryChartView(final BatteryChartView batteryChartView) {
|
|
|
|
|
mHandler.post(() -> setBatteryChartViewInner(batteryChartView));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setBatteryChartViewInner(final BatteryChartView batteryChartView) {
|
|
|
|
|
mBatteryChartView = batteryChartView;
|
|
|
|
|
mBatteryChartView.setOnSelectListener(this);
|
|
|
|
|
forceRefreshUi();
|
|
|
|
|
@@ -174,6 +197,9 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|
|
|
|
mTrapezoidIndex == BatteryChartView.SELECTED_INDEX_INVALID
|
|
|
|
|
? BatteryChartView.SELECTED_INDEX_ALL
|
|
|
|
|
: mTrapezoidIndex;
|
|
|
|
|
if (mBatteryChartView != null) {
|
|
|
|
|
mBatteryChartView.setLevels(mBatteryHistoryLevels);
|
|
|
|
|
}
|
|
|
|
|
refreshUi(refreshIndex, /*isForce=*/ true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -185,12 +211,89 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|
|
|
|
|| (mTrapezoidIndex == trapezoidIndex && !isForce)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
mTrapezoidIndex = trapezoidIndex;
|
|
|
|
|
Log.d(TAG, String.format("refreshUi: index=%d batteryIndexedMap.size=%d",
|
|
|
|
|
mTrapezoidIndex, mBatteryIndexedMap.size()));
|
|
|
|
|
|
|
|
|
|
mTrapezoidIndex = trapezoidIndex;
|
|
|
|
|
mHandler.post(() -> {
|
|
|
|
|
removeAndCacheAllPrefs();
|
|
|
|
|
addAllPreferences();
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addAllPreferences() {
|
|
|
|
|
final List<BatteryDiffEntry> entries =
|
|
|
|
|
mBatteryIndexedMap.get(Integer.valueOf(mTrapezoidIndex));
|
|
|
|
|
if (entries == null) {
|
|
|
|
|
Log.w(TAG, "cannot find BatteryDiffEntry for:" + mTrapezoidIndex);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Separates data into two groups and sort them individually.
|
|
|
|
|
final List<BatteryDiffEntry> appEntries = new ArrayList<>();
|
|
|
|
|
final List<BatteryDiffEntry> systemEntries = new ArrayList<>();
|
|
|
|
|
entries.forEach(entry -> {
|
|
|
|
|
if (entry.isSystemEntry()) {
|
|
|
|
|
systemEntries.add(entry);
|
|
|
|
|
} else {
|
|
|
|
|
appEntries.add(entry);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
Collections.sort(appEntries, BatteryDiffEntry.COMPARATOR);
|
|
|
|
|
Collections.sort(systemEntries, BatteryDiffEntry.COMPARATOR);
|
|
|
|
|
Log.d(TAG, String.format("addAllPreferences() app=%d system=%d",
|
|
|
|
|
appEntries.size(), systemEntries.size()));
|
|
|
|
|
addPreferenceToScreen(appEntries);
|
|
|
|
|
addPreferenceToScreen(systemEntries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@VisibleForTesting
|
|
|
|
|
void addPreferenceToScreen(List<BatteryDiffEntry> entries) {
|
|
|
|
|
if (mAppListPrefGroup == null || entries.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int prefIndex = mAppListPrefGroup.getPreferenceCount();
|
|
|
|
|
for (BatteryDiffEntry entry : entries) {
|
|
|
|
|
final String appLabel = entry.getAppLabel();
|
|
|
|
|
final Drawable appIcon = entry.getAppIcon();
|
|
|
|
|
if (TextUtils.isEmpty(appLabel) || appIcon == null) {
|
|
|
|
|
Log.w(TAG, "cannot find app resource:" + entry.mBatteryHistEntry);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
final String prefKey = entry.mBatteryHistEntry.getKey();
|
|
|
|
|
PowerGaugePreference pref =
|
|
|
|
|
(PowerGaugePreference) mPreferenceCache.get(prefKey);
|
|
|
|
|
// Creates new innstance if cached preference is not found.
|
|
|
|
|
if (pref == null) {
|
|
|
|
|
pref = new PowerGaugePreference(mPrefContext);
|
|
|
|
|
pref.setKey(prefKey);
|
|
|
|
|
mPreferenceCache.put(prefKey, pref);
|
|
|
|
|
}
|
|
|
|
|
pref.setIcon(appIcon);
|
|
|
|
|
pref.setTitle(appLabel);
|
|
|
|
|
pref.setOrder(prefIndex);
|
|
|
|
|
pref.setPercent(entry.getPercentOfTotal());
|
|
|
|
|
mAppListPrefGroup.addPreference(pref);
|
|
|
|
|
prefIndex++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void removeAndCacheAllPrefs() {
|
|
|
|
|
if (mAppListPrefGroup == null
|
|
|
|
|
|| mAppListPrefGroup.getPreferenceCount() == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
final int prefsCount = mAppListPrefGroup.getPreferenceCount();
|
|
|
|
|
for (int index = 0; index < prefsCount; index++) {
|
|
|
|
|
final Preference pref = mAppListPrefGroup.getPreference(index);
|
|
|
|
|
if (TextUtils.isEmpty(pref.getKey())) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
mPreferenceCache.put(pref.getKey(), pref);
|
|
|
|
|
}
|
|
|
|
|
mAppListPrefGroup.removeAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String utcToLocalTime(long[] timestamps) {
|
|
|
|
|
final StringBuilder builder = new StringBuilder();
|
|
|
|
|
for (int index = 0; index < timestamps.length; index++) {
|
|
|
|
|
|