Invoke close() method for BatteryUsageStats to close cursor window
invoke close() method for BatteryUsageStats to close cursor window, since BatteryUsageStats will implement "Closeable" interface in T. (from Dimitri's request) Bug: 201229421 Test: make SettingsRoboTests Change-Id: Ibfca404d00cce51405cffddb636f685869ac130b
This commit is contained in:
@@ -167,7 +167,14 @@ public class BatteryInfo {
|
||||
new String[0], /* includePowerModels */ false).build();
|
||||
}
|
||||
}
|
||||
return getBatteryInfo(context, stats, shortString);
|
||||
final BatteryInfo batteryInfo =
|
||||
getBatteryInfo(context, stats, shortString);
|
||||
try {
|
||||
stats.close();
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "BatteryUsageStats.close() failed", e);
|
||||
}
|
||||
return batteryInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -405,6 +405,11 @@ public class BatteryUtils {
|
||||
batteryUsageStats, estimate, elapsedRealtimeUs, false /* shortString */);
|
||||
BatteryUtils.logRuntime(tag, "BatteryInfoLoader.loadInBackground", startTime);
|
||||
|
||||
try {
|
||||
batteryUsageStats.close();
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "BatteryUsageStats.close() failed", e);
|
||||
}
|
||||
return batteryInfo;
|
||||
}
|
||||
|
||||
|
@@ -22,6 +22,7 @@ import android.os.BatteryStats;
|
||||
import android.os.BatteryStatsManager;
|
||||
import android.os.BatteryUsageStats;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
@@ -34,6 +35,8 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DebugEstimatesLoader extends AsyncLoaderCompat<List<BatteryInfo>> {
|
||||
private static final String TAG = "DebugEstimatesLoader";
|
||||
|
||||
private BatteryStatsHelper mStatsHelper;
|
||||
|
||||
public DebugEstimatesLoader(Context context, BatteryStatsHelper statsHelper) {
|
||||
@@ -58,8 +61,16 @@ public class DebugEstimatesLoader extends AsyncLoaderCompat<List<BatteryInfo>> {
|
||||
Intent batteryBroadcast = getContext().registerReceiver(null,
|
||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
BatteryStats stats = mStatsHelper.getStats();
|
||||
BatteryUsageStats batteryUsageStats =
|
||||
context.getSystemService(BatteryStatsManager.class).getBatteryUsageStats();
|
||||
BatteryUsageStats batteryUsageStats;
|
||||
try {
|
||||
batteryUsageStats = context.getSystemService(BatteryStatsManager.class)
|
||||
.getBatteryUsageStats();
|
||||
} catch (RuntimeException e) {
|
||||
Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e);
|
||||
// Use default BatteryUsageStats.
|
||||
batteryUsageStats = new BatteryUsageStats.Builder(
|
||||
new String[0], /* includePowerModels */ false).build();
|
||||
}
|
||||
BatteryInfo oldinfo = BatteryInfo.getBatteryInfoOld(getContext(), batteryBroadcast,
|
||||
batteryUsageStats, elapsedRealtimeUs, false);
|
||||
|
||||
@@ -74,6 +85,12 @@ public class DebugEstimatesLoader extends AsyncLoaderCompat<List<BatteryInfo>> {
|
||||
List<BatteryInfo> infos = new ArrayList<>();
|
||||
infos.add(oldinfo);
|
||||
infos.add(newInfo);
|
||||
|
||||
try {
|
||||
batteryUsageStats.close();
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "BatteryUsageStats.close() failed", e);
|
||||
}
|
||||
return infos;
|
||||
}
|
||||
}
|
||||
|
@@ -22,6 +22,7 @@ import android.content.Context;
|
||||
import android.os.BatteryUsageStats;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
@@ -103,6 +104,16 @@ public abstract class PowerUsageBase extends DashboardFragment {
|
||||
final long startTime = System.currentTimeMillis();
|
||||
historyPref.setBatteryUsageStats(mBatteryUsageStats);
|
||||
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
|
||||
|
Reference in New Issue
Block a user