Merge "Update the selected period message in battery chart"
This commit is contained in:
committed by
Android (Google) Code Review
commit
85b03538d0
@@ -106,6 +106,10 @@ public class BatteryChartPreferenceControllerV2 extends AbstractPreferenceContro
|
||||
private boolean mIsFooterPrefAdded = false;
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
private FooterPreference mFooterPreference;
|
||||
// Daily view model only saves abbreviated day of week texts (e.g. MON). This field saves the
|
||||
// full day of week texts (e.g. Monday), which is used in category title and battery detail
|
||||
// page.
|
||||
private List<String> mDailyTimestampFullTexts;
|
||||
private BatteryChartViewModel mDailyViewModel;
|
||||
private List<BatteryChartViewModel> mHourlyViewModels;
|
||||
|
||||
@@ -266,15 +270,20 @@ public class BatteryChartPreferenceControllerV2 extends AbstractPreferenceContro
|
||||
});
|
||||
Log.d(TAG, "getBatteryLevelData: " + batteryLevelData);
|
||||
if (batteryLevelData == null) {
|
||||
mDailyTimestampFullTexts = null;
|
||||
mDailyViewModel = null;
|
||||
mHourlyViewModels = null;
|
||||
addFooterPreferenceIfNeeded(false);
|
||||
return;
|
||||
}
|
||||
mDailyTimestampFullTexts = generateTimestampDayOfWeekTexts(
|
||||
mContext, batteryLevelData.getDailyBatteryLevels().getTimestamps(),
|
||||
/* isAbbreviation= */ false);
|
||||
mDailyViewModel = new BatteryChartViewModel(
|
||||
batteryLevelData.getDailyBatteryLevels().getLevels(),
|
||||
generateTimestampDayOfWeekTexts(
|
||||
mContext, batteryLevelData.getDailyBatteryLevels().getTimestamps()),
|
||||
mContext, batteryLevelData.getDailyBatteryLevels().getTimestamps(),
|
||||
/* isAbbreviation= */ true),
|
||||
mDailyChartIndex,
|
||||
BatteryChartViewModel.AxisLabelPosition.CENTER_OF_TRAPEZOIDS);
|
||||
mHourlyViewModels = new ArrayList<>();
|
||||
@@ -506,9 +515,33 @@ public class BatteryChartPreferenceControllerV2 extends AbstractPreferenceContro
|
||||
}
|
||||
}
|
||||
|
||||
private String getSlotInformation() {
|
||||
// TODO: Generate the right slot information from daily and hourly chart selection.
|
||||
return null;
|
||||
@VisibleForTesting
|
||||
String getSlotInformation() {
|
||||
if (mDailyTimestampFullTexts == null || mDailyViewModel == null
|
||||
|| mHourlyViewModels == null) {
|
||||
// No data
|
||||
return null;
|
||||
}
|
||||
if (isAllSelected()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final String selectedDayText = mDailyTimestampFullTexts.get(mDailyChartIndex);
|
||||
if (mHourlyChartIndex == BatteryChartViewModel.SELECTED_INDEX_ALL) {
|
||||
return selectedDayText;
|
||||
}
|
||||
|
||||
final String fromHourText = mHourlyViewModels.get(mDailyChartIndex).texts().get(
|
||||
mHourlyChartIndex);
|
||||
final String toHourText = mHourlyViewModels.get(mDailyChartIndex).texts().get(
|
||||
mHourlyChartIndex + 1);
|
||||
final String selectedHourText =
|
||||
String.format("%s%s%s", fromHourText, mIs24HourFormat ? "-" : " - ", toHourText);
|
||||
if (isBatteryLevelDataInOneDay()) {
|
||||
return selectedHourText;
|
||||
}
|
||||
|
||||
return String.format("%s %s", selectedDayText, selectedHourText);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
@@ -578,14 +611,20 @@ public class BatteryChartPreferenceControllerV2 extends AbstractPreferenceContro
|
||||
}
|
||||
|
||||
private boolean isBatteryLevelDataInOneDay() {
|
||||
return mHourlyViewModels.size() == 1;
|
||||
return mHourlyViewModels != null && mHourlyViewModels.size() == 1;
|
||||
}
|
||||
|
||||
private static List<String> generateTimestampDayOfWeekTexts(
|
||||
@NonNull final Context context, @NonNull final List<Long> timestamps) {
|
||||
private boolean isAllSelected() {
|
||||
return (isBatteryLevelDataInOneDay()
|
||||
|| mDailyChartIndex == BatteryChartViewModel.SELECTED_INDEX_ALL)
|
||||
&& mHourlyChartIndex == BatteryChartViewModel.SELECTED_INDEX_ALL;
|
||||
}
|
||||
|
||||
private static List<String> generateTimestampDayOfWeekTexts(@NonNull final Context context,
|
||||
@NonNull final List<Long> timestamps, final boolean isAbbreviation) {
|
||||
final ArrayList<String> texts = new ArrayList<>();
|
||||
for (Long timestamp : timestamps) {
|
||||
texts.add(ConvertUtils.utcToLocalTimeDayOfWeek(context, timestamp));
|
||||
texts.add(ConvertUtils.utcToLocalTimeDayOfWeek(context, timestamp, isAbbreviation));
|
||||
}
|
||||
return texts;
|
||||
}
|
||||
|
@@ -150,10 +150,12 @@ public final class ConvertUtils {
|
||||
}
|
||||
|
||||
/** Converts UTC timestamp to local time day of week data. */
|
||||
public static String utcToLocalTimeDayOfWeek(final Context context, final long timestamp) {
|
||||
public static String utcToLocalTimeDayOfWeek(
|
||||
final Context context, final long timestamp, final boolean isAbbreviation) {
|
||||
final Locale locale = getLocale(context);
|
||||
final String pattern = DateFormat.getBestDateTimePattern(locale, "E");
|
||||
return DateFormat.format(pattern, timestamp).toString().toUpperCase(locale);
|
||||
final String pattern = DateFormat.getBestDateTimePattern(locale,
|
||||
isAbbreviation ? "E" : "EEEE");
|
||||
return DateFormat.format(pattern, timestamp).toString();
|
||||
}
|
||||
|
||||
/** Gets indexed battery usage data for each corresponding time slot. */
|
||||
|
Reference in New Issue
Block a user