blob: 0c7d71b1d0e759853091275fe05359cf22f350c2 [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
19import android.app.ActivityManager;
20import android.app.PendingIntent;
21import android.content.ComponentName;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.ServiceConnection;
26import android.content.pm.ApplicationInfo;
27import android.content.pm.PackageManager;
28import android.content.pm.PackageManager.NameNotFoundException;
29import android.content.pm.ResolveInfo;
30import android.content.pm.ServiceInfo;
31import android.content.pm.UserInfo;
32import android.database.ContentObserver;
33import android.net.Uri;
34import android.os.Build;
35import android.os.Handler;
36import android.os.IBinder;
37import android.os.IInterface;
38import android.os.RemoteException;
39import android.os.UserHandle;
40import android.os.UserManager;
41import android.provider.Settings;
42import android.text.TextUtils;
43import android.util.ArraySet;
John Spurlock4db0d982014-08-13 09:19:03 -040044import android.util.Log;
John Spurlock7340fc82014-04-24 18:50:12 -040045import android.util.Slog;
46import android.util.SparseArray;
47
John Spurlock25e2d242014-06-27 13:58:23 -040048import com.android.server.notification.NotificationManagerService.DumpFilter;
49
John Spurlock7340fc82014-04-24 18:50:12 -040050import java.io.PrintWriter;
51import java.util.ArrayList;
John Spurlocke77bb362014-04-26 10:24:59 -040052import java.util.Arrays;
John Spurlock7340fc82014-04-24 18:50:12 -040053import java.util.List;
54import java.util.Set;
55
56/**
57 * Manages the lifecycle of application-provided services bound by system server.
58 *
59 * Services managed by this helper must have:
60 * - An associated system settings value with a list of enabled component names.
61 * - A well-known action for services to use in their intent-filter.
62 * - A system permission for services to require in order to ensure system has exclusive binding.
63 * - A settings page for user configuration of enabled services, and associated intent action.
64 * - A remote interface definition (aidl) provided by the service used for communication.
65 */
66abstract public class ManagedServices {
67 protected final String TAG = getClass().getSimpleName();
John Spurlock4db0d982014-08-13 09:19:03 -040068 protected final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
John Spurlock7340fc82014-04-24 18:50:12 -040069
70 private static final String ENABLED_SERVICES_SEPARATOR = ":";
71
John Spurlockaf8d6c42014-05-07 17:49:08 -040072 protected final Context mContext;
John Spurlocke77bb362014-04-26 10:24:59 -040073 protected final Object mMutex;
John Spurlock7340fc82014-04-24 18:50:12 -040074 private final UserProfiles mUserProfiles;
75 private final SettingsObserver mSettingsObserver;
76 private final Config mConfig;
77
78 // contains connections to all connected services, including app services
79 // and system services
80 protected final ArrayList<ManagedServiceInfo> mServices = new ArrayList<ManagedServiceInfo>();
81 // things that will be put into mServices as soon as they're ready
82 private final ArrayList<String> mServicesBinding = new ArrayList<String>();
83 // lists the component names of all enabled (and therefore connected)
84 // app services for current profiles.
85 private ArraySet<ComponentName> mEnabledServicesForCurrentProfiles
86 = new ArraySet<ComponentName>();
87 // Just the packages from mEnabledServicesForCurrentProfiles
88 private ArraySet<String> mEnabledServicesPackageNames = new ArraySet<String>();
89
Christoph Studerb53dfd42014-09-12 14:45:59 +020090 // Kept to de-dupe user change events (experienced after boot, when we receive a settings and a
91 // user change).
92 private int[] mLastSeenProfileIds;
93
John Spurlock7340fc82014-04-24 18:50:12 -040094 public ManagedServices(Context context, Handler handler, Object mutex,
95 UserProfiles userProfiles) {
96 mContext = context;
97 mMutex = mutex;
98 mUserProfiles = userProfiles;
99 mConfig = getConfig();
100 mSettingsObserver = new SettingsObserver(handler);
101 }
102
103 abstract protected Config getConfig();
104
105 private String getCaption() {
106 return mConfig.caption;
107 }
108
109 abstract protected IInterface asInterface(IBinder binder);
110
John Spurlock3b98b3f2014-05-01 09:08:48 -0400111 abstract protected void onServiceAdded(ManagedServiceInfo info);
John Spurlock7340fc82014-04-24 18:50:12 -0400112
John Spurlocke77bb362014-04-26 10:24:59 -0400113 protected void onServiceRemovedLocked(ManagedServiceInfo removed) { }
114
John Spurlock7340fc82014-04-24 18:50:12 -0400115 private ManagedServiceInfo newServiceInfo(IInterface service,
116 ComponentName component, int userid, boolean isSystem, ServiceConnection connection,
117 int targetSdkVersion) {
118 return new ManagedServiceInfo(service, component, userid, isSystem, connection,
119 targetSdkVersion);
120 }
121
122 public void onBootPhaseAppsCanStart() {
123 mSettingsObserver.observe();
124 }
125
John Spurlock25e2d242014-06-27 13:58:23 -0400126 public void dump(PrintWriter pw, DumpFilter filter) {
John Spurlocke77bb362014-04-26 10:24:59 -0400127 pw.println(" All " + getCaption() + "s (" + mEnabledServicesForCurrentProfiles.size()
John Spurlock7340fc82014-04-24 18:50:12 -0400128 + ") enabled for current profiles:");
129 for (ComponentName cmpt : mEnabledServicesForCurrentProfiles) {
John Spurlock25e2d242014-06-27 13:58:23 -0400130 if (filter != null && !filter.matches(cmpt)) continue;
John Spurlocke77bb362014-04-26 10:24:59 -0400131 pw.println(" " + cmpt);
John Spurlock7340fc82014-04-24 18:50:12 -0400132 }
133
John Spurlocke77bb362014-04-26 10:24:59 -0400134 pw.println(" Live " + getCaption() + "s (" + mServices.size() + "):");
John Spurlock7340fc82014-04-24 18:50:12 -0400135 for (ManagedServiceInfo info : mServices) {
John Spurlock25e2d242014-06-27 13:58:23 -0400136 if (filter != null && !filter.matches(info.component)) continue;
John Spurlocke77bb362014-04-26 10:24:59 -0400137 pw.println(" " + info.component
John Spurlock7340fc82014-04-24 18:50:12 -0400138 + " (user " + info.userid + "): " + info.service
139 + (info.isSystem?" SYSTEM":""));
140 }
141 }
142
143 public void onPackagesChanged(boolean queryReplace, String[] pkgList) {
John Spurlocke77bb362014-04-26 10:24:59 -0400144 if (DEBUG) Slog.d(TAG, "onPackagesChanged queryReplace=" + queryReplace
145 + " pkgList=" + (pkgList == null ? null : Arrays.asList(pkgList))
146 + " mEnabledServicesPackageNames=" + mEnabledServicesPackageNames);
John Spurlock7340fc82014-04-24 18:50:12 -0400147 boolean anyServicesInvolved = false;
148 if (pkgList != null && (pkgList.length > 0)) {
149 for (String pkgName : pkgList) {
150 if (mEnabledServicesPackageNames.contains(pkgName)) {
151 anyServicesInvolved = true;
152 }
153 }
154 }
155
156 if (anyServicesInvolved) {
157 // if we're not replacing a package, clean up orphaned bits
158 if (!queryReplace) {
159 disableNonexistentServices();
160 }
161 // make sure we're still bound to any of our services who may have just upgraded
162 rebindServices();
163 }
164 }
165
Christoph Studerb53dfd42014-09-12 14:45:59 +0200166 public void onUserSwitched() {
167 if (DEBUG) Slog.d(TAG, "onUserSwitched");
168 if (Arrays.equals(mLastSeenProfileIds, mUserProfiles.getCurrentProfileIds())) {
169 if (DEBUG) Slog.d(TAG, "Current profile IDs didn't change, skipping rebindServices().");
170 return;
171 }
172 rebindServices();
173 }
174
John Spurlock7340fc82014-04-24 18:50:12 -0400175 public ManagedServiceInfo checkServiceTokenLocked(IInterface service) {
176 checkNotNull(service);
177 final IBinder token = service.asBinder();
178 final int N = mServices.size();
179 for (int i=0; i<N; i++) {
180 final ManagedServiceInfo info = mServices.get(i);
181 if (info.service.asBinder() == token) return info;
182 }
183 throw new SecurityException("Disallowed call from unknown " + getCaption() + ": "
184 + service);
185 }
186
187 public void unregisterService(IInterface service, int userid) {
188 checkNotNull(service);
189 // no need to check permissions; if your service binder is in the list,
190 // that's proof that you had permission to add it in the first place
191 unregisterServiceImpl(service, userid);
192 }
193
194 public void registerService(IInterface service, ComponentName component, int userid) {
195 checkNotNull(service);
Christoph Studer3e144d32014-05-22 16:48:40 +0200196 ManagedServiceInfo info = registerServiceImpl(service, component, userid);
197 if (info != null) {
198 onServiceAdded(info);
199 }
John Spurlock7340fc82014-04-24 18:50:12 -0400200 }
201
202 /**
203 * Remove access for any services that no longer exist.
204 */
205 private void disableNonexistentServices() {
206 int[] userIds = mUserProfiles.getCurrentProfileIds();
207 final int N = userIds.length;
208 for (int i = 0 ; i < N; ++i) {
209 disableNonexistentServices(userIds[i]);
210 }
211 }
212
213 private void disableNonexistentServices(int userId) {
214 String flatIn = Settings.Secure.getStringForUser(
215 mContext.getContentResolver(),
216 mConfig.secureSettingName,
217 userId);
218 if (!TextUtils.isEmpty(flatIn)) {
219 if (DEBUG) Slog.v(TAG, "flat before: " + flatIn);
220 PackageManager pm = mContext.getPackageManager();
221 List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
222 new Intent(mConfig.serviceInterface),
223 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
224 userId);
John Spurlocke77bb362014-04-26 10:24:59 -0400225 if (DEBUG) Slog.v(TAG, mConfig.serviceInterface + " services: " + installedServices);
John Spurlock7340fc82014-04-24 18:50:12 -0400226 Set<ComponentName> installed = new ArraySet<ComponentName>();
227 for (int i = 0, count = installedServices.size(); i < count; i++) {
228 ResolveInfo resolveInfo = installedServices.get(i);
229 ServiceInfo info = resolveInfo.serviceInfo;
230
231 if (!mConfig.bindPermission.equals(info.permission)) {
232 Slog.w(TAG, "Skipping " + getCaption() + " service "
233 + info.packageName + "/" + info.name
234 + ": it does not require the permission "
235 + mConfig.bindPermission);
236 continue;
237 }
238 installed.add(new ComponentName(info.packageName, info.name));
239 }
240
241 String flatOut = "";
242 if (!installed.isEmpty()) {
243 String[] enabled = flatIn.split(ENABLED_SERVICES_SEPARATOR);
244 ArrayList<String> remaining = new ArrayList<String>(enabled.length);
245 for (int i = 0; i < enabled.length; i++) {
246 ComponentName enabledComponent = ComponentName.unflattenFromString(enabled[i]);
247 if (installed.contains(enabledComponent)) {
248 remaining.add(enabled[i]);
249 }
250 }
251 flatOut = TextUtils.join(ENABLED_SERVICES_SEPARATOR, remaining);
252 }
253 if (DEBUG) Slog.v(TAG, "flat after: " + flatOut);
254 if (!flatIn.equals(flatOut)) {
255 Settings.Secure.putStringForUser(mContext.getContentResolver(),
256 mConfig.secureSettingName,
257 flatOut, userId);
258 }
259 }
260 }
261
262 /**
263 * Called whenever packages change, the user switches, or the secure setting
264 * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
265 */
266 private void rebindServices() {
267 if (DEBUG) Slog.d(TAG, "rebindServices");
268 final int[] userIds = mUserProfiles.getCurrentProfileIds();
269 final int nUserIds = userIds.length;
270
271 final SparseArray<String> flat = new SparseArray<String>();
272
273 for (int i = 0; i < nUserIds; ++i) {
274 flat.put(userIds[i], Settings.Secure.getStringForUser(
275 mContext.getContentResolver(),
276 mConfig.secureSettingName,
277 userIds[i]));
278 }
279
Christoph Studer5d423842014-05-23 13:15:54 +0200280 ArrayList<ManagedServiceInfo> toRemove = new ArrayList<ManagedServiceInfo>();
John Spurlock7340fc82014-04-24 18:50:12 -0400281 final SparseArray<ArrayList<ComponentName>> toAdd
282 = new SparseArray<ArrayList<ComponentName>>();
283
284 synchronized (mMutex) {
Christoph Studer5d423842014-05-23 13:15:54 +0200285 // Unbind automatically bound services, retain system services.
286 for (ManagedServiceInfo service : mServices) {
287 if (!service.isSystem) {
288 toRemove.add(service);
289 }
290 }
John Spurlock7340fc82014-04-24 18:50:12 -0400291
292 final ArraySet<ComponentName> newEnabled = new ArraySet<ComponentName>();
293 final ArraySet<String> newPackages = new ArraySet<String>();
294
295 for (int i = 0; i < nUserIds; ++i) {
296 final ArrayList<ComponentName> add = new ArrayList<ComponentName>();
297 toAdd.put(userIds[i], add);
298
299 // decode the list of components
300 String toDecode = flat.get(userIds[i]);
301 if (toDecode != null) {
302 String[] components = toDecode.split(ENABLED_SERVICES_SEPARATOR);
303 for (int j = 0; j < components.length; j++) {
304 final ComponentName component
305 = ComponentName.unflattenFromString(components[j]);
306 if (component != null) {
307 newEnabled.add(component);
308 add.add(component);
309 newPackages.add(component.getPackageName());
310 }
311 }
312
313 }
314 }
315 mEnabledServicesForCurrentProfiles = newEnabled;
316 mEnabledServicesPackageNames = newPackages;
317 }
318
319 for (ManagedServiceInfo info : toRemove) {
320 final ComponentName component = info.component;
321 final int oldUser = info.userid;
322 Slog.v(TAG, "disabling " + getCaption() + " for user "
323 + oldUser + ": " + component);
324 unregisterService(component, info.userid);
325 }
326
327 for (int i = 0; i < nUserIds; ++i) {
328 final ArrayList<ComponentName> add = toAdd.get(userIds[i]);
329 final int N = add.size();
330 for (int j = 0; j < N; j++) {
331 final ComponentName component = add.get(j);
332 Slog.v(TAG, "enabling " + getCaption() + " for user " + userIds[i] + ": "
333 + component);
334 registerService(component, userIds[i]);
335 }
336 }
Christoph Studerb53dfd42014-09-12 14:45:59 +0200337
338 mLastSeenProfileIds = mUserProfiles.getCurrentProfileIds();
John Spurlock7340fc82014-04-24 18:50:12 -0400339 }
340
341 /**
342 * Version of registerService that takes the name of a service component to bind to.
343 */
344 private void registerService(final ComponentName name, final int userid) {
345 if (DEBUG) Slog.v(TAG, "registerService: " + name + " u=" + userid);
346
347 synchronized (mMutex) {
348 final String servicesBindingTag = name.toString() + "/" + userid;
349 if (mServicesBinding.contains(servicesBindingTag)) {
350 // stop registering this thing already! we're working on it
351 return;
352 }
353 mServicesBinding.add(servicesBindingTag);
354
355 final int N = mServices.size();
356 for (int i=N-1; i>=0; i--) {
357 final ManagedServiceInfo info = mServices.get(i);
358 if (name.equals(info.component)
359 && info.userid == userid) {
360 // cut old connections
361 if (DEBUG) Slog.v(TAG, " disconnecting old " + getCaption() + ": "
362 + info.service);
John Spurlocke77bb362014-04-26 10:24:59 -0400363 removeServiceLocked(i);
John Spurlock7340fc82014-04-24 18:50:12 -0400364 if (info.connection != null) {
365 mContext.unbindService(info.connection);
366 }
367 }
368 }
369
370 Intent intent = new Intent(mConfig.serviceInterface);
371 intent.setComponent(name);
372
373 intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel);
374
375 final PendingIntent pendingIntent = PendingIntent.getActivity(
376 mContext, 0, new Intent(mConfig.settingsAction), 0);
377 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
378
379 ApplicationInfo appInfo = null;
380 try {
381 appInfo = mContext.getPackageManager().getApplicationInfo(
382 name.getPackageName(), 0);
383 } catch (NameNotFoundException e) {
384 // Ignore if the package doesn't exist we won't be able to bind to the service.
385 }
386 final int targetSdkVersion =
387 appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE;
388
389 try {
390 if (DEBUG) Slog.v(TAG, "binding: " + intent);
391 if (!mContext.bindServiceAsUser(intent,
392 new ServiceConnection() {
393 IInterface mService;
394
395 @Override
396 public void onServiceConnected(ComponentName name, IBinder binder) {
397 boolean added = false;
John Spurlock3b98b3f2014-05-01 09:08:48 -0400398 ManagedServiceInfo info = null;
John Spurlock7340fc82014-04-24 18:50:12 -0400399 synchronized (mMutex) {
400 mServicesBinding.remove(servicesBindingTag);
401 try {
402 mService = asInterface(binder);
John Spurlock3b98b3f2014-05-01 09:08:48 -0400403 info = newServiceInfo(mService, name,
John Spurlock7340fc82014-04-24 18:50:12 -0400404 userid, false /*isSystem*/, this, targetSdkVersion);
405 binder.linkToDeath(info, 0);
406 added = mServices.add(info);
407 } catch (RemoteException e) {
408 // already dead
409 }
410 }
411 if (added) {
John Spurlock3b98b3f2014-05-01 09:08:48 -0400412 onServiceAdded(info);
John Spurlock7340fc82014-04-24 18:50:12 -0400413 }
414 }
415
416 @Override
417 public void onServiceDisconnected(ComponentName name) {
418 Slog.v(TAG, getCaption() + " connection lost: " + name);
419 }
420 },
421 Context.BIND_AUTO_CREATE,
422 new UserHandle(userid)))
423 {
424 mServicesBinding.remove(servicesBindingTag);
425 Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent);
426 return;
427 }
428 } catch (SecurityException ex) {
429 Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex);
430 return;
431 }
432 }
433 }
434
435 /**
436 * Remove a service for the given user by ComponentName
437 */
438 private void unregisterService(ComponentName name, int userid) {
439 synchronized (mMutex) {
440 final int N = mServices.size();
441 for (int i=N-1; i>=0; i--) {
442 final ManagedServiceInfo info = mServices.get(i);
443 if (name.equals(info.component)
444 && info.userid == userid) {
John Spurlocke77bb362014-04-26 10:24:59 -0400445 removeServiceLocked(i);
John Spurlock7340fc82014-04-24 18:50:12 -0400446 if (info.connection != null) {
447 try {
448 mContext.unbindService(info.connection);
449 } catch (IllegalArgumentException ex) {
450 // something happened to the service: we think we have a connection
451 // but it's bogus.
452 Slog.e(TAG, getCaption() + " " + name + " could not be unbound: " + ex);
453 }
454 }
455 }
456 }
457 }
458 }
459
460 /**
461 * Removes a service from the list but does not unbind
462 *
463 * @return the removed service.
464 */
465 private ManagedServiceInfo removeServiceImpl(IInterface service, final int userid) {
John Spurlocke77bb362014-04-26 10:24:59 -0400466 if (DEBUG) Slog.d(TAG, "removeServiceImpl service=" + service + " u=" + userid);
John Spurlock7340fc82014-04-24 18:50:12 -0400467 ManagedServiceInfo serviceInfo = null;
468 synchronized (mMutex) {
469 final int N = mServices.size();
470 for (int i=N-1; i>=0; i--) {
471 final ManagedServiceInfo info = mServices.get(i);
472 if (info.service.asBinder() == service.asBinder()
473 && info.userid == userid) {
John Spurlocke77bb362014-04-26 10:24:59 -0400474 if (DEBUG) Slog.d(TAG, "Removing active service " + info.component);
475 serviceInfo = removeServiceLocked(i);
John Spurlock7340fc82014-04-24 18:50:12 -0400476 }
477 }
478 }
479 return serviceInfo;
480 }
481
John Spurlocke77bb362014-04-26 10:24:59 -0400482 private ManagedServiceInfo removeServiceLocked(int i) {
483 final ManagedServiceInfo info = mServices.remove(i);
484 onServiceRemovedLocked(info);
485 return info;
486 }
487
John Spurlock7340fc82014-04-24 18:50:12 -0400488 private void checkNotNull(IInterface service) {
489 if (service == null) {
490 throw new IllegalArgumentException(getCaption() + " must not be null");
491 }
492 }
493
Christoph Studer3e144d32014-05-22 16:48:40 +0200494 private ManagedServiceInfo registerServiceImpl(final IInterface service,
John Spurlock7340fc82014-04-24 18:50:12 -0400495 final ComponentName component, final int userid) {
496 synchronized (mMutex) {
497 try {
498 ManagedServiceInfo info = newServiceInfo(service, component, userid,
Dianne Hackborn955d8d62014-10-07 20:17:19 -0700499 true /*isSystem*/, null, Build.VERSION_CODES.LOLLIPOP);
John Spurlock7340fc82014-04-24 18:50:12 -0400500 service.asBinder().linkToDeath(info, 0);
501 mServices.add(info);
Christoph Studer3e144d32014-05-22 16:48:40 +0200502 return info;
John Spurlock7340fc82014-04-24 18:50:12 -0400503 } catch (RemoteException e) {
504 // already dead
505 }
506 }
Christoph Studer3e144d32014-05-22 16:48:40 +0200507 return null;
John Spurlock7340fc82014-04-24 18:50:12 -0400508 }
509
510 /**
511 * Removes a service from the list and unbinds.
512 */
513 private void unregisterServiceImpl(IInterface service, int userid) {
514 ManagedServiceInfo info = removeServiceImpl(service, userid);
515 if (info != null && info.connection != null) {
516 mContext.unbindService(info.connection);
517 }
518 }
519
520 private class SettingsObserver extends ContentObserver {
521 private final Uri mSecureSettingsUri = Settings.Secure.getUriFor(mConfig.secureSettingName);
522
523 private SettingsObserver(Handler handler) {
524 super(handler);
525 }
526
527 private void observe() {
528 ContentResolver resolver = mContext.getContentResolver();
529 resolver.registerContentObserver(mSecureSettingsUri,
530 false, this, UserHandle.USER_ALL);
531 update(null);
532 }
533
534 @Override
535 public void onChange(boolean selfChange, Uri uri) {
536 update(uri);
537 }
538
539 private void update(Uri uri) {
540 if (uri == null || mSecureSettingsUri.equals(uri)) {
Christoph Studerb53dfd42014-09-12 14:45:59 +0200541 if (DEBUG) Slog.d(TAG, "Setting changed: mSecureSettingsUri=" + mSecureSettingsUri +
542 " / uri=" + uri);
John Spurlock7340fc82014-04-24 18:50:12 -0400543 rebindServices();
544 }
545 }
546 }
547
548 public class ManagedServiceInfo implements IBinder.DeathRecipient {
549 public IInterface service;
550 public ComponentName component;
551 public int userid;
552 public boolean isSystem;
553 public ServiceConnection connection;
554 public int targetSdkVersion;
555
556 public ManagedServiceInfo(IInterface service, ComponentName component,
557 int userid, boolean isSystem, ServiceConnection connection, int targetSdkVersion) {
558 this.service = service;
559 this.component = component;
560 this.userid = userid;
561 this.isSystem = isSystem;
562 this.connection = connection;
563 this.targetSdkVersion = targetSdkVersion;
564 }
565
John Spurlocke77bb362014-04-26 10:24:59 -0400566 @Override
567 public String toString() {
568 return new StringBuilder("ManagedServiceInfo[")
569 .append("component=").append(component)
570 .append(",userid=").append(userid)
571 .append(",isSystem=").append(isSystem)
572 .append(",targetSdkVersion=").append(targetSdkVersion)
573 .append(",connection=").append(connection == null ? null : "<connection>")
574 .append(",service=").append(service)
575 .append(']').toString();
576 }
577
John Spurlock7340fc82014-04-24 18:50:12 -0400578 public boolean enabledAndUserMatches(int nid) {
579 if (!isEnabledForCurrentProfiles()) {
580 return false;
581 }
582 if (this.userid == UserHandle.USER_ALL) return true;
583 if (nid == UserHandle.USER_ALL || nid == this.userid) return true;
584 return supportsProfiles() && mUserProfiles.isCurrentProfile(nid);
585 }
586
587 public boolean supportsProfiles() {
Dianne Hackborn955d8d62014-10-07 20:17:19 -0700588 return targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
John Spurlock7340fc82014-04-24 18:50:12 -0400589 }
590
591 @Override
592 public void binderDied() {
John Spurlocke77bb362014-04-26 10:24:59 -0400593 if (DEBUG) Slog.d(TAG, "binderDied");
John Spurlock7340fc82014-04-24 18:50:12 -0400594 // Remove the service, but don't unbind from the service. The system will bring the
595 // service back up, and the onServiceConnected handler will readd the service with the
596 // new binding. If this isn't a bound service, and is just a registered
597 // service, just removing it from the list is all we need to do anyway.
598 removeServiceImpl(this.service, this.userid);
599 }
600
601 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
602 public boolean isEnabledForCurrentProfiles() {
603 if (this.isSystem) return true;
604 if (this.connection == null) return false;
605 return mEnabledServicesForCurrentProfiles.contains(this.component);
606 }
607 }
608
609 public static class UserProfiles {
610 // Profiles of the current user.
611 private final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<UserInfo>();
612
613 public void updateCache(Context context) {
614 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
615 if (userManager != null) {
616 int currentUserId = ActivityManager.getCurrentUser();
617 List<UserInfo> profiles = userManager.getProfiles(currentUserId);
618 synchronized (mCurrentProfiles) {
619 mCurrentProfiles.clear();
620 for (UserInfo user : profiles) {
621 mCurrentProfiles.put(user.id, user);
622 }
623 }
624 }
625 }
626
627 public int[] getCurrentProfileIds() {
628 synchronized (mCurrentProfiles) {
629 int[] users = new int[mCurrentProfiles.size()];
630 final int N = mCurrentProfiles.size();
631 for (int i = 0; i < N; ++i) {
632 users[i] = mCurrentProfiles.keyAt(i);
633 }
634 return users;
635 }
636 }
637
638 public boolean isCurrentProfile(int userId) {
639 synchronized (mCurrentProfiles) {
640 return mCurrentProfiles.get(userId) != null;
641 }
642 }
643 }
644
645 protected static class Config {
646 String caption;
647 String serviceInterface;
648 String secureSettingName;
649 String bindPermission;
650 String settingsAction;
651 int clientLabel;
652 }
653}