John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | package com.android.server.notification; |
| 18 | |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 19 | import android.app.ActivityManager; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 20 | import android.app.AlarmManager; |
| 21 | import android.app.PendingIntent; |
| 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.net.Uri; |
Julia Reynolds | fe58f1f | 2016-07-19 10:18:32 -0400 | [diff] [blame] | 28 | import android.os.Binder; |
| 29 | import android.provider.Settings; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 30 | import android.service.notification.Condition; |
| 31 | import android.service.notification.IConditionProvider; |
| 32 | import android.service.notification.ZenModeConfig; |
| 33 | import android.service.notification.ZenModeConfig.ScheduleInfo; |
Julia Reynolds | fe58f1f | 2016-07-19 10:18:32 -0400 | [diff] [blame] | 34 | import android.text.TextUtils; |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 35 | import android.util.ArrayMap; |
Julia Reynolds | fe58f1f | 2016-07-19 10:18:32 -0400 | [diff] [blame] | 36 | import android.util.ArraySet; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | import android.util.Slog; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 39 | |
| 40 | import com.android.server.notification.NotificationManagerService.DumpFilter; |
| 41 | |
| 42 | import java.io.PrintWriter; |
Julia Reynolds | 2ea7b09 | 2016-09-14 11:24:30 -0400 | [diff] [blame] | 43 | import java.util.ArrayList; |
Julia Reynolds | 83eec70 | 2016-05-25 12:29:55 -0400 | [diff] [blame] | 44 | import java.util.Calendar; |
Julia Reynolds | 2ea7b09 | 2016-09-14 11:24:30 -0400 | [diff] [blame] | 45 | import java.util.List; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 46 | import java.util.TimeZone; |
| 47 | |
| 48 | /** |
| 49 | * Built-in zen condition provider for daily scheduled time-based conditions. |
| 50 | */ |
| 51 | public class ScheduleConditionProvider extends SystemConditionProviderService { |
Julia Reynolds | 9eb2c1e | 2016-06-02 12:53:42 -0400 | [diff] [blame] | 52 | static final String TAG = "ConditionProviders.SCP"; |
| 53 | static final boolean DEBUG = true || Log.isLoggable("ConditionProviders", Log.DEBUG); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 54 | |
| 55 | public static final ComponentName COMPONENT = |
| 56 | new ComponentName("android", ScheduleConditionProvider.class.getName()); |
| 57 | private static final String NOT_SHOWN = "..."; |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame] | 58 | private static final String SIMPLE_NAME = ScheduleConditionProvider.class.getSimpleName(); |
| 59 | private static final String ACTION_EVALUATE = SIMPLE_NAME + ".EVALUATE"; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 60 | private static final int REQUEST_CODE_EVALUATE = 1; |
| 61 | private static final String EXTRA_TIME = "time"; |
Julia Reynolds | fe58f1f | 2016-07-19 10:18:32 -0400 | [diff] [blame] | 62 | private static final String SEPARATOR = ";"; |
| 63 | private static final String SCP_SETTING = "snoozed_schedule_condition_provider"; |
| 64 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 65 | |
| 66 | private final Context mContext = this; |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 67 | private final ArrayMap<Uri, ScheduleCalendar> mSubscriptions = new ArrayMap<>(); |
Julia Reynolds | fe58f1f | 2016-07-19 10:18:32 -0400 | [diff] [blame] | 68 | private ArraySet<Uri> mSnoozed = new ArraySet<>(); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 69 | |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 70 | private AlarmManager mAlarmManager; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 71 | private boolean mConnected; |
| 72 | private boolean mRegistered; |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 73 | private long mNextAlarmTime; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 74 | |
| 75 | public ScheduleConditionProvider() { |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame] | 76 | if (DEBUG) Slog.d(TAG, "new " + SIMPLE_NAME + "()"); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public ComponentName getComponent() { |
| 81 | return COMPONENT; |
| 82 | } |
| 83 | |
| 84 | @Override |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 85 | public boolean isValidConditionId(Uri id) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 86 | return ZenModeConfig.isValidScheduleConditionId(id); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void dump(PrintWriter pw, DumpFilter filter) { |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame] | 91 | pw.print(" "); pw.print(SIMPLE_NAME); pw.println(":"); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 92 | pw.print(" mConnected="); pw.println(mConnected); |
| 93 | pw.print(" mRegistered="); pw.println(mRegistered); |
| 94 | pw.println(" mSubscriptions="); |
| 95 | final long now = System.currentTimeMillis(); |
Julia Reynolds | 2ea7b09 | 2016-09-14 11:24:30 -0400 | [diff] [blame] | 96 | synchronized (mSubscriptions) { |
| 97 | for (Uri conditionId : mSubscriptions.keySet()) { |
| 98 | pw.print(" "); |
| 99 | pw.print(meetsSchedule(mSubscriptions.get(conditionId), now) ? "* " : " "); |
| 100 | pw.println(conditionId); |
| 101 | pw.print(" "); |
| 102 | pw.println(mSubscriptions.get(conditionId).toString()); |
| 103 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 104 | } |
Julia Reynolds | fe58f1f | 2016-07-19 10:18:32 -0400 | [diff] [blame] | 105 | pw.println(" snoozed due to alarm: " + TextUtils.join(SEPARATOR, mSnoozed)); |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 106 | dumpUpcomingTime(pw, "mNextAlarmTime", mNextAlarmTime, now); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void onConnected() { |
| 111 | if (DEBUG) Slog.d(TAG, "onConnected"); |
| 112 | mConnected = true; |
Julia Reynolds | fe58f1f | 2016-07-19 10:18:32 -0400 | [diff] [blame] | 113 | readSnoozed(); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame] | 117 | public void onBootComplete() { |
| 118 | // noop |
| 119 | } |
| 120 | |
| 121 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 122 | public void onDestroy() { |
| 123 | super.onDestroy(); |
| 124 | if (DEBUG) Slog.d(TAG, "onDestroy"); |
| 125 | mConnected = false; |
| 126 | } |
| 127 | |
| 128 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 129 | public void onSubscribe(Uri conditionId) { |
| 130 | if (DEBUG) Slog.d(TAG, "onSubscribe " + conditionId); |
| 131 | if (!ZenModeConfig.isValidScheduleConditionId(conditionId)) { |
Julia Reynolds | 2ea7b09 | 2016-09-14 11:24:30 -0400 | [diff] [blame] | 132 | notifyCondition(createCondition(conditionId, Condition.STATE_FALSE, "badCondition")); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 133 | return; |
| 134 | } |
Julia Reynolds | 2ea7b09 | 2016-09-14 11:24:30 -0400 | [diff] [blame] | 135 | synchronized (mSubscriptions) { |
| 136 | mSubscriptions.put(conditionId, toScheduleCalendar(conditionId)); |
| 137 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 138 | evaluateSubscriptions(); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void onUnsubscribe(Uri conditionId) { |
| 143 | if (DEBUG) Slog.d(TAG, "onUnsubscribe " + conditionId); |
Julia Reynolds | 2ea7b09 | 2016-09-14 11:24:30 -0400 | [diff] [blame] | 144 | synchronized (mSubscriptions) { |
| 145 | mSubscriptions.remove(conditionId); |
| 146 | } |
Julia Reynolds | fe58f1f | 2016-07-19 10:18:32 -0400 | [diff] [blame] | 147 | removeSnoozed(conditionId); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 148 | evaluateSubscriptions(); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void attachBase(Context base) { |
| 153 | attachBaseContext(base); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public IConditionProvider asInterface() { |
| 158 | return (IConditionProvider) onBind(null); |
| 159 | } |
| 160 | |
| 161 | private void evaluateSubscriptions() { |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 162 | if (mAlarmManager == null) { |
| 163 | mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
| 164 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 165 | final long now = System.currentTimeMillis(); |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 166 | mNextAlarmTime = 0; |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 167 | long nextUserAlarmTime = getNextAlarm(); |
Julia Reynolds | 2ea7b09 | 2016-09-14 11:24:30 -0400 | [diff] [blame] | 168 | List<Condition> conditionsToNotify = new ArrayList<>(); |
| 169 | synchronized (mSubscriptions) { |
| 170 | setRegistered(!mSubscriptions.isEmpty()); |
| 171 | for (Uri conditionId : mSubscriptions.keySet()) { |
| 172 | final ScheduleCalendar cal = mSubscriptions.get(conditionId); |
| 173 | if (cal != null && cal.isInSchedule(now)) { |
| 174 | if (conditionSnoozed(conditionId) || cal.shouldExitForAlarm(now)) { |
| 175 | conditionsToNotify.add(createCondition( |
| 176 | conditionId, Condition.STATE_FALSE, "alarmCanceled")); |
| 177 | addSnoozed(conditionId); |
| 178 | } else { |
| 179 | conditionsToNotify.add(createCondition( |
| 180 | conditionId, Condition.STATE_TRUE, "meetsSchedule")); |
| 181 | } |
Julia Reynolds | 9eb2c1e | 2016-06-02 12:53:42 -0400 | [diff] [blame] | 182 | cal.maybeSetNextAlarm(now, nextUserAlarmTime); |
Julia Reynolds | 2ea7b09 | 2016-09-14 11:24:30 -0400 | [diff] [blame] | 183 | } else { |
| 184 | conditionsToNotify.add(createCondition( |
| 185 | conditionId, Condition.STATE_FALSE, "!meetsSchedule")); |
| 186 | removeSnoozed(conditionId); |
| 187 | if (cal != null && nextUserAlarmTime == 0) { |
| 188 | cal.maybeSetNextAlarm(now, nextUserAlarmTime); |
| 189 | } |
Julia Reynolds | 9eb2c1e | 2016-06-02 12:53:42 -0400 | [diff] [blame] | 190 | } |
Julia Reynolds | 2ea7b09 | 2016-09-14 11:24:30 -0400 | [diff] [blame] | 191 | if (cal != null) { |
| 192 | final long nextChangeTime = cal.getNextChangeTime(now); |
| 193 | if (nextChangeTime > 0 && nextChangeTime > now) { |
| 194 | if (mNextAlarmTime == 0 || nextChangeTime < mNextAlarmTime) { |
| 195 | mNextAlarmTime = nextChangeTime; |
| 196 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
Julia Reynolds | 2ea7b09 | 2016-09-14 11:24:30 -0400 | [diff] [blame] | 201 | notifyConditions(conditionsToNotify.toArray(new Condition[conditionsToNotify.size()])); |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 202 | updateAlarm(now, mNextAlarmTime); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | private void updateAlarm(long now, long time) { |
| 206 | final AlarmManager alarms = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
| 207 | final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, |
| 208 | REQUEST_CODE_EVALUATE, |
| 209 | new Intent(ACTION_EVALUATE) |
| 210 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 211 | .putExtra(EXTRA_TIME, time), |
| 212 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 213 | alarms.cancel(pendingIntent); |
| 214 | if (time > now) { |
| 215 | if (DEBUG) Slog.d(TAG, String.format("Scheduling evaluate for %s, in %s, now=%s", |
| 216 | ts(time), formatDuration(time - now), ts(now))); |
| 217 | alarms.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent); |
| 218 | } else { |
| 219 | if (DEBUG) Slog.d(TAG, "Not scheduling evaluate"); |
| 220 | } |
| 221 | } |
| 222 | |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 223 | public long getNextAlarm() { |
| 224 | final AlarmManager.AlarmClockInfo info = mAlarmManager.getNextAlarmClock( |
| 225 | ActivityManager.getCurrentUser()); |
| 226 | return info != null ? info.getTriggerTime() : 0; |
| 227 | } |
| 228 | |
Julia Reynolds | fe58f1f | 2016-07-19 10:18:32 -0400 | [diff] [blame] | 229 | private boolean meetsSchedule(ScheduleCalendar cal, long time) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 230 | return cal != null && cal.isInSchedule(time); |
| 231 | } |
| 232 | |
| 233 | private static ScheduleCalendar toScheduleCalendar(Uri conditionId) { |
| 234 | final ScheduleInfo schedule = ZenModeConfig.tryParseScheduleConditionId(conditionId); |
| 235 | if (schedule == null || schedule.days == null || schedule.days.length == 0) return null; |
| 236 | final ScheduleCalendar sc = new ScheduleCalendar(); |
| 237 | sc.setSchedule(schedule); |
| 238 | sc.setTimeZone(TimeZone.getDefault()); |
| 239 | return sc; |
| 240 | } |
| 241 | |
| 242 | private void setRegistered(boolean registered) { |
| 243 | if (mRegistered == registered) return; |
| 244 | if (DEBUG) Slog.d(TAG, "setRegistered " + registered); |
| 245 | mRegistered = registered; |
| 246 | if (mRegistered) { |
| 247 | final IntentFilter filter = new IntentFilter(); |
| 248 | filter.addAction(Intent.ACTION_TIME_CHANGED); |
| 249 | filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
| 250 | filter.addAction(ACTION_EVALUATE); |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 251 | filter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 252 | registerReceiver(mReceiver, filter); |
| 253 | } else { |
| 254 | unregisterReceiver(mReceiver); |
| 255 | } |
| 256 | } |
| 257 | |
Julia Reynolds | 2ea7b09 | 2016-09-14 11:24:30 -0400 | [diff] [blame] | 258 | private Condition createCondition(Uri id, int state, String reason) { |
| 259 | if (DEBUG) Slog.d(TAG, "notifyCondition " + id |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 260 | + " " + Condition.stateToString(state) |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 261 | + " reason=" + reason); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 262 | final String summary = NOT_SHOWN; |
| 263 | final String line1 = NOT_SHOWN; |
| 264 | final String line2 = NOT_SHOWN; |
| 265 | return new Condition(id, summary, line1, line2, 0, state, Condition.FLAG_RELEVANT_ALWAYS); |
| 266 | } |
| 267 | |
Julia Reynolds | fe58f1f | 2016-07-19 10:18:32 -0400 | [diff] [blame] | 268 | private boolean conditionSnoozed(Uri conditionId) { |
| 269 | synchronized (mSnoozed) { |
| 270 | return mSnoozed.contains(conditionId); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | private void addSnoozed(Uri conditionId) { |
| 275 | synchronized (mSnoozed) { |
| 276 | mSnoozed.add(conditionId); |
| 277 | saveSnoozedLocked(); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | private void removeSnoozed(Uri conditionId) { |
| 282 | synchronized (mSnoozed) { |
| 283 | mSnoozed.remove(conditionId); |
| 284 | saveSnoozedLocked(); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | public void saveSnoozedLocked() { |
| 289 | final String setting = TextUtils.join(SEPARATOR, mSnoozed); |
| 290 | final int currentUser = ActivityManager.getCurrentUser(); |
| 291 | Settings.Secure.putStringForUser(mContext.getContentResolver(), |
| 292 | SCP_SETTING, |
| 293 | setting, |
| 294 | currentUser); |
| 295 | } |
| 296 | |
| 297 | public void readSnoozed() { |
| 298 | synchronized (mSnoozed) { |
| 299 | long identity = Binder.clearCallingIdentity(); |
| 300 | try { |
| 301 | final String setting = Settings.Secure.getStringForUser( |
| 302 | mContext.getContentResolver(), |
| 303 | SCP_SETTING, |
| 304 | ActivityManager.getCurrentUser()); |
| 305 | if (setting != null) { |
| 306 | final String[] tokens = setting.split(SEPARATOR); |
| 307 | for (int i = 0; i < tokens.length; i++) { |
| 308 | String token = tokens[i]; |
| 309 | if (token != null) { |
| 310 | token = token.trim(); |
| 311 | } |
| 312 | if (TextUtils.isEmpty(token)) { |
| 313 | continue; |
| 314 | } |
| 315 | mSnoozed.add(Uri.parse(token)); |
| 316 | } |
| 317 | } |
| 318 | } finally { |
| 319 | Binder.restoreCallingIdentity(identity); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 324 | private BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 325 | @Override |
| 326 | public void onReceive(Context context, Intent intent) { |
| 327 | if (DEBUG) Slog.d(TAG, "onReceive " + intent.getAction()); |
Julia Reynolds | 83eec70 | 2016-05-25 12:29:55 -0400 | [diff] [blame] | 328 | if (Intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
Julia Reynolds | 2ea7b09 | 2016-09-14 11:24:30 -0400 | [diff] [blame] | 329 | synchronized (mSubscriptions) { |
| 330 | for (Uri conditionId : mSubscriptions.keySet()) { |
| 331 | final ScheduleCalendar cal = mSubscriptions.get(conditionId); |
| 332 | if (cal != null) { |
| 333 | cal.setTimeZone(Calendar.getInstance().getTimeZone()); |
| 334 | } |
Julia Reynolds | 83eec70 | 2016-05-25 12:29:55 -0400 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 338 | evaluateSubscriptions(); |
| 339 | } |
| 340 | }; |
| 341 | |
| 342 | } |