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

Commit eeda14fb authored by Wesley.CW Wang's avatar Wesley.CW Wang Committed by Wesley Wang
Browse files

Make AOD battery text field support BatteryDefender

 - Reupload CL from ag/13090497 to fix the merge conflict
 - Add overheat flag into BatteryStatus
 - Update AOD battery indication to support new state when BatteryDefender is enable
 Screenshot: https://screenshot.googleplex.com/BYyUYS7VGtyG3i7.png

Bug: 173080412
Test: atest SystemUITests:com.android.systemui.statusbar.KeyguardIndicationControllerTest
Change-Id: I74f32136ba034d2dea19f71e1e7cecc022cd9fa9
parent 2b1787f2
Loading
Loading
Loading
Loading
+10 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package com.android.settingslib.fuelgauge;
package com.android.settingslib.fuelgauge;


import static android.os.BatteryManager.BATTERY_HEALTH_OVERHEAT;
import static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN;
import static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN;
import static android.os.BatteryManager.BATTERY_STATUS_FULL;
import static android.os.BatteryManager.BATTERY_STATUS_FULL;
import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
@@ -127,6 +128,15 @@ public class BatteryStatus {
        return level < LOW_BATTERY_THRESHOLD;
        return level < LOW_BATTERY_THRESHOLD;
    }
    }


    /**
     * Whether battery is overheated.
     *
     * @return true if battery is overheated
     */
    public boolean isOverheated() {
        return health == BATTERY_HEALTH_OVERHEAT;
    }

    /**
    /**
     * Return current chargin speed is fast, slow or normal.
     * Return current chargin speed is fast, slow or normal.
     *
     *
+5 −0
Original line number Original line Diff line number Diff line
@@ -2686,6 +2686,11 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
            return true;
            return true;
        }
        }


        // change in battery overheat
        if (current.health != old.health) {
            return true;
        }

        return false;
        return false;
    }
    }


+14 −6
Original line number Original line Diff line number Diff line
@@ -117,6 +117,8 @@ public class KeyguardIndicationController implements StateListener,
    private boolean mPowerPluggedIn;
    private boolean mPowerPluggedIn;
    private boolean mPowerPluggedInWired;
    private boolean mPowerPluggedInWired;
    private boolean mPowerCharged;
    private boolean mPowerCharged;
    private boolean mBatteryOverheated;
    private boolean mEnableBatteryDefender;
    private int mChargingSpeed;
    private int mChargingSpeed;
    private int mChargingWattage;
    private int mChargingWattage;
    private int mBatteryLevel;
    private int mBatteryLevel;
@@ -410,7 +412,7 @@ public class KeyguardIndicationController implements StateListener,
            } else if (!TextUtils.isEmpty(mAlignmentIndication)) {
            } else if (!TextUtils.isEmpty(mAlignmentIndication)) {
                mTextView.switchIndication(mAlignmentIndication);
                mTextView.switchIndication(mAlignmentIndication);
                mTextView.setTextColor(mContext.getColor(R.color.misalignment_text_color));
                mTextView.setTextColor(mContext.getColor(R.color.misalignment_text_color));
            } else if (mPowerPluggedIn) {
            } else if (mPowerPluggedIn || mEnableBatteryDefender) {
                String indication = computePowerIndication();
                String indication = computePowerIndication();
                if (animate) {
                if (animate) {
                    animateText(mTextView, indication);
                    animateText(mTextView, indication);
@@ -430,7 +432,7 @@ public class KeyguardIndicationController implements StateListener,
        String trustManagedIndication = getTrustManagedIndication();
        String trustManagedIndication = getTrustManagedIndication();


        String powerIndication = null;
        String powerIndication = null;
        if (mPowerPluggedIn) {
        if (mPowerPluggedIn || mEnableBatteryDefender) {
            powerIndication = computePowerIndication();
            powerIndication = computePowerIndication();
        }
        }


@@ -465,7 +467,7 @@ public class KeyguardIndicationController implements StateListener,
            mTextView.switchIndication(mAlignmentIndication);
            mTextView.switchIndication(mAlignmentIndication);
            isError = true;
            isError = true;
            hideIndication = !mBatteryPresent;
            hideIndication = !mBatteryPresent;
        } else if (mPowerPluggedIn) {
        } else if (mPowerPluggedIn || mEnableBatteryDefender) {
            if (DEBUG_CHARGING_SPEED) {
            if (DEBUG_CHARGING_SPEED) {
                powerIndication += ",  " + (mChargingWattage / 1000) + " mW";
                powerIndication += ",  " + (mChargingWattage / 1000) + " mW";
            }
            }
@@ -545,8 +547,14 @@ public class KeyguardIndicationController implements StateListener,
            return mContext.getResources().getString(R.string.keyguard_charged);
            return mContext.getResources().getString(R.string.keyguard_charged);
        }
        }


        final boolean hasChargingTime = mChargingTimeRemaining > 0;
        int chargingId;
        int chargingId;
        String percentage = NumberFormat.getPercentInstance().format(mBatteryLevel / 100f);
        if (mBatteryOverheated) {
            chargingId = R.string.keyguard_plugged_in_charging_limited;
            return mContext.getResources().getString(chargingId, percentage);
        }

        final boolean hasChargingTime = mChargingTimeRemaining > 0;
        if (mPowerPluggedInWired) {
        if (mPowerPluggedInWired) {
            switch (mChargingSpeed) {
            switch (mChargingSpeed) {
                case BatteryStatus.CHARGING_FAST:
                case BatteryStatus.CHARGING_FAST:
@@ -571,8 +579,6 @@ public class KeyguardIndicationController implements StateListener,
                    : R.string.keyguard_plugged_in_wireless;
                    : R.string.keyguard_plugged_in_wireless;
        }
        }


        String percentage = NumberFormat.getPercentInstance()
                .format(mBatteryLevel / 100f);
        if (hasChargingTime) {
        if (hasChargingTime) {
            String chargingTimeFormatted = Formatter.formatShortElapsedTimeRoundingUpToMinutes(
            String chargingTimeFormatted = Formatter.formatShortElapsedTimeRoundingUpToMinutes(
                    mContext, mChargingTimeRemaining);
                    mContext, mChargingTimeRemaining);
@@ -692,6 +698,8 @@ public class KeyguardIndicationController implements StateListener,
            mChargingSpeed = status.getChargingSpeed(mContext);
            mChargingSpeed = status.getChargingSpeed(mContext);
            mBatteryLevel = status.level;
            mBatteryLevel = status.level;
            mBatteryPresent = status.present;
            mBatteryPresent = status.present;
            mBatteryOverheated = status.isOverheated();
            mEnableBatteryDefender = mBatteryOverheated && status.isPluggedIn();
            try {
            try {
                mChargingTimeRemaining = mPowerPluggedIn
                mChargingTimeRemaining = mPowerPluggedIn
                        ? mBatteryInfo.computeChargeTimeRemaining() : -1;
                        ? mBatteryInfo.computeChargeTimeRemaining() : -1;
+65 −0
Original line number Original line Diff line number Diff line
@@ -82,6 +82,7 @@ import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.MockitoAnnotations;


import java.text.NumberFormat;
import java.util.Collections;
import java.util.Collections;


@SmallTest
@SmallTest
@@ -546,4 +547,68 @@ public class KeyguardIndicationControllerTest extends SysuiTestCase {
                pluggedIndication, powerIndication);
                pluggedIndication, powerIndication);
        assertThat(mTextView.getText()).isEqualTo(pluggedIndication);
        assertThat(mTextView.getText()).isEqualTo(pluggedIndication);
    }
    }

    @Test
    public void onRefreshBatteryInfo_chargingWithOverheat_presentChargingLimited() {
        createController();
        BatteryStatus status = new BatteryStatus(BatteryManager.BATTERY_STATUS_CHARGING,
                80 /* level */, BatteryManager.BATTERY_PLUGGED_AC,
                BatteryManager.BATTERY_HEALTH_OVERHEAT, 0 /* maxChargingWattage */,
                true /* present */);

        mController.getKeyguardCallback().onRefreshBatteryInfo(status);
        mController.setVisible(true);

        String percentage = NumberFormat.getPercentInstance().format(80 / 100f);
        String pluggedIndication = mContext.getString(
                R.string.keyguard_plugged_in_charging_limited, percentage);
        assertThat(mTextView.getText()).isEqualTo(pluggedIndication);
    }

    @Test
    public void onRefreshBatteryInfo_pluggedWithOverheat_presentChargingLimited() {
        createController();
        BatteryStatus status = new BatteryStatus(BatteryManager.BATTERY_STATUS_DISCHARGING,
                80 /* level */, BatteryManager.BATTERY_PLUGGED_AC,
                BatteryManager.BATTERY_HEALTH_OVERHEAT, 0 /* maxChargingWattage */,
                true /* present */);

        mController.getKeyguardCallback().onRefreshBatteryInfo(status);
        mController.setVisible(true);

        String percentage = NumberFormat.getPercentInstance().format(80 / 100f);
        String pluggedIndication = mContext.getString(
                R.string.keyguard_plugged_in_charging_limited, percentage);
        assertThat(mTextView.getText()).isEqualTo(pluggedIndication);
    }

    @Test
    public void onRefreshBatteryInfo_fullChargedWithOverheat_presentCharged() {
        createController();
        BatteryStatus status = new BatteryStatus(BatteryManager.BATTERY_STATUS_CHARGING,
                100 /* level */, BatteryManager.BATTERY_PLUGGED_AC,
                BatteryManager.BATTERY_HEALTH_OVERHEAT, 0 /* maxChargingWattage */,
                true /* present */);

        mController.getKeyguardCallback().onRefreshBatteryInfo(status);
        mController.setVisible(true);

        String chargedIndication = mContext.getString(R.string.keyguard_charged);
        assertThat(mTextView.getText()).isEqualTo(chargedIndication);
    }

    @Test
    public void onRefreshBatteryInfo_dischargingWithOverheat_presentBatteryPercentage() {
        createController();
        BatteryStatus status = new BatteryStatus(BatteryManager.BATTERY_STATUS_DISCHARGING,
                90 /* level */, 0 /* plugged */, BatteryManager.BATTERY_HEALTH_OVERHEAT,
                0 /* maxChargingWattage */, true /* present */);

        mController.getKeyguardCallback().onRefreshBatteryInfo(status);
        mController.setDozing(true);
        mController.setVisible(true);

        String percentage = NumberFormat.getPercentInstance().format(90 / 100f);
        assertThat(mTextView.getText()).isEqualTo(percentage);
    }
}
}