John Spurlock | d60258f | 2015-04-30 09:30:52 -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 | |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 19 | import android.app.AlarmManager; |
| 20 | import android.app.PendingIntent; |
| 21 | import android.content.BroadcastReceiver; |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 26 | import android.net.Uri; |
| 27 | import android.service.notification.Condition; |
| 28 | import android.service.notification.IConditionProvider; |
| 29 | import android.service.notification.ZenModeConfig; |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 30 | import android.service.notification.ZenModeConfig.EventInfo; |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 31 | import android.util.ArraySet; |
| 32 | import android.util.Log; |
| 33 | import android.util.Slog; |
| 34 | |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 35 | import com.android.server.notification.CalendarTracker.CheckEventResult; |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 36 | import com.android.server.notification.NotificationManagerService.DumpFilter; |
| 37 | |
| 38 | import java.io.PrintWriter; |
| 39 | |
| 40 | /** |
| 41 | * Built-in zen condition provider for calendar event-based conditions. |
| 42 | */ |
| 43 | public class EventConditionProvider extends SystemConditionProviderService { |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 44 | private static final String TAG = "ConditionProviders.ECP"; |
| 45 | private static final boolean DEBUG = Log.isLoggable("ConditionProviders", Log.DEBUG); |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 46 | |
| 47 | public static final ComponentName COMPONENT = |
| 48 | new ComponentName("android", EventConditionProvider.class.getName()); |
| 49 | private static final String NOT_SHOWN = "..."; |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 50 | private static final String SIMPLE_NAME = EventConditionProvider.class.getSimpleName(); |
| 51 | private static final String ACTION_EVALUATE = SIMPLE_NAME + ".EVALUATE"; |
| 52 | private static final int REQUEST_CODE_EVALUATE = 1; |
| 53 | private static final String EXTRA_TIME = "time"; |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 54 | |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 55 | private final Context mContext = this; |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 56 | private final ArraySet<Uri> mSubscriptions = new ArraySet<Uri>(); |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 57 | private final CalendarTracker mTracker = new CalendarTracker(mContext); |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 58 | |
| 59 | private boolean mConnected; |
| 60 | private boolean mRegistered; |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 61 | private boolean mBootComplete; // don't hammer the calendar provider until boot completes. |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 62 | |
| 63 | public EventConditionProvider() { |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 64 | if (DEBUG) Slog.d(TAG, "new " + SIMPLE_NAME + "()"); |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public ComponentName getComponent() { |
| 69 | return COMPONENT; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean isValidConditionId(Uri id) { |
| 74 | return ZenModeConfig.isValidEventConditionId(id); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void dump(PrintWriter pw, DumpFilter filter) { |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 79 | pw.print(" "); pw.print(SIMPLE_NAME); pw.println(":"); |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 80 | pw.print(" mConnected="); pw.println(mConnected); |
| 81 | pw.print(" mRegistered="); pw.println(mRegistered); |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 82 | pw.print(" mBootComplete="); pw.println(mBootComplete); |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 83 | pw.println(" mSubscriptions="); |
| 84 | for (Uri conditionId : mSubscriptions) { |
| 85 | pw.print(" "); |
| 86 | pw.println(conditionId); |
| 87 | } |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 88 | pw.println(" mTracker="); |
| 89 | mTracker.dump(" ", pw); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void onBootComplete() { |
| 94 | if (DEBUG) Slog.d(TAG, "onBootComplete"); |
| 95 | if (mBootComplete) return; |
| 96 | mBootComplete = true; |
| 97 | evaluateSubscriptions(); |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void onConnected() { |
| 102 | if (DEBUG) Slog.d(TAG, "onConnected"); |
| 103 | mConnected = true; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void onDestroy() { |
| 108 | super.onDestroy(); |
| 109 | if (DEBUG) Slog.d(TAG, "onDestroy"); |
| 110 | mConnected = false; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void onRequestConditions(int relevance) { |
| 115 | if (DEBUG) Slog.d(TAG, "onRequestConditions relevance=" + relevance); |
| 116 | // does not advertise conditions |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onSubscribe(Uri conditionId) { |
| 121 | if (DEBUG) Slog.d(TAG, "onSubscribe " + conditionId); |
| 122 | if (!ZenModeConfig.isValidEventConditionId(conditionId)) { |
| 123 | notifyCondition(conditionId, Condition.STATE_FALSE, "badCondition"); |
| 124 | return; |
| 125 | } |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 126 | if (mSubscriptions.add(conditionId)) { |
| 127 | evaluateSubscriptions(); |
| 128 | } |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void onUnsubscribe(Uri conditionId) { |
| 133 | if (DEBUG) Slog.d(TAG, "onUnsubscribe " + conditionId); |
| 134 | if (mSubscriptions.remove(conditionId)) { |
| 135 | evaluateSubscriptions(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void attachBase(Context base) { |
| 141 | attachBaseContext(base); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public IConditionProvider asInterface() { |
| 146 | return (IConditionProvider) onBind(null); |
| 147 | } |
| 148 | |
| 149 | private void evaluateSubscriptions() { |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 150 | if (DEBUG) Log.d(TAG, "evaluateSubscriptions"); |
| 151 | if (!mBootComplete) { |
| 152 | if (DEBUG) Log.d(TAG, "Skipping evaluate before boot complete"); |
| 153 | return; |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 154 | } |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 155 | final long now = System.currentTimeMillis(); |
| 156 | mTracker.setCallback(mSubscriptions.isEmpty() ? null : mTrackerCallback); |
| 157 | setRegistered(!mSubscriptions.isEmpty()); |
| 158 | long reevaluateAt = 0; |
| 159 | for (Uri conditionId : mSubscriptions) { |
| 160 | final EventInfo event = ZenModeConfig.tryParseEventConditionId(conditionId); |
| 161 | if (event == null) { |
| 162 | notifyCondition(conditionId, Condition.STATE_FALSE, "badConditionId"); |
| 163 | continue; |
| 164 | } |
| 165 | final CheckEventResult result = mTracker.checkEvent(event, now); |
| 166 | if (result.recheckAt != 0 && (reevaluateAt == 0 || result.recheckAt < reevaluateAt)) { |
| 167 | reevaluateAt = result.recheckAt; |
| 168 | } |
| 169 | if (!result.inEvent) { |
| 170 | notifyCondition(conditionId, Condition.STATE_FALSE, "!inEventNow"); |
| 171 | continue; |
| 172 | } |
| 173 | notifyCondition(conditionId, Condition.STATE_TRUE, "inEventNow"); |
| 174 | } |
| 175 | updateAlarm(now, reevaluateAt); |
| 176 | if (DEBUG) Log.d(TAG, "evaluateSubscriptions took " + (System.currentTimeMillis() - now)); |
| 177 | } |
| 178 | |
| 179 | private void updateAlarm(long now, long time) { |
| 180 | final AlarmManager alarms = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
| 181 | final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, |
| 182 | REQUEST_CODE_EVALUATE, |
| 183 | new Intent(ACTION_EVALUATE) |
| 184 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 185 | .putExtra(EXTRA_TIME, time), |
| 186 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 187 | alarms.cancel(pendingIntent); |
| 188 | if (time == 0 || time < now) { |
| 189 | if (DEBUG) Slog.d(TAG, "Not scheduling evaluate: " + (time == 0 ? "no time specified" |
| 190 | : "specified time in the past")); |
| 191 | return; |
| 192 | } |
| 193 | if (DEBUG) Slog.d(TAG, String.format("Scheduling evaluate for %s, in %s, now=%s", |
| 194 | ts(time), formatDuration(time - now), ts(now))); |
| 195 | alarms.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent); |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | private void notifyCondition(Uri conditionId, int state, String reason) { |
| 199 | if (DEBUG) Slog.d(TAG, "notifyCondition " + Condition.stateToString(state) |
| 200 | + " reason=" + reason); |
| 201 | notifyCondition(createCondition(conditionId, state)); |
| 202 | } |
| 203 | |
| 204 | private Condition createCondition(Uri id, int state) { |
| 205 | final String summary = NOT_SHOWN; |
| 206 | final String line1 = NOT_SHOWN; |
| 207 | final String line2 = NOT_SHOWN; |
| 208 | return new Condition(id, summary, line1, line2, 0, state, Condition.FLAG_RELEVANT_ALWAYS); |
| 209 | } |
| 210 | |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame^] | 211 | private void setRegistered(boolean registered) { |
| 212 | if (mRegistered == registered) return; |
| 213 | if (DEBUG) Slog.d(TAG, "setRegistered " + registered); |
| 214 | mRegistered = registered; |
| 215 | if (mRegistered) { |
| 216 | final IntentFilter filter = new IntentFilter(); |
| 217 | filter.addAction(Intent.ACTION_TIME_CHANGED); |
| 218 | filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
| 219 | filter.addAction(ACTION_EVALUATE); |
| 220 | registerReceiver(mReceiver, filter); |
| 221 | } else { |
| 222 | unregisterReceiver(mReceiver); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private final CalendarTracker.Callback mTrackerCallback = new CalendarTracker.Callback() { |
| 227 | @Override |
| 228 | public void onChanged() { |
| 229 | if (DEBUG) Log.d(TAG, "mTrackerCallback.onChanged"); |
| 230 | evaluateSubscriptions(); |
| 231 | } |
| 232 | }; |
| 233 | |
| 234 | private BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 235 | @Override |
| 236 | public void onReceive(Context context, Intent intent) { |
| 237 | if (DEBUG) Slog.d(TAG, "onReceive " + intent.getAction()); |
| 238 | evaluateSubscriptions(); |
| 239 | } |
| 240 | }; |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 241 | } |