2 * Copyright (c) 2014, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server.notification;
19 import android.annotation.NonNull;
20 import android.app.INotificationManager;
21 import android.app.NotificationManager;
22 import android.content.ComponentName;
23 import android.content.ContentResolver;
24 import android.content.Context;
25 import android.net.Uri;
26 import android.os.Handler;
27 import android.os.IBinder;
28 import android.os.IInterface;
29 import android.os.RemoteException;
30 import android.os.UserHandle;
31 import android.provider.Settings;
32 import android.service.notification.Condition;
33 import android.service.notification.ConditionProviderService;
34 import android.service.notification.IConditionProvider;
35 import android.text.TextUtils;
36 import android.util.ArrayMap;
37 import android.util.ArraySet;
38 import android.util.Slog;
40 import com.android.internal.R;
41 import com.android.server.notification.NotificationManagerService.DumpFilter;
43 import java.io.PrintWriter;
44 import java.util.ArrayList;
45 import java.util.Arrays;
47 public class ConditionProviders extends ManagedServices {
48 private final ArrayList<ConditionRecord> mRecords = new ArrayList<>();
49 private final ArraySet<String> mSystemConditionProviderNames;
50 private final ArraySet<SystemConditionProviderService> mSystemConditionProviders
53 private Callback mCallback;
55 public ConditionProviders(Context context, Handler handler, UserProfiles userProfiles) {
56 super(context, handler, new Object(), userProfiles);
57 mSystemConditionProviderNames = safeSet(PropConfig.getStringArray(mContext,
58 "system.condition.providers",
59 R.array.config_system_condition_providers));
62 public void setCallback(Callback callback) {
66 public boolean isSystemProviderEnabled(String path) {
67 return mSystemConditionProviderNames.contains(path);
70 public void addSystemProvider(SystemConditionProviderService service) {
71 mSystemConditionProviders.add(service);
72 service.attachBase(mContext);
73 registerService(service.asInterface(), service.getComponent(), UserHandle.USER_SYSTEM);
76 public Iterable<SystemConditionProviderService> getSystemProviders() {
77 return mSystemConditionProviders;
81 protected Config getConfig() {
82 final Config c = new Config();
83 c.caption = "condition provider";
84 c.serviceInterface = ConditionProviderService.SERVICE_INTERFACE;
85 c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES;
86 c.secondarySettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS;
87 c.bindPermission = android.Manifest.permission.BIND_CONDITION_PROVIDER_SERVICE;
88 c.settingsAction = Settings.ACTION_CONDITION_PROVIDER_SETTINGS;
89 c.clientLabel = R.string.condition_provider_service_binding_label;
94 public void dump(PrintWriter pw, DumpFilter filter) {
95 super.dump(pw, filter);
96 synchronized(mMutex) {
97 pw.print(" mRecords("); pw.print(mRecords.size()); pw.println("):");
98 for (int i = 0; i < mRecords.size(); i++) {
99 final ConditionRecord r = mRecords.get(i);
100 if (filter != null && !filter.matches(r.component)) continue;
101 pw.print(" "); pw.println(r);
102 final String countdownDesc = CountdownConditionProvider.tryParseDescription(r.id);
103 if (countdownDesc != null) {
104 pw.print(" ("); pw.print(countdownDesc); pw.println(")");
108 pw.print(" mSystemConditionProviders: "); pw.println(mSystemConditionProviderNames);
109 for (int i = 0; i < mSystemConditionProviders.size(); i++) {
110 mSystemConditionProviders.valueAt(i).dump(pw, filter);
115 protected IInterface asInterface(IBinder binder) {
116 return IConditionProvider.Stub.asInterface(binder);
120 protected boolean checkType(IInterface service) {
121 return service instanceof IConditionProvider;
125 public void onBootPhaseAppsCanStart() {
126 super.onBootPhaseAppsCanStart();
127 for (int i = 0; i < mSystemConditionProviders.size(); i++) {
128 mSystemConditionProviders.valueAt(i).onBootComplete();
130 if (mCallback != null) {
131 mCallback.onBootComplete();
136 public void onUserSwitched(int user) {
137 super.onUserSwitched(user);
138 if (mCallback != null) {
139 mCallback.onUserSwitched();
144 protected void onServiceAdded(ManagedServiceInfo info) {
145 final IConditionProvider provider = provider(info);
147 provider.onConnected();
148 } catch (RemoteException e) {
151 if (mCallback != null) {
152 mCallback.onServiceAdded(info.component);
157 protected void onServiceRemovedLocked(ManagedServiceInfo removed) {
158 if (removed == null) return;
159 for (int i = mRecords.size() - 1; i >= 0; i--) {
160 final ConditionRecord r = mRecords.get(i);
161 if (!r.component.equals(removed.component)) continue;
167 public void onPackagesChanged(boolean removingPackage, String[] pkgList) {
168 if (removingPackage) {
169 INotificationManager inm = NotificationManager.getService();
171 if (pkgList != null && (pkgList.length > 0)) {
172 for (String pkgName : pkgList) {
174 inm.removeAutomaticZenRules(pkgName);
175 inm.setNotificationPolicyAccessGranted(pkgName, false);
176 } catch (Exception e) {
177 Slog.e(TAG, "Failed to clean up rules for " + pkgName, e);
182 super.onPackagesChanged(removingPackage, pkgList);
185 public ManagedServiceInfo checkServiceToken(IConditionProvider provider) {
186 synchronized(mMutex) {
187 return checkServiceTokenLocked(provider);
191 private Condition[] removeDuplicateConditions(String pkg, Condition[] conditions) {
192 if (conditions == null || conditions.length == 0) return null;
193 final int N = conditions.length;
194 final ArrayMap<Uri, Condition> valid = new ArrayMap<Uri, Condition>(N);
195 for (int i = 0; i < N; i++) {
196 final Uri id = conditions[i].id;
197 if (valid.containsKey(id)) {
198 Slog.w(TAG, "Ignoring condition from " + pkg + " for duplicate id: " + id);
201 valid.put(id, conditions[i]);
203 if (valid.size() == 0) return null;
204 if (valid.size() == N) return conditions;
205 final Condition[] rt = new Condition[valid.size()];
206 for (int i = 0; i < rt.length; i++) {
207 rt[i] = valid.valueAt(i);
212 private ConditionRecord getRecordLocked(Uri id, ComponentName component, boolean create) {
213 if (id == null || component == null) return null;
214 final int N = mRecords.size();
215 for (int i = 0; i < N; i++) {
216 final ConditionRecord r = mRecords.get(i);
217 if (r.id.equals(id) && r.component.equals(component)) {
222 final ConditionRecord r = new ConditionRecord(id, component);
229 public void notifyConditions(String pkg, ManagedServiceInfo info, Condition[] conditions) {
230 synchronized(mMutex) {
231 if (DEBUG) Slog.d(TAG, "notifyConditions pkg=" + pkg + " info=" + info + " conditions="
232 + (conditions == null ? null : Arrays.asList(conditions)));
233 conditions = removeDuplicateConditions(pkg, conditions);
234 if (conditions == null || conditions.length == 0) return;
235 final int N = conditions.length;
236 for (int i = 0; i < N; i++) {
237 final Condition c = conditions[i];
238 final ConditionRecord r = getRecordLocked(c.id, info.component, true /*create*/);
243 final int N = conditions.length;
244 for (int i = 0; i < N; i++) {
245 final Condition c = conditions[i];
246 if (mCallback != null) {
247 mCallback.onConditionChanged(c.id, c);
252 public IConditionProvider findConditionProvider(ComponentName component) {
253 if (component == null) return null;
254 for (ManagedServiceInfo service : mServices) {
255 if (component.equals(service.component)) {
256 return provider(service);
262 public Condition findCondition(ComponentName component, Uri conditionId) {
263 if (component == null || conditionId == null) return null;
264 synchronized (mMutex) {
265 final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/);
266 return r != null ? r.condition : null;
270 public void ensureRecordExists(ComponentName component, Uri conditionId,
271 IConditionProvider provider) {
272 // constructed by convention, make sure the record exists...
273 final ConditionRecord r = getRecordLocked(conditionId, component, true /*create*/);
274 if (r.info == null) {
275 // ... and is associated with the in-process service
276 r.info = checkServiceTokenLocked(provider);
281 protected @NonNull ArraySet<ComponentName> loadComponentNamesFromSetting(String settingName,
283 final ContentResolver cr = mContext.getContentResolver();
284 String settingValue = Settings.Secure.getStringForUser(
288 if (TextUtils.isEmpty(settingValue))
289 return new ArraySet<>();
290 String[] packages = settingValue.split(ENABLED_SERVICES_SEPARATOR);
291 ArraySet<ComponentName> result = new ArraySet<>(packages.length);
292 for (int i = 0; i < packages.length; i++) {
293 if (!TextUtils.isEmpty(packages[i])) {
294 final ComponentName component = ComponentName.unflattenFromString(packages[i]);
295 if (component != null) {
296 result.addAll(queryPackageForServices(component.getPackageName(), userId));
298 result.addAll(queryPackageForServices(packages[i], userId));
305 public boolean subscribeIfNecessary(ComponentName component, Uri conditionId) {
306 synchronized (mMutex) {
307 final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/);
309 Slog.w(TAG, "Unable to subscribe to " + component + " " + conditionId);
312 if (r.subscribed) return true;
318 public void unsubscribeIfNecessary(ComponentName component, Uri conditionId) {
319 synchronized (mMutex) {
320 final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/);
322 Slog.w(TAG, "Unable to unsubscribe to " + component + " " + conditionId);
325 if (!r.subscribed) return;
326 unsubscribeLocked(r);;
330 private void subscribeLocked(ConditionRecord r) {
331 if (DEBUG) Slog.d(TAG, "subscribeLocked " + r);
332 final IConditionProvider provider = provider(r);
333 RemoteException re = null;
334 if (provider != null) {
336 Slog.d(TAG, "Subscribing to " + r.id + " with " + r.component);
337 provider.onSubscribe(r.id);
339 } catch (RemoteException e) {
340 Slog.w(TAG, "Error subscribing to " + r, e);
344 ZenLog.traceSubscribe(r != null ? r.id : null, provider, re);
348 private static <T> ArraySet<T> safeSet(T... items) {
349 final ArraySet<T> rt = new ArraySet<T>();
350 if (items == null || items.length == 0) return rt;
351 final int N = items.length;
352 for (int i = 0; i < N; i++) {
353 final T item = items[i];
361 private void unsubscribeLocked(ConditionRecord r) {
362 if (DEBUG) Slog.d(TAG, "unsubscribeLocked " + r);
363 final IConditionProvider provider = provider(r);
364 RemoteException re = null;
365 if (provider != null) {
367 provider.onUnsubscribe(r.id);
368 } catch (RemoteException e) {
369 Slog.w(TAG, "Error unsubscribing to " + r, e);
372 r.subscribed = false;
374 ZenLog.traceUnsubscribe(r != null ? r.id : null, provider, re);
377 private static IConditionProvider provider(ConditionRecord r) {
378 return r == null ? null : provider(r.info);
381 private static IConditionProvider provider(ManagedServiceInfo info) {
382 return info == null ? null : (IConditionProvider) info.service;
385 private static class ConditionRecord {
387 public final ComponentName component;
388 public Condition condition;
389 public ManagedServiceInfo info;
390 public boolean subscribed;
392 private ConditionRecord(Uri id, ComponentName component) {
394 this.component = component;
398 public String toString() {
399 final StringBuilder sb = new StringBuilder("ConditionRecord[id=")
400 .append(id).append(",component=").append(component)
401 .append(",subscribed=").append(subscribed);
402 return sb.append(']').toString();
406 public interface Callback {
407 void onBootComplete();
408 void onServiceAdded(ComponentName component);
409 void onConditionChanged(Uri id, Condition condition);
410 void onUserSwitched();