Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c12b9500 authored by ykhung's avatar ykhung Committed by YK Hung
Browse files

Refine the BatteryUsageStats close() method invoke timing

Refine invoke batteryUsageStats.close() timing to 1) avoid closing it before the UI is ready to ignore showing the battery usage app list data for AOSP version, and 2) avoid close the BatteryUsageStats which is passed from caller components in the BatteryInfo with unexpected behavior.

Bug: 220717612
Test: make RunSettingsRoboTests -j56 ROBOTEST_FILTER="com.android.settings.fuelgauge"
Change-Id: I0f75580ac68d72b41fa9a7f43aa6ea694f0992ff
parent 7828dda5
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -153,6 +153,7 @@ public class BatteryInfo {
        new AsyncTask<Void, Void, BatteryInfo>() {
            @Override
            protected BatteryInfo doInBackground(Void... params) {
                boolean shouldCloseBatteryUsageStats = false;
                BatteryUsageStats stats;
                if (batteryUsageStats != null) {
                    stats = batteryUsageStats;
@@ -160,6 +161,7 @@ public class BatteryInfo {
                    try {
                        stats = context.getSystemService(BatteryStatsManager.class)
                                .getBatteryUsageStats();
                        shouldCloseBatteryUsageStats = true;
                    } catch (RuntimeException e) {
                        Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e);
                        // Use default BatteryUsageStats.
@@ -168,11 +170,13 @@ public class BatteryInfo {
                }
                final BatteryInfo batteryInfo =
                        getBatteryInfo(context, stats, shortString);
                if (shouldCloseBatteryUsageStats) {
                    try {
                        stats.close();
                    } catch (Exception e) {
                        Log.e(TAG, "BatteryUsageStats.close() failed", e);
                    }
                }
                return batteryInfo;
            }

+15 −10
Original line number Diff line number Diff line
@@ -83,6 +83,7 @@ public abstract class PowerUsageBase extends DashboardFragment {
    public void onStop() {
        super.onStop();
        mBatteryBroadcastReceiver.unRegister();
        closeBatteryUsageStatsIfNeeded();
    }

    protected void restartBatteryStatsLoader(int refreshType) {
@@ -104,16 +105,6 @@ 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
@@ -130,6 +121,7 @@ public abstract class PowerUsageBase extends DashboardFragment {
        @Override
        public void onLoadFinished(Loader<BatteryUsageStats> loader,
                BatteryUsageStats batteryUsageStats) {
            closeBatteryUsageStatsIfNeeded();
            mBatteryUsageStats = batteryUsageStats;
            PowerUsageBase.this.onLoadFinished(mRefreshType);
        }
@@ -138,4 +130,17 @@ public abstract class PowerUsageBase extends DashboardFragment {
        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;
        }
    }
}