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

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

Merge "Remove potential duplicate runnable + add logging"

parents 21348934 7ad2c179
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;
    }