Listen ACTION_BATTERY_LEVEL_CHANGED in Settings and fetch usage data
when battery is full charged. Bug: 253395332 Test: make RunSettingsRoboTests + manually Change-Id: Ie83e5f319074ff404a600c4eb375fbecad651b6d
This commit is contained in:
@@ -20,20 +20,24 @@ import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settingslib.fuelgauge.BatteryStatus;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/** A {@link BatteryUsageBroadcastReceiver} for battery usage data requesting. */
|
||||
public final class BatteryUsageBroadcastReceiver extends BroadcastReceiver {
|
||||
private static final String TAG = "BatteryUsageBroadcastReceiver";
|
||||
/** An intent action to request Settings to fetch usage data. */
|
||||
public static final String ACTION_FETCH_BATTERY_USAGE_DATA =
|
||||
"com.android.settings.battery.action.FETCH_BATTERY_USAGE_DATA";
|
||||
/** An intent action to request Settings to clear cache data. */
|
||||
public static final String ACTION_CLEAR_BATTERY_CACHE_DATA =
|
||||
"com.android.settings.battery.action.CLEAR_BATTERY_CACHE_DATA";
|
||||
|
||||
@VisibleForTesting
|
||||
static long sBroadcastDelayFromBoot = Duration.ofMinutes(40).toMillis();
|
||||
@VisibleForTesting
|
||||
static boolean sIsDebugMode = Build.TYPE.equals("userdebug");
|
||||
|
||||
@@ -47,9 +51,8 @@ public final class BatteryUsageBroadcastReceiver extends BroadcastReceiver {
|
||||
}
|
||||
Log.d(TAG, "onReceive:" + intent.getAction());
|
||||
switch (intent.getAction()) {
|
||||
case ACTION_FETCH_BATTERY_USAGE_DATA:
|
||||
mFetchBatteryUsageData = true;
|
||||
BatteryUsageDataLoader.enqueueWork(context);
|
||||
case Intent.ACTION_BATTERY_LEVEL_CHANGED:
|
||||
tryToFetchUsageData(context);
|
||||
break;
|
||||
case ACTION_CLEAR_BATTERY_CACHE_DATA:
|
||||
if (sIsDebugMode) {
|
||||
@@ -59,4 +62,23 @@ public final class BatteryUsageBroadcastReceiver extends BroadcastReceiver {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void tryToFetchUsageData(Context context) {
|
||||
final Intent batteryIntent = DatabaseUtils.getBatteryIntent(context);
|
||||
// Returns when battery is not fully charged.
|
||||
if (!BatteryStatus.isCharged(batteryIntent)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final long broadcastDelay = sBroadcastDelayFromBoot - SystemClock.elapsedRealtime();
|
||||
// If current boot time is smaller than expected delay, cancel sending the broadcast.
|
||||
if (broadcastDelay > 0) {
|
||||
Log.d(TAG, "cancel sendBroadcastToFetchUsageData when broadcastDelay is"
|
||||
+ broadcastDelay + "ms.");
|
||||
return;
|
||||
}
|
||||
|
||||
mFetchBatteryUsageData = true;
|
||||
BatteryUsageDataLoader.enqueueWork(context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,6 +141,12 @@ public final class DatabaseUtils {
|
||||
});
|
||||
}
|
||||
|
||||
/** Gets the latest sticky battery intent from framework. */
|
||||
static Intent getBatteryIntent(Context context) {
|
||||
return context.registerReceiver(
|
||||
/*receiver=*/ null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
}
|
||||
|
||||
static List<ContentValues> sendBatteryEntryData(
|
||||
Context context,
|
||||
List<BatteryEntry> batteryEntryList,
|
||||
@@ -300,12 +306,6 @@ public final class DatabaseUtils {
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
/** Gets the latest sticky battery intent from framework. */
|
||||
private static Intent getBatteryIntent(Context context) {
|
||||
return context.registerReceiver(
|
||||
/*receiver=*/ null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
}
|
||||
|
||||
private static int getBatteryLevel(Intent intent) {
|
||||
final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
|
||||
final int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
|
||||
|
||||
Reference in New Issue
Block a user