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

Commit bfedf0b8 authored by Evan Charlton's avatar Evan Charlton
Browse files

Allow the user to define 'quiet hours' for notifications.

Introduces a notion of user-defined 'quiet hours'. During this period,
notifications will be: (1) silenced, (2) have vibrations removed, and
(3) have their requested colors muted (to dim the LED on capable hardware).

Note that (3) works well on Nexus One and gracefully degrades on sapphire.
That is, LED colors are reduced, but hardware still displays them the same.

Bug: 2164
Bug: 2252
Change-Id: I66b4dbab8ccc30741b15629fb713ff18b4511b47
parent c3727190
Loading
Loading
Loading
Loading
+43 −1
Original line number Diff line number Diff line
@@ -2198,6 +2198,42 @@ public final class Settings {
         */
        public static final String HDPI_BATTERY_ALIGNMENT = "hdpi_battery_alignment";

        /**
         * Whether to enable quiet hours.
         * @hide
         */
        public static final String QUIET_HOURS_ENABLED = "quiet_hours_enabled";

        /**
         * Sets when quiet hours starts. This is stored in minutes from the start of the day.
         * @hide
         */
        public static final String QUIET_HOURS_START = "quiet_hours_start";

        /**
         * Sets when quiet hours end. This is stored in minutes from the start of the day.
         * @hide
         */
        public static final String QUIET_HOURS_END = "quiet_hours_end";

        /**
         * Whether to remove the sound from outgoing notifications during quiet hours.
         * @hide
         */
        public static final String QUIET_HOURS_MUTE = "quiet_hours_mute";

        /**
         * Whether to remove the vibration from outgoing notifications during quiet hours.
         * @hide
         */
        public static final String QUIET_HOURS_STILL = "quiet_hours_still";

        /**
         * Whether to attempt to dim the LED color during quiet hours.
         * @hide
         */
        public static final String QUIET_HOURS_DIM = "quiet_hours_dim";

        /**
         * Settings to backup. This is here so that it's in the same place as the settings
         * keys and easy to update.
@@ -2268,7 +2304,13 @@ public final class Settings {
            HAPTIC_UP_ARRAY_DEFAULT,
            HAPTIC_LONG_ARRAY_DEFAULT,
            HAPTIC_TAP_ARRAY,
            HAPTIC_TAP_ARRAY_DEFAULT
            HAPTIC_TAP_ARRAY_DEFAULT,
            QUIET_HOURS_ENABLED,
            QUIET_HOURS_START,
            QUIET_HOURS_END,
            QUIET_HOURS_MUTE,
            QUIET_HOURS_STILL,
            QUIET_HOURS_DIM
        };

        // Settings moved to Settings.Secure
+70 −14
Original line number Diff line number Diff line
@@ -74,6 +74,7 @@ import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Random;

//For Notification Colors.
@@ -142,6 +143,18 @@ class NotificationManagerService extends INotificationManager.Stub
    private int mBatteryLevel;
    private NotificationRecord mLedNotification;

    private boolean mQuietHoursEnabled = false;
    // Minutes from midnight when quiet hours begin.
    private int mQuietHoursStart = 0;
    // Minutes from midnight when quiet hours end.
    private int mQuietHoursEnd = 0;
    // Don't play sounds.
    private boolean mQuietHoursMute = true;
    // Don't vibrate.
    private boolean mQuietHoursStill = true;
    // Dim LED if hardware supports it.
    private boolean mQuietHoursDim = true;

    private static final int BATTERY_LOW_ARGB = 0xFFFF0000; // Charging Low - red solid on
    private static final int BATTERY_MEDIUM_ARGB = 0xFFFFFF00;    // Charging - orange solid on
    private static final int BATTERY_FULL_ARGB = 0xFF00FF00; // Charging Full - green solid on
@@ -414,6 +427,18 @@ class NotificationManagerService extends INotificationManager.Stub
                    Settings.Secure.ADB_ENABLED), false, this);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
            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_MUTE), false, this);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.QUIET_HOURS_STILL), false, this);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.QUIET_HOURS_DIM), false, this);
            update();
        }

@@ -435,6 +460,19 @@ class NotificationManagerService extends INotificationManager.Stub
                mNotificationPulseEnabled = pulseEnabled;
                updateNotificationPulse();
            }

            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);
            mQuietHoursMute = Settings.System.getInt(resolver,
                        Settings.System.QUIET_HOURS_MUTE, 0) != 0;
            mQuietHoursStill = Settings.System.getInt(resolver,
                        Settings.System.QUIET_HOURS_STILL, 0) != 0;
            mQuietHoursDim = Settings.System.getInt(resolver,
                        Settings.System.QUIET_HOURS_DIM, 0) != 0;
        }
    }

@@ -714,6 +752,21 @@ class NotificationManagerService extends INotificationManager.Stub
        }

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

            NotificationRecord r = new NotificationRecord(pkg, tag, id, notification);
            NotificationRecord old = null;

@@ -808,7 +861,7 @@ class NotificationManagerService extends INotificationManager.Stub
                // sound
                final boolean useDefaultSound =
                    (notification.defaults & Notification.DEFAULT_SOUND) != 0;
                if (useDefaultSound || notification.sound != null) {
                if ((inQuietHours && !mQuietHoursMute) && (useDefaultSound || notification.sound != null)) {
                    Uri uri;
                    if (useDefaultSound) {
                        uri = Settings.System.DEFAULT_NOTIFICATION_URI;
@@ -839,7 +892,7 @@ class NotificationManagerService extends INotificationManager.Stub
                // vibrate
                final boolean useDefaultVibrate =
                    (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
                if ((useDefaultVibrate || notification.vibrate != null)
                if ((inQuietHours && !mQuietHoursStill) && (useDefaultVibrate || notification.vibrate != null)
                        && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
                    mVibrateNotification = r;

@@ -849,7 +902,17 @@ class NotificationManagerService extends INotificationManager.Stub
                }
            }

            // this option doesn't shut off the lights
            // Adjust the LED for quiet hours
            if (inQuietHours && mQuietHoursDim) {
                // Cut all of the channels by a factor of 16 to dim on capable hardware.
                // Note that this should fail gracefully on other hardware.
                int argb = notification.ledARGB;
                int red = (((argb & 0xFF0000) >>> 16) >>> 4);
                int green = (((argb & 0xFF00) >>> 8 ) >>> 4);
                int blue = ((argb & 0xFF) >>> 4);

                notification.ledARGB = (0xFF000000 | (red << 16) | (green << 8) | blue);
            }

            // light
            // the most recent thing gets the light
@@ -857,20 +920,13 @@ class NotificationManagerService extends INotificationManager.Stub
            if (mLedNotification == old) {
                mLedNotification = null;
            }
	    //updatePackageList(pkg);
            //Slog.i(TAG, "notification.lights="
            //        + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
	    //if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
            if (checkLight(notification, pkg)) {
                mLights.add(r);
                updateLightsLocked();
            } else {
		if (old != null) {
			if(checkLight(old.notification, old.pkg))
            } else if (old != null && checkLight(old.notification, old.pkg)) {
                updateLightsLocked();
            }
        }
        }

        idOut[0] = id;
    }