Merge "Refine the BatteryUsageStats close() method invoke timing" into tm-dev am: 55fcb02aca

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/18030883

Change-Id: Ife069490f25bb4b7fbabf49992537711b21d5266
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
YK Hung
2022-04-29 17:12:15 +00:00
committed by Automerger Merge Worker
2 changed files with 23 additions and 14 deletions

View File

@@ -153,6 +153,7 @@ public class BatteryInfo {
new AsyncTask<Void, Void, BatteryInfo>() { new AsyncTask<Void, Void, BatteryInfo>() {
@Override @Override
protected BatteryInfo doInBackground(Void... params) { protected BatteryInfo doInBackground(Void... params) {
boolean shouldCloseBatteryUsageStats = false;
BatteryUsageStats stats; BatteryUsageStats stats;
if (batteryUsageStats != null) { if (batteryUsageStats != null) {
stats = batteryUsageStats; stats = batteryUsageStats;
@@ -160,6 +161,7 @@ public class BatteryInfo {
try { try {
stats = context.getSystemService(BatteryStatsManager.class) stats = context.getSystemService(BatteryStatsManager.class)
.getBatteryUsageStats(); .getBatteryUsageStats();
shouldCloseBatteryUsageStats = true;
} catch (RuntimeException e) { } catch (RuntimeException e) {
Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e); Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e);
// Use default BatteryUsageStats. // Use default BatteryUsageStats.
@@ -168,10 +170,12 @@ public class BatteryInfo {
} }
final BatteryInfo batteryInfo = final BatteryInfo batteryInfo =
getBatteryInfo(context, stats, shortString); getBatteryInfo(context, stats, shortString);
try { if (shouldCloseBatteryUsageStats) {
stats.close(); try {
} catch (Exception e) { stats.close();
Log.e(TAG, "BatteryUsageStats.close() failed", e); } catch (Exception e) {
Log.e(TAG, "BatteryUsageStats.close() failed", e);
}
} }
return batteryInfo; return batteryInfo;
} }

View File

@@ -83,6 +83,7 @@ public abstract class PowerUsageBase extends DashboardFragment {
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
mBatteryBroadcastReceiver.unRegister(); mBatteryBroadcastReceiver.unRegister();
closeBatteryUsageStatsIfNeeded();
} }
protected void restartBatteryStatsLoader(int refreshType) { protected void restartBatteryStatsLoader(int refreshType) {
@@ -104,16 +105,6 @@ public abstract class PowerUsageBase extends DashboardFragment {
final long startTime = System.currentTimeMillis(); final long startTime = System.currentTimeMillis();
historyPref.setBatteryUsageStats(mBatteryUsageStats); historyPref.setBatteryUsageStats(mBatteryUsageStats);
BatteryUtils.logRuntime(TAG, "updatePreference", startTime); BatteryUtils.logRuntime(TAG, "updatePreference", startTime);
if (mBatteryUsageStats == null) {
return;
}
try {
mBatteryUsageStats.close();
} catch (Exception e) {
Log.e(TAG, "BatteryUsageStats.close() failed", e);
} finally {
mBatteryUsageStats = null;
}
} }
private class BatteryUsageStatsLoaderCallbacks private class BatteryUsageStatsLoaderCallbacks
@@ -130,6 +121,7 @@ public abstract class PowerUsageBase extends DashboardFragment {
@Override @Override
public void onLoadFinished(Loader<BatteryUsageStats> loader, public void onLoadFinished(Loader<BatteryUsageStats> loader,
BatteryUsageStats batteryUsageStats) { BatteryUsageStats batteryUsageStats) {
closeBatteryUsageStatsIfNeeded();
mBatteryUsageStats = batteryUsageStats; mBatteryUsageStats = batteryUsageStats;
PowerUsageBase.this.onLoadFinished(mRefreshType); PowerUsageBase.this.onLoadFinished(mRefreshType);
} }
@@ -138,4 +130,17 @@ public abstract class PowerUsageBase extends DashboardFragment {
public void onLoaderReset(Loader<BatteryUsageStats> loader) { public void onLoaderReset(Loader<BatteryUsageStats> loader) {
} }
} }
private void closeBatteryUsageStatsIfNeeded() {
if (mBatteryUsageStats == null) {
return;
}
try {
mBatteryUsageStats.close();
} catch (Exception e) {
Log.e(TAG, "BatteryUsageStats.close() failed", e);
} finally {
mBatteryUsageStats = null;
}
}
} }