ZenModeHelper.java revision 6ac5f8df62a4b6d87cf32797d2886efab8e28226
1/**
2 * Copyright (c) 2014, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.notification;
18
19import static android.media.AudioAttributes.USAGE_ALARM;
20import static android.media.AudioAttributes.USAGE_NOTIFICATION_RINGTONE;
21import static android.media.AudioAttributes.USAGE_UNKNOWN;
22
23import android.app.AlarmManager;
24import android.app.AppOpsManager;
25import android.app.Notification;
26import android.app.PendingIntent;
27import android.content.BroadcastReceiver;
28import android.content.ComponentName;
29import android.content.ContentResolver;
30import android.content.Context;
31import android.content.Intent;
32import android.content.IntentFilter;
33import android.content.res.Resources;
34import android.content.res.XmlResourceParser;
35import android.database.ContentObserver;
36import android.media.AudioAttributes;
37import android.media.AudioManager;
38import android.net.Uri;
39import android.os.Handler;
40import android.os.IBinder;
41import android.os.UserHandle;
42import android.provider.Settings.Global;
43import android.provider.Settings.Secure;
44import android.service.notification.ZenModeConfig;
45import android.telecomm.TelecommManager;
46import android.util.Slog;
47
48import com.android.internal.R;
49
50import libcore.io.IoUtils;
51
52import org.xmlpull.v1.XmlPullParser;
53import org.xmlpull.v1.XmlPullParserException;
54import org.xmlpull.v1.XmlSerializer;
55
56import java.io.IOException;
57import java.io.PrintWriter;
58import java.util.ArrayList;
59import java.util.Calendar;
60import java.util.Date;
61import java.util.Objects;
62
63/**
64 * NotificationManagerService helper for functionality related to zen mode.
65 */
66public class ZenModeHelper {
67    private static final String TAG = "ZenModeHelper";
68
69    private static final String ACTION_ENTER_ZEN = "enter_zen";
70    private static final int REQUEST_CODE_ENTER = 100;
71    private static final String ACTION_EXIT_ZEN = "exit_zen";
72    private static final int REQUEST_CODE_EXIT = 101;
73    private static final String EXTRA_TIME = "time";
74
75    private final Context mContext;
76    private final Handler mHandler;
77    private final SettingsObserver mSettingsObserver;
78    private final AppOpsManager mAppOps;
79    private final ZenModeConfig mDefaultConfig;
80    private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
81
82    private ComponentName mDefaultPhoneApp;
83    private int mZenMode;
84    private ZenModeConfig mConfig;
85    private AudioManager mAudioManager;
86    private int mPreviousRingerMode = -1;
87
88    public ZenModeHelper(Context context, Handler handler) {
89        mContext = context;
90        mHandler = handler;
91        mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
92        mDefaultConfig = readDefaultConfig(context.getResources());
93        mConfig = mDefaultConfig;
94        mSettingsObserver = new SettingsObserver(mHandler);
95        mSettingsObserver.observe();
96
97        final IntentFilter filter = new IntentFilter();
98        filter.addAction(ACTION_ENTER_ZEN);
99        filter.addAction(ACTION_EXIT_ZEN);
100        filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
101        mContext.registerReceiver(new ZenBroadcastReceiver(), filter);
102    }
103
104    public static ZenModeConfig readDefaultConfig(Resources resources) {
105        XmlResourceParser parser = null;
106        try {
107            parser = resources.getXml(R.xml.default_zen_mode_config);
108            while (parser.next() != XmlPullParser.END_DOCUMENT) {
109                final ZenModeConfig config = ZenModeConfig.readXml(parser);
110                if (config != null) return config;
111            }
112        } catch (Exception e) {
113            Slog.w(TAG, "Error reading default zen mode config from resource", e);
114        } finally {
115            IoUtils.closeQuietly(parser);
116        }
117        return new ZenModeConfig();
118    }
119
120    public void addCallback(Callback callback) {
121        mCallbacks.add(callback);
122    }
123
124    public void setAudioManager(AudioManager audioManager) {
125        mAudioManager = audioManager;
126    }
127
128    public boolean shouldIntercept(NotificationRecord record) {
129        if (mZenMode != Global.ZEN_MODE_OFF) {
130            if (isSystem(record)) {
131                return false;
132            }
133            if (isAlarm(record)) {
134                if (mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) {
135                    ZenLog.traceIntercepted(record, "alarm");
136                    return true;
137                }
138                return false;
139            }
140            // allow user-prioritized packages through in priority mode
141            if (mZenMode == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) {
142                if (record.getPackagePriority() == Notification.PRIORITY_MAX) {
143                    ZenLog.traceNotIntercepted(record, "priorityApp");
144                    return false;
145                }
146            }
147            // audience has veto power over all following rules
148            if (!audienceMatches(record)) {
149                ZenLog.traceIntercepted(record, "!audienceMatches");
150                return true;
151            }
152            if (isCall(record)) {
153                if (!mConfig.allowCalls) {
154                    ZenLog.traceIntercepted(record, "!allowCalls");
155                    return true;
156                }
157                return false;
158            }
159            if (isMessage(record)) {
160                if (!mConfig.allowMessages) {
161                    ZenLog.traceIntercepted(record, "!allowMessages");
162                    return true;
163                }
164                return false;
165            }
166            ZenLog.traceIntercepted(record, "!allowed");
167            return true;
168        }
169        return false;
170    }
171
172    public int getZenMode() {
173        return mZenMode;
174    }
175
176    public void setZenMode(int zenModeValue) {
177        Global.putInt(mContext.getContentResolver(), Global.ZEN_MODE, zenModeValue);
178    }
179
180    public void updateZenMode() {
181        final int mode = Global.getInt(mContext.getContentResolver(),
182                Global.ZEN_MODE, Global.ZEN_MODE_OFF);
183        if (mode != mZenMode) {
184            Slog.d(TAG, String.format("updateZenMode: %s -> %s",
185                    Global.zenModeToString(mZenMode),
186                    Global.zenModeToString(mode)));
187            ZenLog.traceUpdateZenMode(mZenMode, mode);
188        }
189        mZenMode = mode;
190        final boolean zen = mZenMode != Global.ZEN_MODE_OFF;
191        final String[] exceptionPackages = null; // none (for now)
192
193        // call restrictions
194        final boolean muteCalls = zen && !mConfig.allowCalls;
195        mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, USAGE_NOTIFICATION_RINGTONE,
196                muteCalls ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
197                exceptionPackages);
198        mAppOps.setRestriction(AppOpsManager.OP_PLAY_AUDIO, USAGE_NOTIFICATION_RINGTONE,
199                muteCalls ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
200                exceptionPackages);
201
202        // restrict vibrations with no hints
203        mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, USAGE_UNKNOWN,
204                zen ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
205                exceptionPackages);
206
207        // alarm restrictions
208        final boolean muteAlarms = mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS;
209        mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, USAGE_ALARM,
210                muteAlarms ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
211                exceptionPackages);
212        mAppOps.setRestriction(AppOpsManager.OP_PLAY_AUDIO, USAGE_ALARM,
213                muteAlarms ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
214                exceptionPackages);
215
216        // force ringer mode into compliance
217        if (mAudioManager != null) {
218            int ringerMode = mAudioManager.getRingerMode();
219            int forcedRingerMode = -1;
220            if (mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) {
221                if (ringerMode != AudioManager.RINGER_MODE_SILENT) {
222                    mPreviousRingerMode = ringerMode;
223                    Slog.d(TAG, "Silencing ringer");
224                    forcedRingerMode = AudioManager.RINGER_MODE_SILENT;
225                }
226            } else {
227                if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
228                    Slog.d(TAG, "Unsilencing ringer");
229                    forcedRingerMode = mPreviousRingerMode != -1 ? mPreviousRingerMode
230                            : AudioManager.RINGER_MODE_NORMAL;
231                    mPreviousRingerMode = -1;
232                }
233            }
234            if (forcedRingerMode != -1) {
235                mAudioManager.setRingerMode(forcedRingerMode);
236                ZenLog.traceSetRingerMode(forcedRingerMode);
237            }
238        }
239        dispatchOnZenModeChanged();
240    }
241
242    public boolean allowDisable(int what, IBinder token, String pkg) {
243        // TODO(cwren): delete this API before the next release. Bug:15344099
244        boolean allowDisable = true;
245        String reason = null;
246        if (isDefaultPhoneApp(pkg)) {
247            allowDisable = mZenMode == Global.ZEN_MODE_OFF || mConfig.allowCalls;
248            reason = mZenMode == Global.ZEN_MODE_OFF ? "zenOff" : "allowCalls";
249        }
250        ZenLog.traceAllowDisable(pkg, allowDisable, reason);
251        return allowDisable;
252    }
253
254    public void dump(PrintWriter pw, String prefix) {
255        pw.print(prefix); pw.print("mZenMode=");
256        pw.println(Global.zenModeToString(mZenMode));
257        pw.print(prefix); pw.print("mConfig="); pw.println(mConfig);
258        pw.print(prefix); pw.print("mDefaultConfig="); pw.println(mDefaultConfig);
259        pw.print(prefix); pw.print("mPreviousRingerMode="); pw.println(mPreviousRingerMode);
260        pw.print(prefix); pw.print("mDefaultPhoneApp="); pw.println(mDefaultPhoneApp);
261    }
262
263    public void readXml(XmlPullParser parser) throws XmlPullParserException, IOException {
264        final ZenModeConfig config = ZenModeConfig.readXml(parser);
265        if (config != null) {
266            setConfig(config);
267        }
268    }
269
270    public void writeXml(XmlSerializer out) throws IOException {
271        mConfig.writeXml(out);
272    }
273
274    public ZenModeConfig getConfig() {
275        return mConfig;
276    }
277
278    public boolean setConfig(ZenModeConfig config) {
279        if (config == null || !config.isValid()) return false;
280        if (config.equals(mConfig)) return true;
281        ZenLog.traceConfig(mConfig, config);
282        mConfig = config;
283        dispatchOnConfigChanged();
284        final String val = Integer.toString(mConfig.hashCode());
285        Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_CONFIG_ETAG, val);
286        updateAlarms();
287        updateZenMode();
288        return true;
289    }
290
291    private void handleRingerModeChanged() {
292        if (mAudioManager != null) {
293            // follow ringer mode if necessary
294            final int ringerMode = mAudioManager.getRingerMode();
295            int newZen = -1;
296            if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
297                if (mZenMode != Global.ZEN_MODE_NO_INTERRUPTIONS) {
298                    newZen = Global.ZEN_MODE_NO_INTERRUPTIONS;
299                }
300            } else if ((ringerMode == AudioManager.RINGER_MODE_NORMAL
301                    || ringerMode == AudioManager.RINGER_MODE_VIBRATE)
302                    && mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) {
303                newZen = Global.ZEN_MODE_OFF;
304            }
305            if (newZen != -1) {
306                ZenLog.traceFollowRingerMode(ringerMode, mZenMode, newZen);
307                setZenMode(newZen);
308            }
309        }
310    }
311
312    private void dispatchOnConfigChanged() {
313        for (Callback callback : mCallbacks) {
314            callback.onConfigChanged();
315        }
316    }
317
318    private void dispatchOnZenModeChanged() {
319        for (Callback callback : mCallbacks) {
320            callback.onZenModeChanged();
321        }
322    }
323
324    private boolean isSystem(NotificationRecord record) {
325        return record.isCategory(Notification.CATEGORY_SYSTEM);
326    }
327
328    private boolean isAlarm(NotificationRecord record) {
329        return record.isCategory(Notification.CATEGORY_ALARM)
330                || record.isCategory(Notification.CATEGORY_EVENT)
331                || record.isAudioStream(AudioManager.STREAM_ALARM)
332                || record.isAudioAttributesUsage(AudioAttributes.USAGE_ALARM);
333    }
334
335    private boolean isCall(NotificationRecord record) {
336        return isDefaultPhoneApp(record.sbn.getPackageName())
337                || record.isCategory(Notification.CATEGORY_CALL);
338    }
339
340    private boolean isDefaultPhoneApp(String pkg) {
341        if (mDefaultPhoneApp == null) {
342            final TelecommManager telecomm =
343                    (TelecommManager) mContext.getSystemService(Context.TELECOMM_SERVICE);
344            mDefaultPhoneApp = telecomm != null ? telecomm.getDefaultPhoneApp() : null;
345            Slog.d(TAG, "Default phone app: " + mDefaultPhoneApp);
346        }
347        return pkg != null && mDefaultPhoneApp != null
348                && pkg.equals(mDefaultPhoneApp.getPackageName());
349    }
350
351    private boolean isDefaultMessagingApp(NotificationRecord record) {
352        final int userId = record.getUserId();
353        if (userId == UserHandle.USER_NULL || userId == UserHandle.USER_ALL) return false;
354        final String defaultApp = Secure.getStringForUser(mContext.getContentResolver(),
355                Secure.SMS_DEFAULT_APPLICATION, userId);
356        return Objects.equals(defaultApp, record.sbn.getPackageName());
357    }
358
359    private boolean isMessage(NotificationRecord record) {
360        return record.isCategory(Notification.CATEGORY_MESSAGE) || isDefaultMessagingApp(record);
361    }
362
363    private boolean audienceMatches(NotificationRecord record) {
364        switch (mConfig.allowFrom) {
365            case ZenModeConfig.SOURCE_ANYONE:
366                return true;
367            case ZenModeConfig.SOURCE_CONTACT:
368                return record.getContactAffinity() >= ValidateNotificationPeople.VALID_CONTACT;
369            case ZenModeConfig.SOURCE_STAR:
370                return record.getContactAffinity() >= ValidateNotificationPeople.STARRED_CONTACT;
371            default:
372                Slog.w(TAG, "Encountered unknown source: " + mConfig.allowFrom);
373                return true;
374        }
375    }
376
377    private void updateAlarms() {
378        updateAlarm(ACTION_ENTER_ZEN, REQUEST_CODE_ENTER,
379                mConfig.sleepStartHour, mConfig.sleepStartMinute);
380        updateAlarm(ACTION_EXIT_ZEN, REQUEST_CODE_EXIT,
381                mConfig.sleepEndHour, mConfig.sleepEndMinute);
382    }
383
384    private void updateAlarm(String action, int requestCode, int hr, int min) {
385        final AlarmManager alarms = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
386        final long now = System.currentTimeMillis();
387        final Calendar c = Calendar.getInstance();
388        c.setTimeInMillis(now);
389        c.set(Calendar.HOUR_OF_DAY, hr);
390        c.set(Calendar.MINUTE, min);
391        c.set(Calendar.SECOND, 0);
392        c.set(Calendar.MILLISECOND, 0);
393        if (c.getTimeInMillis() <= now) {
394            c.add(Calendar.DATE, 1);
395        }
396        final long time = c.getTimeInMillis();
397        final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, requestCode,
398                new Intent(action).putExtra(EXTRA_TIME, time), PendingIntent.FLAG_UPDATE_CURRENT);
399        alarms.cancel(pendingIntent);
400        if (mConfig.sleepMode != null) {
401            Slog.d(TAG, String.format("Scheduling %s for %s, %s in the future, now=%s",
402                    action, ts(time), time - now, ts(now)));
403            alarms.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
404        }
405    }
406
407    private static String ts(long time) {
408        return new Date(time) + " (" + time + ")";
409    }
410
411    private final Runnable mRingerModeChanged = new Runnable() {
412        @Override
413        public void run() {
414            handleRingerModeChanged();
415        }
416    };
417
418    private class SettingsObserver extends ContentObserver {
419        private final Uri ZEN_MODE = Global.getUriFor(Global.ZEN_MODE);
420
421        public SettingsObserver(Handler handler) {
422            super(handler);
423        }
424
425        public void observe() {
426            final ContentResolver resolver = mContext.getContentResolver();
427            resolver.registerContentObserver(ZEN_MODE, false /*notifyForDescendents*/, this);
428            update(null);
429        }
430
431        @Override
432        public void onChange(boolean selfChange, Uri uri) {
433            update(uri);
434        }
435
436        public void update(Uri uri) {
437            if (ZEN_MODE.equals(uri)) {
438                updateZenMode();
439            }
440        }
441    }
442
443    private class ZenBroadcastReceiver extends BroadcastReceiver {
444        private final Calendar mCalendar = Calendar.getInstance();
445
446        @Override
447        public void onReceive(Context context, Intent intent) {
448            if (ACTION_ENTER_ZEN.equals(intent.getAction())) {
449                setZenMode(intent, Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
450            } else if (ACTION_EXIT_ZEN.equals(intent.getAction())) {
451                setZenMode(intent, Global.ZEN_MODE_OFF);
452            } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(intent.getAction())) {
453                mHandler.post(mRingerModeChanged);
454            }
455        }
456
457        private void setZenMode(Intent intent, int zenModeValue) {
458            final long schTime = intent.getLongExtra(EXTRA_TIME, 0);
459            final long now = System.currentTimeMillis();
460            Slog.d(TAG, String.format("%s scheduled for %s, fired at %s, delta=%s",
461                    intent.getAction(), ts(schTime), ts(now), now - schTime));
462
463            final int[] days = ZenModeConfig.tryParseDays(mConfig.sleepMode);
464            boolean enter = false;
465            final int day = getDayOfWeek(schTime);
466            if (days != null) {
467                for (int i = 0; i < days.length; i++) {
468                    if (days[i] == day) {
469                        enter = true;
470                        ZenModeHelper.this.setZenMode(zenModeValue);
471                        break;
472                    }
473                }
474            }
475            ZenLog.traceDowntime(enter, day, days);
476            updateAlarms();
477        }
478
479        private int getDayOfWeek(long time) {
480            mCalendar.setTimeInMillis(time);
481            return mCalendar.get(Calendar.DAY_OF_WEEK);
482        }
483    }
484
485    public static class Callback {
486        void onConfigChanged() {}
487        void onZenModeChanged() {}
488    }
489}
490