blob: 9441d88f250486d98f50a9f86972a269e8a142f4 [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
John Spurlock3b98b3f2014-05-01 09:08:48 -040019import android.content.ComponentName;
Julia Reynoldsc279b992015-10-30 08:23:51 -040020import android.content.ContentResolver;
John Spurlock7340fc82014-04-24 18:50:12 -040021import android.content.Context;
John Spurlocke77bb362014-04-26 10:24:59 -040022import android.net.Uri;
John Spurlock7340fc82014-04-24 18:50:12 -040023import android.os.Handler;
24import android.os.IBinder;
25import android.os.IInterface;
John Spurlocke77bb362014-04-26 10:24:59 -040026import android.os.RemoteException;
John Spurlock856edeb2014-06-01 20:36:47 -040027import android.os.UserHandle;
John Spurlock7340fc82014-04-24 18:50:12 -040028import android.provider.Settings;
John Spurlocke77bb362014-04-26 10:24:59 -040029import android.service.notification.Condition;
John Spurlock7340fc82014-04-24 18:50:12 -040030import android.service.notification.ConditionProviderService;
John Spurlocke77bb362014-04-26 10:24:59 -040031import android.service.notification.IConditionListener;
32import android.service.notification.IConditionProvider;
Julia Reynoldsc279b992015-10-30 08:23:51 -040033import android.text.TextUtils;
John Spurlocke77bb362014-04-26 10:24:59 -040034import android.util.ArrayMap;
John Spurlock3b98b3f2014-05-01 09:08:48 -040035import android.util.ArraySet;
John Spurlock7340fc82014-04-24 18:50:12 -040036import android.util.Slog;
37
38import com.android.internal.R;
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 {
John Spurlockb2278d62015-04-07 12:47:12 -040046 private final ArrayList<ConditionRecord> mRecords = new ArrayList<>();
47 private final ArrayMap<IBinder, IConditionListener> mListeners = new ArrayMap<>();
48 private final ArraySet<String> mSystemConditionProviderNames;
49 private final ArraySet<SystemConditionProviderService> mSystemConditionProviders
50 = new ArraySet<>();
John Spurlock7340fc82014-04-24 18:50:12 -040051
John Spurlockb2278d62015-04-07 12:47:12 -040052 private Callback mCallback;
John Spurlock856edeb2014-06-01 20:36:47 -040053
John Spurlockb2278d62015-04-07 12:47:12 -040054 public ConditionProviders(Context context, Handler handler, UserProfiles userProfiles) {
John Spurlocke77bb362014-04-26 10:24:59 -040055 super(context, handler, new Object(), userProfiles);
John Spurlockb2278d62015-04-07 12:47:12 -040056 mSystemConditionProviderNames = safeSet(PropConfig.getStringArray(mContext,
John Spurlock530052a2014-11-30 16:26:19 -050057 "system.condition.providers",
58 R.array.config_system_condition_providers));
John Spurlock530052a2014-11-30 16:26:19 -050059 }
60
John Spurlockb2278d62015-04-07 12:47:12 -040061 public void setCallback(Callback callback) {
62 mCallback = callback;
63 }
64
65 public boolean isSystemProviderEnabled(String path) {
66 return mSystemConditionProviderNames.contains(path);
67 }
68
69 public void addSystemProvider(SystemConditionProviderService service) {
70 mSystemConditionProviders.add(service);
71 service.attachBase(mContext);
Xiaohui Chenddbe4ca2015-08-13 16:20:56 -070072 registerService(service.asInterface(), service.getComponent(), UserHandle.USER_SYSTEM);
John Spurlockb2278d62015-04-07 12:47:12 -040073 }
74
75 public Iterable<SystemConditionProviderService> getSystemProviders() {
76 return mSystemConditionProviders;
John Spurlock7340fc82014-04-24 18:50:12 -040077 }
78
79 @Override
80 protected Config getConfig() {
John Spurlockb2278d62015-04-07 12:47:12 -040081 final Config c = new Config();
John Spurlock7340fc82014-04-24 18:50:12 -040082 c.caption = "condition provider";
83 c.serviceInterface = ConditionProviderService.SERVICE_INTERFACE;
Julia Reynoldsc279b992015-10-30 08:23:51 -040084 c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES;
John Spurlock7340fc82014-04-24 18:50:12 -040085 c.bindPermission = android.Manifest.permission.BIND_CONDITION_PROVIDER_SERVICE;
86 c.settingsAction = Settings.ACTION_CONDITION_PROVIDER_SETTINGS;
87 c.clientLabel = R.string.condition_provider_service_binding_label;
88 return c;
89 }
90
91 @Override
John Spurlock25e2d242014-06-27 13:58:23 -040092 public void dump(PrintWriter pw, DumpFilter filter) {
93 super.dump(pw, filter);
John Spurlocke77bb362014-04-26 10:24:59 -040094 synchronized(mMutex) {
John Spurlock3b98b3f2014-05-01 09:08:48 -040095 pw.print(" mRecords("); pw.print(mRecords.size()); pw.println("):");
96 for (int i = 0; i < mRecords.size(); i++) {
John Spurlock856edeb2014-06-01 20:36:47 -040097 final ConditionRecord r = mRecords.get(i);
John Spurlock25e2d242014-06-27 13:58:23 -040098 if (filter != null && !filter.matches(r.component)) continue;
John Spurlock856edeb2014-06-01 20:36:47 -040099 pw.print(" "); pw.println(r);
100 final String countdownDesc = CountdownConditionProvider.tryParseDescription(r.id);
101 if (countdownDesc != null) {
102 pw.print(" ("); pw.print(countdownDesc); pw.println(")");
103 }
John Spurlocke77bb362014-04-26 10:24:59 -0400104 }
105 }
John Spurlockb2278d62015-04-07 12:47:12 -0400106 if (filter == null) {
107 pw.print(" mListeners("); pw.print(mListeners.size()); pw.println("):");
108 for (int i = 0; i < mListeners.size(); i++) {
109 pw.print(" "); pw.println(mListeners.keyAt(i));
110 }
John Spurlock530052a2014-11-30 16:26:19 -0500111 }
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
John Spurlock856edeb2014-06-01 20:36:47 -0400124 public void onBootPhaseAppsCanStart() {
125 super.onBootPhaseAppsCanStart();
John Spurlock2f096ed2015-05-04 11:58:26 -0400126 for (int i = 0; i < mSystemConditionProviders.size(); i++) {
127 mSystemConditionProviders.valueAt(i).onBootComplete();
128 }
John Spurlockb2278d62015-04-07 12:47:12 -0400129 if (mCallback != null) {
130 mCallback.onBootComplete();
John Spurlock530052a2014-11-30 16:26:19 -0500131 }
John Spurlock37bc92c2014-11-03 11:01:51 -0500132 }
133
134 @Override
John Spurlock1b8b22b2015-05-20 09:47:13 -0400135 public void onUserSwitched(int user) {
136 super.onUserSwitched(user);
John Spurlockb2278d62015-04-07 12:47:12 -0400137 if (mCallback != null) {
138 mCallback.onUserSwitched();
John Spurlock530052a2014-11-30 16:26:19 -0500139 }
John Spurlock856edeb2014-06-01 20:36:47 -0400140 }
141
142 @Override
John Spurlock3b98b3f2014-05-01 09:08:48 -0400143 protected void onServiceAdded(ManagedServiceInfo info) {
John Spurlock3b98b3f2014-05-01 09:08:48 -0400144 final IConditionProvider provider = provider(info);
John Spurlocke77bb362014-04-26 10:24:59 -0400145 try {
146 provider.onConnected();
147 } catch (RemoteException e) {
148 // we tried
149 }
John Spurlock39581cc2015-04-10 11:59:01 -0400150 if (mCallback != null) {
151 mCallback.onServiceAdded(info.component);
152 }
John Spurlocke77bb362014-04-26 10:24:59 -0400153 }
154
155 @Override
156 protected void onServiceRemovedLocked(ManagedServiceInfo removed) {
157 if (removed == null) return;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400158 for (int i = mRecords.size() - 1; i >= 0; i--) {
159 final ConditionRecord r = mRecords.get(i);
160 if (!r.component.equals(removed.component)) continue;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400161 mRecords.remove(i);
John Spurlocke77bb362014-04-26 10:24:59 -0400162 }
163 }
164
165 public ManagedServiceInfo checkServiceToken(IConditionProvider provider) {
166 synchronized(mMutex) {
167 return checkServiceTokenLocked(provider);
168 }
169 }
170
John Spurlockb2278d62015-04-07 12:47:12 -0400171 public void requestConditions(IConditionListener callback, int relevance) {
John Spurlocke77bb362014-04-26 10:24:59 -0400172 synchronized(mMutex) {
John Spurlockb2278d62015-04-07 12:47:12 -0400173 if (DEBUG) Slog.d(TAG, "requestConditions callback=" + callback
John Spurlock3b98b3f2014-05-01 09:08:48 -0400174 + " relevance=" + Condition.relevanceToString(relevance));
John Spurlocke77bb362014-04-26 10:24:59 -0400175 if (callback == null) return;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400176 relevance = relevance & (Condition.FLAG_RELEVANT_NOW | Condition.FLAG_RELEVANT_ALWAYS);
177 if (relevance != 0) {
John Spurlocke77bb362014-04-26 10:24:59 -0400178 mListeners.put(callback.asBinder(), callback);
John Spurlock3b98b3f2014-05-01 09:08:48 -0400179 requestConditionsLocked(relevance);
John Spurlocke77bb362014-04-26 10:24:59 -0400180 } else {
181 mListeners.remove(callback.asBinder());
182 if (mListeners.isEmpty()) {
183 requestConditionsLocked(0);
184 }
185 }
186 }
187 }
188
John Spurlock3b98b3f2014-05-01 09:08:48 -0400189 private Condition[] validateConditions(String pkg, Condition[] conditions) {
190 if (conditions == null || conditions.length == 0) return null;
191 final int N = conditions.length;
192 final ArrayMap<Uri, Condition> valid = new ArrayMap<Uri, Condition>(N);
193 for (int i = 0; i < N; i++) {
194 final Uri id = conditions[i].id;
195 if (!Condition.isValidId(id, pkg)) {
196 Slog.w(TAG, "Ignoring condition from " + pkg + " for invalid id: " + id);
197 continue;
198 }
199 if (valid.containsKey(id)) {
200 Slog.w(TAG, "Ignoring condition from " + pkg + " for duplicate id: " + id);
201 continue;
202 }
203 valid.put(id, conditions[i]);
204 }
205 if (valid.size() == 0) return null;
206 if (valid.size() == N) return conditions;
207 final Condition[] rt = new Condition[valid.size()];
208 for (int i = 0; i < rt.length; i++) {
209 rt[i] = valid.valueAt(i);
210 }
211 return rt;
212 }
213
John Spurlockb2278d62015-04-07 12:47:12 -0400214 private ConditionRecord getRecordLocked(Uri id, ComponentName component, boolean create) {
215 if (id == null || component == null) return null;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400216 final int N = mRecords.size();
217 for (int i = 0; i < N; i++) {
218 final ConditionRecord r = mRecords.get(i);
219 if (r.id.equals(id) && r.component.equals(component)) {
220 return r;
221 }
222 }
John Spurlockb2278d62015-04-07 12:47:12 -0400223 if (create) {
224 final ConditionRecord r = new ConditionRecord(id, component);
225 mRecords.add(r);
226 return r;
227 }
228 return null;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400229 }
230
John Spurlocke77bb362014-04-26 10:24:59 -0400231 public void notifyConditions(String pkg, ManagedServiceInfo info, Condition[] conditions) {
232 synchronized(mMutex) {
233 if (DEBUG) Slog.d(TAG, "notifyConditions pkg=" + pkg + " info=" + info + " conditions="
234 + (conditions == null ? null : Arrays.asList(conditions)));
John Spurlock3b98b3f2014-05-01 09:08:48 -0400235 conditions = validateConditions(pkg, conditions);
John Spurlocke77bb362014-04-26 10:24:59 -0400236 if (conditions == null || conditions.length == 0) return;
237 final int N = conditions.length;
John Spurlocke77bb362014-04-26 10:24:59 -0400238 for (IConditionListener listener : mListeners.values()) {
239 try {
240 listener.onConditionsReceived(conditions);
241 } catch (RemoteException e) {
242 Slog.w(TAG, "Error sending conditions to listener " + listener, e);
243 }
244 }
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;
John Spurlockb2278d62015-04-07 12:47:12 -0400250 if (mCallback != null) {
251 mCallback.onConditionChanged(c.id, c);
John Spurlocke77bb362014-04-26 10:24:59 -0400252 }
253 }
254 }
255 }
256
John Spurlock39581cc2015-04-10 11:59:01 -0400257 public IConditionProvider findConditionProvider(ComponentName component) {
258 if (component == null) return null;
259 for (ManagedServiceInfo service : mServices) {
260 if (component.equals(service.component)) {
261 return provider(service);
262 }
263 }
264 return null;
265 }
266
John Spurlock21258a32015-05-27 18:22:55 -0400267 public Condition findCondition(ComponentName component, Uri conditionId) {
268 if (component == null || conditionId == null) return null;
269 synchronized (mMutex) {
270 final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/);
271 return r != null ? r.condition : null;
272 }
273 }
274
John Spurlockb2278d62015-04-07 12:47:12 -0400275 public void ensureRecordExists(ComponentName component, Uri conditionId,
276 IConditionProvider provider) {
John Spurlock530052a2014-11-30 16:26:19 -0500277 // constructed by convention, make sure the record exists...
John Spurlockb2278d62015-04-07 12:47:12 -0400278 final ConditionRecord r = getRecordLocked(conditionId, component, true /*create*/);
John Spurlock530052a2014-11-30 16:26:19 -0500279 if (r.info == null) {
280 // ... and is associated with the in-process service
281 r.info = checkServiceTokenLocked(provider);
282 }
283 }
284
Julia Reynoldsc279b992015-10-30 08:23:51 -0400285 @Override
286 protected ArraySet<ComponentName> loadComponentNamesFromSetting(String settingName,
287 int userId) {
288 final ContentResolver cr = mContext.getContentResolver();
289 String settingValue = Settings.Secure.getStringForUser(
290 cr,
291 settingName,
292 userId);
293 if (TextUtils.isEmpty(settingValue))
294 return null;
295 String[] packages = settingValue.split(ENABLED_SERVICES_SEPARATOR);
296 ArraySet<ComponentName> result = new ArraySet<>(packages.length);
297 for (int i = 0; i < packages.length; i++) {
298 if (!TextUtils.isEmpty(packages[i])) {
299 result.addAll(queryPackageForServices(packages[i], userId));
300 }
301 }
302 return result;
303 }
304
John Spurlockb2278d62015-04-07 12:47:12 -0400305 public boolean subscribeIfNecessary(ComponentName component, Uri conditionId) {
306 synchronized (mMutex) {
307 final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/);
308 if (r == null) {
309 Slog.w(TAG, "Unable to subscribe to " + component + " " + conditionId);
310 return false;
John Spurlock856edeb2014-06-01 20:36:47 -0400311 }
John Spurlockb2278d62015-04-07 12:47:12 -0400312 if (r.subscribed) return true;
313 subscribeLocked(r);
314 return r.subscribed;
315 }
316 }
317
318 public void unsubscribeIfNecessary(ComponentName component, Uri conditionId) {
319 synchronized (mMutex) {
320 final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/);
321 if (r == null) {
322 Slog.w(TAG, "Unable to unsubscribe to " + component + " " + conditionId);
323 return;
John Spurlocke77bb362014-04-26 10:24:59 -0400324 }
John Spurlockb2278d62015-04-07 12:47:12 -0400325 if (!r.subscribed) return;
326 unsubscribeLocked(r);;
John Spurlocke77bb362014-04-26 10:24:59 -0400327 }
328 }
329
John Spurlock3b98b3f2014-05-01 09:08:48 -0400330 private void subscribeLocked(ConditionRecord r) {
331 if (DEBUG) Slog.d(TAG, "subscribeLocked " + r);
332 final IConditionProvider provider = provider(r);
John Spurlock6ae82a72014-07-16 16:23:01 -0400333 RemoteException re = null;
334 if (provider != null) {
335 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400336 Slog.d(TAG, "Subscribing to " + r.id + " with " + r.component);
John Spurlock6ae82a72014-07-16 16:23:01 -0400337 provider.onSubscribe(r.id);
John Spurlockb2278d62015-04-07 12:47:12 -0400338 r.subscribed = true;
John Spurlock6ae82a72014-07-16 16:23:01 -0400339 } catch (RemoteException e) {
340 Slog.w(TAG, "Error subscribing to " + r, e);
341 re = e;
342 }
John Spurlocke77bb362014-04-26 10:24:59 -0400343 }
John Spurlock6ae82a72014-07-16 16:23:01 -0400344 ZenLog.traceSubscribe(r != null ? r.id : null, provider, re);
John Spurlock3b98b3f2014-05-01 09:08:48 -0400345 }
346
John Spurlock530052a2014-11-30 16:26:19 -0500347 @SafeVarargs
John Spurlock3b98b3f2014-05-01 09:08:48 -0400348 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];
354 if (item != null) {
355 rt.add(item);
356 }
357 }
358 return rt;
359 }
360
John Spurlock3b98b3f2014-05-01 09:08:48 -0400361 private void unsubscribeLocked(ConditionRecord r) {
362 if (DEBUG) Slog.d(TAG, "unsubscribeLocked " + r);
363 final IConditionProvider provider = provider(r);
John Spurlock6ae82a72014-07-16 16:23:01 -0400364 RemoteException re = null;
365 if (provider != null) {
366 try {
367 provider.onUnsubscribe(r.id);
368 } catch (RemoteException e) {
369 Slog.w(TAG, "Error unsubscribing to " + r, e);
370 re = e;
371 }
John Spurlockb2278d62015-04-07 12:47:12 -0400372 r.subscribed = false;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400373 }
John Spurlock6ae82a72014-07-16 16:23:01 -0400374 ZenLog.traceUnsubscribe(r != null ? r.id : null, provider, re);
John Spurlock3b98b3f2014-05-01 09:08:48 -0400375 }
376
377 private static IConditionProvider provider(ConditionRecord r) {
378 return r == null ? null : provider(r.info);
John Spurlocke77bb362014-04-26 10:24:59 -0400379 }
380
381 private static IConditionProvider provider(ManagedServiceInfo info) {
382 return info == null ? null : (IConditionProvider) info.service;
383 }
384
385 private void requestConditionsLocked(int flags) {
386 for (ManagedServiceInfo info : mServices) {
387 final IConditionProvider provider = provider(info);
388 if (provider == null) continue;
John Spurlock50806fc2014-07-15 10:22:02 -0400389 // clear all stored conditions from this provider that we no longer care about
390 for (int i = mRecords.size() - 1; i >= 0; i--) {
391 final ConditionRecord r = mRecords.get(i);
392 if (r.info != info) continue;
John Spurlockb2278d62015-04-07 12:47:12 -0400393 if (r.subscribed) continue;
John Spurlock50806fc2014-07-15 10:22:02 -0400394 mRecords.remove(i);
395 }
John Spurlocke77bb362014-04-26 10:24:59 -0400396 try {
397 provider.onRequestConditions(flags);
398 } catch (RemoteException e) {
399 Slog.w(TAG, "Error requesting conditions from " + info.component, e);
400 }
401 }
John Spurlock7340fc82014-04-24 18:50:12 -0400402 }
John Spurlock1c923a32014-04-27 16:42:29 -0400403
John Spurlock3b98b3f2014-05-01 09:08:48 -0400404 private static class ConditionRecord {
405 public final Uri id;
406 public final ComponentName component;
407 public Condition condition;
408 public ManagedServiceInfo info;
John Spurlockb2278d62015-04-07 12:47:12 -0400409 public boolean subscribed;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400410
411 private ConditionRecord(Uri id, ComponentName component) {
412 this.id = id;
413 this.component = component;
414 }
415
416 @Override
417 public String toString() {
418 final StringBuilder sb = new StringBuilder("ConditionRecord[id=")
John Spurlockb2278d62015-04-07 12:47:12 -0400419 .append(id).append(",component=").append(component)
420 .append(",subscribed=").append(subscribed);
John Spurlock3b98b3f2014-05-01 09:08:48 -0400421 return sb.append(']').toString();
422 }
423 }
John Spurlockb2278d62015-04-07 12:47:12 -0400424
425 public interface Callback {
426 void onBootComplete();
John Spurlock39581cc2015-04-10 11:59:01 -0400427 void onServiceAdded(ComponentName component);
John Spurlockb2278d62015-04-07 12:47:12 -0400428 void onConditionChanged(Uri id, Condition condition);
429 void onUserSwitched();
430 }
431
John Spurlock7340fc82014-04-24 18:50:12 -0400432}