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 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.net.Uri; |
| 21 | import android.service.notification.Condition; |
| 22 | import android.service.notification.IConditionListener; |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 23 | import android.service.notification.IConditionProvider; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 24 | import android.service.notification.ZenModeConfig; |
| 25 | import android.service.notification.ZenModeConfig.ZenRule; |
| 26 | import android.util.ArrayMap; |
| 27 | import android.util.ArraySet; |
| 28 | import android.util.Log; |
| 29 | |
| 30 | import java.io.PrintWriter; |
| 31 | import java.util.Objects; |
| 32 | |
| 33 | public class ZenModeConditions implements ConditionProviders.Callback { |
| 34 | private static final String TAG = ZenModeHelper.TAG; |
| 35 | private static final boolean DEBUG = ZenModeHelper.DEBUG; |
| 36 | |
| 37 | private final ZenModeHelper mHelper; |
| 38 | private final ConditionProviders mConditionProviders; |
| 39 | private final ArrayMap<Uri, ComponentName> mSubscriptions = new ArrayMap<>(); |
| 40 | |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 41 | private boolean mFirstEvaluation = true; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 42 | |
| 43 | public ZenModeConditions(ZenModeHelper helper, ConditionProviders conditionProviders) { |
| 44 | mHelper = helper; |
| 45 | mConditionProviders = conditionProviders; |
| 46 | if (mConditionProviders.isSystemProviderEnabled(ZenModeConfig.COUNTDOWN_PATH)) { |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 47 | mConditionProviders.addSystemProvider(new CountdownConditionProvider()); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 48 | } |
| 49 | if (mConditionProviders.isSystemProviderEnabled(ZenModeConfig.SCHEDULE_PATH)) { |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 50 | mConditionProviders.addSystemProvider(new ScheduleConditionProvider()); |
| 51 | } |
| 52 | if (mConditionProviders.isSystemProviderEnabled(ZenModeConfig.EVENT_PATH)) { |
Julia Reynolds | 7034caf | 2016-03-09 10:41:00 -0500 | [diff] [blame] | 53 | mConditionProviders.addSystemProvider(new EventConditionProvider()); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 54 | } |
| 55 | mConditionProviders.setCallback(this); |
| 56 | } |
| 57 | |
| 58 | public void dump(PrintWriter pw, String prefix) { |
| 59 | pw.print(prefix); pw.print("mSubscriptions="); pw.println(mSubscriptions); |
| 60 | } |
| 61 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 62 | public void evaluateConfig(ZenModeConfig config, boolean processSubscriptions) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 63 | if (config == null) return; |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 64 | if (config.manualRule != null && config.manualRule.condition != null |
| 65 | && !config.manualRule.isTrueOrUnknown()) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 66 | if (DEBUG) Log.d(TAG, "evaluateConfig: clearing manual rule"); |
| 67 | config.manualRule = null; |
| 68 | } |
| 69 | final ArraySet<Uri> current = new ArraySet<>(); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 70 | evaluateRule(config.manualRule, current, processSubscriptions); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 71 | for (ZenRule automaticRule : config.automaticRules.values()) { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 72 | evaluateRule(automaticRule, current, processSubscriptions); |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 73 | updateSnoozing(automaticRule); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 74 | } |
Shibin George | 23d28ce | 2016-05-07 16:16:50 +0530 | [diff] [blame] | 75 | |
| 76 | synchronized (mSubscriptions) { |
| 77 | final int N = mSubscriptions.size(); |
| 78 | for (int i = N - 1; i >= 0; i--) { |
| 79 | final Uri id = mSubscriptions.keyAt(i); |
| 80 | final ComponentName component = mSubscriptions.valueAt(i); |
| 81 | if (processSubscriptions) { |
| 82 | if (!current.contains(id)) { |
| 83 | mConditionProviders.unsubscribeIfNecessary(component, id); |
| 84 | mSubscriptions.removeAt(i); |
| 85 | } |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 86 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 87 | } |
| 88 | } |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 89 | mFirstEvaluation = false; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void onBootComplete() { |
| 94 | // noop |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void onUserSwitched() { |
| 99 | // noop |
| 100 | } |
| 101 | |
| 102 | @Override |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 103 | public void onServiceAdded(ComponentName component) { |
| 104 | if (DEBUG) Log.d(TAG, "onServiceAdded " + component); |
Julia Reynolds | 89aeab0 | 2016-09-15 11:07:50 -0400 | [diff] [blame] | 105 | mHelper.setConfig(mHelper.getConfig(), "zmc.onServiceAdded"); |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 109 | public void onConditionChanged(Uri id, Condition condition) { |
| 110 | if (DEBUG) Log.d(TAG, "onConditionChanged " + id + " " + condition); |
| 111 | ZenModeConfig config = mHelper.getConfig(); |
| 112 | if (config == null) return; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 113 | boolean updated = updateCondition(id, condition, config.manualRule); |
| 114 | for (ZenRule automaticRule : config.automaticRules.values()) { |
| 115 | updated |= updateCondition(id, condition, automaticRule); |
| 116 | updated |= updateSnoozing(automaticRule); |
| 117 | } |
| 118 | if (updated) { |
Julia Reynolds | 89aeab0 | 2016-09-15 11:07:50 -0400 | [diff] [blame] | 119 | mHelper.setConfig(config, "conditionChanged"); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 123 | private void evaluateRule(ZenRule rule, ArraySet<Uri> current, boolean processSubscriptions) { |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 124 | if (rule == null || rule.conditionId == null) return; |
| 125 | final Uri id = rule.conditionId; |
| 126 | boolean isSystemCondition = false; |
| 127 | for (SystemConditionProviderService sp : mConditionProviders.getSystemProviders()) { |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 128 | if (sp.isValidConditionId(id)) { |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 129 | mConditionProviders.ensureRecordExists(sp.getComponent(), id, sp.asInterface()); |
| 130 | rule.component = sp.getComponent(); |
| 131 | isSystemCondition = true; |
| 132 | } |
| 133 | } |
| 134 | if (!isSystemCondition) { |
| 135 | final IConditionProvider cp = mConditionProviders.findConditionProvider(rule.component); |
| 136 | if (DEBUG) Log.d(TAG, "Ensure external rule exists: " + (cp != null) + " for " + id); |
| 137 | if (cp != null) { |
| 138 | mConditionProviders.ensureRecordExists(rule.component, id, cp); |
| 139 | } |
| 140 | } |
| 141 | if (rule.component == null) { |
| 142 | Log.w(TAG, "No component found for automatic rule: " + rule.conditionId); |
| 143 | rule.enabled = false; |
| 144 | return; |
| 145 | } |
| 146 | if (current != null) { |
| 147 | current.add(id); |
| 148 | } |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 149 | if (processSubscriptions) { |
| 150 | if (mConditionProviders.subscribeIfNecessary(rule.component, rule.conditionId)) { |
Shibin George | 23d28ce | 2016-05-07 16:16:50 +0530 | [diff] [blame] | 151 | synchronized (mSubscriptions) { |
| 152 | mSubscriptions.put(rule.conditionId, rule.component); |
| 153 | } |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 154 | } else { |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 155 | rule.condition = null; |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 156 | if (DEBUG) Log.d(TAG, "zmc failed to subscribe"); |
| 157 | } |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 158 | } |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 159 | if (rule.condition == null) { |
| 160 | rule.condition = mConditionProviders.findCondition(rule.component, rule.conditionId); |
| 161 | if (rule.condition != null && DEBUG) Log.d(TAG, "Found existing condition for: " |
| 162 | + rule.conditionId); |
| 163 | } |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | private boolean isAutomaticActive(ComponentName component) { |
| 167 | if (component == null) return false; |
| 168 | final ZenModeConfig config = mHelper.getConfig(); |
| 169 | if (config == null) return false; |
| 170 | for (ZenRule rule : config.automaticRules.values()) { |
| 171 | if (component.equals(rule.component) && rule.isAutomaticActive()) { |
| 172 | return true; |
| 173 | } |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 178 | private boolean updateSnoozing(ZenRule rule) { |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 179 | if (rule != null && rule.snoozing && (mFirstEvaluation || !rule.isTrueOrUnknown())) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 180 | rule.snoozing = false; |
| 181 | if (DEBUG) Log.d(TAG, "Snoozing reset for " + rule.conditionId); |
| 182 | return true; |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | private boolean updateCondition(Uri id, Condition condition, ZenRule rule) { |
| 188 | if (id == null || rule == null || rule.conditionId == null) return false; |
| 189 | if (!rule.conditionId.equals(id)) return false; |
| 190 | if (Objects.equals(condition, rule.condition)) return false; |
| 191 | rule.condition = condition; |
| 192 | return true; |
| 193 | } |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 194 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 195 | } |