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

Commit d53335a3 authored by Salvador Martinez's avatar Salvador Martinez Committed by android-build-merger
Browse files

Merge "Prevent estimates from updating while callbacks are happening" into qt-dev

am: 364b3849

Change-Id: I9e262f8d578e99333e5f483cf15edb5cf3ebf64d
parents c3caffd8 364b3849
Loading
Loading
Loading
Loading
+14 −11
Original line number Original line Diff line number Diff line
@@ -212,6 +212,7 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC


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


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


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

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