blob: c0fbfbb20b9551ec94d3269f2facdf8b658756ef [file] [log] [blame]
John Spurlock7340fc82014-04-24 18:50:12 -04001/**
2 * Copyright (c) 2014, 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
Julia Reynolds6434eb22016-08-08 17:19:26 -040019import android.app.INotificationManager;
20import android.app.NotificationManager;
John Spurlock3b98b3f2014-05-01 09:08:48 -040021import android.content.ComponentName;
John Spurlock7340fc82014-04-24 18:50:12 -040022import android.content.Context;
Julia Reynoldsb852e562017-06-06 16:14:18 -040023import android.content.pm.IPackageManager;
John Spurlocke77bb362014-04-26 10:24:59 -040024import android.net.Uri;
John Spurlock7340fc82014-04-24 18:50:12 -040025import android.os.IBinder;
26import android.os.IInterface;
John Spurlocke77bb362014-04-26 10:24:59 -040027import android.os.RemoteException;
John Spurlock856edeb2014-06-01 20:36:47 -040028import android.os.UserHandle;
John Spurlock7340fc82014-04-24 18:50:12 -040029import android.provider.Settings;
John Spurlocke77bb362014-04-26 10:24:59 -040030import android.service.notification.Condition;
John Spurlock7340fc82014-04-24 18:50:12 -040031import android.service.notification.ConditionProviderService;
John Spurlocke77bb362014-04-26 10:24:59 -040032import android.service.notification.IConditionProvider;
33import android.util.ArrayMap;
John Spurlock3b98b3f2014-05-01 09:08:48 -040034import android.util.ArraySet;
John Spurlock7340fc82014-04-24 18:50:12 -040035import android.util.Slog;
36
37import com.android.internal.R;
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -040038import com.android.internal.annotations.VisibleForTesting;
John Spurlock25e2d242014-06-27 13:58:23 -040039import com.android.server.notification.NotificationManagerService.DumpFilter;
John Spurlock7340fc82014-04-24 18:50:12 -040040
John Spurlocke77bb362014-04-26 10:24:59 -040041import java.io.PrintWriter;
John Spurlock3b98b3f2014-05-01 09:08:48 -040042import java.util.ArrayList;
John Spurlocke77bb362014-04-26 10:24:59 -040043import java.util.Arrays;
44
John Spurlock7340fc82014-04-24 18:50:12 -040045public class ConditionProviders extends ManagedServices {
Julia Reynoldsb852e562017-06-06 16:14:18 -040046
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -040047 @VisibleForTesting
48 static final String TAG_ENABLED_DND_APPS = "dnd_apps";
Julia Reynoldsb852e562017-06-06 16:14:18 -040049
John Spurlockb2278d62015-04-07 12:47:12 -040050 private final ArrayList<ConditionRecord> mRecords = new ArrayList<>();
John Spurlockb2278d62015-04-07 12:47:12 -040051 private final ArraySet<String> mSystemConditionProviderNames;
52 private final ArraySet<SystemConditionProviderService> mSystemConditionProviders
53 = new ArraySet<>();
John Spurlock7340fc82014-04-24 18:50:12 -040054
John Spurlockb2278d62015-04-07 12:47:12 -040055 private Callback mCallback;
John Spurlock856edeb2014-06-01 20:36:47 -040056
Julia Reynoldsb852e562017-06-06 16:14:18 -040057 public ConditionProviders(Context context, UserProfiles userProfiles, IPackageManager pm) {
58 super(context, new Object(), userProfiles, pm);
John Spurlockb2278d62015-04-07 12:47:12 -040059 mSystemConditionProviderNames = safeSet(PropConfig.getStringArray(mContext,
John Spurlock530052a2014-11-30 16:26:19 -050060 "system.condition.providers",
61 R.array.config_system_condition_providers));
Julia Reynoldsb852e562017-06-06 16:14:18 -040062 mApprovalLevel = APPROVAL_BY_PACKAGE;
John Spurlock530052a2014-11-30 16:26:19 -050063 }
64
John Spurlockb2278d62015-04-07 12:47:12 -040065 public void setCallback(Callback callback) {
66 mCallback = callback;
67 }
68
69 public boolean isSystemProviderEnabled(String path) {
70 return mSystemConditionProviderNames.contains(path);
71 }
72
73 public void addSystemProvider(SystemConditionProviderService service) {
74 mSystemConditionProviders.add(service);
75 service.attachBase(mContext);
Xiaohui Chenddbe4ca2015-08-13 16:20:56 -070076 registerService(service.asInterface(), service.getComponent(), UserHandle.USER_SYSTEM);
John Spurlockb2278d62015-04-07 12:47:12 -040077 }
78
79 public Iterable<SystemConditionProviderService> getSystemProviders() {
80 return mSystemConditionProviders;
John Spurlock7340fc82014-04-24 18:50:12 -040081 }
82
83 @Override
84 protected Config getConfig() {
John Spurlockb2278d62015-04-07 12:47:12 -040085 final Config c = new Config();
John Spurlock7340fc82014-04-24 18:50:12 -040086 c.caption = "condition provider";
87 c.serviceInterface = ConditionProviderService.SERVICE_INTERFACE;
Julia Reynoldsc279b992015-10-30 08:23:51 -040088 c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES;
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -040089 c.xmlTag = TAG_ENABLED_DND_APPS;
Julia Reynolds6e839b02016-04-13 10:01:17 -040090 c.secondarySettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS;
John Spurlock7340fc82014-04-24 18:50:12 -040091 c.bindPermission = android.Manifest.permission.BIND_CONDITION_PROVIDER_SERVICE;
92 c.settingsAction = Settings.ACTION_CONDITION_PROVIDER_SETTINGS;
93 c.clientLabel = R.string.condition_provider_service_binding_label;
94 return c;
95 }
96
97 @Override
John Spurlock25e2d242014-06-27 13:58:23 -040098 public void dump(PrintWriter pw, DumpFilter filter) {
99 super.dump(pw, filter);
John Spurlocke77bb362014-04-26 10:24:59 -0400100 synchronized(mMutex) {
John Spurlock3b98b3f2014-05-01 09:08:48 -0400101 pw.print(" mRecords("); pw.print(mRecords.size()); pw.println("):");
102 for (int i = 0; i < mRecords.size(); i++) {
John Spurlock856edeb2014-06-01 20:36:47 -0400103 final ConditionRecord r = mRecords.get(i);
John Spurlock25e2d242014-06-27 13:58:23 -0400104 if (filter != null && !filter.matches(r.component)) continue;
John Spurlock856edeb2014-06-01 20:36:47 -0400105 pw.print(" "); pw.println(r);
106 final String countdownDesc = CountdownConditionProvider.tryParseDescription(r.id);
107 if (countdownDesc != null) {
108 pw.print(" ("); pw.print(countdownDesc); pw.println(")");
109 }
John Spurlocke77bb362014-04-26 10:24:59 -0400110 }
111 }
John Spurlockb2278d62015-04-07 12:47:12 -0400112 pw.print(" mSystemConditionProviders: "); pw.println(mSystemConditionProviderNames);
113 for (int i = 0; i < mSystemConditionProviders.size(); i++) {
114 mSystemConditionProviders.valueAt(i).dump(pw, filter);
John Spurlock530052a2014-11-30 16:26:19 -0500115 }
John Spurlocke77bb362014-04-26 10:24:59 -0400116 }
117
118 @Override
John Spurlock7340fc82014-04-24 18:50:12 -0400119 protected IInterface asInterface(IBinder binder) {
120 return IConditionProvider.Stub.asInterface(binder);
121 }
122
123 @Override
Chris Wren51017d02015-12-15 15:34:46 -0500124 protected boolean checkType(IInterface service) {
125 return service instanceof IConditionProvider;
126 }
127
128 @Override
John Spurlock856edeb2014-06-01 20:36:47 -0400129 public void onBootPhaseAppsCanStart() {
130 super.onBootPhaseAppsCanStart();
John Spurlock2f096ed2015-05-04 11:58:26 -0400131 for (int i = 0; i < mSystemConditionProviders.size(); i++) {
132 mSystemConditionProviders.valueAt(i).onBootComplete();
133 }
John Spurlockb2278d62015-04-07 12:47:12 -0400134 if (mCallback != null) {
135 mCallback.onBootComplete();
John Spurlock530052a2014-11-30 16:26:19 -0500136 }
John Spurlock37bc92c2014-11-03 11:01:51 -0500137 }
138
139 @Override
John Spurlock1b8b22b2015-05-20 09:47:13 -0400140 public void onUserSwitched(int user) {
141 super.onUserSwitched(user);
John Spurlockb2278d62015-04-07 12:47:12 -0400142 if (mCallback != null) {
143 mCallback.onUserSwitched();
John Spurlock530052a2014-11-30 16:26:19 -0500144 }
John Spurlock856edeb2014-06-01 20:36:47 -0400145 }
146
147 @Override
John Spurlock3b98b3f2014-05-01 09:08:48 -0400148 protected void onServiceAdded(ManagedServiceInfo info) {
John Spurlock3b98b3f2014-05-01 09:08:48 -0400149 final IConditionProvider provider = provider(info);
John Spurlocke77bb362014-04-26 10:24:59 -0400150 try {
151 provider.onConnected();
152 } catch (RemoteException e) {
153 // we tried
154 }
John Spurlock39581cc2015-04-10 11:59:01 -0400155 if (mCallback != null) {
156 mCallback.onServiceAdded(info.component);
157 }
John Spurlocke77bb362014-04-26 10:24:59 -0400158 }
159
160 @Override
161 protected void onServiceRemovedLocked(ManagedServiceInfo removed) {
162 if (removed == null) return;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400163 for (int i = mRecords.size() - 1; i >= 0; i--) {
164 final ConditionRecord r = mRecords.get(i);
165 if (!r.component.equals(removed.component)) continue;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400166 mRecords.remove(i);
John Spurlocke77bb362014-04-26 10:24:59 -0400167 }
168 }
169
Julia Reynolds6434eb22016-08-08 17:19:26 -0400170 @Override
Julia Reynoldsb852e562017-06-06 16:14:18 -0400171 public void onPackagesChanged(boolean removingPackage, String[] pkgList, int[] uid) {
Julia Reynolds6434eb22016-08-08 17:19:26 -0400172 if (removingPackage) {
173 INotificationManager inm = NotificationManager.getService();
174
175 if (pkgList != null && (pkgList.length > 0)) {
176 for (String pkgName : pkgList) {
177 try {
178 inm.removeAutomaticZenRules(pkgName);
179 inm.setNotificationPolicyAccessGranted(pkgName, false);
180 } catch (Exception e) {
181 Slog.e(TAG, "Failed to clean up rules for " + pkgName, e);
182 }
183 }
184 }
185 }
Julia Reynoldsb852e562017-06-06 16:14:18 -0400186 super.onPackagesChanged(removingPackage, pkgList, uid);
Julia Reynolds6434eb22016-08-08 17:19:26 -0400187 }
188
Julia Reynoldse5ce071bf2017-09-15 14:26:32 -0400189 @Override
190 protected boolean isValidEntry(String packageOrComponent, int userId) {
191 return true;
192 }
193
John Spurlocke77bb362014-04-26 10:24:59 -0400194 public ManagedServiceInfo checkServiceToken(IConditionProvider provider) {
195 synchronized(mMutex) {
196 return checkServiceTokenLocked(provider);
197 }
198 }
199
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500200 private Condition[] removeDuplicateConditions(String pkg, Condition[] conditions) {
John Spurlock3b98b3f2014-05-01 09:08:48 -0400201 if (conditions == null || conditions.length == 0) return null;
202 final int N = conditions.length;
203 final ArrayMap<Uri, Condition> valid = new ArrayMap<Uri, Condition>(N);
204 for (int i = 0; i < N; i++) {
205 final Uri id = conditions[i].id;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400206 if (valid.containsKey(id)) {
207 Slog.w(TAG, "Ignoring condition from " + pkg + " for duplicate id: " + id);
208 continue;
209 }
210 valid.put(id, conditions[i]);
211 }
212 if (valid.size() == 0) return null;
213 if (valid.size() == N) return conditions;
214 final Condition[] rt = new Condition[valid.size()];
215 for (int i = 0; i < rt.length; i++) {
216 rt[i] = valid.valueAt(i);
217 }
218 return rt;
219 }
220
John Spurlockb2278d62015-04-07 12:47:12 -0400221 private ConditionRecord getRecordLocked(Uri id, ComponentName component, boolean create) {
222 if (id == null || component == null) return null;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400223 final int N = mRecords.size();
224 for (int i = 0; i < N; i++) {
225 final ConditionRecord r = mRecords.get(i);
226 if (r.id.equals(id) && r.component.equals(component)) {
227 return r;
228 }
229 }
John Spurlockb2278d62015-04-07 12:47:12 -0400230 if (create) {
231 final ConditionRecord r = new ConditionRecord(id, component);
232 mRecords.add(r);
233 return r;
234 }
235 return null;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400236 }
237
John Spurlocke77bb362014-04-26 10:24:59 -0400238 public void notifyConditions(String pkg, ManagedServiceInfo info, Condition[] conditions) {
239 synchronized(mMutex) {
240 if (DEBUG) Slog.d(TAG, "notifyConditions pkg=" + pkg + " info=" + info + " conditions="
241 + (conditions == null ? null : Arrays.asList(conditions)));
Julia Reynolds1d6d16d2016-03-07 13:51:02 -0500242 conditions = removeDuplicateConditions(pkg, conditions);
John Spurlocke77bb362014-04-26 10:24:59 -0400243 if (conditions == null || conditions.length == 0) return;
244 final int N = conditions.length;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400245 for (int i = 0; i < N; i++) {
246 final Condition c = conditions[i];
John Spurlockb2278d62015-04-07 12:47:12 -0400247 final ConditionRecord r = getRecordLocked(c.id, info.component, true /*create*/);
John Spurlock3b98b3f2014-05-01 09:08:48 -0400248 r.info = info;
249 r.condition = c;
Julia Reynolds0f50e4e2016-02-11 08:54:24 -0500250 }
251 }
252 final int N = conditions.length;
253 for (int i = 0; i < N; i++) {
254 final Condition c = conditions[i];
255 if (mCallback != null) {
256 mCallback.onConditionChanged(c.id, c);
John Spurlocke77bb362014-04-26 10:24:59 -0400257 }
258 }
259 }
260
John Spurlock39581cc2015-04-10 11:59:01 -0400261 public IConditionProvider findConditionProvider(ComponentName component) {
262 if (component == null) return null;
Julia Reynolds00314d92017-04-14 10:01:24 -0400263 for (ManagedServiceInfo service : getServices()) {
John Spurlock39581cc2015-04-10 11:59:01 -0400264 if (component.equals(service.component)) {
265 return provider(service);
266 }
267 }
268 return null;
269 }
270
John Spurlock21258a32015-05-27 18:22:55 -0400271 public Condition findCondition(ComponentName component, Uri conditionId) {
272 if (component == null || conditionId == null) return null;
273 synchronized (mMutex) {
274 final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/);
275 return r != null ? r.condition : null;
276 }
277 }
278
John Spurlockb2278d62015-04-07 12:47:12 -0400279 public void ensureRecordExists(ComponentName component, Uri conditionId,
280 IConditionProvider provider) {
John Spurlock530052a2014-11-30 16:26:19 -0500281 // constructed by convention, make sure the record exists...
John Spurlockb2278d62015-04-07 12:47:12 -0400282 final ConditionRecord r = getRecordLocked(conditionId, component, true /*create*/);
John Spurlock530052a2014-11-30 16:26:19 -0500283 if (r.info == null) {
284 // ... and is associated with the in-process service
285 r.info = checkServiceTokenLocked(provider);
286 }
287 }
288
John Spurlockb2278d62015-04-07 12:47:12 -0400289 public boolean subscribeIfNecessary(ComponentName component, Uri conditionId) {
290 synchronized (mMutex) {
291 final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/);
292 if (r == null) {
293 Slog.w(TAG, "Unable to subscribe to " + component + " " + conditionId);
294 return false;
John Spurlock856edeb2014-06-01 20:36:47 -0400295 }
John Spurlockb2278d62015-04-07 12:47:12 -0400296 if (r.subscribed) return true;
297 subscribeLocked(r);
298 return r.subscribed;
299 }
300 }
301
302 public void unsubscribeIfNecessary(ComponentName component, Uri conditionId) {
303 synchronized (mMutex) {
304 final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/);
305 if (r == null) {
306 Slog.w(TAG, "Unable to unsubscribe to " + component + " " + conditionId);
307 return;
John Spurlocke77bb362014-04-26 10:24:59 -0400308 }
John Spurlockb2278d62015-04-07 12:47:12 -0400309 if (!r.subscribed) return;
310 unsubscribeLocked(r);;
John Spurlocke77bb362014-04-26 10:24:59 -0400311 }
312 }
313
John Spurlock3b98b3f2014-05-01 09:08:48 -0400314 private void subscribeLocked(ConditionRecord r) {
315 if (DEBUG) Slog.d(TAG, "subscribeLocked " + r);
316 final IConditionProvider provider = provider(r);
John Spurlock6ae82a72014-07-16 16:23:01 -0400317 RemoteException re = null;
318 if (provider != null) {
319 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400320 Slog.d(TAG, "Subscribing to " + r.id + " with " + r.component);
John Spurlock6ae82a72014-07-16 16:23:01 -0400321 provider.onSubscribe(r.id);
John Spurlockb2278d62015-04-07 12:47:12 -0400322 r.subscribed = true;
John Spurlock6ae82a72014-07-16 16:23:01 -0400323 } catch (RemoteException e) {
324 Slog.w(TAG, "Error subscribing to " + r, e);
325 re = e;
326 }
John Spurlocke77bb362014-04-26 10:24:59 -0400327 }
John Spurlock6ae82a72014-07-16 16:23:01 -0400328 ZenLog.traceSubscribe(r != null ? r.id : null, provider, re);
John Spurlock3b98b3f2014-05-01 09:08:48 -0400329 }
330
John Spurlock530052a2014-11-30 16:26:19 -0500331 @SafeVarargs
John Spurlock3b98b3f2014-05-01 09:08:48 -0400332 private static <T> ArraySet<T> safeSet(T... items) {
333 final ArraySet<T> rt = new ArraySet<T>();
334 if (items == null || items.length == 0) return rt;
335 final int N = items.length;
336 for (int i = 0; i < N; i++) {
337 final T item = items[i];
338 if (item != null) {
339 rt.add(item);
340 }
341 }
342 return rt;
343 }
344
John Spurlock3b98b3f2014-05-01 09:08:48 -0400345 private void unsubscribeLocked(ConditionRecord r) {
346 if (DEBUG) Slog.d(TAG, "unsubscribeLocked " + r);
347 final IConditionProvider provider = provider(r);
John Spurlock6ae82a72014-07-16 16:23:01 -0400348 RemoteException re = null;
349 if (provider != null) {
350 try {
351 provider.onUnsubscribe(r.id);
352 } catch (RemoteException e) {
353 Slog.w(TAG, "Error unsubscribing to " + r, e);
354 re = e;
355 }
John Spurlockb2278d62015-04-07 12:47:12 -0400356 r.subscribed = false;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400357 }
John Spurlock6ae82a72014-07-16 16:23:01 -0400358 ZenLog.traceUnsubscribe(r != null ? r.id : null, provider, re);
John Spurlock3b98b3f2014-05-01 09:08:48 -0400359 }
360
361 private static IConditionProvider provider(ConditionRecord r) {
362 return r == null ? null : provider(r.info);
John Spurlocke77bb362014-04-26 10:24:59 -0400363 }
364
365 private static IConditionProvider provider(ManagedServiceInfo info) {
366 return info == null ? null : (IConditionProvider) info.service;
367 }
368
John Spurlock3b98b3f2014-05-01 09:08:48 -0400369 private static class ConditionRecord {
370 public final Uri id;
371 public final ComponentName component;
372 public Condition condition;
373 public ManagedServiceInfo info;
John Spurlockb2278d62015-04-07 12:47:12 -0400374 public boolean subscribed;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400375
376 private ConditionRecord(Uri id, ComponentName component) {
377 this.id = id;
378 this.component = component;
379 }
380
381 @Override
382 public String toString() {
383 final StringBuilder sb = new StringBuilder("ConditionRecord[id=")
John Spurlockb2278d62015-04-07 12:47:12 -0400384 .append(id).append(",component=").append(component)
385 .append(",subscribed=").append(subscribed);
John Spurlock3b98b3f2014-05-01 09:08:48 -0400386 return sb.append(']').toString();
387 }
388 }
John Spurlockb2278d62015-04-07 12:47:12 -0400389
390 public interface Callback {
391 void onBootComplete();
John Spurlock39581cc2015-04-10 11:59:01 -0400392 void onServiceAdded(ComponentName component);
John Spurlockb2278d62015-04-07 12:47:12 -0400393 void onConditionChanged(Uri id, Condition condition);
394 void onUserSwitched();
395 }
396
John Spurlock7340fc82014-04-24 18:50:12 -0400397}