ScheduleConditionProvider.java revision 83eec7037bfe8a84088cca6dc1446dcf1edb4695
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 17package com.android.server.notification; 18 19import android.app.ActivityManager; 20import android.app.AlarmManager; 21import android.app.PendingIntent; 22import android.content.BroadcastReceiver; 23import android.content.ComponentName; 24import android.content.Context; 25import android.content.Intent; 26import android.content.IntentFilter; 27import android.net.Uri; 28import android.service.notification.Condition; 29import android.service.notification.IConditionProvider; 30import android.service.notification.ZenModeConfig; 31import android.service.notification.ZenModeConfig.ScheduleInfo; 32import android.util.ArrayMap; 33import android.util.Log; 34import android.util.Slog; 35 36import com.android.server.notification.NotificationManagerService.DumpFilter; 37 38import java.io.PrintWriter; 39import java.util.Calendar; 40import java.util.TimeZone; 41 42/** 43 * Built-in zen condition provider for daily scheduled time-based conditions. 44 */ 45public class ScheduleConditionProvider extends SystemConditionProviderService { 46 private static final String TAG = "ConditionProviders.SCP"; 47 private static final boolean DEBUG = true || Log.isLoggable("ConditionProviders", Log.DEBUG); 48 49 public static final ComponentName COMPONENT = 50 new ComponentName("android", ScheduleConditionProvider.class.getName()); 51 private static final String NOT_SHOWN = "..."; 52 private static final String SIMPLE_NAME = ScheduleConditionProvider.class.getSimpleName(); 53 private static final String ACTION_EVALUATE = SIMPLE_NAME + ".EVALUATE"; 54 private static final int REQUEST_CODE_EVALUATE = 1; 55 private static final String EXTRA_TIME = "time"; 56 57 private final Context mContext = this; 58 private final ArrayMap<Uri, ScheduleCalendar> mSubscriptions = new ArrayMap<>(); 59 60 private AlarmManager mAlarmManager; 61 private boolean mConnected; 62 private boolean mRegistered; 63 private long mNextAlarmTime; 64 65 public ScheduleConditionProvider() { 66 if (DEBUG) Slog.d(TAG, "new " + SIMPLE_NAME + "()"); 67 } 68 69 @Override 70 public ComponentName getComponent() { 71 return COMPONENT; 72 } 73 74 @Override 75 public boolean isValidConditionId(Uri id) { 76 return ZenModeConfig.isValidScheduleConditionId(id); 77 } 78 79 @Override 80 public void dump(PrintWriter pw, DumpFilter filter) { 81 pw.print(" "); pw.print(SIMPLE_NAME); pw.println(":"); 82 pw.print(" mConnected="); pw.println(mConnected); 83 pw.print(" mRegistered="); pw.println(mRegistered); 84 pw.println(" mSubscriptions="); 85 final long now = System.currentTimeMillis(); 86 for (Uri conditionId : mSubscriptions.keySet()) { 87 pw.print(" "); 88 pw.print(meetsSchedule(mSubscriptions.get(conditionId), now) ? "* " : " "); 89 pw.println(conditionId); 90 pw.print(" "); 91 pw.println(mSubscriptions.get(conditionId).toString()); 92 } 93 dumpUpcomingTime(pw, "mNextAlarmTime", mNextAlarmTime, now); 94 } 95 96 @Override 97 public void onConnected() { 98 if (DEBUG) Slog.d(TAG, "onConnected"); 99 mConnected = true; 100 } 101 102 @Override 103 public void onBootComplete() { 104 // noop 105 } 106 107 @Override 108 public void onDestroy() { 109 super.onDestroy(); 110 if (DEBUG) Slog.d(TAG, "onDestroy"); 111 mConnected = false; 112 } 113 114 @Override 115 public void onSubscribe(Uri conditionId) { 116 if (DEBUG) Slog.d(TAG, "onSubscribe " + conditionId); 117 if (!ZenModeConfig.isValidScheduleConditionId(conditionId)) { 118 notifyCondition(conditionId, Condition.STATE_FALSE, "badCondition"); 119 return; 120 } 121 mSubscriptions.put(conditionId, toScheduleCalendar(conditionId)); 122 evaluateSubscriptions(); 123 } 124 125 @Override 126 public void onUnsubscribe(Uri conditionId) { 127 if (DEBUG) Slog.d(TAG, "onUnsubscribe " + conditionId); 128 mSubscriptions.remove(conditionId); 129 evaluateSubscriptions(); 130 } 131 132 @Override 133 public void attachBase(Context base) { 134 attachBaseContext(base); 135 } 136 137 @Override 138 public IConditionProvider asInterface() { 139 return (IConditionProvider) onBind(null); 140 } 141 142 private void evaluateSubscriptions() { 143 if (mAlarmManager == null) { 144 mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); 145 } 146 setRegistered(!mSubscriptions.isEmpty()); 147 final long now = System.currentTimeMillis(); 148 mNextAlarmTime = 0; 149 long nextUserAlarmTime = getNextAlarm(); 150 for (Uri conditionId : mSubscriptions.keySet()) { 151 final ScheduleCalendar cal = mSubscriptions.get(conditionId); 152 if (cal != null && cal.isInSchedule(now)) { 153 notifyCondition(conditionId, Condition.STATE_TRUE, "meetsSchedule"); 154 cal.maybeSetNextAlarm(now, nextUserAlarmTime); 155 } else { 156 notifyCondition(conditionId, Condition.STATE_FALSE, "!meetsSchedule"); 157 } 158 if (cal != null) { 159 final long nextChangeTime = cal.getNextChangeTime(now); 160 if (nextChangeTime > 0 && nextChangeTime > now) { 161 if (mNextAlarmTime == 0 || nextChangeTime < mNextAlarmTime) { 162 mNextAlarmTime = nextChangeTime; 163 } 164 } 165 } 166 } 167 updateAlarm(now, mNextAlarmTime); 168 } 169 170 private void updateAlarm(long now, long time) { 171 final AlarmManager alarms = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); 172 final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 173 REQUEST_CODE_EVALUATE, 174 new Intent(ACTION_EVALUATE) 175 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) 176 .putExtra(EXTRA_TIME, time), 177 PendingIntent.FLAG_UPDATE_CURRENT); 178 alarms.cancel(pendingIntent); 179 if (time > now) { 180 if (DEBUG) Slog.d(TAG, String.format("Scheduling evaluate for %s, in %s, now=%s", 181 ts(time), formatDuration(time - now), ts(now))); 182 alarms.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent); 183 } else { 184 if (DEBUG) Slog.d(TAG, "Not scheduling evaluate"); 185 } 186 } 187 188 public long getNextAlarm() { 189 final AlarmManager.AlarmClockInfo info = mAlarmManager.getNextAlarmClock( 190 ActivityManager.getCurrentUser()); 191 return info != null ? info.getTriggerTime() : 0; 192 } 193 194 private static boolean meetsSchedule(ScheduleCalendar cal, long time) { 195 return cal != null && cal.isInSchedule(time); 196 } 197 198 private static ScheduleCalendar toScheduleCalendar(Uri conditionId) { 199 final ScheduleInfo schedule = ZenModeConfig.tryParseScheduleConditionId(conditionId); 200 if (schedule == null || schedule.days == null || schedule.days.length == 0) return null; 201 final ScheduleCalendar sc = new ScheduleCalendar(); 202 sc.setSchedule(schedule); 203 sc.setTimeZone(TimeZone.getDefault()); 204 return sc; 205 } 206 207 private void setRegistered(boolean registered) { 208 if (mRegistered == registered) return; 209 if (DEBUG) Slog.d(TAG, "setRegistered " + registered); 210 mRegistered = registered; 211 if (mRegistered) { 212 final IntentFilter filter = new IntentFilter(); 213 filter.addAction(Intent.ACTION_TIME_CHANGED); 214 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 215 filter.addAction(ACTION_EVALUATE); 216 filter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED); 217 registerReceiver(mReceiver, filter); 218 } else { 219 unregisterReceiver(mReceiver); 220 } 221 } 222 223 private void notifyCondition(Uri conditionId, int state, String reason) { 224 if (DEBUG) Slog.d(TAG, "notifyCondition " + conditionId 225 + " " + Condition.stateToString(state) 226 + " reason=" + reason); 227 notifyCondition(createCondition(conditionId, state)); 228 } 229 230 private Condition createCondition(Uri id, int state) { 231 final String summary = NOT_SHOWN; 232 final String line1 = NOT_SHOWN; 233 final String line2 = NOT_SHOWN; 234 return new Condition(id, summary, line1, line2, 0, state, Condition.FLAG_RELEVANT_ALWAYS); 235 } 236 237 private BroadcastReceiver mReceiver = new BroadcastReceiver() { 238 @Override 239 public void onReceive(Context context, Intent intent) { 240 if (DEBUG) Slog.d(TAG, "onReceive " + intent.getAction()); 241 if (Intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { 242 for (Uri conditionId : mSubscriptions.keySet()) { 243 final ScheduleCalendar cal = mSubscriptions.get(conditionId); 244 if (cal != null) { 245 cal.setTimeZone(Calendar.getInstance().getTimeZone()); 246 } 247 } 248 } 249 evaluateSubscriptions(); 250 } 251 }; 252 253} 254