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

Commit 756f1500 authored by David van Tonder's avatar David van Tonder
Browse files

Battery Light: Add ability to configure (Part 1 of 2)

Adds Quiet hours support and ability to configure the led if the
device supports multiple colors for the Light - enabled via overlay
with default as 'false'

Change-Id: I905dde400ba0780066f2b2a20142eaa27e158655
parent 44f9c784
Loading
Loading
Loading
Loading
+32 −7
Original line number Diff line number Diff line
@@ -1937,13 +1937,6 @@ public final class Settings {
         */
        public static final String NOTIFICATION_LIGHT_PULSE = "notification_light_pulse";

        /**
         * Whether the battery LED should repeatedly flash when the battery is low
         * on charge. The value is boolean (1 or 0).
         * @hide
         */
        public static final String BATTERY_LIGHT_PULSE = "battery_light_pulse";

        /**
         * What color to use for the notification LED by default
         * @hide
@@ -2010,6 +2003,38 @@ public final class Settings {
         */
        public static final String NOTIFICATION_LIGHT_PULSE_CUSTOM_VALUES = "notification_light_pulse_custom_values";

        /**
         * Whether the battery light should be enabled (if hardware supports it)
         * The value is boolean (1 or 0).
         * @hide
         */
        public static final String BATTERY_LIGHT_ENABLED = "battery_light_enabled";

        /**
         * Whether the battery LED should repeatedly flash when the battery is low
         * on charge. The value is boolean (1 or 0).
         * @hide
         */
        public static final String BATTERY_LIGHT_PULSE = "battery_light_pulse";

        /**
         * What color to use for the battery LED while charging - low
         * @hide
         */
        public static final String BATTERY_LIGHT_LOW_COLOR = "battery_light_low_color";

        /**
         * What color to use for the battery LED while charging - medium
         * @hide
         */
        public static final String BATTERY_LIGHT_MEDIUM_COLOR = "battery_light_medium_color";

        /**
         * What color to use for the battery LED while charging - full
         * @hide
         */
        public static final String BATTERY_LIGHT_FULL_COLOR = "battery_light_full_color";

        /** Sprint MWI Quirk: Show message wait indicator notifications
         * @hide
         */
+3 −0
Original line number Diff line number Diff line
@@ -453,6 +453,9 @@
    <!-- Is the battery LED intrusive? Used to decide if there should be a disable option -->
    <bool name="config_intrusiveBatteryLed">false</bool>

    <!-- Does the battery LED support multiple colors? Used to decide if the user can change the colors -->
    <bool name="config_multiColorBatteryLed">false</bool>

    <!-- Default value for LED off time when the battery is low on charge in miliseconds -->
    <integer name="config_notificationsBatteryLedOff">2875</integer>

+119 −28
Original line number Diff line number Diff line
@@ -16,21 +16,20 @@

package com.android.server;

import com.android.internal.app.IBatteryStats;
import com.android.server.am.BatteryStatsService;

import android.app.ActivityManagerNative;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.graphics.Color;
import android.os.BatteryManager;
import android.os.Binder;
import android.os.DropBoxManager;
import android.os.FileUtils;
import android.os.Handler;
import android.os.IBinder;
import android.os.DropBoxManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemClock;
@@ -39,14 +38,15 @@ import android.provider.Settings;
import android.util.EventLog;
import android.util.Slog;

import com.android.internal.app.IBatteryStats;
import com.android.server.am.BatteryStatsService;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

import java.util.Calendar;

/**
 * <p>BatteryService monitors the charging status, and charge level of the device
@@ -130,10 +130,21 @@ class BatteryService extends Binder {
    private int mDischargeStartLevel;

    private Led mLed;
    private boolean mLightEnabled;
    private boolean mLedPulseEnabled;
    private int mBatteryLowARGB;
    private int mBatteryMediumARGB;
    private int mBatteryFullARGB;
    private boolean mMultiColorLed;

    private boolean mSentLowBatteryBroadcast = false;

    // Quiet hours support
    private boolean mQuietHoursEnabled = false;
    private int mQuietHoursStart = 0;
    private int mQuietHoursEnd = 0;
    private boolean mQuietHoursDim = true;

    public BatteryService(Context context, LightsService lights) {
        mContext = context;
        mLed = new Led(context, lights);
@@ -571,9 +582,6 @@ class BatteryService extends Binder {
        private LightsService mLightsService;
        private LightsService.Light mBatteryLight;

        private int mBatteryLowARGB;
        private int mBatteryMediumARGB;
        private int mBatteryFullARGB;
        private int mBatteryLedOn;
        private int mBatteryLedOff;

@@ -582,15 +590,14 @@ class BatteryService extends Binder {
        private boolean mBatteryFull;

        Led(Context context, LightsService lights) {

            mLightsService = lights;
            mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);

            mBatteryLowARGB = mContext.getResources().getInteger(
                    com.android.internal.R.integer.config_notificationsBatteryLowARGB);
            mBatteryMediumARGB = mContext.getResources().getInteger(
                    com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
            mBatteryFullARGB = mContext.getResources().getInteger(
                    com.android.internal.R.integer.config_notificationsBatteryFullARGB);
            // Does the Device support changing battery LED colors?
            mMultiColorLed = mContext.getResources().getBoolean(
                    com.android.internal.R.bool.config_multiColorBatteryLed);

            mBatteryLedOn = mContext.getResources().getInteger(
                    com.android.internal.R.integer.config_notificationsBatteryLedOn);
            mBatteryLedOff = mContext.getResources().getInteger(
@@ -603,12 +610,27 @@ class BatteryService extends Binder {
        void updateLightsLocked() {
            final int level = mBatteryLevel;
            final int status = mBatteryStatus;
            if (level < mLowBatteryWarningLevel) {

            if (!mLightEnabled) {
                // No lights if explicitly disabled
                mBatteryLight.turnOff();
            } else if (inQuietHours() && mQuietHoursDim) {
                if (mLedPulseEnabled && level < mLowBatteryWarningLevel &&
                        status != BatteryManager.BATTERY_STATUS_CHARGING) {
                    // The battery is low, the device is not charging and the low battery pulse
                    // is enabled - ignore Quiet Hours
                    mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED,
                            mBatteryLedOn, mBatteryLedOff);
                } else {
                    // No lights if in Quiet Hours and battery not low
                    mBatteryLight.turnOff();
                }
            } else if (level < mLowBatteryWarningLevel) {
                if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
                    // Solid red when battery is charging
                    // Battery is charging and low
                    mBatteryLight.setColor(mBatteryLowARGB);
                } else if (mLedPulseEnabled) {
                    // Flash red when battery is low and not charging
                    // Battery is low and not charging
                    mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED,
                            mBatteryLedOn, mBatteryLedOff);
                } else {
@@ -618,10 +640,10 @@ class BatteryService extends Binder {
            } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
                        || status == BatteryManager.BATTERY_STATUS_FULL) {
                if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
                    // Solid green when full or charging and nearly full
                    // Battery is full or charging and nearly full
                    mBatteryLight.setColor(mBatteryFullARGB);
                } else {
                    // Solid orange when charging and halfway full
                    // Battery is charging and halfway full
                    mBatteryLight.setColor(mBatteryMediumARGB);
                }
            } else {
@@ -638,8 +660,36 @@ class BatteryService extends Binder {

        void observe() {
            ContentResolver resolver = mContext.getContentResolver();

            // Battery light enabled
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.BATTERY_LIGHT_ENABLED), false, this);

            // Low battery pulse
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.BATTERY_LIGHT_PULSE), false, this);

            // Light colors
            if (mMultiColorLed) {
                // Register observer if we have a multi color led
                resolver.registerContentObserver(Settings.System.getUriFor(
                        Settings.System.BATTERY_LIGHT_LOW_COLOR), false, this);
                resolver.registerContentObserver(Settings.System.getUriFor(
                        Settings.System.BATTERY_LIGHT_MEDIUM_COLOR), false, this);
                resolver.registerContentObserver(Settings.System.getUriFor(
                        Settings.System.BATTERY_LIGHT_FULL_COLOR), false, this);
            }

            // Quiet Hours
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.QUIET_HOURS_ENABLED), false, this);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.QUIET_HOURS_START), false, this);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.QUIET_HOURS_END), false, this);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.QUIET_HOURS_DIM), false, this);

            update();
        }

@@ -649,13 +699,54 @@ class BatteryService extends Binder {

        public void update() {
            ContentResolver resolver = mContext.getContentResolver();
            boolean pulseEnabled = Settings.System.getInt(resolver,
            Resources res = mContext.getResources();

            // Battery light enabled
            mLightEnabled = Settings.System.getInt(resolver,
                    Settings.System.BATTERY_LIGHT_ENABLED, 1) != 0;

            // Low battery pulse
            mLedPulseEnabled = Settings.System.getInt(resolver,
                        Settings.System.BATTERY_LIGHT_PULSE, 1) != 0;
            if (mLedPulseEnabled != pulseEnabled) {
                mLedPulseEnabled = pulseEnabled;

            // Light colors
            mBatteryLowARGB = Settings.System.getInt(resolver,
                    Settings.System.BATTERY_LIGHT_LOW_COLOR,
                    res.getInteger(com.android.internal.R.integer.config_notificationsBatteryLowARGB));
            mBatteryMediumARGB = Settings.System.getInt(resolver,
                    Settings.System.BATTERY_LIGHT_MEDIUM_COLOR,
                    res.getInteger(com.android.internal.R.integer.config_notificationsBatteryMediumARGB));
            mBatteryFullARGB = Settings.System.getInt(resolver,
                    Settings.System.BATTERY_LIGHT_FULL_COLOR,
                    res.getInteger(com.android.internal.R.integer.config_notificationsBatteryFullARGB));

            // Quiet Hours
            mQuietHoursEnabled = Settings.System.getInt(resolver,
                    Settings.System.QUIET_HOURS_ENABLED, 0) != 0;
            mQuietHoursStart = Settings.System.getInt(resolver,
                    Settings.System.QUIET_HOURS_START, 0);
            mQuietHoursEnd = Settings.System.getInt(resolver,
                    Settings.System.QUIET_HOURS_END, 0);
            mQuietHoursDim = Settings.System.getInt(resolver,
                    Settings.System.QUIET_HOURS_DIM, 0) != 0;

            updateLedPulse();
        }
    }

    private boolean inQuietHours() {
        if (mQuietHoursEnabled && (mQuietHoursStart != mQuietHoursEnd)) {
            // Get the date in "quiet hours" format.
            Calendar calendar = Calendar.getInstance();
            int minutes = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE);
            if (mQuietHoursEnd < mQuietHoursStart) {
                // Starts at night, ends in the morning.
                return (minutes > mQuietHoursStart) || (minutes < mQuietHoursEnd);
            } else {
                return (minutes > mQuietHoursStart) && (minutes < mQuietHoursEnd);
            }
        }
        return false;
    }

}