Refoactor 2 util functions from DatabaseUtil to BatteyUtils.
Bug: 252407178 Test: presubmit Change-Id: I393777186cc308298f0a8b76af4672b9012ed681
This commit is contained in:
@@ -23,6 +23,7 @@ import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.BatteryStatsManager;
|
||||
import android.os.BatteryUsageStats;
|
||||
@@ -397,8 +398,7 @@ public class BatteryUtils {
|
||||
final long startTime = System.currentTimeMillis();
|
||||
|
||||
// Stuff we always need to get BatteryInfo
|
||||
final Intent batteryBroadcast = mContext.registerReceiver(null,
|
||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
final Intent batteryBroadcast = getBatteryIntent(mContext);
|
||||
|
||||
final long elapsedRealtimeUs = PowerUtil.convertMsToUs(
|
||||
SystemClock.elapsedRealtime());
|
||||
@@ -577,4 +577,19 @@ public class BatteryUtils {
|
||||
|
||||
return -1L;
|
||||
}
|
||||
|
||||
/** Gets the latest sticky battery intent from the Android system. */
|
||||
public static Intent getBatteryIntent(Context context) {
|
||||
return context.registerReceiver(
|
||||
/*receiver=*/ null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
}
|
||||
|
||||
/** Gets the battery level from the intent. */
|
||||
public static int getBatteryLevel(Intent intent) {
|
||||
final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
|
||||
final int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
|
||||
return scale == 0
|
||||
? -1 /*invalid battery level*/
|
||||
: Math.round((level / (float) scale) * 100f);
|
||||
}
|
||||
}
|
||||
|
@@ -25,6 +25,7 @@ import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.fuelgauge.BatteryUtils;
|
||||
import com.android.settingslib.fuelgauge.BatteryStatus;
|
||||
|
||||
import java.time.Duration;
|
||||
@@ -64,7 +65,7 @@ public final class BatteryUsageBroadcastReceiver extends BroadcastReceiver {
|
||||
}
|
||||
|
||||
private void tryToFetchUsageData(Context context) {
|
||||
final Intent batteryIntent = DatabaseUtils.getBatteryIntent(context);
|
||||
final Intent batteryIntent = BatteryUtils.getBatteryIntent(context);
|
||||
// Returns when battery is not fully charged.
|
||||
if (!BatteryStatus.isCharged(batteryIntent)) {
|
||||
return;
|
||||
|
@@ -19,7 +19,6 @@ import android.content.ContentResolver;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.Cursor;
|
||||
@@ -36,6 +35,7 @@ import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.fuelgauge.BatteryUtils;
|
||||
import com.android.settings.fuelgauge.batteryusage.db.BatteryStateDatabase;
|
||||
import com.android.settingslib.fuelgauge.BatteryStatus;
|
||||
|
||||
@@ -141,24 +141,18 @@ 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,
|
||||
BatteryUsageStats batteryUsageStats) {
|
||||
final long startTime = System.currentTimeMillis();
|
||||
final Intent intent = getBatteryIntent(context);
|
||||
final Intent intent = BatteryUtils.getBatteryIntent(context);
|
||||
if (intent == null) {
|
||||
Log.e(TAG, "sendBatteryEntryData(): cannot fetch battery intent");
|
||||
clearMemory();
|
||||
return null;
|
||||
}
|
||||
final int batteryLevel = getBatteryLevel(intent);
|
||||
final int batteryLevel = BatteryUtils.getBatteryLevel(intent);
|
||||
final int batteryStatus = intent.getIntExtra(
|
||||
BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
|
||||
final int batteryHealth = intent.getIntExtra(
|
||||
@@ -306,14 +300,6 @@ public final class DatabaseUtils {
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
private static int getBatteryLevel(Intent intent) {
|
||||
final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
|
||||
final int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
|
||||
return scale == 0
|
||||
? -1 /*invalid battery level*/
|
||||
: Math.round((level / (float) scale) * 100f);
|
||||
}
|
||||
|
||||
private static void clearMemory() {
|
||||
if (SystemClock.uptimeMillis() > CLEAR_MEMORY_THRESHOLD_MS) {
|
||||
return;
|
||||
|
Reference in New Issue
Block a user