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,9 +458,6 @@ public final class DatabaseUtils {
/** Clears all data and jobs if current timestamp is out of the range of last recorded job. */ /** 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) { public static void clearDataAfterTimeChangedIfNeeded(Context context, Intent intent) {
AsyncTask.execute(
() -> {
try {
if ((intent.getFlags() & FLAG_RECEIVER_REPLACE_PENDING) != 0) { if ((intent.getFlags() & FLAG_RECEIVER_REPLACE_PENDING) != 0) {
BatteryUsageLogUtils.writeLog( BatteryUsageLogUtils.writeLog(
context, context,
@@ -469,44 +466,16 @@ public final class DatabaseUtils {
+ " for the existing pending receiver."); + " for the existing pending receiver.");
return; return;
} }
final List<BatteryEvent> batteryLevelRecordEvents = AsyncTask.execute(
DatabaseUtils.getBatteryEvents( () -> {
context, try {
Calendar.getInstance(), clearDataAfterTimeChangedIfNeededInternal(context);
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);
}
} catch (RuntimeException e) { } catch (RuntimeException e) {
Log.e(TAG, "refreshDataAndJobIfNeededAfterTimeChanged() failed", e); Log.e(TAG, "clearDataAfterTimeChangedIfNeeded() failed", e);
BatteryUsageLogUtils.writeLog( BatteryUsageLogUtils.writeLog(
context, context,
Action.TIME_UPDATED, 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( private static long loadLongFromContentProvider(
Context context, Uri uri, final long defaultValue) { Context context, Uri uri, final long defaultValue) {
return loadFromContentProvider( return loadFromContentProvider(