Extract method getBatteryInfo() from loader
In battery tips, we need synchronized method to get batteryInfo. This cl move the logic to BatteryUtils, then in battery tips we could use this method directly. Bug: 70570352 Test: robotest still pass Change-Id: Id69349a25395ae472a9b3152c6f04127ee32c4e1
This commit is contained in:
@@ -16,15 +16,8 @@
|
||||
package com.android.settings.fuelgauge;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settingslib.utils.AsyncLoader;
|
||||
|
||||
/**
|
||||
@@ -49,40 +42,7 @@ public class BatteryInfoLoader extends AsyncLoader<BatteryInfo>{
|
||||
|
||||
@Override
|
||||
public BatteryInfo loadInBackground() {
|
||||
final long startTime = System.currentTimeMillis();
|
||||
Context context = getContext();
|
||||
PowerUsageFeatureProvider powerUsageFeatureProvider =
|
||||
FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context);
|
||||
|
||||
// Stuff we always need to get BatteryInfo
|
||||
Intent batteryBroadcast = context.registerReceiver(null,
|
||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
final long elapsedRealtimeUs = BatteryUtils.convertMsToUs(SystemClock.elapsedRealtime());
|
||||
BatteryInfo batteryInfo;
|
||||
|
||||
// 0 means we are discharging, anything else means charging
|
||||
boolean discharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;
|
||||
// Get enhanced prediction if available and discharging, otherwise use the old code
|
||||
Cursor cursor = null;
|
||||
if (discharging && powerUsageFeatureProvider != null &&
|
||||
powerUsageFeatureProvider.isEnhancedBatteryPredictionEnabled(context)) {
|
||||
final Uri queryUri = powerUsageFeatureProvider.getEnhancedBatteryPredictionUri();
|
||||
cursor = context.getContentResolver().query(queryUri, null, null, null, null);
|
||||
}
|
||||
BatteryStats stats = mStatsHelper.getStats();
|
||||
BatteryUtils.logRuntime(LOG_TAG, "BatteryInfoLoader post query", startTime);
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
long enhancedEstimate = powerUsageFeatureProvider.getTimeRemainingEstimate(cursor);
|
||||
batteryInfo = BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats,
|
||||
elapsedRealtimeUs, false /* shortString */,
|
||||
BatteryUtils.convertMsToUs(enhancedEstimate), true /* basedOnUsage */);
|
||||
} else {
|
||||
batteryInfo = BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats,
|
||||
elapsedRealtimeUs, false /* shortString */,
|
||||
discharging ? stats.computeBatteryTimeRemaining(elapsedRealtimeUs) : 0,
|
||||
false /* basedOnUsage */);
|
||||
}
|
||||
BatteryUtils.logRuntime(LOG_TAG, "BatteryInfoLoader.loadInBackground", startTime);
|
||||
return batteryInfo;
|
||||
final BatteryUtils batteryUtils = BatteryUtils.getInstance(getContext());
|
||||
return batteryUtils.getBatteryInfo(mStatsHelper, LOG_TAG);
|
||||
}
|
||||
}
|
||||
|
@@ -17,8 +17,13 @@ package com.android.settings.fuelgauge;
|
||||
|
||||
import android.app.AppOpsManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.Bundle;
|
||||
import android.os.Build;
|
||||
@@ -28,6 +33,7 @@ import android.support.annotation.IntDef;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.StringRes;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.annotation.WorkerThread;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.Log;
|
||||
import android.util.SparseLongArray;
|
||||
@@ -73,6 +79,7 @@ public class BatteryUtils {
|
||||
|
||||
private PackageManager mPackageManager;
|
||||
private AppOpsManager mAppOpsManager;
|
||||
private Context mContext;
|
||||
@VisibleForTesting
|
||||
PowerUsageFeatureProvider mPowerUsageFeatureProvider;
|
||||
|
||||
@@ -85,6 +92,7 @@ public class BatteryUtils {
|
||||
|
||||
@VisibleForTesting
|
||||
BatteryUtils(Context context) {
|
||||
mContext = context.getApplicationContext();
|
||||
mPackageManager = context.getPackageManager();
|
||||
mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
|
||||
mPowerUsageFeatureProvider = FeatureFactory.getFactory(
|
||||
@@ -388,6 +396,44 @@ public class BatteryUtils {
|
||||
statsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, userManager.getUserProfiles());
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
public BatteryInfo getBatteryInfo(final BatteryStatsHelper statsHelper, final String tag) {
|
||||
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 long elapsedRealtimeUs = BatteryUtils.convertMsToUs(SystemClock.elapsedRealtime());
|
||||
BatteryInfo batteryInfo;
|
||||
|
||||
// 0 means we are discharging, anything else means charging
|
||||
final boolean discharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
|
||||
== 0;
|
||||
// Get enhanced prediction if available and discharging, otherwise use the old code
|
||||
Cursor cursor = null;
|
||||
if (discharging && mPowerUsageFeatureProvider != null &&
|
||||
mPowerUsageFeatureProvider.isEnhancedBatteryPredictionEnabled(mContext)) {
|
||||
final Uri queryUri = mPowerUsageFeatureProvider.getEnhancedBatteryPredictionUri();
|
||||
cursor = mContext.getContentResolver().query(queryUri, null, null, null, null);
|
||||
}
|
||||
final BatteryStats stats = statsHelper.getStats();
|
||||
BatteryUtils.logRuntime(tag, "BatteryInfoLoader post query", startTime);
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
long enhancedEstimate = mPowerUsageFeatureProvider.getTimeRemainingEstimate(cursor);
|
||||
batteryInfo = BatteryInfo.getBatteryInfo(mContext, batteryBroadcast, stats,
|
||||
elapsedRealtimeUs, false /* shortString */,
|
||||
BatteryUtils.convertMsToUs(enhancedEstimate), true /* basedOnUsage */);
|
||||
} else {
|
||||
batteryInfo = BatteryInfo.getBatteryInfo(mContext, batteryBroadcast, stats,
|
||||
elapsedRealtimeUs, false /* shortString */,
|
||||
discharging ? stats.computeBatteryTimeRemaining(elapsedRealtimeUs) : 0,
|
||||
false /* basedOnUsage */);
|
||||
}
|
||||
BatteryUtils.logRuntime(tag, "BatteryInfoLoader.loadInBackground", startTime);
|
||||
|
||||
return batteryInfo;
|
||||
}
|
||||
|
||||
private boolean isDataCorrupted() {
|
||||
return mPackageManager == null || mAppOpsManager == null;
|
||||
}
|
||||
|
@@ -198,7 +198,7 @@ public class AdvancedPowerUsageDetailTest {
|
||||
doReturn(mPackageManager).when(mTestActivity).getPackageManager();
|
||||
doReturn(mAppOpsManager).when(mTestActivity).getSystemService(Context.APP_OPS_SERVICE);
|
||||
|
||||
mBatteryUtils = spy(BatteryUtils.getInstance(mTestActivity));
|
||||
mBatteryUtils = spy(new BatteryUtils(mContext));
|
||||
doReturn(FOREGROUND_SERVICE_TIME_US).when(mBatteryUtils).getForegroundServiceTotalTimeUs(
|
||||
any(BatteryStats.Uid.class), anyLong());
|
||||
|
||||
|
Reference in New Issue
Block a user