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

Commit ad346e12 authored by ykhung's avatar ykhung Committed by YUKAI HUNG
Browse files

[Robustness] protect get battery stats from DeadSystemException

In some corner cases, we will receive the DeadSystemException from
BatteryStatService when invoking getBatteryUsageStats() method. Before
this issue is resolved by BatteryStatService team, we will add some
protections to avoid Settings app is crashed.

Bug: 195306545
Bug: 195467687
Test: make SettingsRoboTests
Change-Id: I75fcf63f4f69d86d6dce0e12bd4d738b1219ae47
parent 7863a996
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.os.BatteryStatsManager;
import android.os.BatteryUsageStats;
import android.os.SystemClock;
import android.text.format.Formatter;
import android.util.Log;
import android.util.SparseIntArray;

import androidx.annotation.NonNull;
@@ -42,6 +43,7 @@ import com.android.settingslib.utils.PowerUtil;
import com.android.settingslib.utils.StringUtil;

public class BatteryInfo {
    private static final String TAG = "BatteryInfo";

    public CharSequence chargeLabel;
    public CharSequence remainingLabel;
@@ -155,8 +157,13 @@ public class BatteryInfo {
                if (batteryUsageStats != null) {
                    stats = batteryUsageStats;
                } else {
                    try {
                        stats = context.getSystemService(BatteryStatsManager.class)
                                .getBatteryUsageStats();
                    } catch (RuntimeException e) {
                        Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e);
                        return null;
                    }
                }
                return getBatteryInfo(context, stats, shortString);
            }
+8 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.content.Context;
import android.os.BatteryStatsManager;
import android.os.BatteryUsageStats;
import android.os.BatteryUsageStatsQuery;
import android.util.Log;

import com.android.settingslib.utils.AsyncLoaderCompat;

@@ -27,6 +28,7 @@ import com.android.settingslib.utils.AsyncLoaderCompat;
 * Loader to get new {@link BatteryUsageStats} in the background
 */
public class BatteryUsageStatsLoader extends AsyncLoaderCompat<BatteryUsageStats> {
    private static final String TAG = "BatteryUsageStatsLoader";
    private final BatteryStatsManager mBatteryStatsManager;
    private final boolean mIncludeBatteryHistory;

@@ -42,7 +44,12 @@ public class BatteryUsageStatsLoader extends AsyncLoaderCompat<BatteryUsageStats
        if (mIncludeBatteryHistory) {
            builder.includeBatteryHistory();
        }
        try {
            return mBatteryStatsManager.getBatteryUsageStats(builder.build());
        } catch (RuntimeException e) {
            Log.e(TAG, "loadInBackground() for getBatteryUsageStats()", e);
            return null;
        }
    }

    @Override
+8 −2
Original line number Diff line number Diff line
@@ -368,8 +368,14 @@ public class BatteryUtils {
    public BatteryInfo getBatteryInfo(final String tag) {
        final BatteryStatsManager systemService = mContext.getSystemService(
                BatteryStatsManager.class);
        final BatteryUsageStats batteryUsageStats = systemService.getBatteryUsageStats(
        BatteryUsageStats batteryUsageStats;
        try {
            batteryUsageStats = systemService.getBatteryUsageStats(
                    new BatteryUsageStatsQuery.Builder().includeBatteryHistory().build());
        } catch (RuntimeException e) {
            Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e);
            return null;
        }

        final long startTime = System.currentTimeMillis();

+9 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings.Global;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
import androidx.loader.app.LoaderManager;
@@ -103,6 +104,10 @@ public class PowerUsageSummary extends PowerUsageBase implements

                @Override
                public void onLoadFinished(Loader<BatteryInfo> loader, BatteryInfo batteryInfo) {
                    if (batteryInfo == null) {
                        Log.w(TAG, "mBatteryInfoLoaderCallbacks: batteryInfo = null");
                        return;
                    }
                    mBatteryHeaderPreferenceController.updateHeaderPreference(batteryInfo);
                    mBatteryHeaderPreferenceController.updateHeaderByBatteryTips(
                            mBatteryTipPreferenceController.getCurrentBatteryTip(), batteryInfo);
@@ -126,6 +131,10 @@ public class PowerUsageSummary extends PowerUsageBase implements
                @Override
                public void onLoadFinished(Loader<List<BatteryTip>> loader,
                        List<BatteryTip> data) {
                    if (mBatteryInfo == null) {
                        Log.w(TAG, "mBatteryTipsCallbacks: batteryInfo = null");
                        return;
                    }
                    mBatteryTipPreferenceController.updateBatteryTips(data);
                    mBatteryHeaderPreferenceController.updateHeaderByBatteryTips(
                            mBatteryTipPreferenceController.getCurrentBatteryTip(), mBatteryInfo);
+7 −5
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings.fuelgauge.batterytip;

import android.content.Context;
import android.os.BatteryUsageStats;
import android.util.Log;

import androidx.annotation.VisibleForTesting;

@@ -67,16 +68,17 @@ public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> {
        final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(TAG);
        final Context context = getContext();

        tips.add(new EarlyWarningDetector(policy, context).detect());
        if (batteryInfo == null) {
            Log.w(TAG, "loadInBackground() batteryInfo = null");
            return tips;
        }

        tips.add(new LowBatteryDetector(context, policy, batteryInfo).detect());
        tips.add(new HighUsageDetector(context, policy, mBatteryUsageStats, batteryInfo).detect());
        tips.add(new SmartBatteryDetector(
                context, policy, batteryInfo, context.getContentResolver()).detect());
        tips.add(new EarlyWarningDetector(policy, context).detect());
        tips.add(new BatteryDefenderDetector(batteryInfo).detect());
        // Disable this feature now since it introduces false positive cases. We will try to improve
        // it in the future.
        // tips.add(new RestrictAppDetector(context, policy).detect());

        Collections.sort(tips);
        return tips;
    }