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

Commit f1e28fdd authored by DvTonder's avatar DvTonder
Browse files

Sound Settings - Quiet Hours (Part 2 of 2)

This is the frameworks part of the Quiet Hours settings, ported from
CM7

Patch set 3 - Using a separate observer
Patch set 4 - Rebased

Change-Id: Id1756b62ed8146cf6a7cc2d80f2dbe1a5803eb66
parent 148182cf
Loading
Loading
Loading
Loading
+48 −0
Original line number Diff line number Diff line
@@ -2268,6 +2268,48 @@ public final class Settings {
         */
        public static final String MVNO_ROAMING = "mvno_roaming";

        /*
         * 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 disable haptic feedback during quiet hours.
         * @hide
         */
        public static final String QUIET_HOURS_HAPTIC = "quiet_hours_haptic";

        /**
         * 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.
@@ -2337,6 +2379,12 @@ public final class Settings {
            SIP_CALL_OPTIONS,
            SIP_RECEIVE_CALLS,
            POINTER_SPEED,
            QUIET_HOURS_ENABLED,
            QUIET_HOURS_START,
            QUIET_HOURS_END,
            QUIET_HOURS_MUTE,
            QUIET_HOURS_STILL,
            QUIET_HOURS_DIM,
        };

        // Settings moved to Settings.Secure
+86 −8
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Calendar;

/** {@hide} */
public class NotificationManagerService extends INotificationManager.Stub
@@ -116,6 +117,18 @@ public class NotificationManagerService extends INotificationManager.Stub
    private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
    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 String idDebugString(Context baseContext, String packageName, int id) {
        Context c = null;

@@ -373,8 +386,8 @@ public class NotificationManagerService extends INotificationManager.Stub
        }
    };

    class SettingsObserver extends ContentObserver {
        SettingsObserver(Handler handler) {
    class LEDSettingsObserver extends ContentObserver {
        LEDSettingsObserver(Handler handler) {
            super(handler);
        }

@@ -428,6 +441,50 @@ public class NotificationManagerService extends INotificationManager.Stub
        }
    }

    class QuietHoursSettingsObserver extends ContentObserver {
        QuietHoursSettingsObserver(Handler handler) {
            super(handler);
        }

        void observe() {
            ContentResolver resolver = mContext.getContentResolver();
            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();
        }

        @Override public void onChange(boolean selfChange) {
            update();
            updateNotificationPulse();
        }

        public void update() {
            ContentResolver resolver = mContext.getContentResolver();
            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;
        }
    }

    NotificationManagerService(Context context, StatusBarManagerService statusBar,
            LightsService lights)
    {
@@ -481,8 +538,10 @@ public class NotificationManagerService extends INotificationManager.Stub
        IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
        mContext.registerReceiver(mIntentReceiver, sdFilter);

        SettingsObserver observer = new SettingsObserver(mHandler);
        observer.observe();
        LEDSettingsObserver ledObserver = new LEDSettingsObserver(mHandler);
        ledObserver.observe();
        QuietHoursSettingsObserver qhObserver = new QuietHoursSettingsObserver(mHandler);
        qhObserver.observe();
    }

    void systemReady() {
@@ -764,6 +823,8 @@ public class NotificationManagerService extends INotificationManager.Stub
        }

        synchronized (mNotificationList) {
            final boolean inQuietHours = inQuietHours();

            NotificationRecord r = new NotificationRecord(pkg, tag, id, 
                    callingUid, callingPid, 
                    priority,
@@ -839,7 +900,8 @@ 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;
@@ -870,7 +932,8 @@ 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;

@@ -904,6 +967,21 @@ public class NotificationManagerService extends INotificationManager.Stub
        idOut[0] = id;
    }

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

    private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
        AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
        if (!manager.isEnabled()) {
@@ -1130,8 +1208,8 @@ public class NotificationManagerService extends INotificationManager.Stub
        boolean forceWithScreenOff = (mLedNotification.notification.flags &
                Notification.FLAG_FORCE_LED_SCREEN_OFF) != 0;

        // Don't flash while we are in a call or screen is on
        if (mInCall || mScreenOn || (wasScreenOn && !forceWithScreenOff)) {
        // Don't flash while we are in a call, screen is on or we are in quiet hours with light dimmed
        if (mInCall || mScreenOn || (inQuietHours() && mQuietHoursDim) || (wasScreenOn && !forceWithScreenOff)) {
            mNotificationLight.turnOff();
        } else {
            int ledARGB;
+30 −2
Original line number Diff line number Diff line
@@ -30,8 +30,10 @@ import android.os.IBinder;
import android.os.Binder;
import android.os.SystemClock;
import android.os.WorkSource;
import android.provider.Settings;
import android.util.Slog;

import java.util.Calendar;
import java.util.LinkedList;
import java.util.ListIterator;

@@ -116,6 +118,29 @@ public class VibratorService extends IVibratorService.Stub {
        return vibratorExists();
    }

    private boolean inQuietHours() {
        boolean quietHoursEnabled = Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.QUIET_HOURS_ENABLED, 0) != 0;
        int quietHoursStart = Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.QUIET_HOURS_START, 0);
        int quietHoursEnd = Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.QUIET_HOURS_END, 0);
        boolean quietHoursHaptic = Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.QUIET_HOURS_HAPTIC, 0) != 0;
        if (quietHoursEnabled && quietHoursHaptic && (quietHoursStart != quietHoursEnd)) {
            // 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 (quietHoursEnd < quietHoursStart) {
                // Starts at night, ends in the morning.
                return (minutes > quietHoursStart) || (minutes < quietHoursEnd);
            } else {
                return (minutes > quietHoursStart) && (minutes < quietHoursEnd);
            }
        }
        return false;
    }

    public void vibrate(long milliseconds, IBinder token) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
                != PackageManager.PERMISSION_GRANTED) {
@@ -126,7 +151,7 @@ public class VibratorService extends IVibratorService.Stub {
        // timeout of 0 or negative. This will ensure that a vibration has
        // either a timeout of > 0 or a non-null pattern.
        if (milliseconds <= 0 || (mCurrentVibration != null
                && mCurrentVibration.hasLongerTimeout(milliseconds))) {
                && mCurrentVibration.hasLongerTimeout(milliseconds)) || inQuietHours()) {
            // Ignore this vibration since the current vibration will play for
            // longer than milliseconds.
            return;
@@ -155,6 +180,9 @@ public class VibratorService extends IVibratorService.Stub {
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires VIBRATE permission");
        }
        if (inQuietHours()) {
            return;
        }
        int uid = Binder.getCallingUid();
        // so wakelock calls will succeed
        long identity = Binder.clearCallingIdentity();