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

Commit 798a9d7c authored by Evan Charlton's avatar Evan Charlton Committed by Steve Kondik
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 06507942
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
@@ -1972,6 +1972,42 @@ public final class Settings {
         */
        public static final String MENU_UNLOCK_SCREEN = "menu_unlock_screen";

        /**
         * 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.
@@ -2038,6 +2074,12 @@ public final class Settings {
            NOTIFICATION_LIGHT_BLINK,
            NOTIFICATION_LIGHT_ALWAYS_ON,
            NOTIFICATION_LIGHT_CHARGING,
            QUIET_HOURS_ENABLED,
            QUIET_HOURS_START,
            QUIET_HOURS_END,
            QUIET_HOURS_MUTE,
            QUIET_HOURS_STILL,
            QUIET_HOURS_DIM
        };

        // Settings moved to Settings.Secure
+67 −3
Original line number Diff line number Diff line
@@ -66,6 +66,8 @@ import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Random;

/** {@hide} */
public class NotificationManagerService extends INotificationManager.Stub
@@ -140,6 +142,18 @@ public class NotificationManagerService extends INotificationManager.Stub
    private boolean mBatteryFull;
    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
@@ -446,6 +460,18 @@ public class NotificationManagerService extends INotificationManager.Stub
                    Settings.System.NOTIFICATION_LIGHT_ALWAYS_ON), false, this);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.NOTIFICATION_LIGHT_CHARGING), 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();
        }

@@ -479,6 +505,19 @@ public class NotificationManagerService extends INotificationManager.Stub
                mNotificationChargingEnabled = chargingEnabled;
                updateAmberLight();
            }

            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;
        }
    }

@@ -787,6 +826,21 @@ public 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,
                    callingUid, callingPid, notification);
            NotificationRecord old = null;
@@ -857,7 +911,7 @@ public 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;
@@ -888,7 +942,7 @@ public 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;

@@ -898,7 +952,17 @@ public 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