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

Commit 11d3a2dc authored by d34d's avatar d34d Committed by Gerrit Code Review
Browse files

Battery: Show battery fully charged notification

This feature is device configurable using config_showBatteryFullyChargedNotification.
When enabled the BatteryService will show a notification informing the user that the
battery is fully charged and that they should disconnect the device from the charger
to improve battery longevity (citation needed) ;)

Change-Id: I9214c880705c58b67b422fbf3977913e93a0a737
TICKET: PAELLA-91
parent 698f7f24
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -249,4 +249,8 @@
    <!-- Protected Apps Notification -->
    <string name="notify_package_component_protected_title">Activity launch blocked</string>
    <string name="notify_package_component_protected_text"><xliff:g id="app_name">%1$s</xliff:g> is protected from being launched. Tap to authenticate and launch the application.</string>

    <!-- Battery fully charged notification -->
    <string name="notify_battery_fully_charged_title">Battery fully charged</string>
    <string name="notify_battery_fully_charged_text">Disconnect your device from the charger to improve battery longevity.</string>
</resources>
+5 −0
Original line number Diff line number Diff line
@@ -157,4 +157,9 @@
    <!-- Restricted fingerprint config -->
    <java-symbol type="bool" name="config_fingerprintRestrictedToSystemAndOwner" />

    <!-- Show battery fully charged notification -->
    <java-symbol type="bool" name="config_showBatteryFullyChargedNotification" />
    <java-symbol type="string" name="notify_battery_fully_charged_title" />
    <java-symbol type="string" name="notify_battery_fully_charged_text" />

</resources>
+3 −0
Original line number Diff line number Diff line
@@ -2524,4 +2524,7 @@
         automatically try to pair with it when the device exits tablet mode. -->
    <string translatable="false" name="config_packagedKeyboardName"></string>
    <integer name="config_dayColorTemperature">6500</integer>

    <!-- Show battery fully charged notification -->
    <bool name="config_showBatteryFullyChargedNotification">false</bool>
</resources>
+64 −0
Original line number Diff line number Diff line
@@ -25,6 +25,9 @@ import com.android.server.lights.Light;
import com.android.server.lights.LightsManager;

import android.app.ActivityManagerNative;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
@@ -166,6 +169,9 @@ public final class BatteryService extends SystemService {

    private boolean mSentLowBatteryBroadcast = false;

    private boolean mShowBatteryFullyChargedNotification;
    private boolean mIsShowingBatteryFullyChargedNotification;

    public BatteryService(Context context) {
        super(context);

@@ -182,6 +188,8 @@ public final class BatteryService extends SystemService {
                com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
        mShutdownBatteryTemperature = mContext.getResources().getInteger(
                com.android.internal.R.integer.config_shutdownBatteryTemperature);
        mShowBatteryFullyChargedNotification = mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_showBatteryFullyChargedNotification);

        // watch for invalid charger messages if the invalid_charger switch exists
        if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
@@ -498,6 +506,12 @@ public final class BatteryService extends SystemService {
            // Update the battery LED
            mLed.updateLightsLocked();

            if (shouldShowBatteryFullyChargedNotificationLocked()) {
                showBatteryFullyChargedNotificationLocked();
            } else if (shouldClearBatteryFullyChargedNotificationLocked()) {
                clearBatteryFullyChargedNotificationLocked();
            }

            // This needs to be done after sendIntent() so that we get the lastest battery stats.
            if (logOutlier && dischargeDuration != 0) {
                logOutlierLocked(dischargeDuration);
@@ -752,6 +766,56 @@ public final class BatteryService extends SystemService {
        mLed.updateLightsLocked();
    }

    private boolean shouldShowBatteryFullyChargedNotificationLocked() {
        return mShowBatteryFullyChargedNotification && mPlugType != 0
                && mBatteryProps.batteryLevel == BATTERY_SCALE
                && !mIsShowingBatteryFullyChargedNotification;
    }

    private void showBatteryFullyChargedNotificationLocked() {
        NotificationManager nm = mContext.getSystemService(NotificationManager.class);
        Intent intent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
        PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0,
                intent, 0, null, UserHandle.CURRENT);

        CharSequence title = mContext.getText(
                com.android.internal.R.string.notify_battery_fully_charged_title);
        CharSequence message = mContext.getText(
                com.android.internal.R.string.notify_battery_fully_charged_text);

        Notification notification = new Notification.Builder(mContext)
                .setSmallIcon(com.android.internal.R.drawable.stat_sys_battery_charge)
                .setWhen(0)
                .setOngoing(false)
                .setAutoCancel(true)
                .setTicker(title)
                .setDefaults(0)  // please be quiet
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setColor(mContext.getColor(
                        com.android.internal.R.color.system_notification_accent_color))
                .setContentTitle(title)
                .setContentText(message)
                .setStyle(new Notification.BigTextStyle().bigText(message))
                .setContentIntent(pi)
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .build();

        nm.notifyAsUser(null, com.android.internal.R.string.notify_battery_fully_charged_title,
                notification, UserHandle.ALL);
        mIsShowingBatteryFullyChargedNotification = true;
    }

    private boolean shouldClearBatteryFullyChargedNotificationLocked() {
        return mIsShowingBatteryFullyChargedNotification &&
                (mPlugType == 0 || mBatteryProps.batteryLevel < BATTERY_SCALE);
    }

    private void clearBatteryFullyChargedNotificationLocked() {
        NotificationManager nm = mContext.getSystemService(NotificationManager.class);
        nm.cancel(com.android.internal.R.string.notify_battery_fully_charged_title);
        mIsShowingBatteryFullyChargedNotification = false;
    }

    private final class Led {
        private final Light mBatteryLight;