Loading src/com/android/settings/fuelgauge/BatteryChartPreferenceController.java +10 −18 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -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; Loading @@ -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() Loading Loading @@ -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); } Loading Loading @@ -493,9 +497,9 @@ 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]); mBatteryHistoryKeys[(mTrapezoidIndex + 1) * 2], mIs24HourFormat); return String.format("%s - %s", fromHour, toHour); } Loading Loading @@ -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) { Loading src/com/android/settings/fuelgauge/BatteryChartView.java +20 −7 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -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; Loading @@ -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()}; Loading Loading @@ -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. */ Loading Loading @@ -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(); } Loading src/com/android/settings/fuelgauge/ConvertUtils.java +8 −3 Original line number Diff line number Diff line Loading @@ -71,6 +71,7 @@ public final class ConvertUtils { private static String sZoneId; private static String sZoneIdForHour; private static boolean sIs24HourFormat; @VisibleForTesting static SimpleDateFormat sSimpleDateFormat; Loading Loading @@ -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()); Loading tests/robotests/src/com/android/settings/fuelgauge/BatteryChartPreferenceControllerTest.java +5 −12 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyLong; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; Loading Loading @@ -604,7 +605,7 @@ public final class BatteryChartPreferenceControllerTest { mBatteryChartPreferenceController.setTimestampLabel(); verify(mBatteryChartPreferenceController.mBatteryChartView, never()) .setTimestamps(any()); .setLatestTimestamp(anyLong()); } @Test Loading @@ -613,19 +614,11 @@ public final class BatteryChartPreferenceControllerTest { mBatteryChartPreferenceController.mBatteryChartView = spy(new BatteryChartView(mContext)); setUpBatteryHistoryKeys(); // Generates the expected result. final String[] expectedResults = new String[4]; final long timeSlotOffset = DateUtils.HOUR_IN_MILLIS * 8; for (int index = 0; index < expectedResults.length; index++) { expectedResults[index] = ConvertUtils.utcToLocalTimeHour( 1619247636826L - (3 - index) * timeSlotOffset); } mBatteryChartPreferenceController.setTimestampLabel(); verify(mBatteryChartPreferenceController.mBatteryChartView) .setTimestamps(expectedResults); .setLatestTimestamp(1619247636826L); } @Test Loading @@ -638,7 +631,7 @@ public final class BatteryChartPreferenceControllerTest { mBatteryChartPreferenceController.setTimestampLabel(); verify(mBatteryChartPreferenceController.mBatteryChartView) .setTimestamps(any()); .setLatestTimestamp(anyLong()); } @Test Loading Loading @@ -709,7 +702,7 @@ public final class BatteryChartPreferenceControllerTest { private void setUpBatteryHistoryKeys() { mBatteryChartPreferenceController.mBatteryHistoryKeys = new long[] {1619196786769L, 0L, 1619247636826L}; ConvertUtils.utcToLocalTimeHour(/*timestamp=*/ 0); ConvertUtils.utcToLocalTimeHour(/*timestamp=*/ 0, /*is24HourFormat=*/ false); // Simulates the locale in GMT. ConvertUtils.sSimpleDateFormatForHour .setTimeZone(TimeZone.getTimeZone("GMT")); Loading tests/robotests/src/com/android/settings/fuelgauge/BatteryChartViewTest.java +17 −0 Original line number Diff line number Diff line Loading @@ -41,6 +41,7 @@ import org.robolectric.RuntimeEnvironment; import java.util.Arrays; import java.util.ArrayList; import java.util.TimeZone; @RunWith(RobolectricTestRunner.class) public final class BatteryChartViewTest { Loading Loading @@ -227,4 +228,20 @@ public final class BatteryChartViewTest { verify(mBatteryChartView.mHandler) .postDelayed(mBatteryChartView.mUpdateClickableStateRun, 500L); } @Test public void testSetLatestTimestamp_generateExpectedTimestamps() { final long timestamp = 1619196786769L; ConvertUtils.sSimpleDateFormatForHour = null; // Invokes the method first to create the SimpleDateFormat. ConvertUtils.utcToLocalTimeHour(/*timestamp=*/ 0, /*is24HourFormat=*/ false); ConvertUtils.sSimpleDateFormatForHour .setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); final String[] expectedTimestamps = new String[] {"9 am", "5 pm", "1 am", "9 am"}; mBatteryChartView.setLatestTimestamp(timestamp); assertThat(mBatteryChartView.mTimestamps).isEqualTo(expectedTimestamps); } } Loading
src/com/android/settings/fuelgauge/BatteryChartPreferenceController.java +10 −18 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -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; Loading @@ -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() Loading Loading @@ -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); } Loading Loading @@ -493,9 +497,9 @@ 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]); mBatteryHistoryKeys[(mTrapezoidIndex + 1) * 2], mIs24HourFormat); return String.format("%s - %s", fromHour, toHour); } Loading Loading @@ -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) { Loading
src/com/android/settings/fuelgauge/BatteryChartView.java +20 −7 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -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; Loading @@ -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()}; Loading Loading @@ -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. */ Loading Loading @@ -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(); } Loading
src/com/android/settings/fuelgauge/ConvertUtils.java +8 −3 Original line number Diff line number Diff line Loading @@ -71,6 +71,7 @@ public final class ConvertUtils { private static String sZoneId; private static String sZoneIdForHour; private static boolean sIs24HourFormat; @VisibleForTesting static SimpleDateFormat sSimpleDateFormat; Loading Loading @@ -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()); Loading
tests/robotests/src/com/android/settings/fuelgauge/BatteryChartPreferenceControllerTest.java +5 −12 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyLong; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; Loading Loading @@ -604,7 +605,7 @@ public final class BatteryChartPreferenceControllerTest { mBatteryChartPreferenceController.setTimestampLabel(); verify(mBatteryChartPreferenceController.mBatteryChartView, never()) .setTimestamps(any()); .setLatestTimestamp(anyLong()); } @Test Loading @@ -613,19 +614,11 @@ public final class BatteryChartPreferenceControllerTest { mBatteryChartPreferenceController.mBatteryChartView = spy(new BatteryChartView(mContext)); setUpBatteryHistoryKeys(); // Generates the expected result. final String[] expectedResults = new String[4]; final long timeSlotOffset = DateUtils.HOUR_IN_MILLIS * 8; for (int index = 0; index < expectedResults.length; index++) { expectedResults[index] = ConvertUtils.utcToLocalTimeHour( 1619247636826L - (3 - index) * timeSlotOffset); } mBatteryChartPreferenceController.setTimestampLabel(); verify(mBatteryChartPreferenceController.mBatteryChartView) .setTimestamps(expectedResults); .setLatestTimestamp(1619247636826L); } @Test Loading @@ -638,7 +631,7 @@ public final class BatteryChartPreferenceControllerTest { mBatteryChartPreferenceController.setTimestampLabel(); verify(mBatteryChartPreferenceController.mBatteryChartView) .setTimestamps(any()); .setLatestTimestamp(anyLong()); } @Test Loading Loading @@ -709,7 +702,7 @@ public final class BatteryChartPreferenceControllerTest { private void setUpBatteryHistoryKeys() { mBatteryChartPreferenceController.mBatteryHistoryKeys = new long[] {1619196786769L, 0L, 1619247636826L}; ConvertUtils.utcToLocalTimeHour(/*timestamp=*/ 0); ConvertUtils.utcToLocalTimeHour(/*timestamp=*/ 0, /*is24HourFormat=*/ false); // Simulates the locale in GMT. ConvertUtils.sSimpleDateFormatForHour .setTimeZone(TimeZone.getTimeZone("GMT")); Loading
tests/robotests/src/com/android/settings/fuelgauge/BatteryChartViewTest.java +17 −0 Original line number Diff line number Diff line Loading @@ -41,6 +41,7 @@ import org.robolectric.RuntimeEnvironment; import java.util.Arrays; import java.util.ArrayList; import java.util.TimeZone; @RunWith(RobolectricTestRunner.class) public final class BatteryChartViewTest { Loading Loading @@ -227,4 +228,20 @@ public final class BatteryChartViewTest { verify(mBatteryChartView.mHandler) .postDelayed(mBatteryChartView.mUpdateClickableStateRun, 500L); } @Test public void testSetLatestTimestamp_generateExpectedTimestamps() { final long timestamp = 1619196786769L; ConvertUtils.sSimpleDateFormatForHour = null; // Invokes the method first to create the SimpleDateFormat. ConvertUtils.utcToLocalTimeHour(/*timestamp=*/ 0, /*is24HourFormat=*/ false); ConvertUtils.sSimpleDateFormatForHour .setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); final String[] expectedTimestamps = new String[] {"9 am", "5 pm", "1 am", "9 am"}; mBatteryChartView.setLatestTimestamp(timestamp); assertThat(mBatteryChartView.mTimestamps).isEqualTo(expectedTimestamps); } }