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

Commit 373400a5 authored by Salvador Martinez's avatar Salvador Martinez Committed by android-build-team Robot
Browse files

Prevent estimates from updating while callbacks are happening

It seems that for some people the estimate was being set to null
as part of the process to update the estimate as they were trying
to access it. This CL makes it so that we don't allow callbacks
to start if we are currently updating which will ensure that this
doesn't happen again.

Test: repro'd issue without change, issue gone with change
Bug: 131607084
Change-Id: I938d05a3977bb4bba2c5d28e800d94120b8425d2
(cherry picked from commit af36fb3c)
parent a978ba17
Loading
Loading
Loading
Loading
+14 −11
Original line number Diff line number Diff line
@@ -212,6 +212,7 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC

    @Nullable
    private String generateTimeRemainingString() {
        synchronized (mFetchCallbacks) {
            if (mEstimate == null) {
                return null;
            }
@@ -220,6 +221,7 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC
            return PowerUtil.getBatteryRemainingShortStringFormatted(
                    mContext, mEstimate.getEstimateMillis());
        }
    }

    private void updateEstimateInBackground() {
        if (mFetchingEstimate) {
@@ -230,19 +232,20 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC
        mFetchingEstimate = true;
        Dependency.get(Dependency.BG_HANDLER).post(() -> {
            // Only fetch the estimate if they are enabled
            synchronized (mFetchCallbacks) {
                mEstimate = null;
                if (mEstimates.isHybridNotificationEnabled()) {
                    updateEstimate();
                }
            }
            mFetchingEstimate = false;
            Dependency.get(Dependency.MAIN_HANDLER).post(this::notifyEstimateFetchCallbacks);
        });
    }

    private void notifyEstimateFetchCallbacks() {
        String estimate = generateTimeRemainingString();

        synchronized (mFetchCallbacks) {
            String estimate = generateTimeRemainingString();
            for (EstimateFetchCompletion completion : mFetchCallbacks) {
                completion.onBatteryRemainingEstimateRetrieved(estimate);
            }