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

Commit 36cc222c authored by Dan Pasanen's avatar Dan Pasanen
Browse files

QuietHours: clean up and consolidate code

Change-Id: Ib745ba91f800489518f1c7b5d2512ba946039696
parent 33012458
Loading
Loading
Loading
Loading
+48 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.util.cm;

import android.content.Context;
import android.provider.Settings;

import java.util.Calendar;

public class QuietHoursUtils {
    public static boolean inQuietHours(Context context, String option) {
        boolean quietHoursEnabled = Settings.System.getInt(context.getContentResolver(),
                Settings.System.QUIET_HOURS_ENABLED, 0) != 0;
        int quietHoursStart = Settings.System.getInt(context.getContentResolver(),
                Settings.System.QUIET_HOURS_START, 0);
        int quietHoursEnd = Settings.System.getInt(context.getContentResolver(),
                Settings.System.QUIET_HOURS_END, 0);
        boolean quietHoursOption = Settings.System.getInt(context.getContentResolver(),
                option, 0) != 0;
        if (quietHoursEnabled && quietHoursOption && (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;
    }
}
+3 −25
Original line number Diff line number Diff line
@@ -42,9 +42,10 @@ import android.util.Log;
import android.view.KeyEvent;
import android.view.VolumePanel;

import java.util.Calendar;
import java.util.HashMap;

import com.android.internal.util.cm.QuietHoursUtils;

/**
 * AudioManager provides access to volume and ringer mode control.
 * <p>
@@ -1772,7 +1773,7 @@ public class AudioManager {
            return;
        }

        if (inQuietHours()) {
        if (QuietHoursUtils.inQuietHours(mContext, Settings.System.QUIET_HOURS_SYSTEM)) {
            return;
        }

@@ -1819,29 +1820,6 @@ public class AudioManager {
        }
    }

    public 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 quietHoursSystem = Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.QUIET_HOURS_SYSTEM, 0) != 0;
        if (quietHoursEnabled && quietHoursSystem && (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;
    }


    /**
     * Settings has an in memory cache, so this is fast.
+2 −26
Original line number Diff line number Diff line
@@ -56,10 +56,9 @@ import android.view.WindowManager;
import android.view.WindowManagerPolicy;

import com.android.internal.telephony.IccCardConstants;
import com.android.internal.util.cm.QuietHoursUtils;
import com.android.internal.widget.LockPatternUtils;

import java.util.Calendar;

/**
 * Mediates requests related to the keyguard.  This includes queries about the
 * state of the keyguard, power management events that effect whether the keyguard
@@ -1259,7 +1258,7 @@ public class KeyguardViewMediator {
            return;
        }

        if (inQuietHours()) {
        if (QuietHoursUtils.inQuietHours(mContext, Settings.System.QUIET_HOURS_SYSTEM)) {
            return;
        }

@@ -1291,29 +1290,6 @@ public class KeyguardViewMediator {
        }
    }

    public 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 quietHoursSystem = Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.QUIET_HOURS_SYSTEM, 0) != 0;
        if (quietHoursEnabled && quietHoursSystem && (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;
    }

    /**
     * Handle message sent by {@link #showLocked}.
     * @see #SHOW
+3 −20
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server;
import com.android.internal.app.IBatteryStats;
import com.android.internal.os.DeviceDockBatteryHandler;
import com.android.internal.os.IDeviceHandler;
import com.android.internal.util.cm.QuietHoursUtils;
import com.android.server.am.BatteryStatsService;

import android.app.ActivityManagerNative;
@@ -52,7 +53,7 @@ import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;


/**
 * <p>BatteryService monitors the charging status, and charge level of the device
@@ -165,7 +166,6 @@ public final class BatteryService extends Binder {
    private boolean mQuietHoursEnabled = false;
    private int mQuietHoursStart = 0;
    private int mQuietHoursEnd = 0;
    private boolean mQuietHoursDim = true;

    public BatteryService(Context context, LightsService lights, IDeviceHandler deviceHandler) {
        mContext = context;
@@ -794,7 +794,7 @@ public final class BatteryService extends Binder {
            if (!mLightEnabled) {
                // No lights if explicitly disabled
                mBatteryLight.turnOff();
            } else if (inQuietHours() && mQuietHoursDim) {
            } else if (QuietHoursUtils.inQuietHours(mContext, Settings.System.QUIET_HOURS_DIM)) {
                if (mLedPulseEnabled && level < mLowBatteryWarningLevel &&
                        status != BatteryManager.BATTERY_STATUS_CHARGING) {
                    // The battery is low, the device is not charging and the low battery pulse
@@ -907,26 +907,9 @@ public final class BatteryService extends Binder {
                    Settings.System.QUIET_HOURS_START, 0, UserHandle.USER_CURRENT_OR_SELF);
            mQuietHoursEnd = Settings.System.getIntForUser(resolver,
                    Settings.System.QUIET_HOURS_END, 0, UserHandle.USER_CURRENT_OR_SELF);
            mQuietHoursDim = Settings.System.getIntForUser(resolver,
                    Settings.System.QUIET_HOURS_DIM, 0, UserHandle.USER_CURRENT_OR_SELF) != 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;
    }

}
+9 −45
Original line number Diff line number Diff line
@@ -77,6 +77,8 @@ import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;
import android.widget.Toast;

import com.android.internal.util.cm.QuietHoursUtils;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

@@ -180,18 +182,6 @@ 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 final AppOpsManager mAppOps;

    // contains connections to all connected listeners, including app services
@@ -1355,18 +1345,6 @@ public class NotificationManagerService extends INotificationManager.Stub

        public void update() {
            ContentResolver resolver = mContext.getContentResolver();
            mQuietHoursEnabled = Settings.System.getIntForUser(resolver,
                    Settings.System.QUIET_HOURS_ENABLED, 0, UserHandle.USER_CURRENT_OR_SELF) != 0;
            mQuietHoursStart = Settings.System.getIntForUser(resolver,
                    Settings.System.QUIET_HOURS_START, 0, UserHandle.USER_CURRENT_OR_SELF);
            mQuietHoursEnd = Settings.System.getIntForUser(resolver,
                    Settings.System.QUIET_HOURS_END, 0, UserHandle.USER_CURRENT_OR_SELF);
            mQuietHoursMute = Settings.System.getIntForUser(resolver,
                    Settings.System.QUIET_HOURS_MUTE, 0, UserHandle.USER_CURRENT_OR_SELF) != 0;
            mQuietHoursStill = Settings.System.getIntForUser(resolver,
                    Settings.System.QUIET_HOURS_STILL, 0, UserHandle.USER_CURRENT_OR_SELF) != 0;
            mQuietHoursDim = Settings.System.getIntForUser(resolver,
                    Settings.System.QUIET_HOURS_DIM, 0, UserHandle.USER_CURRENT_OR_SELF) != 0;
        }
    }

@@ -1819,8 +1797,6 @@ public class NotificationManagerService extends INotificationManager.Stub
        final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD);

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

            final StatusBarNotification n = new StatusBarNotification(
                    pkg, id, tag, callingUid, callingPid, score, notification, user);
            NotificationRecord r = new NotificationRecord(n);
@@ -1935,14 +1911,16 @@ public class NotificationManagerService extends INotificationManager.Stub

                Uri soundUri = null;

                if (!(inQuietHours && mQuietHoursMute) && useDefaultSound) {
                if (!(QuietHoursUtils.inQuietHours(mContext, Settings.System.QUIET_HOURS_MUTE))
                    && useDefaultSound) {
                    soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;

                    // check to see if the default notification sound is silent
                    ContentResolver resolver = mContext.getContentResolver();
                    hasValidSound = Settings.System.getString(resolver,
                           Settings.System.NOTIFICATION_SOUND) != null;
                } else if (!(inQuietHours && mQuietHoursMute) && notification.sound != null) {
                } else if (!(QuietHoursUtils.inQuietHours(mContext,
                           Settings.System.QUIET_HOURS_MUTE)) && notification.sound != null) {
                    soundUri = notification.sound;
                    hasValidSound = (soundUri != null);
                }
@@ -1995,7 +1973,7 @@ public class NotificationManagerService extends INotificationManager.Stub
                final boolean useDefaultVibrate =
                        (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;

                if (!(inQuietHours && mQuietHoursStill)
                if (!(QuietHoursUtils.inQuietHours(mContext, Settings.System.QUIET_HOURS_MUTE))
                        && (useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
                        && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
                    mVibrateNotification = r;
@@ -2066,21 +2044,6 @@ public class NotificationManagerService extends INotificationManager.Stub
                0, UserHandle.USER_CURRENT_OR_SELF) != 0;
    }

    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()) {
@@ -2354,7 +2317,8 @@ public class NotificationManagerService extends INotificationManager.Stub
        // Don't flash while we are in a call, screen is on or we are
        // in quiet hours with light dimmed
        if (mLedNotification == null || mInCall
                || (mScreenOn && !mDreaming) || (inQuietHours() && mQuietHoursDim)) {
                || (mScreenOn && !mDreaming)
                || (QuietHoursUtils.inQuietHours(mContext, Settings.System.QUIET_HOURS_DIM))) {
            mNotificationLight.turnOff();
        } else if (mNotificationPulseEnabled) {
            final Notification ledno = mLedNotification.sbn.getNotification();
Loading