blob: 8013f7a099b6170b626df4048c60c3bf8dff081f [file] [log] [blame]
John Spurlockb2278d62015-04-07 12:47:12 -04001/**
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.content.ComponentName;
20import android.net.Uri;
21import android.service.notification.Condition;
22import android.service.notification.IConditionListener;
John Spurlock39581cc2015-04-10 11:59:01 -040023import android.service.notification.IConditionProvider;
John Spurlockb2278d62015-04-07 12:47:12 -040024import android.service.notification.ZenModeConfig;
25import android.service.notification.ZenModeConfig.ZenRule;
26import android.util.ArrayMap;
27import android.util.ArraySet;
28import android.util.Log;
29
30import java.io.PrintWriter;
31import java.util.Objects;
32
33public 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 Spurlock39581cc2015-04-10 11:59:01 -040041 private boolean mFirstEvaluation = true;
John Spurlockb2278d62015-04-07 12:47:12 -040042
43 public ZenModeConditions(ZenModeHelper helper, ConditionProviders conditionProviders) {
44 mHelper = helper;
45 mConditionProviders = conditionProviders;
46 if (mConditionProviders.isSystemProviderEnabled(ZenModeConfig.COUNTDOWN_PATH)) {
John Spurlockd60258f2015-04-30 09:30:52 -040047 mConditionProviders.addSystemProvider(new CountdownConditionProvider());
John Spurlockb2278d62015-04-07 12:47:12 -040048 }
49 if (mConditionProviders.isSystemProviderEnabled(ZenModeConfig.SCHEDULE_PATH)) {
John Spurlockd60258f2015-04-30 09:30:52 -040050 mConditionProviders.addSystemProvider(new ScheduleConditionProvider());
51 }
52 if (mConditionProviders.isSystemProviderEnabled(ZenModeConfig.EVENT_PATH)) {
Julia Reynolds7034caf2016-03-09 10:41:00 -050053 mConditionProviders.addSystemProvider(new EventConditionProvider());
John Spurlockb2278d62015-04-07 12:47:12 -040054 }
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
Julia Reynolds4bcf9b82018-07-13 15:12:29 -040062 public void evaluateConfig(ZenModeConfig config, ComponentName trigger,
63 boolean processSubscriptions) {
John Spurlockb2278d62015-04-07 12:47:12 -040064 if (config == null) return;
John Spurlock39581cc2015-04-10 11:59:01 -040065 if (config.manualRule != null && config.manualRule.condition != null
66 && !config.manualRule.isTrueOrUnknown()) {
John Spurlockb2278d62015-04-07 12:47:12 -040067 if (DEBUG) Log.d(TAG, "evaluateConfig: clearing manual rule");
68 config.manualRule = null;
69 }
70 final ArraySet<Uri> current = new ArraySet<>();
Julia Reynolds4bcf9b82018-07-13 15:12:29 -040071 evaluateRule(config.manualRule, current, null, processSubscriptions);
John Spurlockb2278d62015-04-07 12:47:12 -040072 for (ZenRule automaticRule : config.automaticRules.values()) {
Julia Reynolds4bcf9b82018-07-13 15:12:29 -040073 evaluateRule(automaticRule, current, trigger, processSubscriptions);
John Spurlock39581cc2015-04-10 11:59:01 -040074 updateSnoozing(automaticRule);
John Spurlockb2278d62015-04-07 12:47:12 -040075 }
Shibin George23d28ce2016-05-07 16:16:50 +053076
77 synchronized (mSubscriptions) {
78 final int N = mSubscriptions.size();
79 for (int i = N - 1; i >= 0; i--) {
80 final Uri id = mSubscriptions.keyAt(i);
81 final ComponentName component = mSubscriptions.valueAt(i);
82 if (processSubscriptions) {
83 if (!current.contains(id)) {
84 mConditionProviders.unsubscribeIfNecessary(component, id);
85 mSubscriptions.removeAt(i);
86 }
John Spurlocka7d92b12015-05-13 14:48:02 -040087 }
John Spurlockb2278d62015-04-07 12:47:12 -040088 }
89 }
John Spurlock39581cc2015-04-10 11:59:01 -040090 mFirstEvaluation = false;
John Spurlockb2278d62015-04-07 12:47:12 -040091 }
92
93 @Override
94 public void onBootComplete() {
95 // noop
96 }
97
98 @Override
99 public void onUserSwitched() {
100 // noop
101 }
102
103 @Override
John Spurlock39581cc2015-04-10 11:59:01 -0400104 public void onServiceAdded(ComponentName component) {
105 if (DEBUG) Log.d(TAG, "onServiceAdded " + component);
Julia Reynolds4bcf9b82018-07-13 15:12:29 -0400106 mHelper.setConfig(mHelper.getConfig(), component, "zmc.onServiceAdded");
John Spurlock39581cc2015-04-10 11:59:01 -0400107 }
108
109 @Override
John Spurlockb2278d62015-04-07 12:47:12 -0400110 public void onConditionChanged(Uri id, Condition condition) {
111 if (DEBUG) Log.d(TAG, "onConditionChanged " + id + " " + condition);
112 ZenModeConfig config = mHelper.getConfig();
113 if (config == null) return;
Julia Reynolds4bcf9b82018-07-13 15:12:29 -0400114 ComponentName trigger = null;
John Spurlockb2278d62015-04-07 12:47:12 -0400115 boolean updated = updateCondition(id, condition, config.manualRule);
116 for (ZenRule automaticRule : config.automaticRules.values()) {
117 updated |= updateCondition(id, condition, automaticRule);
118 updated |= updateSnoozing(automaticRule);
Julia Reynolds4bcf9b82018-07-13 15:12:29 -0400119 if (updated) {
120 trigger = automaticRule.component;
121 }
John Spurlockb2278d62015-04-07 12:47:12 -0400122 }
123 if (updated) {
Julia Reynolds4bcf9b82018-07-13 15:12:29 -0400124 mHelper.setConfig(config, trigger, "conditionChanged");
John Spurlockb2278d62015-04-07 12:47:12 -0400125 }
126 }
127
Julia Reynolds4bcf9b82018-07-13 15:12:29 -0400128 private void evaluateRule(ZenRule rule, ArraySet<Uri> current, ComponentName trigger,
129 boolean processSubscriptions) {
John Spurlock39581cc2015-04-10 11:59:01 -0400130 if (rule == null || rule.conditionId == null) return;
131 final Uri id = rule.conditionId;
132 boolean isSystemCondition = false;
133 for (SystemConditionProviderService sp : mConditionProviders.getSystemProviders()) {
John Spurlockd60258f2015-04-30 09:30:52 -0400134 if (sp.isValidConditionId(id)) {
John Spurlock39581cc2015-04-10 11:59:01 -0400135 mConditionProviders.ensureRecordExists(sp.getComponent(), id, sp.asInterface());
136 rule.component = sp.getComponent();
137 isSystemCondition = true;
138 }
139 }
140 if (!isSystemCondition) {
141 final IConditionProvider cp = mConditionProviders.findConditionProvider(rule.component);
142 if (DEBUG) Log.d(TAG, "Ensure external rule exists: " + (cp != null) + " for " + id);
143 if (cp != null) {
144 mConditionProviders.ensureRecordExists(rule.component, id, cp);
145 }
146 }
147 if (rule.component == null) {
148 Log.w(TAG, "No component found for automatic rule: " + rule.conditionId);
149 rule.enabled = false;
150 return;
151 }
152 if (current != null) {
153 current.add(id);
154 }
Julia Reynolds4bcf9b82018-07-13 15:12:29 -0400155 if (processSubscriptions && trigger != null && trigger.equals(rule.component)) {
156 if (DEBUG) Log.d(TAG, "Subscribing to " + rule.component);
John Spurlocka7d92b12015-05-13 14:48:02 -0400157 if (mConditionProviders.subscribeIfNecessary(rule.component, rule.conditionId)) {
Shibin George23d28ce2016-05-07 16:16:50 +0530158 synchronized (mSubscriptions) {
159 mSubscriptions.put(rule.conditionId, rule.component);
160 }
John Spurlocka7d92b12015-05-13 14:48:02 -0400161 } else {
Julia Reynolds6434eb22016-08-08 17:19:26 -0400162 rule.condition = null;
John Spurlocka7d92b12015-05-13 14:48:02 -0400163 if (DEBUG) Log.d(TAG, "zmc failed to subscribe");
164 }
John Spurlock39581cc2015-04-10 11:59:01 -0400165 }
John Spurlock21258a32015-05-27 18:22:55 -0400166 if (rule.condition == null) {
167 rule.condition = mConditionProviders.findCondition(rule.component, rule.conditionId);
168 if (rule.condition != null && DEBUG) Log.d(TAG, "Found existing condition for: "
169 + rule.conditionId);
170 }
John Spurlock39581cc2015-04-10 11:59:01 -0400171 }
172
173 private boolean isAutomaticActive(ComponentName component) {
174 if (component == null) return false;
175 final ZenModeConfig config = mHelper.getConfig();
176 if (config == null) return false;
177 for (ZenRule rule : config.automaticRules.values()) {
178 if (component.equals(rule.component) && rule.isAutomaticActive()) {
179 return true;
180 }
181 }
182 return false;
183 }
184
John Spurlockb2278d62015-04-07 12:47:12 -0400185 private boolean updateSnoozing(ZenRule rule) {
John Spurlock39581cc2015-04-10 11:59:01 -0400186 if (rule != null && rule.snoozing && (mFirstEvaluation || !rule.isTrueOrUnknown())) {
John Spurlockb2278d62015-04-07 12:47:12 -0400187 rule.snoozing = false;
188 if (DEBUG) Log.d(TAG, "Snoozing reset for " + rule.conditionId);
189 return true;
190 }
191 return false;
192 }
193
194 private boolean updateCondition(Uri id, Condition condition, ZenRule rule) {
195 if (id == null || rule == null || rule.conditionId == null) return false;
196 if (!rule.conditionId.equals(id)) return false;
197 if (Objects.equals(condition, rule.condition)) return false;
198 rule.condition = condition;
199 return true;
200 }
John Spurlock39581cc2015-04-10 11:59:01 -0400201
John Spurlockb2278d62015-04-07 12:47:12 -0400202}