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