ZenModeHelper.java revision 12aeda802ed91a49977a22166319ce74a3352e30
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;
21
22import android.app.AppOpsManager;
23import android.app.Notification;
24import android.content.BroadcastReceiver;
25import android.content.ComponentName;
26import android.content.ContentResolver;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.res.Resources;
31import android.content.res.XmlResourceParser;
32import android.database.ContentObserver;
33import android.media.AudioAttributes;
34import android.media.AudioManager;
35import android.net.Uri;
36import android.os.Bundle;
37import android.os.Handler;
38import android.os.UserHandle;
39import android.provider.Settings.Global;
40import android.provider.Settings.Secure;
41import android.service.notification.NotificationListenerService;
42import android.service.notification.ZenModeConfig;
43import android.telecom.TelecomManager;
44import android.util.Log;
45import android.util.Slog;
46
47import com.android.internal.R;
48
49import libcore.io.IoUtils;
50
51import org.xmlpull.v1.XmlPullParser;
52import org.xmlpull.v1.XmlPullParserException;
53import org.xmlpull.v1.XmlSerializer;
54
55import java.io.IOException;
56import java.io.PrintWriter;
57import java.util.ArrayList;
58import java.util.Objects;
59
60/**
61 * NotificationManagerService helper for functionality related to zen mode.
62 */
63public class ZenModeHelper {
64    private static final String TAG = "ZenModeHelper";
65    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
66
67    private final Context mContext;
68    private final Handler mHandler;
69    private final SettingsObserver mSettingsObserver;
70    private final AppOpsManager mAppOps;
71    private final ZenModeConfig mDefaultConfig;
72    private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
73
74    private ComponentName mDefaultPhoneApp;
75    private int mZenMode;
76    private ZenModeConfig mConfig;
77    private AudioManager mAudioManager;
78    private int mPreviousRingerMode = -1;
79
80    public ZenModeHelper(Context context, Handler handler) {
81        mContext = context;
82        mHandler = handler;
83        mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
84        mDefaultConfig = readDefaultConfig(context.getResources());
85        mConfig = mDefaultConfig;
86        mSettingsObserver = new SettingsObserver(mHandler);
87        mSettingsObserver.observe();
88
89        final IntentFilter filter = new IntentFilter();
90        filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
91        mContext.registerReceiver(mReceiver, filter);
92    }
93
94    public static ZenModeConfig readDefaultConfig(Resources resources) {
95        XmlResourceParser parser = null;
96        try {
97            parser = resources.getXml(R.xml.default_zen_mode_config);
98            while (parser.next() != XmlPullParser.END_DOCUMENT) {
99                final ZenModeConfig config = ZenModeConfig.readXml(parser);
100                if (config != null) return config;
101            }
102        } catch (Exception e) {
103            Slog.w(TAG, "Error reading default zen mode config from resource", e);
104        } finally {
105            IoUtils.closeQuietly(parser);
106        }
107        return new ZenModeConfig();
108    }
109
110    public void addCallback(Callback callback) {
111        mCallbacks.add(callback);
112    }
113
114    public void setAudioManager(AudioManager audioManager) {
115        mAudioManager = audioManager;
116    }
117
118    public int getZenModeListenerInterruptionFilter() {
119        switch (mZenMode) {
120            case Global.ZEN_MODE_OFF:
121                return NotificationListenerService.INTERRUPTION_FILTER_ALL;
122            case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS:
123                return NotificationListenerService.INTERRUPTION_FILTER_PRIORITY;
124            case Global.ZEN_MODE_NO_INTERRUPTIONS:
125                return NotificationListenerService.INTERRUPTION_FILTER_NONE;
126            default:
127                return 0;
128        }
129    }
130
131    private static int zenModeFromListenerInterruptionFilter(int listenerInterruptionFilter,
132            int defValue) {
133        switch (listenerInterruptionFilter) {
134            case NotificationListenerService.INTERRUPTION_FILTER_ALL:
135                return Global.ZEN_MODE_OFF;
136            case NotificationListenerService.INTERRUPTION_FILTER_PRIORITY:
137                return Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
138            case NotificationListenerService.INTERRUPTION_FILTER_NONE:
139                return Global.ZEN_MODE_NO_INTERRUPTIONS;
140            default:
141                return defValue;
142        }
143    }
144
145    public void requestFromListener(int interruptionFilter) {
146        final int newZen = zenModeFromListenerInterruptionFilter(interruptionFilter, -1);
147        if (newZen != -1) {
148            setZenMode(newZen, "listener");
149        }
150    }
151
152    public boolean shouldIntercept(NotificationRecord record) {
153        if (mZenMode != Global.ZEN_MODE_OFF) {
154            if (isSystem(record)) {
155                return false;
156            }
157            if (isAlarm(record)) {
158                if (mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) {
159                    ZenLog.traceIntercepted(record, "alarm");
160                    return true;
161                }
162                return false;
163            }
164            // allow user-prioritized packages through in priority mode
165            if (mZenMode == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) {
166                if (record.getPackagePriority() == Notification.PRIORITY_MAX) {
167                    ZenLog.traceNotIntercepted(record, "priorityApp");
168                    return false;
169                }
170            }
171            if (isCall(record)) {
172                if (!mConfig.allowCalls) {
173                    ZenLog.traceIntercepted(record, "!allowCalls");
174                    return true;
175                }
176                return shouldInterceptAudience(record);
177            }
178            if (isMessage(record)) {
179                if (!mConfig.allowMessages) {
180                    ZenLog.traceIntercepted(record, "!allowMessages");
181                    return true;
182                }
183                return shouldInterceptAudience(record);
184            }
185            if (isEvent(record)) {
186                if (!mConfig.allowEvents) {
187                    ZenLog.traceIntercepted(record, "!allowEvents");
188                    return true;
189                }
190            }
191            ZenLog.traceIntercepted(record, "!allowed");
192            return true;
193        }
194        return false;
195    }
196
197    private boolean shouldInterceptAudience(NotificationRecord record) {
198        if (!audienceMatches(record.getContactAffinity())) {
199            ZenLog.traceIntercepted(record, "!audienceMatches");
200            return true;
201        }
202        return false;
203    }
204
205    public int getZenMode() {
206        return mZenMode;
207    }
208
209    public void setZenMode(int zenModeValue, String reason) {
210        ZenLog.traceSetZenMode(zenModeValue, reason);
211        Global.putInt(mContext.getContentResolver(), Global.ZEN_MODE, zenModeValue);
212    }
213
214    public void updateZenMode() {
215        final int mode = Global.getInt(mContext.getContentResolver(),
216                Global.ZEN_MODE, Global.ZEN_MODE_OFF);
217        if (mode != mZenMode) {
218            ZenLog.traceUpdateZenMode(mZenMode, mode);
219        }
220        mZenMode = mode;
221        final boolean zen = mZenMode != Global.ZEN_MODE_OFF;
222        final String[] exceptionPackages = null; // none (for now)
223
224        // call restrictions
225        final boolean muteCalls = zen && !mConfig.allowCalls;
226        mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, USAGE_NOTIFICATION_RINGTONE,
227                muteCalls ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
228                exceptionPackages);
229        mAppOps.setRestriction(AppOpsManager.OP_PLAY_AUDIO, USAGE_NOTIFICATION_RINGTONE,
230                muteCalls ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
231                exceptionPackages);
232
233        // alarm restrictions
234        final boolean muteAlarms = mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS;
235        mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, USAGE_ALARM,
236                muteAlarms ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
237                exceptionPackages);
238        mAppOps.setRestriction(AppOpsManager.OP_PLAY_AUDIO, USAGE_ALARM,
239                muteAlarms ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
240                exceptionPackages);
241
242        // force ringer mode into compliance
243        if (mAudioManager != null) {
244            int ringerMode = mAudioManager.getRingerMode();
245            int forcedRingerMode = -1;
246            if (mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) {
247                if (ringerMode != AudioManager.RINGER_MODE_SILENT) {
248                    mPreviousRingerMode = ringerMode;
249                    if (DEBUG) Slog.d(TAG, "Silencing ringer");
250                    forcedRingerMode = AudioManager.RINGER_MODE_SILENT;
251                }
252            } else {
253                if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
254                    if (DEBUG) Slog.d(TAG, "Unsilencing ringer");
255                    forcedRingerMode = mPreviousRingerMode != -1 ? mPreviousRingerMode
256                            : AudioManager.RINGER_MODE_NORMAL;
257                    mPreviousRingerMode = -1;
258                }
259            }
260            if (forcedRingerMode != -1) {
261                mAudioManager.setRingerMode(forcedRingerMode);
262                ZenLog.traceSetRingerMode(forcedRingerMode);
263            }
264        }
265        dispatchOnZenModeChanged();
266    }
267
268    public void dump(PrintWriter pw, String prefix) {
269        pw.print(prefix); pw.print("mZenMode=");
270        pw.println(Global.zenModeToString(mZenMode));
271        pw.print(prefix); pw.print("mConfig="); pw.println(mConfig);
272        pw.print(prefix); pw.print("mDefaultConfig="); pw.println(mDefaultConfig);
273        pw.print(prefix); pw.print("mPreviousRingerMode="); pw.println(mPreviousRingerMode);
274        pw.print(prefix); pw.print("mDefaultPhoneApp="); pw.println(mDefaultPhoneApp);
275    }
276
277    public void readXml(XmlPullParser parser) throws XmlPullParserException, IOException {
278        final ZenModeConfig config = ZenModeConfig.readXml(parser);
279        if (config != null) {
280            setConfig(config);
281        }
282    }
283
284    public void writeXml(XmlSerializer out) throws IOException {
285        mConfig.writeXml(out);
286    }
287
288    public ZenModeConfig getConfig() {
289        return mConfig;
290    }
291
292    public boolean setConfig(ZenModeConfig config) {
293        if (config == null || !config.isValid()) return false;
294        if (config.equals(mConfig)) return true;
295        ZenLog.traceConfig(mConfig, config);
296        mConfig = config;
297        dispatchOnConfigChanged();
298        final String val = Integer.toString(mConfig.hashCode());
299        Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_CONFIG_ETAG, val);
300        updateZenMode();
301        return true;
302    }
303
304    private void handleRingerModeChanged() {
305        if (mAudioManager != null) {
306            // follow ringer mode if necessary
307            final int ringerMode = mAudioManager.getRingerMode();
308            int newZen = -1;
309            if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
310                if (mZenMode == Global.ZEN_MODE_OFF) {
311                    newZen = Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
312                }
313            } else if ((ringerMode == AudioManager.RINGER_MODE_NORMAL
314                    || ringerMode == AudioManager.RINGER_MODE_VIBRATE)
315                    && mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) {
316                newZen = Global.ZEN_MODE_OFF;
317            }
318            if (newZen != -1) {
319                ZenLog.traceFollowRingerMode(ringerMode, mZenMode, newZen);
320                setZenMode(newZen, "ringerMode");
321            }
322        }
323    }
324
325    private void dispatchOnConfigChanged() {
326        for (Callback callback : mCallbacks) {
327            callback.onConfigChanged();
328        }
329    }
330
331    private void dispatchOnZenModeChanged() {
332        for (Callback callback : mCallbacks) {
333            callback.onZenModeChanged();
334        }
335    }
336
337    private static boolean isSystem(NotificationRecord record) {
338        return record.isCategory(Notification.CATEGORY_SYSTEM);
339    }
340
341    private static boolean isAlarm(NotificationRecord record) {
342        return record.isCategory(Notification.CATEGORY_ALARM)
343                || record.isAudioStream(AudioManager.STREAM_ALARM)
344                || record.isAudioAttributesUsage(AudioAttributes.USAGE_ALARM);
345    }
346
347    private static boolean isEvent(NotificationRecord record) {
348        return record.isCategory(Notification.CATEGORY_EVENT);
349    }
350
351    private boolean isCall(NotificationRecord record) {
352        return isDefaultPhoneApp(record.sbn.getPackageName())
353                || record.isCategory(Notification.CATEGORY_CALL);
354    }
355
356    private boolean isDefaultPhoneApp(String pkg) {
357        if (mDefaultPhoneApp == null) {
358            final TelecomManager telecomm =
359                    (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
360            mDefaultPhoneApp = telecomm != null ? telecomm.getDefaultPhoneApp() : null;
361            if (DEBUG) Slog.d(TAG, "Default phone app: " + mDefaultPhoneApp);
362        }
363        return pkg != null && mDefaultPhoneApp != null
364                && pkg.equals(mDefaultPhoneApp.getPackageName());
365    }
366
367    private boolean isDefaultMessagingApp(NotificationRecord record) {
368        final int userId = record.getUserId();
369        if (userId == UserHandle.USER_NULL || userId == UserHandle.USER_ALL) return false;
370        final String defaultApp = Secure.getStringForUser(mContext.getContentResolver(),
371                Secure.SMS_DEFAULT_APPLICATION, userId);
372        return Objects.equals(defaultApp, record.sbn.getPackageName());
373    }
374
375    private boolean isMessage(NotificationRecord record) {
376        return record.isCategory(Notification.CATEGORY_MESSAGE) || isDefaultMessagingApp(record);
377    }
378
379    /**
380     * @param extras extras of the notification with EXTRA_PEOPLE populated
381     * @param contactsTimeoutMs timeout in milliseconds to wait for contacts response
382     * @param timeoutAffinity affinity to return when the timeout specified via
383     *                        <code>contactsTimeoutMs</code> is hit
384     */
385    public boolean matchesCallFilter(UserHandle userHandle, Bundle extras,
386            ValidateNotificationPeople validator, int contactsTimeoutMs, float timeoutAffinity) {
387        final int zen = mZenMode;
388        if (zen == Global.ZEN_MODE_NO_INTERRUPTIONS) return false; // nothing gets through
389        if (zen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) {
390            if (!mConfig.allowCalls) return false; // no calls get through
391            if (validator != null) {
392                final float contactAffinity = validator.getContactAffinity(userHandle, extras,
393                        contactsTimeoutMs, timeoutAffinity);
394                return audienceMatches(contactAffinity);
395            }
396        }
397        return true;
398    }
399
400    private boolean audienceMatches(float contactAffinity) {
401        switch (mConfig.allowFrom) {
402            case ZenModeConfig.SOURCE_ANYONE:
403                return true;
404            case ZenModeConfig.SOURCE_CONTACT:
405                return contactAffinity >= ValidateNotificationPeople.VALID_CONTACT;
406            case ZenModeConfig.SOURCE_STAR:
407                return contactAffinity >= ValidateNotificationPeople.STARRED_CONTACT;
408            default:
409                Slog.w(TAG, "Encountered unknown source: " + mConfig.allowFrom);
410                return true;
411        }
412    }
413
414    private final Runnable mRingerModeChanged = new Runnable() {
415        @Override
416        public void run() {
417            handleRingerModeChanged();
418        }
419    };
420
421    private class SettingsObserver extends ContentObserver {
422        private final Uri ZEN_MODE = Global.getUriFor(Global.ZEN_MODE);
423
424        public SettingsObserver(Handler handler) {
425            super(handler);
426        }
427
428        public void observe() {
429            final ContentResolver resolver = mContext.getContentResolver();
430            resolver.registerContentObserver(ZEN_MODE, false /*notifyForDescendents*/, this);
431            update(null);
432        }
433
434        @Override
435        public void onChange(boolean selfChange, Uri uri) {
436            update(uri);
437        }
438
439        public void update(Uri uri) {
440            if (ZEN_MODE.equals(uri)) {
441                updateZenMode();
442            }
443        }
444    }
445
446    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
447        @Override
448        public void onReceive(Context context, Intent intent) {
449            mHandler.post(mRingerModeChanged);
450        }
451    };
452
453    public static class Callback {
454        void onConfigChanged() {}
455        void onZenModeChanged() {}
456    }
457}
458