Refactor code according to comments in ag/25767430.

Bug: 318308397
Test: presubmit
Change-Id: Idc42d340508d2c41173ca157aabc452b6b0f6036
This commit is contained in:
Zaiyue Xue
2024-01-08 15:10:24 +08:00
parent dea44ebf7b
commit d6cc80362c

View File

@@ -458,55 +458,24 @@ 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) {
if ((intent.getFlags() & FLAG_RECEIVER_REPLACE_PENDING) != 0) {
BatteryUsageLogUtils.writeLog(
context,
Action.TIME_UPDATED,
"Database is not cleared because the time change intent is only"
+ " for the existing pending receiver.");
return;
}
AsyncTask.execute(
() -> {
try {
if ((intent.getFlags() & FLAG_RECEIVER_REPLACE_PENDING) != 0) {
BatteryUsageLogUtils.writeLog(
context,
Action.TIME_UPDATED,
"Database is not cleared because the time change intent is only"
+ " 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);
}
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(