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

Commit 415f35b6 authored by Salvador Martinez's avatar Salvador Martinez
Browse files

Update BatteryUtils to update system battery estimate cache

To try and ensure SysUI and Settings don't show different battery
estimates we have settings always push the estimate to the system
cache which will cause the status bar to update as well.

Test: manually trigger battery broadcasts and observe
Bug: 124030091
Change-Id: I07a17ecf2a42c3022673ad2aca438f8d6e81b41f
parent f3365e91
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -193,6 +193,7 @@ public class BatteryInfo {
                && provider.isEnhancedBatteryPredictionEnabled(context)) {
            Estimate estimate = provider.getEnhancedBatteryPrediction(context);
            if (estimate != null) {
                Estimate.storeCachedEstimate(context, estimate);
                BatteryUtils
                        .logRuntime(LOG_TAG, "time for enhanced BatteryInfo", startTime);
                return BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats,
+3 −2
Original line number Diff line number Diff line
@@ -450,7 +450,6 @@ public class BatteryUtils {
                SystemClock.elapsedRealtime());
        final BatteryStats stats = statsHelper.getStats();
        BatteryInfo batteryInfo;

        Estimate estimate = null;
        // Get enhanced prediction if available
        if (mPowerUsageFeatureProvider != null &&
@@ -458,7 +457,9 @@ public class BatteryUtils {
            estimate = mPowerUsageFeatureProvider.getEnhancedBatteryPrediction(mContext);
        }

        if (estimate == null) {
        if (estimate != null) {
            Estimate.storeCachedEstimate(mContext, estimate);
        } else {
            estimate = new Estimate(
                    PowerUtil.convertUsToMs(stats.computeBatteryTimeRemaining(elapsedRealtimeUs)),
                    false /* isBasedOnUsage */,
+27 −0
Original line number Diff line number Diff line
@@ -20,9 +20,14 @@ import static com.android.settings.fuelgauge.BatteryBroadcastReceiver.BatteryUpd

import android.app.settings.SettingsEnums;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.BatteryStats;
import android.os.Bundle;
import android.os.Handler;
import android.provider.SearchIndexableResource;
import android.provider.Settings;
import android.provider.Settings.Global;
import android.text.format.Formatter;
import android.view.Menu;
import android.view.MenuInflater;
@@ -102,6 +107,13 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
    BatteryTipPreferenceController mBatteryTipPreferenceController;
    private int mStatsType = BatteryStats.STATS_SINCE_CHARGED;

    private final ContentObserver mSettingsObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            restartBatteryInfoLoader();
        }
    };

    @VisibleForTesting
    LoaderManager.LoaderCallbacks<BatteryInfo> mBatteryInfoLoaderCallbacks =
            new LoaderManager.LoaderCallbacks<BatteryInfo>() {
@@ -189,6 +201,21 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
                }
            };

    @Override
    public void onStop() {
        getContentResolver().unregisterContentObserver(mSettingsObserver);
        super.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
        getContentResolver().registerContentObserver(
                Global.getUriFor(Global.BATTERY_ESTIMATES_LAST_UPDATE_TIME),
                false,
                mSettingsObserver);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);