blob: ac6121b8993651f0a3fcafe171ee6238d27d4b02 [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;
Christopher Tate6597e342015-02-17 12:15:25 -080021import android.content.BroadcastReceiver;
John Spurlock7340fc82014-04-24 18:50:12 -040022import android.content.ComponentName;
23import android.content.ContentResolver;
24import android.content.Context;
25import android.content.Intent;
Christopher Tate6597e342015-02-17 12:15:25 -080026import android.content.IntentFilter;
John Spurlock7340fc82014-04-24 18:50:12 -040027import android.content.ServiceConnection;
28import android.content.pm.ApplicationInfo;
29import android.content.pm.PackageManager;
30import android.content.pm.PackageManager.NameNotFoundException;
31import android.content.pm.ResolveInfo;
32import android.content.pm.ServiceInfo;
33import android.content.pm.UserInfo;
34import android.database.ContentObserver;
35import android.net.Uri;
36import android.os.Build;
37import android.os.Handler;
38import android.os.IBinder;
39import android.os.IInterface;
40import android.os.RemoteException;
41import android.os.UserHandle;
42import android.os.UserManager;
43import android.provider.Settings;
44import android.text.TextUtils;
Ruben Brunkdd18a0b2015-12-04 16:16:31 -080045import android.util.ArrayMap;
John Spurlock7340fc82014-04-24 18:50:12 -040046import android.util.ArraySet;
John Spurlock4db0d982014-08-13 09:19:03 -040047import android.util.Log;
John Spurlock7340fc82014-04-24 18:50:12 -040048import android.util.Slog;
49import android.util.SparseArray;
50
John Spurlock25e2d242014-06-27 13:58:23 -040051import com.android.server.notification.NotificationManagerService.DumpFilter;
52
John Spurlock7340fc82014-04-24 18:50:12 -040053import java.io.PrintWriter;
54import java.util.ArrayList;
John Spurlocke77bb362014-04-26 10:24:59 -040055import java.util.Arrays;
Julia Reynolds9a86cc02016-02-10 15:38:15 -050056import java.util.HashSet;
John Spurlock7340fc82014-04-24 18:50:12 -040057import java.util.List;
Ruben Brunkdd18a0b2015-12-04 16:16:31 -080058import java.util.Map.Entry;
Christopher Tate6597e342015-02-17 12:15:25 -080059import java.util.Objects;
John Spurlock7340fc82014-04-24 18:50:12 -040060import java.util.Set;
61
62/**
63 * Manages the lifecycle of application-provided services bound by system server.
64 *
65 * Services managed by this helper must have:
66 * - An associated system settings value with a list of enabled component names.
67 * - A well-known action for services to use in their intent-filter.
68 * - A system permission for services to require in order to ensure system has exclusive binding.
69 * - A settings page for user configuration of enabled services, and associated intent action.
70 * - A remote interface definition (aidl) provided by the service used for communication.
71 */
72abstract public class ManagedServices {
73 protected final String TAG = getClass().getSimpleName();
John Spurlock4db0d982014-08-13 09:19:03 -040074 protected final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
John Spurlock7340fc82014-04-24 18:50:12 -040075
Julia Reynoldsc279b992015-10-30 08:23:51 -040076 protected static final String ENABLED_SERVICES_SEPARATOR = ":";
John Spurlock7340fc82014-04-24 18:50:12 -040077
John Spurlockaf8d6c42014-05-07 17:49:08 -040078 protected final Context mContext;
John Spurlocke77bb362014-04-26 10:24:59 -040079 protected final Object mMutex;
John Spurlock7340fc82014-04-24 18:50:12 -040080 private final UserProfiles mUserProfiles;
81 private final SettingsObserver mSettingsObserver;
82 private final Config mConfig;
Christopher Tate6597e342015-02-17 12:15:25 -080083 private ArraySet<String> mRestored;
John Spurlock7340fc82014-04-24 18:50:12 -040084
85 // contains connections to all connected services, including app services
86 // and system services
87 protected final ArrayList<ManagedServiceInfo> mServices = new ArrayList<ManagedServiceInfo>();
88 // things that will be put into mServices as soon as they're ready
89 private final ArrayList<String> mServicesBinding = new ArrayList<String>();
Chris Wrenab41eec2016-01-04 18:01:27 -050090 // lists the component names of all enabled (and therefore potentially connected)
John Spurlock7340fc82014-04-24 18:50:12 -040091 // app services for current profiles.
92 private ArraySet<ComponentName> mEnabledServicesForCurrentProfiles
93 = new ArraySet<ComponentName>();
94 // Just the packages from mEnabledServicesForCurrentProfiles
95 private ArraySet<String> mEnabledServicesPackageNames = new ArraySet<String>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +020096 // List of packages in restored setting across all mUserProfiles, for quick
97 // filtering upon package updates.
98 private ArraySet<String> mRestoredPackages = new ArraySet<>();
Ruben Brunkdd18a0b2015-12-04 16:16:31 -080099 // State of current service categories
100 private ArrayMap<String, Boolean> mCategoryEnabled = new ArrayMap<>();
Chris Wrenab41eec2016-01-04 18:01:27 -0500101 // List of enabled packages that have nevertheless asked not to be run
102 private ArraySet<ComponentName> mSnoozingForCurrentProfiles = new ArraySet<>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200103
John Spurlock7340fc82014-04-24 18:50:12 -0400104
Christoph Studerb53dfd42014-09-12 14:45:59 +0200105 // Kept to de-dupe user change events (experienced after boot, when we receive a settings and a
106 // user change).
107 private int[] mLastSeenProfileIds;
108
Christopher Tate6597e342015-02-17 12:15:25 -0800109 private final BroadcastReceiver mRestoreReceiver;
110
John Spurlock7340fc82014-04-24 18:50:12 -0400111 public ManagedServices(Context context, Handler handler, Object mutex,
112 UserProfiles userProfiles) {
113 mContext = context;
114 mMutex = mutex;
115 mUserProfiles = userProfiles;
116 mConfig = getConfig();
117 mSettingsObserver = new SettingsObserver(handler);
Christopher Tate6597e342015-02-17 12:15:25 -0800118
119 mRestoreReceiver = new SettingRestoredReceiver();
120 IntentFilter filter = new IntentFilter(Intent.ACTION_SETTING_RESTORED);
121 context.registerReceiver(mRestoreReceiver, filter);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200122 rebuildRestoredPackages();
Christopher Tate6597e342015-02-17 12:15:25 -0800123 }
124
125 class SettingRestoredReceiver extends BroadcastReceiver {
126 @Override
127 public void onReceive(Context context, Intent intent) {
128 if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) {
129 String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
130 if (Objects.equals(element, mConfig.secureSettingName)) {
131 String prevValue = intent.getStringExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE);
132 String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE);
133 settingRestored(element, prevValue, newValue, getSendingUserId());
134 }
135 }
136 }
John Spurlock7340fc82014-04-24 18:50:12 -0400137 }
138
139 abstract protected Config getConfig();
140
141 private String getCaption() {
142 return mConfig.caption;
143 }
144
145 abstract protected IInterface asInterface(IBinder binder);
146
Chris Wren51017d02015-12-15 15:34:46 -0500147 abstract protected boolean checkType(IInterface service);
148
John Spurlock3b98b3f2014-05-01 09:08:48 -0400149 abstract protected void onServiceAdded(ManagedServiceInfo info);
John Spurlock7340fc82014-04-24 18:50:12 -0400150
John Spurlocke77bb362014-04-26 10:24:59 -0400151 protected void onServiceRemovedLocked(ManagedServiceInfo removed) { }
152
John Spurlock7340fc82014-04-24 18:50:12 -0400153 private ManagedServiceInfo newServiceInfo(IInterface service,
154 ComponentName component, int userid, boolean isSystem, ServiceConnection connection,
155 int targetSdkVersion) {
156 return new ManagedServiceInfo(service, component, userid, isSystem, connection,
157 targetSdkVersion);
158 }
159
160 public void onBootPhaseAppsCanStart() {
161 mSettingsObserver.observe();
162 }
163
John Spurlock25e2d242014-06-27 13:58:23 -0400164 public void dump(PrintWriter pw, DumpFilter filter) {
John Spurlocke77bb362014-04-26 10:24:59 -0400165 pw.println(" All " + getCaption() + "s (" + mEnabledServicesForCurrentProfiles.size()
John Spurlock7340fc82014-04-24 18:50:12 -0400166 + ") enabled for current profiles:");
167 for (ComponentName cmpt : mEnabledServicesForCurrentProfiles) {
John Spurlock25e2d242014-06-27 13:58:23 -0400168 if (filter != null && !filter.matches(cmpt)) continue;
John Spurlocke77bb362014-04-26 10:24:59 -0400169 pw.println(" " + cmpt);
John Spurlock7340fc82014-04-24 18:50:12 -0400170 }
171
John Spurlocke77bb362014-04-26 10:24:59 -0400172 pw.println(" Live " + getCaption() + "s (" + mServices.size() + "):");
John Spurlock7340fc82014-04-24 18:50:12 -0400173 for (ManagedServiceInfo info : mServices) {
John Spurlock25e2d242014-06-27 13:58:23 -0400174 if (filter != null && !filter.matches(info.component)) continue;
John Spurlocke77bb362014-04-26 10:24:59 -0400175 pw.println(" " + info.component
John Spurlock7340fc82014-04-24 18:50:12 -0400176 + " (user " + info.userid + "): " + info.service
Chris Wren51017d02015-12-15 15:34:46 -0500177 + (info.isSystem?" SYSTEM":"")
178 + (info.isGuest(this)?" GUEST":""));
John Spurlock7340fc82014-04-24 18:50:12 -0400179 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500180
181 pw.println(" Snoozed " + getCaption() + "s (" +
182 mSnoozingForCurrentProfiles.size() + "):");
183 for (ComponentName name : mSnoozingForCurrentProfiles) {
184 pw.println(" " + name.flattenToShortString());
185 }
John Spurlock7340fc82014-04-24 18:50:12 -0400186 }
187
Christopher Tate6597e342015-02-17 12:15:25 -0800188 // By convention, restored settings are replicated to another settings
189 // entry, named similarly but with a disambiguation suffix.
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200190 public static String restoredSettingName(Config config) {
Christopher Tate6597e342015-02-17 12:15:25 -0800191 return config.secureSettingName + ":restored";
192 }
193
194 // The OS has done a restore of this service's saved state. We clone it to the
195 // 'restored' reserve, and then once we return and the actual write to settings is
196 // performed, our observer will do the work of maintaining the restored vs live
197 // settings data.
198 public void settingRestored(String element, String oldValue, String newValue, int userid) {
199 if (DEBUG) Slog.d(TAG, "Restored managed service setting: " + element
200 + " ovalue=" + oldValue + " nvalue=" + newValue);
201 if (mConfig.secureSettingName.equals(element)) {
202 if (element != null) {
203 mRestored = null;
204 Settings.Secure.putStringForUser(mContext.getContentResolver(),
205 restoredSettingName(mConfig),
206 newValue,
207 userid);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200208 updateSettingsAccordingToInstalledServices(userid);
209 rebuildRestoredPackages();
Christopher Tate6597e342015-02-17 12:15:25 -0800210 }
211 }
212 }
213
John Spurlock80774932015-05-07 17:38:50 -0400214 public boolean isComponentEnabledForPackage(String pkg) {
215 return mEnabledServicesPackageNames.contains(pkg);
216 }
217
John Spurlock7340fc82014-04-24 18:50:12 -0400218 public void onPackagesChanged(boolean queryReplace, String[] pkgList) {
John Spurlocke77bb362014-04-26 10:24:59 -0400219 if (DEBUG) Slog.d(TAG, "onPackagesChanged queryReplace=" + queryReplace
220 + " pkgList=" + (pkgList == null ? null : Arrays.asList(pkgList))
221 + " mEnabledServicesPackageNames=" + mEnabledServicesPackageNames);
John Spurlock7340fc82014-04-24 18:50:12 -0400222 boolean anyServicesInvolved = false;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200223
John Spurlock7340fc82014-04-24 18:50:12 -0400224 if (pkgList != null && (pkgList.length > 0)) {
225 for (String pkgName : pkgList) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200226 if (mEnabledServicesPackageNames.contains(pkgName) ||
227 mRestoredPackages.contains(pkgName)) {
John Spurlock7340fc82014-04-24 18:50:12 -0400228 anyServicesInvolved = true;
229 }
230 }
231 }
232
233 if (anyServicesInvolved) {
234 // if we're not replacing a package, clean up orphaned bits
235 if (!queryReplace) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200236 updateSettingsAccordingToInstalledServices();
237 rebuildRestoredPackages();
John Spurlock7340fc82014-04-24 18:50:12 -0400238 }
239 // make sure we're still bound to any of our services who may have just upgraded
240 rebindServices();
241 }
242 }
243
John Spurlock1b8b22b2015-05-20 09:47:13 -0400244 public void onUserSwitched(int user) {
245 if (DEBUG) Slog.d(TAG, "onUserSwitched u=" + user);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200246 rebuildRestoredPackages();
Christoph Studerb53dfd42014-09-12 14:45:59 +0200247 if (Arrays.equals(mLastSeenProfileIds, mUserProfiles.getCurrentProfileIds())) {
248 if (DEBUG) Slog.d(TAG, "Current profile IDs didn't change, skipping rebindServices().");
249 return;
250 }
251 rebindServices();
252 }
253
Julia Reynoldsa3dcaff2016-02-03 15:04:05 -0500254 public void onUserUnlocked(int user) {
255 if (DEBUG) Slog.d(TAG, "onUserUnlocked u=" + user);
256 rebuildRestoredPackages();
257 rebindServices();
258 }
259
Chris Wrenab41eec2016-01-04 18:01:27 -0500260 public ManagedServiceInfo getServiceFromTokenLocked(IInterface service) {
261 if (service == null) {
262 return null;
263 }
John Spurlock7340fc82014-04-24 18:50:12 -0400264 final IBinder token = service.asBinder();
265 final int N = mServices.size();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200266 for (int i = 0; i < N; i++) {
John Spurlock7340fc82014-04-24 18:50:12 -0400267 final ManagedServiceInfo info = mServices.get(i);
268 if (info.service.asBinder() == token) return info;
269 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500270 return null;
271 }
272
273 public ManagedServiceInfo checkServiceTokenLocked(IInterface service) {
274 checkNotNull(service);
275 ManagedServiceInfo info = getServiceFromTokenLocked(service);
276 if (info != null) {
277 return info;
278 }
John Spurlock7340fc82014-04-24 18:50:12 -0400279 throw new SecurityException("Disallowed call from unknown " + getCaption() + ": "
280 + service);
281 }
282
283 public void unregisterService(IInterface service, int userid) {
284 checkNotNull(service);
285 // no need to check permissions; if your service binder is in the list,
286 // that's proof that you had permission to add it in the first place
287 unregisterServiceImpl(service, userid);
288 }
289
290 public void registerService(IInterface service, ComponentName component, int userid) {
291 checkNotNull(service);
Christoph Studer3e144d32014-05-22 16:48:40 +0200292 ManagedServiceInfo info = registerServiceImpl(service, component, userid);
293 if (info != null) {
294 onServiceAdded(info);
295 }
John Spurlock7340fc82014-04-24 18:50:12 -0400296 }
297
Chris Wren51017d02015-12-15 15:34:46 -0500298 /**
299 * Add a service to our callbacks. The lifecycle of this service is managed externally,
300 * but unlike a system service, it should not be considered privledged.
301 * */
302 public void registerGuestService(ManagedServiceInfo guest) {
303 checkNotNull(guest.service);
304 checkType(guest.service);
305 if (registerServiceImpl(guest) != null) {
306 onServiceAdded(guest);
Chris Wrenab41eec2016-01-04 18:01:27 -0500307 }
308 }
309
310 public void setComponentState(ComponentName component, boolean enabled) {
311 boolean previous = !mSnoozingForCurrentProfiles.contains(component);
312 if (previous == enabled) {
313 return;
314 }
315
316 if (enabled) {
317 mSnoozingForCurrentProfiles.remove(component);
318 } else {
319 mSnoozingForCurrentProfiles.add(component);
320 }
321
322 // State changed
323 if (DEBUG) {
324 Slog.d(TAG, ((enabled) ? "Enabling " : "Disabling ") + "component " +
325 component.flattenToShortString());
326 }
327
Chris Wren0efdb882016-03-01 17:17:47 -0500328
329 synchronized (mMutex) {
330 final int[] userIds = mUserProfiles.getCurrentProfileIds();
331
332 for (int userId : userIds) {
333 if (enabled) {
334 registerServiceLocked(component, userId);
335 } else {
336 unregisterServiceLocked(component, userId);
337 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500338 }
Chris Wren51017d02015-12-15 15:34:46 -0500339 }
340 }
341
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800342 public void setCategoryState(String category, boolean enabled) {
343 synchronized (mMutex) {
344 final Boolean previous = mCategoryEnabled.put(category, enabled);
345 if (!(previous == null || previous != enabled)) {
346 return;
347 }
348
349 // State changed
350 if (DEBUG) {
351 Slog.d(TAG, ((enabled) ? "Enabling " : "Disabling ") + "category " + category);
352 }
353
354 final int[] userIds = mUserProfiles.getCurrentProfileIds();
355 for (int userId : userIds) {
356 final Set<ComponentName> componentNames = queryPackageForServices(null,
357 userId, category);
358
359 // Disallow services not enabled in Settings
360 final ArraySet<ComponentName> userComponents =
361 loadComponentNamesFromSetting(mConfig.secureSettingName, userId);
362 if (userComponents == null) {
363 componentNames.clear();
364 } else {
365 componentNames.retainAll(userComponents);
366 }
367
368 if (DEBUG) {
369 Slog.d(TAG, "Components for category " + category + ": " + componentNames);
370 }
371 for (ComponentName c : componentNames) {
372 if (enabled) {
373 registerServiceLocked(c, userId);
374 } else {
375 unregisterServiceLocked(c, userId);
376 }
377 }
378 }
379
380 }
381 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200382
383 private void rebuildRestoredPackages() {
384 mRestoredPackages.clear();
Chris Wrenab41eec2016-01-04 18:01:27 -0500385 mSnoozingForCurrentProfiles.clear();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200386 String settingName = restoredSettingName(mConfig);
John Spurlock7340fc82014-04-24 18:50:12 -0400387 int[] userIds = mUserProfiles.getCurrentProfileIds();
388 final int N = userIds.length;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200389 for (int i = 0; i < N; ++i) {
390 ArraySet<ComponentName> names = loadComponentNamesFromSetting(settingName, userIds[i]);
391 if (names == null)
392 continue;
393 for (ComponentName name: names) {
394 mRestoredPackages.add(name.getPackageName());
395 }
John Spurlock7340fc82014-04-24 18:50:12 -0400396 }
397 }
398
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200399
Julia Reynoldsc279b992015-10-30 08:23:51 -0400400 protected ArraySet<ComponentName> loadComponentNamesFromSetting(String settingName,
401 int userId) {
Christopher Tate6597e342015-02-17 12:15:25 -0800402 final ContentResolver cr = mContext.getContentResolver();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200403 String settingValue = Settings.Secure.getStringForUser(
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800404 cr,
405 settingName,
406 userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200407 if (TextUtils.isEmpty(settingValue))
408 return null;
409 String[] restored = settingValue.split(ENABLED_SERVICES_SEPARATOR);
410 ArraySet<ComponentName> result = new ArraySet<>(restored.length);
411 for (int i = 0; i < restored.length; i++) {
412 ComponentName value = ComponentName.unflattenFromString(restored[i]);
413 if (null != value) {
414 result.add(value);
Christopher Tate6597e342015-02-17 12:15:25 -0800415 }
416 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200417 return result;
418 }
John Spurlock7340fc82014-04-24 18:50:12 -0400419
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200420 private void storeComponentsToSetting(Set<ComponentName> components,
421 String settingName,
422 int userId) {
423 String[] componentNames = null;
424 if (null != components) {
425 componentNames = new String[components.size()];
426 int index = 0;
427 for (ComponentName c: components) {
428 componentNames[index++] = c.flattenToString();
429 }
430 }
431 final String value = (componentNames == null) ? "" :
432 TextUtils.join(ENABLED_SERVICES_SEPARATOR, componentNames);
433 final ContentResolver cr = mContext.getContentResolver();
434 Settings.Secure.putStringForUser(
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800435 cr,
436 settingName,
437 value,
438 userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200439 }
440
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200441 /**
442 * Remove access for any services that no longer exist.
443 */
444 private void updateSettingsAccordingToInstalledServices() {
445 int[] userIds = mUserProfiles.getCurrentProfileIds();
446 final int N = userIds.length;
447 for (int i = 0; i < N; ++i) {
448 updateSettingsAccordingToInstalledServices(userIds[i]);
449 }
450 rebuildRestoredPackages();
451 }
452
Julia Reynoldsc279b992015-10-30 08:23:51 -0400453 protected Set<ComponentName> queryPackageForServices(String packageName, int userId) {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800454 return queryPackageForServices(packageName, userId, null);
455 }
456
Chris Wren0efdb882016-03-01 17:17:47 -0500457 public Set<ComponentName> queryPackageForServices(String packageName, int userId,
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800458 String category) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200459 Set<ComponentName> installed = new ArraySet<>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200460 final PackageManager pm = mContext.getPackageManager();
Julia Reynoldsc279b992015-10-30 08:23:51 -0400461 Intent queryIntent = new Intent(mConfig.serviceInterface);
462 if (!TextUtils.isEmpty(packageName)) {
463 queryIntent.setPackage(packageName);
464 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800465 if (category != null) {
466 queryIntent.addCategory(category);
467 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200468 List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
Julia Reynoldsc279b992015-10-30 08:23:51 -0400469 queryIntent,
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200470 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
471 userId);
472 if (DEBUG)
473 Slog.v(TAG, mConfig.serviceInterface + " services: " + installedServices);
Julia Reynolds50f05142015-10-30 17:03:59 -0400474 if (installedServices != null) {
475 for (int i = 0, count = installedServices.size(); i < count; i++) {
476 ResolveInfo resolveInfo = installedServices.get(i);
477 ServiceInfo info = resolveInfo.serviceInfo;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200478
Julia Reynolds50f05142015-10-30 17:03:59 -0400479 ComponentName component = new ComponentName(info.packageName, info.name);
480 if (!mConfig.bindPermission.equals(info.permission)) {
481 Slog.w(TAG, "Skipping " + getCaption() + " service "
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800482 + info.packageName + "/" + info.name
483 + ": it does not require the permission "
484 + mConfig.bindPermission);
Julia Reynolds50f05142015-10-30 17:03:59 -0400485 continue;
486 }
487 installed.add(component);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200488 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200489 }
Julia Reynoldsc279b992015-10-30 08:23:51 -0400490 return installed;
491 }
492
493 private void updateSettingsAccordingToInstalledServices(int userId) {
494 boolean restoredChanged = false;
495 boolean currentChanged = false;
496 Set<ComponentName> restored =
497 loadComponentNamesFromSetting(restoredSettingName(mConfig), userId);
498 Set<ComponentName> current =
499 loadComponentNamesFromSetting(mConfig.secureSettingName, userId);
500 // Load all services for all packages.
501 Set<ComponentName> installed = queryPackageForServices(null, userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200502
503 ArraySet<ComponentName> retained = new ArraySet<>();
504
505 for (ComponentName component : installed) {
506 if (null != restored) {
507 boolean wasRestored = restored.remove(component);
508 if (wasRestored) {
509 // Freshly installed package has service that was mentioned in restored setting.
510 if (DEBUG)
511 Slog.v(TAG, "Restoring " + component + " for user " + userId);
512 restoredChanged = true;
513 currentChanged = true;
514 retained.add(component);
John Spurlock7340fc82014-04-24 18:50:12 -0400515 continue;
516 }
John Spurlock7340fc82014-04-24 18:50:12 -0400517 }
518
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200519 if (null != current) {
520 if (current.contains(component))
521 retained.add(component);
John Spurlock7340fc82014-04-24 18:50:12 -0400522 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200523 }
524
525 currentChanged |= ((current == null ? 0 : current.size()) != retained.size());
526
527 if (currentChanged) {
528 if (DEBUG) Slog.v(TAG, "List of " + getCaption() + " services was updated " + current);
529 storeComponentsToSetting(retained, mConfig.secureSettingName, userId);
530 }
531
532 if (restoredChanged) {
533 if (DEBUG) Slog.v(TAG,
534 "List of " + getCaption() + " restored services was updated " + restored);
535 storeComponentsToSetting(restored, restoredSettingName(mConfig), userId);
John Spurlock7340fc82014-04-24 18:50:12 -0400536 }
537 }
538
539 /**
540 * Called whenever packages change, the user switches, or the secure setting
541 * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
542 */
543 private void rebindServices() {
544 if (DEBUG) Slog.d(TAG, "rebindServices");
545 final int[] userIds = mUserProfiles.getCurrentProfileIds();
546 final int nUserIds = userIds.length;
547
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200548 final SparseArray<ArraySet<ComponentName>> componentsByUser = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400549
550 for (int i = 0; i < nUserIds; ++i) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200551 componentsByUser.put(userIds[i],
552 loadComponentNamesFromSetting(mConfig.secureSettingName, userIds[i]));
John Spurlock7340fc82014-04-24 18:50:12 -0400553 }
554
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500555 final ArrayList<ManagedServiceInfo> removableBoundServices = new ArrayList<>();
556 final SparseArray<Set<ComponentName>> toAdd = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400557
558 synchronized (mMutex) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500559 // Potentially unbind automatically bound services, retain system services.
Christoph Studer5d423842014-05-23 13:15:54 +0200560 for (ManagedServiceInfo service : mServices) {
Chris Wren51017d02015-12-15 15:34:46 -0500561 if (!service.isSystem && !service.isGuest(this)) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500562 removableBoundServices.add(service);
Christoph Studer5d423842014-05-23 13:15:54 +0200563 }
564 }
John Spurlock7340fc82014-04-24 18:50:12 -0400565
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200566 final ArraySet<ComponentName> newEnabled = new ArraySet<>();
567 final ArraySet<String> newPackages = new ArraySet<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400568
569 for (int i = 0; i < nUserIds; ++i) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200570 // decode the list of components
571 final ArraySet<ComponentName> userComponents = componentsByUser.get(userIds[i]);
572 if (null == userComponents) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500573 toAdd.put(userIds[i], new HashSet<ComponentName>());
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200574 continue;
575 }
576
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500577 final Set<ComponentName> add = new HashSet<>(userComponents);
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800578
579 // Remove components from disabled categories so that those services aren't run.
580 for (Entry<String, Boolean> e : mCategoryEnabled.entrySet()) {
581 if (!e.getValue()) {
582 Set<ComponentName> c = queryPackageForServices(null, userIds[i],
583 e.getKey());
584 add.removeAll(c);
585 }
586 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500587 add.removeAll(mSnoozingForCurrentProfiles);
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800588
John Spurlock7340fc82014-04-24 18:50:12 -0400589 toAdd.put(userIds[i], add);
590
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200591 newEnabled.addAll(userComponents);
John Spurlock7340fc82014-04-24 18:50:12 -0400592
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200593 for (int j = 0; j < userComponents.size(); j++) {
Chris Wren083094c2015-12-15 16:25:07 -0500594 final ComponentName component = userComponents.valueAt(j);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200595 newPackages.add(component.getPackageName());
John Spurlock7340fc82014-04-24 18:50:12 -0400596 }
597 }
598 mEnabledServicesForCurrentProfiles = newEnabled;
599 mEnabledServicesPackageNames = newPackages;
600 }
601
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500602 for (ManagedServiceInfo info : removableBoundServices) {
John Spurlock7340fc82014-04-24 18:50:12 -0400603 final ComponentName component = info.component;
604 final int oldUser = info.userid;
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500605 final Set<ComponentName> allowedComponents = toAdd.get(info.userid);
606 if (allowedComponents != null) {
607 if (allowedComponents.contains(component)) {
608 // Already bound, don't need to bind again.
609 allowedComponents.remove(component);
610 } else {
611 // No longer allowed to be bound.
612 Slog.v(TAG, "disabling " + getCaption() + " for user "
613 + oldUser + ": " + component);
614 unregisterService(component, oldUser);
615 }
616 }
John Spurlock7340fc82014-04-24 18:50:12 -0400617 }
618
619 for (int i = 0; i < nUserIds; ++i) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500620 final Set<ComponentName> add = toAdd.get(userIds[i]);
621 for (ComponentName component : add) {
John Spurlock7340fc82014-04-24 18:50:12 -0400622 Slog.v(TAG, "enabling " + getCaption() + " for user " + userIds[i] + ": "
623 + component);
624 registerService(component, userIds[i]);
625 }
626 }
Christoph Studerb53dfd42014-09-12 14:45:59 +0200627
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200628 mLastSeenProfileIds = userIds;
John Spurlock7340fc82014-04-24 18:50:12 -0400629 }
630
631 /**
632 * Version of registerService that takes the name of a service component to bind to.
633 */
634 private void registerService(final ComponentName name, final int userid) {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800635 synchronized (mMutex) {
636 registerServiceLocked(name, userid);
637 }
638 }
639
Chris Wren0efdb882016-03-01 17:17:47 -0500640 /**
641 * Inject a system service into the management list.
642 */
643 public void registerSystemService(final ComponentName name, final int userid) {
644 synchronized (mMutex) {
645 registerServiceLocked(name, userid, true /* isSystem */);
646 }
647 }
648
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800649 private void registerServiceLocked(final ComponentName name, final int userid) {
Chris Wren0efdb882016-03-01 17:17:47 -0500650 registerServiceLocked(name, userid, false /* isSystem */);
651 }
652
653 private void registerServiceLocked(final ComponentName name, final int userid,
654 final boolean isSystem) {
John Spurlock7340fc82014-04-24 18:50:12 -0400655 if (DEBUG) Slog.v(TAG, "registerService: " + name + " u=" + userid);
656
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800657 final String servicesBindingTag = name.toString() + "/" + userid;
658 if (mServicesBinding.contains(servicesBindingTag)) {
659 // stop registering this thing already! we're working on it
660 return;
661 }
662 mServicesBinding.add(servicesBindingTag);
John Spurlock7340fc82014-04-24 18:50:12 -0400663
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800664 final int N = mServices.size();
665 for (int i = N - 1; i >= 0; i--) {
666 final ManagedServiceInfo info = mServices.get(i);
667 if (name.equals(info.component)
668 && info.userid == userid) {
669 // cut old connections
670 if (DEBUG) Slog.v(TAG, " disconnecting old " + getCaption() + ": "
671 + info.service);
672 removeServiceLocked(i);
673 if (info.connection != null) {
674 mContext.unbindService(info.connection);
John Spurlock7340fc82014-04-24 18:50:12 -0400675 }
676 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800677 }
John Spurlock7340fc82014-04-24 18:50:12 -0400678
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800679 Intent intent = new Intent(mConfig.serviceInterface);
680 intent.setComponent(name);
John Spurlock7340fc82014-04-24 18:50:12 -0400681
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800682 intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel);
John Spurlock7340fc82014-04-24 18:50:12 -0400683
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800684 final PendingIntent pendingIntent = PendingIntent.getActivity(
685 mContext, 0, new Intent(mConfig.settingsAction), 0);
686 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
John Spurlock7340fc82014-04-24 18:50:12 -0400687
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800688 ApplicationInfo appInfo = null;
689 try {
690 appInfo = mContext.getPackageManager().getApplicationInfo(
691 name.getPackageName(), 0);
692 } catch (NameNotFoundException e) {
693 // Ignore if the package doesn't exist we won't be able to bind to the service.
694 }
695 final int targetSdkVersion =
696 appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE;
John Spurlock7340fc82014-04-24 18:50:12 -0400697
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800698 try {
699 if (DEBUG) Slog.v(TAG, "binding: " + intent);
700 ServiceConnection serviceConnection = new ServiceConnection() {
701 IInterface mService;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200702
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800703 @Override
704 public void onServiceConnected(ComponentName name, IBinder binder) {
705 boolean added = false;
706 ManagedServiceInfo info = null;
707 synchronized (mMutex) {
708 mServicesBinding.remove(servicesBindingTag);
709 try {
710 mService = asInterface(binder);
711 info = newServiceInfo(mService, name,
Chris Wren0efdb882016-03-01 17:17:47 -0500712 userid, isSystem, this, targetSdkVersion);
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800713 binder.linkToDeath(info, 0);
714 added = mServices.add(info);
715 } catch (RemoteException e) {
716 // already dead
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200717 }
718 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800719 if (added) {
720 onServiceAdded(info);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200721 }
John Spurlock7340fc82014-04-24 18:50:12 -0400722 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800723
724 @Override
725 public void onServiceDisconnected(ComponentName name) {
726 Slog.v(TAG, getCaption() + " connection lost: " + name);
727 }
728 };
729 if (!mContext.bindServiceAsUser(intent,
730 serviceConnection,
731 Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE,
732 new UserHandle(userid))) {
733 mServicesBinding.remove(servicesBindingTag);
734 Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent);
John Spurlock7340fc82014-04-24 18:50:12 -0400735 return;
736 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800737 } catch (SecurityException ex) {
738 Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex);
739 return;
John Spurlock7340fc82014-04-24 18:50:12 -0400740 }
741 }
742
743 /**
744 * Remove a service for the given user by ComponentName
745 */
746 private void unregisterService(ComponentName name, int userid) {
747 synchronized (mMutex) {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800748 unregisterServiceLocked(name, userid);
749 }
750 }
751
752 private void unregisterServiceLocked(ComponentName name, int userid) {
753 final int N = mServices.size();
754 for (int i = N - 1; i >= 0; i--) {
755 final ManagedServiceInfo info = mServices.get(i);
756 if (name.equals(info.component)
757 && info.userid == userid) {
758 removeServiceLocked(i);
759 if (info.connection != null) {
760 try {
761 mContext.unbindService(info.connection);
762 } catch (IllegalArgumentException ex) {
763 // something happened to the service: we think we have a connection
764 // but it's bogus.
765 Slog.e(TAG, getCaption() + " " + name + " could not be unbound: " + ex);
John Spurlock7340fc82014-04-24 18:50:12 -0400766 }
767 }
768 }
769 }
770 }
771
772 /**
773 * Removes a service from the list but does not unbind
774 *
775 * @return the removed service.
776 */
777 private ManagedServiceInfo removeServiceImpl(IInterface service, final int userid) {
John Spurlocke77bb362014-04-26 10:24:59 -0400778 if (DEBUG) Slog.d(TAG, "removeServiceImpl service=" + service + " u=" + userid);
John Spurlock7340fc82014-04-24 18:50:12 -0400779 ManagedServiceInfo serviceInfo = null;
780 synchronized (mMutex) {
781 final int N = mServices.size();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200782 for (int i = N - 1; i >= 0; i--) {
John Spurlock7340fc82014-04-24 18:50:12 -0400783 final ManagedServiceInfo info = mServices.get(i);
784 if (info.service.asBinder() == service.asBinder()
785 && info.userid == userid) {
John Spurlocke77bb362014-04-26 10:24:59 -0400786 if (DEBUG) Slog.d(TAG, "Removing active service " + info.component);
787 serviceInfo = removeServiceLocked(i);
John Spurlock7340fc82014-04-24 18:50:12 -0400788 }
789 }
790 }
791 return serviceInfo;
792 }
793
John Spurlocke77bb362014-04-26 10:24:59 -0400794 private ManagedServiceInfo removeServiceLocked(int i) {
795 final ManagedServiceInfo info = mServices.remove(i);
796 onServiceRemovedLocked(info);
797 return info;
798 }
799
John Spurlock7340fc82014-04-24 18:50:12 -0400800 private void checkNotNull(IInterface service) {
801 if (service == null) {
802 throw new IllegalArgumentException(getCaption() + " must not be null");
803 }
804 }
805
Christoph Studer3e144d32014-05-22 16:48:40 +0200806 private ManagedServiceInfo registerServiceImpl(final IInterface service,
John Spurlock7340fc82014-04-24 18:50:12 -0400807 final ComponentName component, final int userid) {
Chris Wren51017d02015-12-15 15:34:46 -0500808 ManagedServiceInfo info = newServiceInfo(service, component, userid,
809 true /*isSystem*/, null /*connection*/, Build.VERSION_CODES.LOLLIPOP);
810 return registerServiceImpl(info);
811 }
812
813 private ManagedServiceInfo registerServiceImpl(ManagedServiceInfo info) {
John Spurlock7340fc82014-04-24 18:50:12 -0400814 synchronized (mMutex) {
815 try {
Chris Wren51017d02015-12-15 15:34:46 -0500816 info.service.asBinder().linkToDeath(info, 0);
John Spurlock7340fc82014-04-24 18:50:12 -0400817 mServices.add(info);
Christoph Studer3e144d32014-05-22 16:48:40 +0200818 return info;
John Spurlock7340fc82014-04-24 18:50:12 -0400819 } catch (RemoteException e) {
820 // already dead
821 }
822 }
Christoph Studer3e144d32014-05-22 16:48:40 +0200823 return null;
John Spurlock7340fc82014-04-24 18:50:12 -0400824 }
825
826 /**
827 * Removes a service from the list and unbinds.
828 */
829 private void unregisterServiceImpl(IInterface service, int userid) {
830 ManagedServiceInfo info = removeServiceImpl(service, userid);
Chris Wren51017d02015-12-15 15:34:46 -0500831 if (info != null && info.connection != null && !info.isGuest(this)) {
John Spurlock7340fc82014-04-24 18:50:12 -0400832 mContext.unbindService(info.connection);
833 }
834 }
835
836 private class SettingsObserver extends ContentObserver {
837 private final Uri mSecureSettingsUri = Settings.Secure.getUriFor(mConfig.secureSettingName);
838
839 private SettingsObserver(Handler handler) {
840 super(handler);
841 }
842
843 private void observe() {
844 ContentResolver resolver = mContext.getContentResolver();
845 resolver.registerContentObserver(mSecureSettingsUri,
846 false, this, UserHandle.USER_ALL);
847 update(null);
848 }
849
850 @Override
851 public void onChange(boolean selfChange, Uri uri) {
852 update(uri);
853 }
854
855 private void update(Uri uri) {
856 if (uri == null || mSecureSettingsUri.equals(uri)) {
Christoph Studerb53dfd42014-09-12 14:45:59 +0200857 if (DEBUG) Slog.d(TAG, "Setting changed: mSecureSettingsUri=" + mSecureSettingsUri +
858 " / uri=" + uri);
John Spurlock7340fc82014-04-24 18:50:12 -0400859 rebindServices();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200860 rebuildRestoredPackages();
John Spurlock7340fc82014-04-24 18:50:12 -0400861 }
862 }
863 }
864
865 public class ManagedServiceInfo implements IBinder.DeathRecipient {
866 public IInterface service;
867 public ComponentName component;
868 public int userid;
869 public boolean isSystem;
870 public ServiceConnection connection;
871 public int targetSdkVersion;
872
873 public ManagedServiceInfo(IInterface service, ComponentName component,
874 int userid, boolean isSystem, ServiceConnection connection, int targetSdkVersion) {
875 this.service = service;
876 this.component = component;
877 this.userid = userid;
878 this.isSystem = isSystem;
879 this.connection = connection;
880 this.targetSdkVersion = targetSdkVersion;
881 }
882
Chris Wren51017d02015-12-15 15:34:46 -0500883 public boolean isGuest(ManagedServices host) {
884 return ManagedServices.this != host;
885 }
886
Chris Wrenab41eec2016-01-04 18:01:27 -0500887 public ManagedServices getOwner() {
888 return ManagedServices.this;
889 }
890
John Spurlocke77bb362014-04-26 10:24:59 -0400891 @Override
892 public String toString() {
893 return new StringBuilder("ManagedServiceInfo[")
894 .append("component=").append(component)
895 .append(",userid=").append(userid)
896 .append(",isSystem=").append(isSystem)
897 .append(",targetSdkVersion=").append(targetSdkVersion)
898 .append(",connection=").append(connection == null ? null : "<connection>")
899 .append(",service=").append(service)
900 .append(']').toString();
901 }
902
John Spurlock7340fc82014-04-24 18:50:12 -0400903 public boolean enabledAndUserMatches(int nid) {
904 if (!isEnabledForCurrentProfiles()) {
905 return false;
906 }
907 if (this.userid == UserHandle.USER_ALL) return true;
Chris Wren0efdb882016-03-01 17:17:47 -0500908 if (this.userid == UserHandle.USER_SYSTEM) return true;
John Spurlock7340fc82014-04-24 18:50:12 -0400909 if (nid == UserHandle.USER_ALL || nid == this.userid) return true;
910 return supportsProfiles() && mUserProfiles.isCurrentProfile(nid);
911 }
912
913 public boolean supportsProfiles() {
Dianne Hackborn955d8d62014-10-07 20:17:19 -0700914 return targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
John Spurlock7340fc82014-04-24 18:50:12 -0400915 }
916
917 @Override
918 public void binderDied() {
John Spurlocke77bb362014-04-26 10:24:59 -0400919 if (DEBUG) Slog.d(TAG, "binderDied");
John Spurlock7340fc82014-04-24 18:50:12 -0400920 // Remove the service, but don't unbind from the service. The system will bring the
921 // service back up, and the onServiceConnected handler will readd the service with the
922 // new binding. If this isn't a bound service, and is just a registered
923 // service, just removing it from the list is all we need to do anyway.
924 removeServiceImpl(this.service, this.userid);
925 }
926
927 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
928 public boolean isEnabledForCurrentProfiles() {
929 if (this.isSystem) return true;
930 if (this.connection == null) return false;
931 return mEnabledServicesForCurrentProfiles.contains(this.component);
932 }
933 }
934
Chris Wrenab41eec2016-01-04 18:01:27 -0500935 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
936 public boolean isComponentEnabledForCurrentProfiles(ComponentName component) {
937 return mEnabledServicesForCurrentProfiles.contains(component);
938 }
939
John Spurlock7340fc82014-04-24 18:50:12 -0400940 public static class UserProfiles {
941 // Profiles of the current user.
942 private final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<UserInfo>();
943
944 public void updateCache(Context context) {
945 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
946 if (userManager != null) {
947 int currentUserId = ActivityManager.getCurrentUser();
948 List<UserInfo> profiles = userManager.getProfiles(currentUserId);
949 synchronized (mCurrentProfiles) {
950 mCurrentProfiles.clear();
951 for (UserInfo user : profiles) {
952 mCurrentProfiles.put(user.id, user);
953 }
954 }
955 }
956 }
957
958 public int[] getCurrentProfileIds() {
959 synchronized (mCurrentProfiles) {
960 int[] users = new int[mCurrentProfiles.size()];
961 final int N = mCurrentProfiles.size();
962 for (int i = 0; i < N; ++i) {
963 users[i] = mCurrentProfiles.keyAt(i);
964 }
965 return users;
966 }
967 }
968
969 public boolean isCurrentProfile(int userId) {
970 synchronized (mCurrentProfiles) {
971 return mCurrentProfiles.get(userId) != null;
972 }
973 }
974 }
975
976 protected static class Config {
977 String caption;
978 String serviceInterface;
979 String secureSettingName;
980 String bindPermission;
981 String settingsAction;
982 int clientLabel;
983 }
984}