Merge "[QPR1] Add time unit into battery usage chart and refine some UI" into sc-qpr1-dev

This commit is contained in:
Android Build Prod User
2021-08-26 21:42:36 +00:00
committed by Android (Google) Code Review
8 changed files with 31 additions and 178 deletions

View File

@@ -57,7 +57,8 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
Arrays.asList("SwitchAccessService", "TalkBackService", "JustSpeakService");
private static final int DEFAULT_TRAPEZOID_COUNT = 12;
private static final int DEFAULT_TIMESTAMP_COUNT = 5;
private static final int DEFAULT_TIMESTAMP_COUNT = 4;
private static final int TIMESTAMP_GAPS_COUNT = DEFAULT_TIMESTAMP_COUNT - 1;
private static final int DIVIDER_COLOR = Color.parseColor("#CDCCC5");
private static final long UPDATE_STATE_DELAYED_TIME = 500L;
@@ -91,7 +92,7 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
new Rect[] {new Rect(), new Rect(), new Rect()};
// For drawing the timestamp information.
private final Rect[] mTimestampsBounds =
new Rect[] {new Rect(), new Rect(), new Rect(), new Rect(), new Rect()};
new Rect[] {new Rect(), new Rect(), new Rect(), new Rect()};
@VisibleForTesting
Handler mHandler = new Handler();
@@ -198,13 +199,14 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
if (mTimestamps == null) {
mTimestamps = new String[DEFAULT_TIMESTAMP_COUNT];
}
final long timeSlotOffset = DateUtils.HOUR_IN_MILLIS * 6;
final long timeSlotOffset =
DateUtils.HOUR_IN_MILLIS * (/*total 24 hours*/ 24 / TIMESTAMP_GAPS_COUNT);
final boolean is24HourFormat = DateFormat.is24HourFormat(getContext());
for (int index = 0; index < DEFAULT_TIMESTAMP_COUNT; index++) {
mTimestamps[index] =
ConvertUtils.utcToLocalTimeHour(
getContext(),
latestTimestamp - (4 - index) * timeSlotOffset,
latestTimestamp - (TIMESTAMP_GAPS_COUNT - index) * timeSlotOffset,
is24HourFormat);
}
requestLayout();
@@ -426,8 +428,9 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
final float[] xOffsets = new float[DEFAULT_TIMESTAMP_COUNT];
final float baselineX = mDividerWidth * .5f;
final float offsetX = mDividerWidth + unitWidth;
final int slotBarOffset = (/*total 12 bars*/ 12) / TIMESTAMP_GAPS_COUNT;
for (int index = 0; index < DEFAULT_TIMESTAMP_COUNT; index++) {
xOffsets[index] = baselineX + index * offsetX * 3;
xOffsets[index] = baselineX + index * offsetX * slotBarOffset;
}
drawTimestamp(canvas, xOffsets);
}
@@ -439,13 +442,15 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
mTimestamps[0],
xOffsets[0] - mTimestampsBounds[0].left,
getTimestampY(0), mTextPaint);
final int latestIndex = DEFAULT_TIMESTAMP_COUNT - 1;
// Draws the last timestamp info.
canvas.drawText(
mTimestamps[4],
xOffsets[4] - mTimestampsBounds[4].width() - mTimestampsBounds[4].left,
getTimestampY(4), mTextPaint);
mTimestamps[latestIndex],
xOffsets[latestIndex] - mTimestampsBounds[latestIndex].width()
- mTimestampsBounds[latestIndex].left,
getTimestampY(latestIndex), mTextPaint);
// Draws the rest of timestamp info since it is located in the center.
for (int index = 1; index <= 3; index++) {
for (int index = 1; index <= DEFAULT_TIMESTAMP_COUNT - 2; index++) {
canvas.drawText(
mTimestamps[index],
xOffsets[index] -

View File

@@ -19,6 +19,7 @@ import android.content.Context;
import android.os.BatteryUsageStats;
import android.os.LocaleList;
import android.os.UserHandle;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.util.Log;
@@ -26,10 +27,8 @@ import androidx.annotation.VisibleForTesting;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -76,19 +75,6 @@ public final class ConvertUtils {
public static final int CONSUMER_TYPE_USER_BATTERY = 2;
public static final int CONSUMER_TYPE_SYSTEM_BATTERY = 3;
// For language is changed.
@VisibleForTesting static Locale sLocale;
@VisibleForTesting static Locale sLocaleForHour;
// For time zone is changed.
@VisibleForTesting static String sZoneId;
@VisibleForTesting static String sZoneIdForHour;
private static boolean sIs24HourFormat;
@VisibleForTesting
static SimpleDateFormat sSimpleDateFormat;
@VisibleForTesting
static SimpleDateFormat sSimpleDateFormatForHour;
private ConvertUtils() {}
public static ContentValues convert(
@@ -136,36 +122,21 @@ public final class ConvertUtils {
/** Converts UTC timestamp to human readable local time string. */
public static String utcToLocalTime(Context context, long timestamp) {
final Locale currentLocale = getLocale(context);
final String currentZoneId = TimeZone.getDefault().getID();
if (!currentZoneId.equals(sZoneId)
|| !currentLocale.equals(sLocale)
|| sSimpleDateFormat == null) {
sLocale = currentLocale;
sZoneId = currentZoneId;
sSimpleDateFormat =
new SimpleDateFormat("MMM dd,yyyy HH:mm:ss", currentLocale);
}
return sSimpleDateFormat.format(new Date(timestamp));
final Locale locale = getLocale(context);
final String pattern =
DateFormat.getBestDateTimePattern(locale, "MMM dd,yyyy HH:mm:ss");
return DateFormat.format(pattern, timestamp).toString();
}
/** Converts UTC timestamp to local time hour data. */
public static String utcToLocalTimeHour(
Context context, long timestamp, boolean is24HourFormat) {
final Locale currentLocale = getLocale(context);
final String currentZoneId = TimeZone.getDefault().getID();
if (!currentZoneId.equals(sZoneIdForHour)
|| !currentLocale.equals(sLocaleForHour)
|| sIs24HourFormat != is24HourFormat
|| sSimpleDateFormatForHour == null) {
sLocaleForHour = currentLocale;
sZoneIdForHour = currentZoneId;
sIs24HourFormat = is24HourFormat;
sSimpleDateFormatForHour = new SimpleDateFormat(
sIs24HourFormat ? "HH" : "h", currentLocale);
}
return sSimpleDateFormatForHour.format(new Date(timestamp))
.toLowerCase(currentLocale);
final Locale locale = getLocale(context);
// e.g. for 12-hour format: 9 pm
// e.g. for 24-hour format: 09:00
final String skeleton = is24HourFormat ? "HHm" : "ha";
final String pattern = DateFormat.getBestDateTimePattern(locale, skeleton);
return DateFormat.format(pattern, timestamp).toString().toLowerCase(locale);
}
/** Gets indexed battery usage data for each corresponding time slot. */