blob: 571f799154842a429029613d17f7c7a0db006e78 [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;
John Spurlock39581cc2015-04-10 11:59:01 -040022import android.service.notification.IConditionProvider;
John Spurlockb2278d62015-04-07 12:47:12 -040023import android.service.notification.ZenModeConfig;
24import android.service.notification.ZenModeConfig.ZenRule;
25import android.util.ArrayMap;
26import android.util.ArraySet;
27import android.util.Log;
28
Beverly2f43b642018-08-14 09:43:11 -040029import com.android.internal.annotations.VisibleForTesting;
30
John Spurlockb2278d62015-04-07 12:47:12 -040031import java.io.PrintWriter;
John Spurlockb2278d62015-04-07 12:47:12 -040032
Julia Reynolds68062072018-08-06 15:38:21 -040033/**
34 * Helper class for managing active rules from
35 * {@link android.service.notification.ConditionProviderService CPSes}.
36 */
John Spurlockb2278d62015-04-07 12:47:12 -040037public class ZenModeConditions implements ConditionProviders.Callback {
38 private static final String TAG = ZenModeHelper.TAG;
39 private static final boolean DEBUG = ZenModeHelper.DEBUG;
40
41 private final ZenModeHelper mHelper;
42 private final ConditionProviders mConditionProviders;
Beverly2f43b642018-08-14 09:43:11 -040043
44 @VisibleForTesting
45 protected final ArrayMap<Uri, ComponentName> mSubscriptions = new ArrayMap<>();
John Spurlockb2278d62015-04-07 12:47:12 -040046
John Spurlockb2278d62015-04-07 12:47:12 -040047 public ZenModeConditions(ZenModeHelper helper, ConditionProviders conditionProviders) {
48 mHelper = helper;
49 mConditionProviders = conditionProviders;
50 if (mConditionProviders.isSystemProviderEnabled(ZenModeConfig.COUNTDOWN_PATH)) {
John Spurlockd60258f2015-04-30 09:30:52 -040051 mConditionProviders.addSystemProvider(new CountdownConditionProvider());
John Spurlockb2278d62015-04-07 12:47:12 -040052 }
53 if (mConditionProviders.isSystemProviderEnabled(ZenModeConfig.SCHEDULE_PATH)) {
John Spurlockd60258f2015-04-30 09:30:52 -040054 mConditionProviders.addSystemProvider(new ScheduleConditionProvider());
55 }
56 if (mConditionProviders.isSystemProviderEnabled(ZenModeConfig.EVENT_PATH)) {
Julia Reynolds7034caf2016-03-09 10:41:00 -050057 mConditionProviders.addSystemProvider(new EventConditionProvider());
John Spurlockb2278d62015-04-07 12:47:12 -040058 }
59 mConditionProviders.setCallback(this);
60 }
61
62 public void dump(PrintWriter pw, String prefix) {
63 pw.print(prefix); pw.print("mSubscriptions="); pw.println(mSubscriptions);
64 }
65
Julia Reynolds8f056002018-07-13 15:12:29 -040066 public void evaluateConfig(ZenModeConfig config, ComponentName trigger,
67 boolean processSubscriptions) {
John Spurlockb2278d62015-04-07 12:47:12 -040068 if (config == null) return;
John Spurlock39581cc2015-04-10 11:59:01 -040069 if (config.manualRule != null && config.manualRule.condition != null
70 && !config.manualRule.isTrueOrUnknown()) {
John Spurlockb2278d62015-04-07 12:47:12 -040071 if (DEBUG) Log.d(TAG, "evaluateConfig: clearing manual rule");
72 config.manualRule = null;
73 }
74 final ArraySet<Uri> current = new ArraySet<>();
Julia Reynolds8f056002018-07-13 15:12:29 -040075 evaluateRule(config.manualRule, current, null, processSubscriptions);
John Spurlockb2278d62015-04-07 12:47:12 -040076 for (ZenRule automaticRule : config.automaticRules.values()) {
Julia Reynolds68062072018-08-06 15:38:21 -040077 if (automaticRule.component != null) {
78 evaluateRule(automaticRule, current, trigger, processSubscriptions);
79 updateSnoozing(automaticRule);
80 }
John Spurlockb2278d62015-04-07 12:47:12 -040081 }
Shibin George23d28ce2016-05-07 16:16:50 +053082
83 synchronized (mSubscriptions) {
84 final int N = mSubscriptions.size();
85 for (int i = N - 1; i >= 0; i--) {
86 final Uri id = mSubscriptions.keyAt(i);
87 final ComponentName component = mSubscriptions.valueAt(i);
88 if (processSubscriptions) {
89 if (!current.contains(id)) {
90 mConditionProviders.unsubscribeIfNecessary(component, id);
91 mSubscriptions.removeAt(i);
92 }
John Spurlocka7d92b12015-05-13 14:48:02 -040093 }
John Spurlockb2278d62015-04-07 12:47:12 -040094 }
95 }
John Spurlockb2278d62015-04-07 12:47:12 -040096 }
97
98 @Override
99 public void onBootComplete() {
100 // noop
101 }
102
103 @Override
104 public void onUserSwitched() {
105 // noop
106 }
107
108 @Override
John Spurlock39581cc2015-04-10 11:59:01 -0400109 public void onServiceAdded(ComponentName component) {
110 if (DEBUG) Log.d(TAG, "onServiceAdded " + component);
Julia Reynolds8f056002018-07-13 15:12:29 -0400111 mHelper.setConfig(mHelper.getConfig(), component, "zmc.onServiceAdded");
John Spurlock39581cc2015-04-10 11:59:01 -0400112 }
113
114 @Override
John Spurlockb2278d62015-04-07 12:47:12 -0400115 public void onConditionChanged(Uri id, Condition condition) {
116 if (DEBUG) Log.d(TAG, "onConditionChanged " + id + " " + condition);
117 ZenModeConfig config = mHelper.getConfig();
118 if (config == null) return;
Julia Reynolds68062072018-08-06 15:38:21 -0400119 mHelper.setAutomaticZenRuleState(id, condition);
John Spurlockb2278d62015-04-07 12:47:12 -0400120 }
121
Julia Reynolds68062072018-08-06 15:38:21 -0400122 // Only valid for CPS backed rules
Julia Reynolds8f056002018-07-13 15:12:29 -0400123 private void evaluateRule(ZenRule rule, ArraySet<Uri> current, ComponentName trigger,
124 boolean processSubscriptions) {
John Spurlock39581cc2015-04-10 11:59:01 -0400125 if (rule == null || rule.conditionId == null) return;
Julia Reynolds68062072018-08-06 15:38:21 -0400126 if (rule.configurationActivity != null) return;
John Spurlock39581cc2015-04-10 11:59:01 -0400127 final Uri id = rule.conditionId;
128 boolean isSystemCondition = false;
129 for (SystemConditionProviderService sp : mConditionProviders.getSystemProviders()) {
John Spurlockd60258f2015-04-30 09:30:52 -0400130 if (sp.isValidConditionId(id)) {
John Spurlock39581cc2015-04-10 11:59:01 -0400131 mConditionProviders.ensureRecordExists(sp.getComponent(), id, sp.asInterface());
132 rule.component = sp.getComponent();
133 isSystemCondition = true;
134 }
135 }
Julia Reynolds68062072018-08-06 15:38:21 -0400136 // ensure that we have a record of the rule if it's backed by an currently alive CPS
John Spurlock39581cc2015-04-10 11:59:01 -0400137 if (!isSystemCondition) {
138 final IConditionProvider cp = mConditionProviders.findConditionProvider(rule.component);
139 if (DEBUG) Log.d(TAG, "Ensure external rule exists: " + (cp != null) + " for " + id);
140 if (cp != null) {
141 mConditionProviders.ensureRecordExists(rule.component, id, cp);
142 }
143 }
Julia Reynolds68062072018-08-06 15:38:21 -0400144 // empty rule? disable and bail early
145 if (rule.component == null && rule.enabler == null) {
John Spurlock39581cc2015-04-10 11:59:01 -0400146 Log.w(TAG, "No component found for automatic rule: " + rule.conditionId);
147 rule.enabled = false;
148 return;
149 }
150 if (current != null) {
151 current.add(id);
152 }
Julia Reynolds68062072018-08-06 15:38:21 -0400153
154 // If the rule is bound by a CPS and the CPS is alive, tell them about the rule
Beverly2f43b642018-08-14 09:43:11 -0400155 if (processSubscriptions && ((trigger != null && trigger.equals(rule.component))
156 || isSystemCondition)) {
Julia Reynolds8f056002018-07-13 15:12:29 -0400157 if (DEBUG) Log.d(TAG, "Subscribing to " + rule.component);
John Spurlocka7d92b12015-05-13 14:48:02 -0400158 if (mConditionProviders.subscribeIfNecessary(rule.component, rule.conditionId)) {
Shibin George23d28ce2016-05-07 16:16:50 +0530159 synchronized (mSubscriptions) {
160 mSubscriptions.put(rule.conditionId, rule.component);
161 }
John Spurlocka7d92b12015-05-13 14:48:02 -0400162 } else {
Julia Reynolds6434eb22016-08-08 17:19:26 -0400163 rule.condition = null;
John Spurlocka7d92b12015-05-13 14:48:02 -0400164 if (DEBUG) Log.d(TAG, "zmc failed to subscribe");
165 }
John Spurlock39581cc2015-04-10 11:59:01 -0400166 }
Julia Reynolds68062072018-08-06 15:38:21 -0400167 // backfill the rule state from CPS backed components if it's missing
168 if (rule.component != null && rule.condition == null) {
John Spurlock21258a32015-05-27 18:22:55 -0400169 rule.condition = mConditionProviders.findCondition(rule.component, rule.conditionId);
170 if (rule.condition != null && DEBUG) Log.d(TAG, "Found existing condition for: "
171 + rule.conditionId);
172 }
John Spurlock39581cc2015-04-10 11:59:01 -0400173 }
174
John Spurlockb2278d62015-04-07 12:47:12 -0400175 private boolean updateSnoozing(ZenRule rule) {
Julia Reynolds68062072018-08-06 15:38:21 -0400176 if (rule != null && rule.snoozing && !rule.isTrueOrUnknown()) {
John Spurlockb2278d62015-04-07 12:47:12 -0400177 rule.snoozing = false;
178 if (DEBUG) Log.d(TAG, "Snoozing reset for " + rule.conditionId);
179 return true;
180 }
181 return false;
182 }
John Spurlockb2278d62015-04-07 12:47:12 -0400183}