Merge "Remove potential duplicate runnable + add logging"

This commit is contained in:
TreeHugger Robot
2019-02-12 06:21:21 +00:00
committed by Android (Google) Code Review
2 changed files with 27 additions and 6 deletions

View File

@@ -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);
}
/**

View File

@@ -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;
@@ -355,13 +360,26 @@ public class PowerUI extends SystemUI {
// Only show the low warning once per charge cycle & no battery saver
final boolean canShowWarning = !mLowWarningShownThisChargeCycle && !isPowerSaver
&& (timeRemaining < mEnhancedEstimates.getLowWarningThreshold()
|| mBatteryLevel <= warnLevel);
|| mBatteryLevel <= warnLevel);
// Only show the severe warning once per charge cycle
final boolean canShowSevereWarning = !mSevereWarningShownThisChargeCycle
&& (timeRemaining < mEnhancedEstimates.getSevereWarningThreshold()
|| mBatteryLevel <= critLevel);
|| 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;
}