Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 211afbea authored by YK Hung's avatar YK Hung Committed by Android (Google) Code Review
Browse files

Merge changes from topic "timestamp_update" into main

* changes:
  Update database clear & job refresh mechanism for time zone change intent
  Update database clear & job refresh mechanism for time change intent
  Update time format for the first timestamp on usage chartview.
parents 8daf3357 d3ce9034
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3343,6 +3343,7 @@
                <action android:name="com.google.android.setupwizard.SETUP_WIZARD_FINISHED"/>
                <action android:name="com.android.settings.battery.action.PERIODIC_JOB_RECHECK"/>
                <action android:name="android.intent.action.TIME_SET"/>
                <action android:name="android.intent.action.TIMEZONE_CHANGED"/>
            </intent-filter>
        </receiver>

+1 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ message BatteryUsageHistoricalLogEntry {
    FETCH_USAGE_DATA = 4;
    INSERT_USAGE_DATA = 5;
    TIME_UPDATED = 6;
    TIMEZONE_UPDATED = 7;
  }

  optional int64 timestamp = 1;
+3 −6
Original line number Diff line number Diff line
@@ -649,9 +649,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll

    private final class HourlyChartLabelTextGenerator extends BaseLabelTextGenerator
            implements BatteryChartViewModel.LabelTextGenerator {
        private static final int FULL_CHARGE_BATTERY_LEVEL = 100;

        private boolean mIsFromFullCharge;
        private boolean mIsStartTimestamp;
        private long mFistTimestamp;
        private long mLatestTimestamp;

@@ -664,7 +662,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
            long timestamp = timestamps.get(index);
            boolean showMinute = false;
            if (Objects.equal(timestamp, mFistTimestamp)) {
                if (mIsFromFullCharge) {
                if (mIsStartTimestamp) {
                    showMinute = true;
                } else {
                    // starts from 7 days ago
@@ -699,8 +697,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
                @NonNull final BatteryLevelData batteryLevelData) {
            BatteryLevelData.PeriodBatteryLevelData firstDayLevelData =
                    batteryLevelData.getHourlyBatteryLevelsPerDay().get(0);
            this.mIsFromFullCharge =
                    firstDayLevelData.getLevels().get(0) == FULL_CHARGE_BATTERY_LEVEL;
            this.mIsStartTimestamp = firstDayLevelData.isStartTimestamp();
            this.mFistTimestamp = firstDayLevelData.getTimestamps().get(0);
            this.mLatestTimestamp =
                    getLast(
+23 −4
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import androidx.annotation.VisibleForTesting;
import androidx.core.util.Preconditions;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
@@ -39,17 +40,24 @@ public final class BatteryLevelData {
    private static final long MIN_SIZE = 2;
    private static final long TIME_SLOT = DateUtils.HOUR_IN_MILLIS * 2;

    // For testing only.
    @VisibleForTesting @Nullable static Calendar sTestCalendar;

    /** A container for the battery timestamp and level data. */
    public static final class PeriodBatteryLevelData {
        // The length of mTimestamps and mLevels must be the same. mLevels[index] might be null when
        // there is no level data for the corresponding timestamp.
        private final List<Long> mTimestamps;
        private final List<Integer> mLevels;
        private final boolean mIsStartTimestamp;

        public PeriodBatteryLevelData(
                @NonNull Map<Long, Integer> batteryLevelMap, @NonNull List<Long> timestamps) {
                @NonNull Map<Long, Integer> batteryLevelMap,
                @NonNull List<Long> timestamps,
                boolean isStartTimestamp) {
            mTimestamps = timestamps;
            mLevels = new ArrayList<>(timestamps.size());
            mIsStartTimestamp = isStartTimestamp;
            for (Long timestamp : timestamps) {
                mLevels.add(
                        batteryLevelMap.containsKey(timestamp)
@@ -66,6 +74,10 @@ public final class BatteryLevelData {
            return mLevels;
        }

        public boolean isStartTimestamp() {
            return mIsStartTimestamp;
        }

        @Override
        public String toString() {
            return String.format(
@@ -105,14 +117,21 @@ public final class BatteryLevelData {

        final List<Long> timestampList = new ArrayList<>(batteryLevelMap.keySet());
        Collections.sort(timestampList);
        final long minTimestamp = timestampList.get(0);
        final long sixDaysAgoTimestamp =
                DatabaseUtils.getTimestampSixDaysAgo(sTestCalendar != null ? sTestCalendar : null);
        final boolean isStartTimestamp = minTimestamp > sixDaysAgoTimestamp;
        final List<Long> dailyTimestamps = getDailyTimestamps(timestampList);
        final List<List<Long>> hourlyTimestamps = getHourlyTimestamps(dailyTimestamps);

        mDailyBatteryLevels = new PeriodBatteryLevelData(batteryLevelMap, dailyTimestamps);
        mDailyBatteryLevels =
                new PeriodBatteryLevelData(batteryLevelMap, dailyTimestamps, isStartTimestamp);
        mHourlyBatteryLevelsPerDay = new ArrayList<>(hourlyTimestamps.size());
        for (List<Long> hourlyTimestampsPerDay : hourlyTimestamps) {
        for (int i = 0; i < hourlyTimestamps.size(); i++) {
            final List<Long> hourlyTimestampsPerDay = hourlyTimestamps.get(i);
            mHourlyBatteryLevelsPerDay.add(
                    new PeriodBatteryLevelData(batteryLevelMap, hourlyTimestampsPerDay));
                    new PeriodBatteryLevelData(
                            batteryLevelMap, hourlyTimestampsPerDay, isStartTimestamp && i == 0));
        }
    }

+5 −1
Original line number Diff line number Diff line
@@ -67,9 +67,13 @@ public final class BootBroadcastReceiver extends BroadcastReceiver {
                refreshJobs(context);
                break;
            case Intent.ACTION_TIME_CHANGED:
                Log.d(TAG, "refresh job and clear all data from action=" + action);
                Log.d(TAG, "refresh job and clear data from action=" + action);
                DatabaseUtils.clearDataAfterTimeChangedIfNeeded(context, intent);
                break;
            case Intent.ACTION_TIMEZONE_CHANGED:
                Log.d(TAG, "refresh job and clear all data from action=" + action);
                DatabaseUtils.clearDataAfterTimeZoneChangedIfNeeded(context);
                break;
            default:
                Log.w(TAG, "receive unsupported action=" + action);
        }
Loading