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

Commit d6cc8036 authored by Zaiyue Xue's avatar Zaiyue Xue
Browse files

Refactor code according to comments in ag/25767430.

Bug: 318308397
Test: presubmit
Change-Id: Idc42d340508d2c41173ca157aabc452b6b0f6036
parent dea44ebf
Loading
Loading
Loading
Loading
+45 −42
Original line number Diff line number Diff line
@@ -458,9 +458,6 @@ public final class DatabaseUtils {

    /** Clears all data and jobs if current timestamp is out of the range of last recorded job. */
    public static void clearDataAfterTimeChangedIfNeeded(Context context, Intent intent) {
        AsyncTask.execute(
                () -> {
                    try {
        if ((intent.getFlags() & FLAG_RECEIVER_REPLACE_PENDING) != 0) {
            BatteryUsageLogUtils.writeLog(
                    context,
@@ -469,44 +466,16 @@ public final class DatabaseUtils {
                            + " for the existing pending receiver.");
            return;
        }
                        final List<BatteryEvent> batteryLevelRecordEvents =
                                DatabaseUtils.getBatteryEvents(
                                        context,
                                        Calendar.getInstance(),
                                        getLastFullChargeTime(context),
                                        BATTERY_LEVEL_RECORD_EVENTS);
                        final long lastRecordTimestamp =
                                batteryLevelRecordEvents.isEmpty()
                                        ? INVALID_TIMESTAMP
                                        : batteryLevelRecordEvents.get(0).getTimestamp();
                        final long nextRecordTimestamp =
                                TimestampUtils.getNextEvenHourTimestamp(lastRecordTimestamp);
                        final long currentTime = System.currentTimeMillis();
                        final boolean isOutOfTimeRange =
                                lastRecordTimestamp == INVALID_TIMESTAMP
                                        || currentTime < lastRecordTimestamp
                                        || currentTime > nextRecordTimestamp;
                        final String logInfo =
                                String.format(
                                        Locale.ENGLISH,
                                        "clear database = %b, current time = %d, "
                                                + "last record time = %d",
                                        isOutOfTimeRange,
                                        currentTime,
                                        lastRecordTimestamp);
                        Log.d(TAG, logInfo);
                        BatteryUsageLogUtils.writeLog(context, Action.TIME_UPDATED, logInfo);
                        if (isOutOfTimeRange) {
                            DatabaseUtils.clearAll(context);
                            PeriodicJobManager.getInstance(context)
                                    .refreshJob(/* fromBoot= */ false);
                        }
        AsyncTask.execute(
                () -> {
                    try {
                        clearDataAfterTimeChangedIfNeededInternal(context);
                    } catch (RuntimeException e) {
                        Log.e(TAG, "refreshDataAndJobIfNeededAfterTimeChanged() failed", e);
                        Log.e(TAG, "clearDataAfterTimeChangedIfNeeded() failed", e);
                        BatteryUsageLogUtils.writeLog(
                                context,
                                Action.TIME_UPDATED,
                                "refreshDataAndJobIfNeededAfterTimeChanged() failed" + e);
                                "clearDataAfterTimeChangedIfNeeded() failed" + e);
                    }
                });
    }
@@ -890,6 +859,40 @@ public final class DatabaseUtils {
        }
    }

    private static void clearDataAfterTimeChangedIfNeededInternal(Context context) {
        final List<BatteryEvent> batteryLevelRecordEvents =
                DatabaseUtils.getBatteryEvents(
                        context,
                        Calendar.getInstance(),
                        getLastFullChargeTime(context),
                        BATTERY_LEVEL_RECORD_EVENTS);
        final long lastRecordTimestamp =
                batteryLevelRecordEvents.isEmpty()
                        ? INVALID_TIMESTAMP
                        : batteryLevelRecordEvents.get(0).getTimestamp();
        final long nextRecordTimestamp =
                TimestampUtils.getNextEvenHourTimestamp(lastRecordTimestamp);
        final long currentTime = System.currentTimeMillis();
        final boolean isOutOfTimeRange =
                lastRecordTimestamp == INVALID_TIMESTAMP
                        || currentTime < lastRecordTimestamp
                        || currentTime > nextRecordTimestamp;
        final String logInfo =
                String.format(
                        Locale.ENGLISH,
                        "clear database = %b, current time = %d, last record time = %d",
                        isOutOfTimeRange,
                        currentTime,
                        lastRecordTimestamp);
        Log.d(TAG, logInfo);
        BatteryUsageLogUtils.writeLog(context, Action.TIME_UPDATED, logInfo);
        if (isOutOfTimeRange) {
            DatabaseUtils.clearAll(context);
            PeriodicJobManager.getInstance(context)
                    .refreshJob(/* fromBoot= */ false);
        }
    }

    private static long loadLongFromContentProvider(
            Context context, Uri uri, final long defaultValue) {
        return loadFromContentProvider(