Update battery usage data clear mechanism while time change.
- Accept time updated in the last job schedule range: [ max (last-full-charge / last-even-hours) , next-even-hours ] Bug: 308714066 Fix: 308714066 Test: manual Change-Id: Id92fffddb7666d63fce66fee696a27d957c8b537
This commit is contained in:
@@ -43,6 +43,7 @@ message BatteryUsageHistoricalLogEntry {
|
|||||||
RECHECK_JOB = 3;
|
RECHECK_JOB = 3;
|
||||||
FETCH_USAGE_DATA = 4;
|
FETCH_USAGE_DATA = 4;
|
||||||
INSERT_USAGE_DATA = 5;
|
INSERT_USAGE_DATA = 5;
|
||||||
|
TIME_UPDATED = 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
optional int64 timestamp = 1;
|
optional int64 timestamp = 1;
|
||||||
|
@@ -70,8 +70,7 @@ public final class BootBroadcastReceiver extends BroadcastReceiver {
|
|||||||
break;
|
break;
|
||||||
case Intent.ACTION_TIME_CHANGED:
|
case Intent.ACTION_TIME_CHANGED:
|
||||||
Log.d(TAG, "refresh job and clear all data from action=" + action);
|
Log.d(TAG, "refresh job and clear all data from action=" + action);
|
||||||
DatabaseUtils.clearAll(context);
|
DatabaseUtils.clearDataAfterTimeChangedIfNeeded(context);
|
||||||
PeriodicJobManager.getInstance(context).refreshJob(/*fromBoot=*/ false);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Log.w(TAG, "receive unsupported action=" + action);
|
Log.w(TAG, "receive unsupported action=" + action);
|
||||||
|
@@ -72,8 +72,6 @@ public class DataProcessManager {
|
|||||||
private static final String TAG = "DataProcessManager";
|
private static final String TAG = "DataProcessManager";
|
||||||
private static final List<BatteryEventType> POWER_CONNECTION_EVENTS =
|
private static final List<BatteryEventType> POWER_CONNECTION_EVENTS =
|
||||||
List.of(BatteryEventType.POWER_CONNECTED, BatteryEventType.POWER_DISCONNECTED);
|
List.of(BatteryEventType.POWER_CONNECTED, BatteryEventType.POWER_DISCONNECTED);
|
||||||
private static final List<BatteryEventType> BATTERY_LEVEL_RECORD_EVENTS =
|
|
||||||
List.of(BatteryEventType.FULL_CHARGED, BatteryEventType.EVEN_HOUR);
|
|
||||||
|
|
||||||
// For testing only.
|
// For testing only.
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
@@ -575,7 +573,7 @@ public class DataProcessManager {
|
|||||||
final List<BatteryEvent> batteryLevelRecordEvents =
|
final List<BatteryEvent> batteryLevelRecordEvents =
|
||||||
DatabaseUtils.getBatteryEvents(
|
DatabaseUtils.getBatteryEvents(
|
||||||
context, Calendar.getInstance(), lastFullChargeTime,
|
context, Calendar.getInstance(), lastFullChargeTime,
|
||||||
BATTERY_LEVEL_RECORD_EVENTS);
|
DatabaseUtils.BATTERY_LEVEL_RECORD_EVENTS);
|
||||||
final long startTimestamp = batteryLevelRecordEvents.isEmpty()
|
final long startTimestamp = batteryLevelRecordEvents.isEmpty()
|
||||||
? lastFullChargeTime : batteryLevelRecordEvents.get(0).getTimestamp();
|
? lastFullChargeTime : batteryLevelRecordEvents.get(0).getTimestamp();
|
||||||
final BatteryLevelData batteryLevelData = getPeriodBatteryLevelData(context, handler,
|
final BatteryLevelData batteryLevelData = getPeriodBatteryLevelData(context, handler,
|
||||||
|
@@ -53,6 +53,7 @@ import java.time.Duration;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
@@ -133,6 +134,9 @@ public final class DatabaseUtils {
|
|||||||
.authority(AUTHORITY)
|
.authority(AUTHORITY)
|
||||||
.appendPath(BATTERY_USAGE_SLOT_TABLE)
|
.appendPath(BATTERY_USAGE_SLOT_TABLE)
|
||||||
.build();
|
.build();
|
||||||
|
/** A list of level record event types to access battery usage data. */
|
||||||
|
public static final List<BatteryEventType> BATTERY_LEVEL_RECORD_EVENTS =
|
||||||
|
List.of(BatteryEventType.FULL_CHARGED, BatteryEventType.EVEN_HOUR);
|
||||||
|
|
||||||
// For testing only.
|
// For testing only.
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
@@ -408,6 +412,35 @@ 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) {
|
||||||
|
AsyncTask.execute(() -> {
|
||||||
|
try {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
Log.e(TAG, "refreshDataAndJobIfNeededAfterTimeChanged() failed", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns the timestamp for 00:00 6 days before the calendar date. */
|
/** Returns the timestamp for 00:00 6 days before the calendar date. */
|
||||||
public static long getTimestampSixDaysAgo(Calendar calendar) {
|
public static long getTimestampSixDaysAgo(Calendar calendar) {
|
||||||
Calendar startCalendar =
|
Calendar startCalendar =
|
||||||
|
Reference in New Issue
Block a user