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

Commit 7ad2c179 authored by Salvador Martinez's avatar Salvador Martinez
Browse files

Remove potential duplicate runnable + add logging

This CL removes an existing runnable for showing the low battery
warning if it hasn't finished. Additionally, it adds some logging
to the trigger method to make it possible to debug issues.

Test: PowerUITests still pass
Bug: 111374781
Change-Id: Iaaf6fa8233cc23f2e608a7968dac617eab0d5ce3
parent 07d709cf
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.os.Looper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ThreadUtils {

@@ -59,12 +60,14 @@ public class ThreadUtils {

    /**
     * Posts runnable in background using shared background thread pool.
     *
     * @Return A future of the task that can be monitored for updates or cancelled.
     */
    public static void postOnBackgroundThread(Runnable runnable) {
    public static Future postOnBackgroundThread(Runnable runnable) {
        if (sSingleThreadExecutor == null) {
            sSingleThreadExecutor = Executors.newSingleThreadExecutor();
        }
        sSingleThreadExecutor.execute(runnable);
        return sSingleThreadExecutor.submit(runnable);
    }

    /**
+23 −5
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.time.Duration;
import java.util.Arrays;
import java.util.concurrent.Future;

public class PowerUI extends SystemUI {
    static final String TAG = "PowerUI";
@@ -80,6 +81,7 @@ public class PowerUI extends SystemUI {
    private Estimate mLastEstimate;
    private boolean mLowWarningShownThisChargeCycle;
    private boolean mSevereWarningShownThisChargeCycle;
    private Future mLastShowWarningTask;

    private int mLowBatteryAlertCloseLevel;
    private final int[] mLowBatteryReminderLevels = new int[2];
@@ -247,7 +249,10 @@ public class PowerUI extends SystemUI {
                }

                // Show the correct version of low battery warning if needed
                ThreadUtils.postOnBackgroundThread(() -> {
                if (mLastShowWarningTask != null) {
                    mLastShowWarningTask.cancel(true);
                }
                mLastShowWarningTask = ThreadUtils.postOnBackgroundThread(() -> {
                    maybeShowBatteryWarning(
                            oldBatteryLevel, plugged, oldPlugged, oldBucket, bucket);
                });
@@ -276,7 +281,7 @@ public class PowerUI extends SystemUI {
                estimate = mEnhancedEstimates.getEstimate();
                mLastEstimate = estimate;
            }
            // Turbo is not always booted once SysUI is running so we have ot make sure we actually
            // Turbo is not always booted once SysUI is running so we have to make sure we actually
            // get data back
            if (estimate != null) {
                mTimeRemaining = estimate.estimateMillis;
@@ -362,6 +367,19 @@ public class PowerUI extends SystemUI {
                && (timeRemaining < mEnhancedEstimates.getSevereWarningThreshold()
                || mBatteryLevel <= critLevel);

        final boolean canShow = canShowWarning || canShowSevereWarning;
        if (DEBUG) {
            Slog.d(TAG, "Enhanced trigger is: " + canShow + "\nwith values: "
                    + " mLowWarningShownThisChargeCycle: " + mLowWarningShownThisChargeCycle
                    + " mSevereWarningShownThisChargeCycle: " + mSevereWarningShownThisChargeCycle
                    + " mEnhancedEstimates.timeremaining: " + timeRemaining
                    + " mBatteryLevel: " + mBatteryLevel
                    + " canShowWarning: " + canShowWarning
                    + " canShowSevereWarning: " + canShowSevereWarning
                    + " plugged: " + plugged
                    + " batteryStatus: " + batteryStatus
                    + " isPowerSaver: " + isPowerSaver);
        }
        return canShowWarning || canShowSevereWarning;
    }