blob: 40b0be2991f6b1933844e386a6eda5dfeecedf21 [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
John Spurlock21258a32015-05-27 18:22:55 -040062 public void evaluateConfig(ZenModeConfig config, boolean processSubscriptions) {
John Spurlockb2278d62015-04-07 12:47:12 -040063 if (config == null) return;
John Spurlock39581cc2015-04-10 11:59:01 -040064 if (config.manualRule != null && config.manualRule.condition != null
65 && !config.manualRule.isTrueOrUnknown()) {
John Spurlockb2278d62015-04-07 12:47:12 -040066 if (DEBUG) Log.d(TAG, "evaluateConfig: clearing manual rule");
67 config.manualRule = null;
68 }
69 final ArraySet<Uri> current = new ArraySet<>();
John Spurlock21258a32015-05-27 18:22:55 -040070 evaluateRule(config.manualRule, current, processSubscriptions);
John Spurlockb2278d62015-04-07 12:47:12 -040071 for (ZenRule automaticRule : config.automaticRules.values()) {
John Spurlock21258a32015-05-27 18:22:55 -040072 evaluateRule(automaticRule, current, processSubscriptions);
John Spurlock39581cc2015-04-10 11:59:01 -040073 updateSnoozing(automaticRule);
John Spurlockb2278d62015-04-07 12:47:12 -040074 }
75 final int N = mSubscriptions.size();
76 for (int i = N - 1; i >= 0; i--) {
77 final Uri id = mSubscriptions.keyAt(i);
78 final ComponentName component = mSubscriptions.valueAt(i);
John Spurlock21258a32015-05-27 18:22:55 -040079 if (processSubscriptions) {
John Spurlocka7d92b12015-05-13 14:48:02 -040080 if (!current.contains(id)) {
81 mConditionProviders.unsubscribeIfNecessary(component, id);
82 mSubscriptions.removeAt(i);
83 }
John Spurlockb2278d62015-04-07 12:47:12 -040084 }
85 }
John Spurlock39581cc2015-04-10 11:59:01 -040086 mFirstEvaluation = false;
John Spurlockb2278d62015-04-07 12:47:12 -040087 }
88
89 @Override
90 public void onBootComplete() {
91 // noop
92 }
93
94 @Override
95 public void onUserSwitched() {
96 // noop
97 }
98
99 @Override
John Spurlock39581cc2015-04-10 11:59:01 -0400100 public void onServiceAdded(ComponentName component) {
101 if (DEBUG) Log.d(TAG, "onServiceAdded " + component);
Julia Reynolds3ebe72b2016-09-15 11:07:50 -0400102 mHelper.setConfig(mHelper.getConfig(), "zmc.onServiceAdded");
John Spurlock39581cc2015-04-10 11:59:01 -0400103 }
104
105 @Override
John Spurlockb2278d62015-04-07 12:47:12 -0400106 public void onConditionChanged(Uri id, Condition condition) {
107 if (DEBUG) Log.d(TAG, "onConditionChanged " + id + " " + condition);
108 ZenModeConfig config = mHelper.getConfig();
109 if (config == null) return;
John Spurlockb2278d62015-04-07 12:47:12 -0400110 boolean updated = updateCondition(id, condition, config.manualRule);
111 for (ZenRule automaticRule : config.automaticRules.values()) {
112 updated |= updateCondition(id, condition, automaticRule);
113 updated |= updateSnoozing(automaticRule);
114 }
115 if (updated) {
Julia Reynolds3ebe72b2016-09-15 11:07:50 -0400116 mHelper.setConfig(config, "conditionChanged");
John Spurlockb2278d62015-04-07 12:47:12 -0400117 }
118 }
119
John Spurlocka7d92b12015-05-13 14:48:02 -0400120 private void evaluateRule(ZenRule rule, ArraySet<Uri> current, boolean processSubscriptions) {
John Spurlock39581cc2015-04-10 11:59:01 -0400121 if (rule == null || rule.conditionId == null) return;
122 final Uri id = rule.conditionId;
123 boolean isSystemCondition = false;
124 for (SystemConditionProviderService sp : mConditionProviders.getSystemProviders()) {
John Spurlockd60258f2015-04-30 09:30:52 -0400125 if (sp.isValidConditionId(id)) {
John Spurlock39581cc2015-04-10 11:59:01 -0400126 mConditionProviders.ensureRecordExists(sp.getComponent(), id, sp.asInterface());
127 rule.component = sp.getComponent();
128 isSystemCondition = true;
129 }
130 }
131 if (!isSystemCondition) {
132 final IConditionProvider cp = mConditionProviders.findConditionProvider(rule.component);
133 if (DEBUG) Log.d(TAG, "Ensure external rule exists: " + (cp != null) + " for " + id);
134 if (cp != null) {
135 mConditionProviders.ensureRecordExists(rule.component, id, cp);
136 }
137 }
138 if (rule.component == null) {
139 Log.w(TAG, "No component found for automatic rule: " + rule.conditionId);
140 rule.enabled = false;
141 return;
142 }
143 if (current != null) {
144 current.add(id);
145 }
John Spurlocka7d92b12015-05-13 14:48:02 -0400146 if (processSubscriptions) {
147 if (mConditionProviders.subscribeIfNecessary(rule.component, rule.conditionId)) {
148 mSubscriptions.put(rule.conditionId, rule.component);
149 } else {
Julia Reynolds6434eb22016-08-08 17:19:26 -0400150 rule.condition = null;
John Spurlocka7d92b12015-05-13 14:48:02 -0400151 if (DEBUG) Log.d(TAG, "zmc failed to subscribe");
152 }
John Spurlock39581cc2015-04-10 11:59:01 -0400153 }
John Spurlock21258a32015-05-27 18:22:55 -0400154 if (rule.condition == null) {
155 rule.condition = mConditionProviders.findCondition(rule.component, rule.conditionId);
156 if (rule.condition != null && DEBUG) Log.d(TAG, "Found existing condition for: "
157 + rule.conditionId);
158 }
John Spurlock39581cc2015-04-10 11:59:01 -0400159 }
160
161 private boolean isAutomaticActive(ComponentName component) {
162 if (component == null) return false;
163 final ZenModeConfig config = mHelper.getConfig();
164 if (config == null) return false;
165 for (ZenRule rule : config.automaticRules.values()) {
166 if (component.equals(rule.component) && rule.isAutomaticActive()) {
167 return true;
168 }
169 }
170 return false;
171 }
172
John Spurlockb2278d62015-04-07 12:47:12 -0400173 private boolean updateSnoozing(ZenRule rule) {
John Spurlock39581cc2015-04-10 11:59:01 -0400174 if (rule != null && rule.snoozing && (mFirstEvaluation || !rule.isTrueOrUnknown())) {
John Spurlockb2278d62015-04-07 12:47:12 -0400175 rule.snoozing = false;
176 if (DEBUG) Log.d(TAG, "Snoozing reset for " + rule.conditionId);
177 return true;
178 }
179 return false;
180 }
181
182 private boolean updateCondition(Uri id, Condition condition, ZenRule rule) {
183 if (id == null || rule == null || rule.conditionId == null) return false;
184 if (!rule.conditionId.equals(id)) return false;
185 if (Objects.equals(condition, rule.condition)) return false;
186 rule.condition = condition;
187 return true;
188 }
John Spurlock39581cc2015-04-10 11:59:01 -0400189
John Spurlockb2278d62015-04-07 12:47:12 -0400190}