ZenModeHelper.java revision 3332ba54ae85df14d761447d86d2aa19d448ce11
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 android.app.AlarmManager;
20import android.app.AppOpsManager;
21import android.app.Notification;
22import android.app.PendingIntent;
23import android.content.BroadcastReceiver;
24import android.content.ContentResolver;
25import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.content.res.Resources;
29import android.content.res.XmlResourceParser;
30import android.database.ContentObserver;
31import android.media.AudioManager;
32import android.net.Uri;
33import android.os.Handler;
34import android.os.IBinder;
35import android.provider.Settings.Global;
36import android.service.notification.ZenModeConfig;
37import android.util.Slog;
38
39import com.android.internal.R;
40
41import libcore.io.IoUtils;
42
43import org.xmlpull.v1.XmlPullParser;
44import org.xmlpull.v1.XmlPullParserException;
45import org.xmlpull.v1.XmlSerializer;
46
47import java.io.IOException;
48import java.io.PrintWriter;
49import java.util.ArrayList;
50import java.util.Arrays;
51import java.util.Calendar;
52import java.util.Date;
53import java.util.HashSet;
54import java.util.Set;
55
56/**
57 * NotificationManagerService helper for functionality related to zen mode.
58 */
59public class ZenModeHelper {
60    private static final String TAG = "ZenModeHelper";
61
62    private static final String ACTION_ENTER_ZEN = "enter_zen";
63    private static final int REQUEST_CODE_ENTER = 100;
64    private static final String ACTION_EXIT_ZEN = "exit_zen";
65    private static final int REQUEST_CODE_EXIT = 101;
66    private static final String EXTRA_TIME = "time";
67
68    private final Context mContext;
69    private final Handler mHandler;
70    private final SettingsObserver mSettingsObserver;
71    private final AppOpsManager mAppOps;
72    private final ZenModeConfig mDefaultConfig;
73    private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
74
75    private int mZenMode;
76    private ZenModeConfig mConfig;
77
78    // temporary, until we update apps to provide metadata
79    private static final Set<String> CALL_PACKAGES = new HashSet<String>(Arrays.asList(
80            "com.google.android.dialer",
81            "com.android.phone",
82            "com.android.example.notificationshowcase"
83            ));
84    private static final Set<String> MESSAGE_PACKAGES = new HashSet<String>(Arrays.asList(
85            "com.google.android.talk",
86            "com.android.mms",
87            "com.android.example.notificationshowcase"
88            ));
89    private static final Set<String> ALARM_PACKAGES = new HashSet<String>(Arrays.asList(
90            "com.google.android.deskclock"
91            ));
92    private static final Set<String> SYSTEM_PACKAGES = new HashSet<String>(Arrays.asList(
93            "android",
94            "com.android.systemui"
95            ));
96
97    public ZenModeHelper(Context context, Handler handler) {
98        mContext = context;
99        mHandler = handler;
100        mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
101        mDefaultConfig = readDefaultConfig(context.getResources());
102        mConfig = mDefaultConfig;
103        mSettingsObserver = new SettingsObserver(mHandler);
104        mSettingsObserver.observe();
105
106        final IntentFilter filter = new IntentFilter();
107        filter.addAction(ACTION_ENTER_ZEN);
108        filter.addAction(ACTION_EXIT_ZEN);
109        mContext.registerReceiver(new ZenBroadcastReceiver(), filter);
110    }
111
112    public static ZenModeConfig readDefaultConfig(Resources resources) {
113        XmlResourceParser parser = null;
114        try {
115            parser = resources.getXml(R.xml.default_zen_mode_config);
116            while (parser.next() != XmlPullParser.END_DOCUMENT) {
117                final ZenModeConfig config = ZenModeConfig.readXml(parser);
118                if (config != null) return config;
119            }
120        } catch (Exception e) {
121            Slog.w(TAG, "Error reading default zen mode config from resource", e);
122        } finally {
123            IoUtils.closeQuietly(parser);
124        }
125        return new ZenModeConfig();
126    }
127
128    public void addCallback(Callback callback) {
129        mCallbacks.add(callback);
130    }
131
132    public boolean shouldIntercept(NotificationRecord record, boolean previouslySeen) {
133        if (mZenMode != Global.ZEN_MODE_OFF) {
134            if (previouslySeen && !record.isIntercepted()) {
135                // notifications never transition from not intercepted to intercepted
136                return false;
137            }
138            if (isSystem(record)) {
139                return false;
140            }
141            if (isAlarm(record)) {
142                return false;
143            }
144            // audience has veto power over all following rules
145            if (!audienceMatches(record)) {
146                return true;
147            }
148            if (isCall(record)) {
149                return !mConfig.allowCalls;
150            }
151            if (isMessage(record)) {
152                return !mConfig.allowMessages;
153            }
154            return true;
155        }
156        return false;
157    }
158
159    public int getZenMode() {
160        return mZenMode;
161    }
162
163    public void setZenMode(int zenModeValue) {
164        Global.putInt(mContext.getContentResolver(), Global.ZEN_MODE, zenModeValue);
165    }
166
167    public void updateZenMode() {
168        final int mode = Global.getInt(mContext.getContentResolver(),
169                Global.ZEN_MODE, Global.ZEN_MODE_OFF);
170        if (mode != mZenMode) {
171            Slog.d(TAG, String.format("updateZenMode: %s -> %s",
172                    Global.zenModeToString(mZenMode),
173                    Global.zenModeToString(mode)));
174        }
175        mZenMode = mode;
176        final boolean zen = mZenMode != Global.ZEN_MODE_OFF;
177        final String[] exceptionPackages = null; // none (for now)
178
179        // call restrictions
180        final boolean muteCalls = zen && !mConfig.allowCalls;
181        mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, AudioManager.STREAM_RING,
182                muteCalls ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
183                exceptionPackages);
184        mAppOps.setRestriction(AppOpsManager.OP_PLAY_AUDIO, AudioManager.STREAM_RING,
185                muteCalls ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
186                exceptionPackages);
187
188        // restrict vibrations with no hints
189        mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, AudioManager.USE_DEFAULT_STREAM_TYPE,
190                zen ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
191                exceptionPackages);
192        dispatchOnZenModeChanged();
193    }
194
195    public boolean allowDisable(int what, IBinder token, String pkg) {
196        // TODO(cwren): delete this API before the next release. Bug:15344099
197        if (CALL_PACKAGES.contains(pkg)) {
198            return mZenMode == Global.ZEN_MODE_OFF || mConfig.allowCalls;
199        }
200        return true;
201    }
202
203    public void dump(PrintWriter pw, String prefix) {
204        pw.print(prefix); pw.print("mZenMode=");
205        pw.println(Global.zenModeToString(mZenMode));
206        pw.print(prefix); pw.print("mConfig="); pw.println(mConfig);
207        pw.print(prefix); pw.print("mDefaultConfig="); pw.println(mDefaultConfig);
208    }
209
210    public void readXml(XmlPullParser parser) throws XmlPullParserException, IOException {
211        final ZenModeConfig config = ZenModeConfig.readXml(parser);
212        if (config != null) {
213            setConfig(config);
214        }
215    }
216
217    public void writeXml(XmlSerializer out) throws IOException {
218        mConfig.writeXml(out);
219    }
220
221    public ZenModeConfig getConfig() {
222        return mConfig;
223    }
224
225    public boolean setConfig(ZenModeConfig config) {
226        if (config == null || !config.isValid()) return false;
227        if (config.equals(mConfig)) return true;
228        mConfig = config;
229        Slog.d(TAG, "mConfig=" + mConfig);
230        dispatchOnConfigChanged();
231        final String val = Integer.toString(mConfig.hashCode());
232        Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_CONFIG_ETAG, val);
233        updateAlarms();
234        updateZenMode();
235        return true;
236    }
237
238    private void dispatchOnConfigChanged() {
239        for (Callback callback : mCallbacks) {
240            callback.onConfigChanged();
241        }
242    }
243
244    private void dispatchOnZenModeChanged() {
245        for (Callback callback : mCallbacks) {
246            callback.onZenModeChanged();
247        }
248    }
249
250    private boolean isSystem(NotificationRecord record) {
251        return SYSTEM_PACKAGES.contains(record.sbn.getPackageName())
252                && Notification.CATEGORY_SYSTEM.equals(record.getNotification().category);
253    }
254
255    private boolean isAlarm(NotificationRecord record) {
256        return ALARM_PACKAGES.contains(record.sbn.getPackageName());
257    }
258
259    private boolean isCall(NotificationRecord record) {
260        return CALL_PACKAGES.contains(record.sbn.getPackageName());
261    }
262
263    private boolean isMessage(NotificationRecord record) {
264        return MESSAGE_PACKAGES.contains(record.sbn.getPackageName());
265    }
266
267    private boolean audienceMatches(NotificationRecord record) {
268        switch (mConfig.allowFrom) {
269            case ZenModeConfig.SOURCE_ANYONE:
270                return true;
271            case ZenModeConfig.SOURCE_CONTACT:
272                return record.getContactAffinity() >= ValidateNotificationPeople.VALID_CONTACT;
273            case ZenModeConfig.SOURCE_STAR:
274                return record.getContactAffinity() >= ValidateNotificationPeople.STARRED_CONTACT;
275            default:
276                Slog.w(TAG, "Encountered unknown source: " + mConfig.allowFrom);
277                return true;
278        }
279    }
280
281    private void updateAlarms() {
282        updateAlarm(ACTION_ENTER_ZEN, REQUEST_CODE_ENTER,
283                mConfig.sleepStartHour, mConfig.sleepStartMinute);
284        updateAlarm(ACTION_EXIT_ZEN, REQUEST_CODE_EXIT,
285                mConfig.sleepEndHour, mConfig.sleepEndMinute);
286    }
287
288    private void updateAlarm(String action, int requestCode, int hr, int min) {
289        final AlarmManager alarms = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
290        final long now = System.currentTimeMillis();
291        final Calendar c = Calendar.getInstance();
292        c.setTimeInMillis(now);
293        c.set(Calendar.HOUR_OF_DAY, hr);
294        c.set(Calendar.MINUTE, min);
295        c.set(Calendar.SECOND, 0);
296        c.set(Calendar.MILLISECOND, 0);
297        if (c.getTimeInMillis() <= now) {
298            c.add(Calendar.DATE, 1);
299        }
300        final long time = c.getTimeInMillis();
301        final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, requestCode,
302                new Intent(action).putExtra(EXTRA_TIME, time), PendingIntent.FLAG_UPDATE_CURRENT);
303        alarms.cancel(pendingIntent);
304        if (mConfig.sleepMode != null) {
305            Slog.d(TAG, String.format("Scheduling %s for %s, %s in the future, now=%s",
306                    action, ts(time), time - now, ts(now)));
307            alarms.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
308        }
309    }
310
311    private static String ts(long time) {
312        return new Date(time) + " (" + time + ")";
313    }
314
315    public static boolean isWeekend(long time, int offsetDays) {
316        final Calendar c = Calendar.getInstance();
317        c.setTimeInMillis(time);
318        if (offsetDays != 0) {
319            c.add(Calendar.DATE, offsetDays);
320        }
321        final int day = c.get(Calendar.DAY_OF_WEEK);
322        return day == Calendar.SATURDAY || day == Calendar.SUNDAY;
323    }
324
325    private class SettingsObserver extends ContentObserver {
326        private final Uri ZEN_MODE = Global.getUriFor(Global.ZEN_MODE);
327
328        public SettingsObserver(Handler handler) {
329            super(handler);
330        }
331
332        public void observe() {
333            final ContentResolver resolver = mContext.getContentResolver();
334            resolver.registerContentObserver(ZEN_MODE, false /*notifyForDescendents*/, this);
335            update(null);
336        }
337
338        @Override
339        public void onChange(boolean selfChange, Uri uri) {
340            update(uri);
341        }
342
343        public void update(Uri uri) {
344            if (ZEN_MODE.equals(uri)) {
345                updateZenMode();
346            }
347        }
348    }
349
350    private class ZenBroadcastReceiver extends BroadcastReceiver {
351        @Override
352        public void onReceive(Context context, Intent intent) {
353            if (ACTION_ENTER_ZEN.equals(intent.getAction())) {
354                setZenMode(intent, 1, Global.ZEN_MODE_ON);
355            } else if (ACTION_EXIT_ZEN.equals(intent.getAction())) {
356                setZenMode(intent, 0, Global.ZEN_MODE_OFF);
357            }
358        }
359
360        private void setZenMode(Intent intent, int wkendOffsetDays, int zenModeValue) {
361            final long schTime = intent.getLongExtra(EXTRA_TIME, 0);
362            final long now = System.currentTimeMillis();
363            Slog.d(TAG, String.format("%s scheduled for %s, fired at %s, delta=%s",
364                    intent.getAction(), ts(schTime), ts(now), now - schTime));
365
366            final boolean skip = ZenModeConfig.SLEEP_MODE_WEEKNIGHTS.equals(mConfig.sleepMode) &&
367                    isWeekend(schTime, wkendOffsetDays);
368
369            if (skip) {
370                Slog.d(TAG, "Skipping zen mode update for the weekend");
371            } else {
372                ZenModeHelper.this.setZenMode(zenModeValue);
373            }
374            updateAlarms();
375        }
376    }
377
378    public static class Callback {
379        void onConfigChanged() {}
380        void onZenModeChanged() {}
381    }
382}
383