ZenModeHelper.java revision 1c923a386ee4d8c31cc289f8628b8fc46bf08e86
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            ));
83    private static final Set<String> MESSAGE_PACKAGES = new HashSet<String>(Arrays.asList(
84            "com.google.android.talk",
85            "com.android.mms"
86            ));
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        mContext.registerReceiver(new ZenBroadcastReceiver(), filter);
101    }
102
103    public static ZenModeConfig readDefaultConfig(Resources resources) {
104        XmlResourceParser parser = null;
105        try {
106            parser = resources.getXml(R.xml.default_zen_mode_config);
107            while (parser.next() != XmlPullParser.END_DOCUMENT) {
108                final ZenModeConfig config = ZenModeConfig.readXml(parser);
109                if (config != null) return config;
110            }
111        } catch (Exception e) {
112            Slog.w(TAG, "Error reading default zen mode config from resource", e);
113        } finally {
114            IoUtils.closeQuietly(parser);
115        }
116        return new ZenModeConfig();
117    }
118
119    public void addCallback(Callback callback) {
120        mCallbacks.add(callback);
121    }
122
123    public boolean shouldIntercept(String pkg, Notification n) {
124        if (mZenMode != Global.ZEN_MODE_OFF) {
125            if (isCall(pkg, n)) {
126                return !mConfig.allowCalls;
127            }
128            if (isMessage(pkg, n)) {
129                return !mConfig.allowMessages;
130            }
131            return true;
132        }
133        return false;
134    }
135
136    public int getZenMode() {
137        return mZenMode;
138    }
139
140    public void setZenMode(int zenModeValue) {
141        Global.putInt(mContext.getContentResolver(), Global.ZEN_MODE, zenModeValue);
142    }
143
144    public void updateZenMode() {
145        final int mode = Global.getInt(mContext.getContentResolver(),
146                Global.ZEN_MODE, Global.ZEN_MODE_OFF);
147        if (mode != mZenMode) {
148            Slog.d(TAG, String.format("updateZenMode: %s -> %s",
149                    Global.zenModeToString(mZenMode),
150                    Global.zenModeToString(mode)));
151        }
152        mZenMode = mode;
153        final boolean zen = mZenMode != Global.ZEN_MODE_OFF;
154        final String[] exceptionPackages = null; // none (for now)
155
156        // call restrictions
157        final boolean muteCalls = zen && !mConfig.allowCalls;
158        mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, AudioManager.STREAM_RING,
159                muteCalls ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
160                exceptionPackages);
161        mAppOps.setRestriction(AppOpsManager.OP_PLAY_AUDIO, AudioManager.STREAM_RING,
162                muteCalls ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
163                exceptionPackages);
164
165        // restrict vibrations with no hints
166        mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, AudioManager.USE_DEFAULT_STREAM_TYPE,
167                zen ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
168                exceptionPackages);
169        dispatchOnZenModeChanged();
170    }
171
172    public boolean allowDisable(int what, IBinder token, String pkg) {
173        if (isCall(pkg, null)) {
174            return mZenMode == Global.ZEN_MODE_OFF || mConfig.allowCalls;
175        }
176        return true;
177    }
178
179    public void dump(PrintWriter pw, String prefix) {
180        pw.print(prefix); pw.print("mZenMode=");
181        pw.println(Global.zenModeToString(mZenMode));
182        pw.print(prefix); pw.print("mConfig="); pw.println(mConfig);
183        pw.print(prefix); pw.print("mDefaultConfig="); pw.println(mDefaultConfig);
184    }
185
186    public void readXml(XmlPullParser parser) throws XmlPullParserException, IOException {
187        final ZenModeConfig config = ZenModeConfig.readXml(parser);
188        if (config != null) {
189            setConfig(config);
190        }
191    }
192
193    public void writeXml(XmlSerializer out) throws IOException {
194        mConfig.writeXml(out);
195    }
196
197    public ZenModeConfig getConfig() {
198        return mConfig;
199    }
200
201    public boolean setConfig(ZenModeConfig config) {
202        if (config == null || !config.isValid()) return false;
203        if (config.equals(mConfig)) return true;
204        mConfig = config;
205        Slog.d(TAG, "mConfig=" + mConfig);
206        dispatchOnConfigChanged();
207        final String val = Integer.toString(mConfig.hashCode());
208        Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_CONFIG_ETAG, val);
209        updateAlarms();
210        updateZenMode();
211        return true;
212    }
213
214    private void dispatchOnConfigChanged() {
215        for (Callback callback : mCallbacks) {
216            callback.onConfigChanged();
217        }
218    }
219
220    private void dispatchOnZenModeChanged() {
221        for (Callback callback : mCallbacks) {
222            callback.onZenModeChanged();
223        }
224    }
225
226    private boolean isCall(String pkg, Notification n) {
227        return CALL_PACKAGES.contains(pkg);
228    }
229
230    private boolean isMessage(String pkg, Notification n) {
231        return MESSAGE_PACKAGES.contains(pkg);
232    }
233
234    private void updateAlarms() {
235        updateAlarm(ACTION_ENTER_ZEN, REQUEST_CODE_ENTER,
236                mConfig.sleepStartHour, mConfig.sleepStartMinute);
237        updateAlarm(ACTION_EXIT_ZEN, REQUEST_CODE_EXIT,
238                mConfig.sleepEndHour, mConfig.sleepEndMinute);
239    }
240
241    private void updateAlarm(String action, int requestCode, int hr, int min) {
242        final AlarmManager alarms = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
243        final long now = System.currentTimeMillis();
244        final Calendar c = Calendar.getInstance();
245        c.setTimeInMillis(now);
246        c.set(Calendar.HOUR_OF_DAY, hr);
247        c.set(Calendar.MINUTE, min);
248        c.set(Calendar.SECOND, 0);
249        c.set(Calendar.MILLISECOND, 0);
250        if (c.getTimeInMillis() <= now) {
251            c.add(Calendar.DATE, 1);
252        }
253        final long time = c.getTimeInMillis();
254        final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, requestCode,
255                new Intent(action).putExtra(EXTRA_TIME, time), PendingIntent.FLAG_UPDATE_CURRENT);
256        alarms.cancel(pendingIntent);
257        if (mConfig.sleepMode != null) {
258            Slog.d(TAG, String.format("Scheduling %s for %s, %s in the future, now=%s",
259                    action, ts(time), time - now, ts(now)));
260            alarms.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
261        }
262    }
263
264    private static String ts(long time) {
265        return new Date(time) + " (" + time + ")";
266    }
267
268    public static boolean isWeekend(long time, int offsetDays) {
269        final Calendar c = Calendar.getInstance();
270        c.setTimeInMillis(time);
271        if (offsetDays != 0) {
272            c.add(Calendar.DATE, offsetDays);
273        }
274        final int day = c.get(Calendar.DAY_OF_WEEK);
275        return day == Calendar.SATURDAY || day == Calendar.SUNDAY;
276    }
277
278    private class SettingsObserver extends ContentObserver {
279        private final Uri ZEN_MODE = Global.getUriFor(Global.ZEN_MODE);
280
281        public SettingsObserver(Handler handler) {
282            super(handler);
283        }
284
285        public void observe() {
286            final ContentResolver resolver = mContext.getContentResolver();
287            resolver.registerContentObserver(ZEN_MODE, false /*notifyForDescendents*/, this);
288            update(null);
289        }
290
291        @Override
292        public void onChange(boolean selfChange, Uri uri) {
293            update(uri);
294        }
295
296        public void update(Uri uri) {
297            if (ZEN_MODE.equals(uri)) {
298                updateZenMode();
299            }
300        }
301    }
302
303    private class ZenBroadcastReceiver extends BroadcastReceiver {
304        @Override
305        public void onReceive(Context context, Intent intent) {
306            if (ACTION_ENTER_ZEN.equals(intent.getAction())) {
307                setZenMode(intent, 1, Global.ZEN_MODE_ON);
308            } else if (ACTION_EXIT_ZEN.equals(intent.getAction())) {
309                setZenMode(intent, 0, Global.ZEN_MODE_OFF);
310            }
311        }
312
313        private void setZenMode(Intent intent, int wkendOffsetDays, int zenModeValue) {
314            final long schTime = intent.getLongExtra(EXTRA_TIME, 0);
315            final long now = System.currentTimeMillis();
316            Slog.d(TAG, String.format("%s scheduled for %s, fired at %s, delta=%s",
317                    intent.getAction(), ts(schTime), ts(now), now - schTime));
318
319            final boolean skip = ZenModeConfig.SLEEP_MODE_WEEKNIGHTS.equals(mConfig.sleepMode) &&
320                    isWeekend(schTime, wkendOffsetDays);
321
322            if (skip) {
323                Slog.d(TAG, "Skipping zen mode update for the weekend");
324            } else {
325                ZenModeHelper.this.setZenMode(zenModeValue);
326            }
327            updateAlarms();
328        }
329    }
330
331    public static class Callback {
332        void onConfigChanged() {}
333        void onZenModeChanged() {}
334    }
335}
336