Refresh category title by different time slot condition

Bug: 177406865
Bug: 185187729
Test: make SettingsRoboTests
Test: make SettingsGoogleRoboTests
Change-Id: Ib124e1cfb9549f838540ff547bc616049f65180d
This commit is contained in:
ykhung
2021-04-24 14:29:25 +08:00
committed by YUKAI HUNG
parent 219dabcabf
commit 41fd8a7494
7 changed files with 196 additions and 12 deletions

View File

@@ -65,6 +65,8 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
@VisibleForTesting PreferenceGroup mAppListPrefGroup;
@VisibleForTesting BatteryChartView mBatteryChartView;
@VisibleForTesting ExpandDividerPreference mExpandDividerPreference;
@VisibleForTesting CategoryTitleType mCategoryTitleType =
CategoryTitleType.TYPE_UNKNOWN;
@VisibleForTesting int[] mBatteryHistoryLevels;
@VisibleForTesting long[] mBatteryHistoryKeys;
@@ -84,6 +86,14 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
@VisibleForTesting
final List<BatteryDiffEntry> mSystemEntries = new ArrayList<>();
/** Which component data will be shown in the screen. */
enum CategoryTitleType {
TYPE_UNKNOWN,
TYPE_APP_COMPONENT,
TYPE_SYSTEM_COMPONENT,
TYPE_ALL_COMPONENTS
}
public BatteryChartPreferenceController(
Context context, String preferenceKey,
Lifecycle lifecycle, SettingsActivity activity,
@@ -263,11 +273,13 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
mHandler.post(() -> {
removeAndCacheAllPrefs();
addAllPreferences();
refreshCategoryTitle();
});
return true;
}
private void addAllPreferences() {
mCategoryTitleType = CategoryTitleType.TYPE_UNKNOWN;
final List<BatteryDiffEntry> entries =
mBatteryIndexedMap.get(Integer.valueOf(mTrapezoidIndex));
if (entries == null) {
@@ -296,6 +308,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
// Adds app entries to the list if it is not empty.
if (!appEntries.isEmpty()) {
addPreferenceToScreen(appEntries);
mCategoryTitleType = CategoryTitleType.TYPE_APP_COMPONENT;
}
// Adds the expabable divider if we have two sections data.
if (!appEntries.isEmpty() && !mSystemEntries.isEmpty()) {
@@ -306,6 +319,9 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
mExpandDividerPreference.setOrder(
mAppListPrefGroup.getPreferenceCount());
mAppListPrefGroup.addPreference(mExpandDividerPreference);
mCategoryTitleType = CategoryTitleType.TYPE_ALL_COMPONENTS;
} else if (appEntries.isEmpty() && !mSystemEntries.isEmpty()) {
mCategoryTitleType = CategoryTitleType.TYPE_SYSTEM_COMPONENT;
}
refreshExpandUi();
}
@@ -385,6 +401,68 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
}
}
@VisibleForTesting
void refreshCategoryTitle() {
final String slotInformation = getSlotInformation();
Log.d(TAG, String.format("refreshCategoryTitle:%s slotInfo:%s",
mCategoryTitleType, slotInformation));
refreshPreferenceCategoryTitle(slotInformation);
refreshExpandableDividerTitle(slotInformation);
}
private void refreshExpandableDividerTitle(String slotInformation) {
if (mExpandDividerPreference == null) {
return;
}
mExpandDividerPreference.setTitle(
mCategoryTitleType == CategoryTitleType.TYPE_ALL_COMPONENTS
? getSlotInformation(/*isApp=*/ false, slotInformation)
: null);
}
private void refreshPreferenceCategoryTitle(String slotInformation) {
if (mAppListPrefGroup == null) {
return;
}
switch (mCategoryTitleType) {
case TYPE_APP_COMPONENT:
case TYPE_ALL_COMPONENTS:
mAppListPrefGroup.setTitle(
getSlotInformation(/*isApp=*/ true, slotInformation));
break;
case TYPE_SYSTEM_COMPONENT:
mAppListPrefGroup.setTitle(
getSlotInformation(/*isApp=*/ false, slotInformation));
break;
default:
mAppListPrefGroup.setTitle(R.string.battery_app_usage_for_past_24);
}
}
private String getSlotInformation(boolean isApp, String slotInformation) {
// Null means we show all information without a specific time slot.
if (slotInformation == null) {
return isApp
? mPrefContext.getString(R.string.battery_app_usage_for_past_24)
: mPrefContext.getString(R.string.battery_system_usage_for_past_24);
} else {
return isApp
? mPrefContext.getString(R.string.battery_app_usage_for, slotInformation)
: mPrefContext.getString(R.string.battery_system_usage_for ,slotInformation);
}
}
private String getSlotInformation() {
if (mTrapezoidIndex < 0) {
return null;
}
final String fromHour = ConvertUtils.utcToLocalTimeHour(
mBatteryHistoryKeys[mTrapezoidIndex * 2]);
final String toHour = ConvertUtils.utcToLocalTimeHour(
mBatteryHistoryKeys[(mTrapezoidIndex + 1) * 2]);
return String.format("%s-%s", fromHour, toHour);
}
@VisibleForTesting
void setPreferenceSummary(
PowerGaugePreference preference, BatteryDiffEntry entry) {