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

Commit 364b3849 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

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

parents 58bb67ab af36fb3c
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);
            }