Merge "Align system time 12-24 hour format in the time slot information" into sc-dev am: 80f2f43d59

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/14609994

Change-Id: I56c5dbb6a03ea06afd292e56d84914ffa9b27150
This commit is contained in:
YUKAI HUNG
2021-05-18 15:45:24 +00:00
committed by Automerger Merge Worker
6 changed files with 90 additions and 57 deletions

View File

@@ -24,6 +24,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.util.Log;
@@ -47,7 +48,6 @@ import com.android.settingslib.core.lifecycle.events.OnResume;
import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState;
import com.android.settingslib.utils.StringUtil;
import java.time.Clock;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
@@ -88,6 +88,8 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
@VisibleForTesting long[] mBatteryHistoryKeys;
@VisibleForTesting int mTrapezoidIndex = BatteryChartView.SELECTED_INDEX_INVALID;
private boolean mIs24HourFormat = false;
private final String mPreferenceKey;
private final SettingsActivity mActivity;
private final InstrumentedPreferenceFragment mFragment;
@@ -110,6 +112,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
mActivity = activity;
mFragment = fragment;
mPreferenceKey = preferenceKey;
mIs24HourFormat = DateFormat.is24HourFormat(context);
mNotAllowShowSummaryPackages = context.getResources()
.getTextArray(R.array.allowlist_hide_summary_in_battery_usage);
mNotAllowShowEntryPackages = context.getResources()
@@ -144,6 +147,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
BatteryDiffEntry.clearCache();
Log.d(TAG, "clear icon and label cache since uiMode is changed");
}
mIs24HourFormat = DateFormat.is24HourFormat(mContext);
mMetricsFeatureProvider.action(mPrefContext, SettingsEnums.OPEN_BATTERY_USAGE);
}
@@ -493,10 +497,10 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
return null;
}
final String fromHour = ConvertUtils.utcToLocalTimeHour(
mBatteryHistoryKeys[mTrapezoidIndex * 2]);
mBatteryHistoryKeys[mTrapezoidIndex * 2], mIs24HourFormat);
final String toHour = ConvertUtils.utcToLocalTimeHour(
mBatteryHistoryKeys[(mTrapezoidIndex + 1) * 2]);
return String.format("%s-%s", fromHour, toHour);
mBatteryHistoryKeys[(mTrapezoidIndex + 1) * 2], mIs24HourFormat);
return String.format("%s - %s", fromHour, toHour);
}
@VisibleForTesting
@@ -563,21 +567,9 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
if (mBatteryChartView == null || mBatteryHistoryKeys == null) {
return;
}
long latestTimestamp =
final long latestTimestamp =
mBatteryHistoryKeys[mBatteryHistoryKeys.length - 1];
// Uses the current time if we don't have history data.
if (latestTimestamp == 0) {
latestTimestamp = Clock.systemUTC().millis();
}
// Generates timestamp label for chart graph (every 8 hours).
final long timeSlotOffset = DateUtils.HOUR_IN_MILLIS * 8;
final String[] timestampLabels = new String[4];
for (int index = 0; index < timestampLabels.length; index++) {
timestampLabels[index] =
ConvertUtils.utcToLocalTimeHour(
latestTimestamp - (3 - index) * timeSlotOffset);
}
mBatteryChartView.setTimestamps(timestampLabels);
mBatteryChartView.setLatestTimestamp(latestTimestamp);
}
private static String utcToLocalTime(long[] timestamps) {

View File

@@ -25,6 +25,8 @@ import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Handler;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.HapticFeedbackConstants;
@@ -40,6 +42,7 @@ import com.android.settings.R;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.Utils;
import java.time.Clock;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
@@ -74,6 +77,7 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
private boolean mIsSlotsClickabled;
@VisibleForTesting int mSelectedIndex;
@VisibleForTesting String[] mTimestamps;
// Colors for drawing the trapezoid shape and dividers.
private int mTrapezoidColor;
@@ -84,7 +88,6 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
private final Rect[] mPercentageBounds =
new Rect[] {new Rect(), new Rect(), new Rect()};
// For drawing the timestamp information.
private String[] mTimestamps;
private final Rect[] mTimestampsBounds =
new Rect[] {new Rect(), new Rect(), new Rect(), new Rect()};
@@ -116,6 +119,7 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
setSelectedIndex(SELECTED_INDEX_ALL);
setTrapezoidCount(DEFAULT_TRAPEZOID_COUNT);
setClickable(false);
setLatestTimestamp(0);
}
/** Sets the total trapezoid count for drawing. */
@@ -182,12 +186,21 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
requestLayout();
}
/** Sets timestamps for drawing into x-axis information. */
public void setTimestamps(String[] timestamps) {
mTimestamps = timestamps;
if (timestamps != null
&& timestamps.length != DEFAULT_TIMESTAMP_COUNT) {
mTimestamps = null;
/** Sets the latest timestamp for drawing into x-axis information. */
public void setLatestTimestamp(long latestTimestamp) {
if (latestTimestamp == 0) {
latestTimestamp = Clock.systemUTC().millis();
}
if (mTimestamps == null) {
mTimestamps = new String[DEFAULT_TIMESTAMP_COUNT];
}
final long timeSlotOffset = DateUtils.HOUR_IN_MILLIS * 8;
final boolean is24HourFormat = DateFormat.is24HourFormat(getContext());
for (int index = 0; index < DEFAULT_TIMESTAMP_COUNT; index++) {
mTimestamps[index] =
ConvertUtils.utcToLocalTimeHour(
latestTimestamp - (3 - index) * timeSlotOffset,
is24HourFormat);
}
requestLayout();
}

View File

@@ -71,6 +71,7 @@ public final class ConvertUtils {
private static String sZoneId;
private static String sZoneIdForHour;
private static boolean sIs24HourFormat;
@VisibleForTesting
static SimpleDateFormat sSimpleDateFormat;
@@ -134,11 +135,15 @@ public final class ConvertUtils {
}
/** Converts UTC timestamp to local time hour data. */
public static String utcToLocalTimeHour(long timestamp) {
public static String utcToLocalTimeHour(long timestamp, boolean is24HourFormat) {
final String currentZoneId = TimeZone.getDefault().getID();
if (!currentZoneId.equals(sZoneIdForHour) || sSimpleDateFormatForHour == null) {
if (!currentZoneId.equals(sZoneIdForHour)
|| sIs24HourFormat != is24HourFormat
|| sSimpleDateFormatForHour == null) {
sZoneIdForHour = currentZoneId;
sSimpleDateFormatForHour = new SimpleDateFormat("h aa", Locale.ENGLISH);
sIs24HourFormat = is24HourFormat;
sSimpleDateFormatForHour = new SimpleDateFormat(
sIs24HourFormat ? "HH" : "h aa", Locale.ENGLISH);
}
return sSimpleDateFormatForHour.format(new Date(timestamp))
.toLowerCase(Locale.getDefault());