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

Commit 5a558b99 authored by Mill Chen's avatar Mill Chen
Browse files

Fix NullPointerException on estimate object

The severe low battery warning notification isn't working if a device
which uses time based estimates doesn't connect to Wi-Fi or mobile data.
This is caused by a null estimate object which is obtained from
EnhancedEstimates. This change is to make EnhancedEstimates return an
unknown estimate and add checking time remaining condition to see if
time remaining is valid. AOSP behavior is unchanged and this only
affects devices which use time based estimates.

Bug: 152880156
Test: atest SystemUITests
Change-Id: I8f8ac67ebe66ffd748d958d34f9b31324c52f681
parent df98b270
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
package com.android.systemui.power;

import com.android.settingslib.fuelgauge.Estimate;
import com.android.settingslib.fuelgauge.EstimateKt;

import javax.inject.Inject;
import javax.inject.Singleton;
@@ -19,7 +20,10 @@ public class EnhancedEstimatesImpl implements EnhancedEstimates {

    @Override
    public Estimate getEstimate() {
        return null;
        // Returns an unknown estimate.
        return new Estimate(EstimateKt.ESTIMATE_MILLIS_UNKNOWN,
                false /* isBasedOnUsage */,
                EstimateKt.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN);
    }

    @Override
+10 −5
Original line number Diff line number Diff line
@@ -374,8 +374,10 @@ public class PowerUI extends SystemUI implements CommandQueue.Callbacks {
            BatteryStateSnapshot lastSnapshot) {
        // if we are now over 45% battery & 6 hours remaining so we can trigger hybrid
        // notification again
        final long timeRemainingMillis = currentSnapshot.getTimeRemainingMillis();
        if (currentSnapshot.getBatteryLevel() >= CHARGE_CYCLE_PERCENT_RESET
                && currentSnapshot.getTimeRemainingMillis() > SIX_HOURS_MILLIS) {
                && (timeRemainingMillis > SIX_HOURS_MILLIS
                || timeRemainingMillis == NO_ESTIMATE_AVAILABLE)) {
            mLowWarningShownThisChargeCycle = false;
            mSevereWarningShownThisChargeCycle = false;
            if (DEBUG) {
@@ -390,8 +392,8 @@ public class PowerUI extends SystemUI implements CommandQueue.Callbacks {
            mWarnings.showLowBatteryWarning(playSound);
            // mark if we've already shown a warning this cycle. This will prevent the notification
            // trigger from spamming users by only showing low/critical warnings once per cycle
            if (currentSnapshot.getTimeRemainingMillis()
                    <= currentSnapshot.getSevereThresholdMillis()
            if ((timeRemainingMillis != NO_ESTIMATE_AVAILABLE
                    && timeRemainingMillis <= currentSnapshot.getSevereThresholdMillis())
                    || currentSnapshot.getBatteryLevel()
                    <= currentSnapshot.getSevereLevelThreshold()) {
                mSevereWarningShownThisChargeCycle = true;
@@ -426,15 +428,18 @@ public class PowerUI extends SystemUI implements CommandQueue.Callbacks {
            return false;
        }

        final long timeRemainingMillis = snapshot.getTimeRemainingMillis();
        // Only show the low warning if enabled once per charge cycle & no battery saver
        final boolean canShowWarning = snapshot.isLowWarningEnabled()
                && !mLowWarningShownThisChargeCycle && !snapshot.isPowerSaver()
                && (snapshot.getTimeRemainingMillis() < snapshot.getLowThresholdMillis()
                && ((timeRemainingMillis != NO_ESTIMATE_AVAILABLE
                && timeRemainingMillis < snapshot.getLowThresholdMillis())
                || snapshot.getBatteryLevel() <= snapshot.getLowLevelThreshold());

        // Only show the severe warning once per charge cycle
        final boolean canShowSevereWarning = !mSevereWarningShownThisChargeCycle
                && (snapshot.getTimeRemainingMillis() < snapshot.getSevereThresholdMillis()
                && ((timeRemainingMillis != NO_ESTIMATE_AVAILABLE
                && timeRemainingMillis < snapshot.getSevereThresholdMillis())
                || snapshot.getBatteryLevel() <= snapshot.getSevereLevelThreshold());

        final boolean canShow = canShowWarning || canShowSevereWarning;