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

Commit 8a4094e7 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

am: d53335a3

Change-Id: Ide9d326b81372f8ef6a293958d2fb075088cb413
parents e7c846cd d53335a3
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);
            }