blob: 17313b685ae9cdb9dd7ab5e958217f500943bae2 [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
Ruben Brunke24b9a62016-02-16 21:38:24 -080019import android.annotation.NonNull;
John Spurlock7340fc82014-04-24 18:50:12 -040020import android.app.ActivityManager;
21import android.app.PendingIntent;
Christopher Tate6597e342015-02-17 12:15:25 -080022import android.content.BroadcastReceiver;
John Spurlock7340fc82014-04-24 18:50:12 -040023import android.content.ComponentName;
24import android.content.ContentResolver;
25import android.content.Context;
26import android.content.Intent;
Christopher Tate6597e342015-02-17 12:15:25 -080027import android.content.IntentFilter;
John Spurlock7340fc82014-04-24 18:50:12 -040028import android.content.ServiceConnection;
29import android.content.pm.ApplicationInfo;
30import android.content.pm.PackageManager;
31import android.content.pm.PackageManager.NameNotFoundException;
32import android.content.pm.ResolveInfo;
33import android.content.pm.ServiceInfo;
34import android.content.pm.UserInfo;
35import android.database.ContentObserver;
36import android.net.Uri;
37import android.os.Build;
38import android.os.Handler;
39import android.os.IBinder;
40import android.os.IInterface;
41import android.os.RemoteException;
42import android.os.UserHandle;
43import android.os.UserManager;
44import android.provider.Settings;
45import android.text.TextUtils;
Ruben Brunkdd18a0b2015-12-04 16:16:31 -080046import android.util.ArrayMap;
John Spurlock7340fc82014-04-24 18:50:12 -040047import android.util.ArraySet;
John Spurlock4db0d982014-08-13 09:19:03 -040048import android.util.Log;
John Spurlock7340fc82014-04-24 18:50:12 -040049import android.util.Slog;
50import android.util.SparseArray;
51
John Spurlock25e2d242014-06-27 13:58:23 -040052import com.android.server.notification.NotificationManagerService.DumpFilter;
53
John Spurlock7340fc82014-04-24 18:50:12 -040054import java.io.PrintWriter;
55import java.util.ArrayList;
John Spurlocke77bb362014-04-26 10:24:59 -040056import java.util.Arrays;
Julia Reynolds9a86cc02016-02-10 15:38:15 -050057import java.util.HashSet;
John Spurlock7340fc82014-04-24 18:50:12 -040058import java.util.List;
Ruben Brunkdd18a0b2015-12-04 16:16:31 -080059import java.util.Map.Entry;
Christopher Tate6597e342015-02-17 12:15:25 -080060import java.util.Objects;
John Spurlock7340fc82014-04-24 18:50:12 -040061import java.util.Set;
62
63/**
64 * Manages the lifecycle of application-provided services bound by system server.
65 *
66 * Services managed by this helper must have:
67 * - An associated system settings value with a list of enabled component names.
68 * - A well-known action for services to use in their intent-filter.
69 * - A system permission for services to require in order to ensure system has exclusive binding.
70 * - A settings page for user configuration of enabled services, and associated intent action.
71 * - A remote interface definition (aidl) provided by the service used for communication.
72 */
73abstract public class ManagedServices {
74 protected final String TAG = getClass().getSimpleName();
John Spurlock4db0d982014-08-13 09:19:03 -040075 protected final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
John Spurlock7340fc82014-04-24 18:50:12 -040076
Julia Reynoldsc279b992015-10-30 08:23:51 -040077 protected static final String ENABLED_SERVICES_SEPARATOR = ":";
John Spurlock7340fc82014-04-24 18:50:12 -040078
John Spurlockaf8d6c42014-05-07 17:49:08 -040079 protected final Context mContext;
John Spurlocke77bb362014-04-26 10:24:59 -040080 protected final Object mMutex;
John Spurlock7340fc82014-04-24 18:50:12 -040081 private final UserProfiles mUserProfiles;
82 private final SettingsObserver mSettingsObserver;
83 private final Config mConfig;
Christopher Tate6597e342015-02-17 12:15:25 -080084 private ArraySet<String> mRestored;
John Spurlock7340fc82014-04-24 18:50:12 -040085
86 // contains connections to all connected services, including app services
87 // and system services
88 protected final ArrayList<ManagedServiceInfo> mServices = new ArrayList<ManagedServiceInfo>();
89 // things that will be put into mServices as soon as they're ready
90 private final ArrayList<String> mServicesBinding = new ArrayList<String>();
Chris Wrenab41eec2016-01-04 18:01:27 -050091 // lists the component names of all enabled (and therefore potentially connected)
John Spurlock7340fc82014-04-24 18:50:12 -040092 // app services for current profiles.
93 private ArraySet<ComponentName> mEnabledServicesForCurrentProfiles
94 = new ArraySet<ComponentName>();
95 // Just the packages from mEnabledServicesForCurrentProfiles
96 private ArraySet<String> mEnabledServicesPackageNames = new ArraySet<String>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +020097 // List of packages in restored setting across all mUserProfiles, for quick
98 // filtering upon package updates.
99 private ArraySet<String> mRestoredPackages = new ArraySet<>();
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800100 // State of current service categories
101 private ArrayMap<String, Boolean> mCategoryEnabled = new ArrayMap<>();
Chris Wrenab41eec2016-01-04 18:01:27 -0500102 // List of enabled packages that have nevertheless asked not to be run
103 private ArraySet<ComponentName> mSnoozingForCurrentProfiles = new ArraySet<>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200104
John Spurlock7340fc82014-04-24 18:50:12 -0400105
Christoph Studerb53dfd42014-09-12 14:45:59 +0200106 // Kept to de-dupe user change events (experienced after boot, when we receive a settings and a
107 // user change).
108 private int[] mLastSeenProfileIds;
109
Christopher Tate6597e342015-02-17 12:15:25 -0800110 private final BroadcastReceiver mRestoreReceiver;
111
John Spurlock7340fc82014-04-24 18:50:12 -0400112 public ManagedServices(Context context, Handler handler, Object mutex,
113 UserProfiles userProfiles) {
114 mContext = context;
115 mMutex = mutex;
116 mUserProfiles = userProfiles;
117 mConfig = getConfig();
118 mSettingsObserver = new SettingsObserver(handler);
Christopher Tate6597e342015-02-17 12:15:25 -0800119
120 mRestoreReceiver = new SettingRestoredReceiver();
121 IntentFilter filter = new IntentFilter(Intent.ACTION_SETTING_RESTORED);
122 context.registerReceiver(mRestoreReceiver, filter);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200123 rebuildRestoredPackages();
Christopher Tate6597e342015-02-17 12:15:25 -0800124 }
125
126 class SettingRestoredReceiver extends BroadcastReceiver {
127 @Override
128 public void onReceive(Context context, Intent intent) {
129 if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) {
130 String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
131 if (Objects.equals(element, mConfig.secureSettingName)) {
132 String prevValue = intent.getStringExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE);
133 String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE);
134 settingRestored(element, prevValue, newValue, getSendingUserId());
135 }
136 }
137 }
John Spurlock7340fc82014-04-24 18:50:12 -0400138 }
139
140 abstract protected Config getConfig();
141
142 private String getCaption() {
143 return mConfig.caption;
144 }
145
146 abstract protected IInterface asInterface(IBinder binder);
147
Chris Wren51017d02015-12-15 15:34:46 -0500148 abstract protected boolean checkType(IInterface service);
149
John Spurlock3b98b3f2014-05-01 09:08:48 -0400150 abstract protected void onServiceAdded(ManagedServiceInfo info);
John Spurlock7340fc82014-04-24 18:50:12 -0400151
John Spurlocke77bb362014-04-26 10:24:59 -0400152 protected void onServiceRemovedLocked(ManagedServiceInfo removed) { }
153
John Spurlock7340fc82014-04-24 18:50:12 -0400154 private ManagedServiceInfo newServiceInfo(IInterface service,
155 ComponentName component, int userid, boolean isSystem, ServiceConnection connection,
156 int targetSdkVersion) {
157 return new ManagedServiceInfo(service, component, userid, isSystem, connection,
158 targetSdkVersion);
159 }
160
161 public void onBootPhaseAppsCanStart() {
162 mSettingsObserver.observe();
163 }
164
John Spurlock25e2d242014-06-27 13:58:23 -0400165 public void dump(PrintWriter pw, DumpFilter filter) {
John Spurlocke77bb362014-04-26 10:24:59 -0400166 pw.println(" All " + getCaption() + "s (" + mEnabledServicesForCurrentProfiles.size()
John Spurlock7340fc82014-04-24 18:50:12 -0400167 + ") enabled for current profiles:");
168 for (ComponentName cmpt : mEnabledServicesForCurrentProfiles) {
John Spurlock25e2d242014-06-27 13:58:23 -0400169 if (filter != null && !filter.matches(cmpt)) continue;
John Spurlocke77bb362014-04-26 10:24:59 -0400170 pw.println(" " + cmpt);
John Spurlock7340fc82014-04-24 18:50:12 -0400171 }
172
John Spurlocke77bb362014-04-26 10:24:59 -0400173 pw.println(" Live " + getCaption() + "s (" + mServices.size() + "):");
John Spurlock7340fc82014-04-24 18:50:12 -0400174 for (ManagedServiceInfo info : mServices) {
John Spurlock25e2d242014-06-27 13:58:23 -0400175 if (filter != null && !filter.matches(info.component)) continue;
John Spurlocke77bb362014-04-26 10:24:59 -0400176 pw.println(" " + info.component
John Spurlock7340fc82014-04-24 18:50:12 -0400177 + " (user " + info.userid + "): " + info.service
Chris Wren51017d02015-12-15 15:34:46 -0500178 + (info.isSystem?" SYSTEM":"")
179 + (info.isGuest(this)?" GUEST":""));
John Spurlock7340fc82014-04-24 18:50:12 -0400180 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500181
182 pw.println(" Snoozed " + getCaption() + "s (" +
183 mSnoozingForCurrentProfiles.size() + "):");
184 for (ComponentName name : mSnoozingForCurrentProfiles) {
185 pw.println(" " + name.flattenToShortString());
186 }
John Spurlock7340fc82014-04-24 18:50:12 -0400187 }
188
Christopher Tate6597e342015-02-17 12:15:25 -0800189 // By convention, restored settings are replicated to another settings
190 // entry, named similarly but with a disambiguation suffix.
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200191 public static String restoredSettingName(Config config) {
Christopher Tate6597e342015-02-17 12:15:25 -0800192 return config.secureSettingName + ":restored";
193 }
194
195 // The OS has done a restore of this service's saved state. We clone it to the
196 // 'restored' reserve, and then once we return and the actual write to settings is
197 // performed, our observer will do the work of maintaining the restored vs live
198 // settings data.
199 public void settingRestored(String element, String oldValue, String newValue, int userid) {
200 if (DEBUG) Slog.d(TAG, "Restored managed service setting: " + element
201 + " ovalue=" + oldValue + " nvalue=" + newValue);
202 if (mConfig.secureSettingName.equals(element)) {
203 if (element != null) {
204 mRestored = null;
205 Settings.Secure.putStringForUser(mContext.getContentResolver(),
206 restoredSettingName(mConfig),
207 newValue,
208 userid);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200209 updateSettingsAccordingToInstalledServices(userid);
210 rebuildRestoredPackages();
Christopher Tate6597e342015-02-17 12:15:25 -0800211 }
212 }
213 }
214
John Spurlock80774932015-05-07 17:38:50 -0400215 public boolean isComponentEnabledForPackage(String pkg) {
216 return mEnabledServicesPackageNames.contains(pkg);
217 }
218
John Spurlock7340fc82014-04-24 18:50:12 -0400219 public void onPackagesChanged(boolean queryReplace, String[] pkgList) {
John Spurlocke77bb362014-04-26 10:24:59 -0400220 if (DEBUG) Slog.d(TAG, "onPackagesChanged queryReplace=" + queryReplace
221 + " pkgList=" + (pkgList == null ? null : Arrays.asList(pkgList))
222 + " mEnabledServicesPackageNames=" + mEnabledServicesPackageNames);
John Spurlock7340fc82014-04-24 18:50:12 -0400223 boolean anyServicesInvolved = false;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200224
John Spurlock7340fc82014-04-24 18:50:12 -0400225 if (pkgList != null && (pkgList.length > 0)) {
226 for (String pkgName : pkgList) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200227 if (mEnabledServicesPackageNames.contains(pkgName) ||
228 mRestoredPackages.contains(pkgName)) {
John Spurlock7340fc82014-04-24 18:50:12 -0400229 anyServicesInvolved = true;
230 }
231 }
232 }
233
234 if (anyServicesInvolved) {
235 // if we're not replacing a package, clean up orphaned bits
236 if (!queryReplace) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200237 updateSettingsAccordingToInstalledServices();
238 rebuildRestoredPackages();
John Spurlock7340fc82014-04-24 18:50:12 -0400239 }
240 // make sure we're still bound to any of our services who may have just upgraded
241 rebindServices();
242 }
243 }
244
John Spurlock1b8b22b2015-05-20 09:47:13 -0400245 public void onUserSwitched(int user) {
246 if (DEBUG) Slog.d(TAG, "onUserSwitched u=" + user);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200247 rebuildRestoredPackages();
Christoph Studerb53dfd42014-09-12 14:45:59 +0200248 if (Arrays.equals(mLastSeenProfileIds, mUserProfiles.getCurrentProfileIds())) {
249 if (DEBUG) Slog.d(TAG, "Current profile IDs didn't change, skipping rebindServices().");
250 return;
251 }
252 rebindServices();
253 }
254
Julia Reynoldsa3dcaff2016-02-03 15:04:05 -0500255 public void onUserUnlocked(int user) {
256 if (DEBUG) Slog.d(TAG, "onUserUnlocked u=" + user);
257 rebuildRestoredPackages();
258 rebindServices();
259 }
260
Chris Wrenab41eec2016-01-04 18:01:27 -0500261 public ManagedServiceInfo getServiceFromTokenLocked(IInterface service) {
262 if (service == null) {
263 return null;
264 }
John Spurlock7340fc82014-04-24 18:50:12 -0400265 final IBinder token = service.asBinder();
266 final int N = mServices.size();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200267 for (int i = 0; i < N; i++) {
John Spurlock7340fc82014-04-24 18:50:12 -0400268 final ManagedServiceInfo info = mServices.get(i);
269 if (info.service.asBinder() == token) return info;
270 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500271 return null;
272 }
273
274 public ManagedServiceInfo checkServiceTokenLocked(IInterface service) {
275 checkNotNull(service);
276 ManagedServiceInfo info = getServiceFromTokenLocked(service);
277 if (info != null) {
278 return info;
279 }
John Spurlock7340fc82014-04-24 18:50:12 -0400280 throw new SecurityException("Disallowed call from unknown " + getCaption() + ": "
281 + service);
282 }
283
284 public void unregisterService(IInterface service, int userid) {
285 checkNotNull(service);
286 // no need to check permissions; if your service binder is in the list,
287 // that's proof that you had permission to add it in the first place
288 unregisterServiceImpl(service, userid);
289 }
290
291 public void registerService(IInterface service, ComponentName component, int userid) {
292 checkNotNull(service);
Christoph Studer3e144d32014-05-22 16:48:40 +0200293 ManagedServiceInfo info = registerServiceImpl(service, component, userid);
294 if (info != null) {
295 onServiceAdded(info);
296 }
John Spurlock7340fc82014-04-24 18:50:12 -0400297 }
298
Chris Wren51017d02015-12-15 15:34:46 -0500299 /**
300 * Add a service to our callbacks. The lifecycle of this service is managed externally,
301 * but unlike a system service, it should not be considered privledged.
302 * */
303 public void registerGuestService(ManagedServiceInfo guest) {
304 checkNotNull(guest.service);
Ruben Brunke24b9a62016-02-16 21:38:24 -0800305 if (!checkType(guest.service)) {
306 throw new IllegalArgumentException();
307 }
Chris Wren51017d02015-12-15 15:34:46 -0500308 if (registerServiceImpl(guest) != null) {
309 onServiceAdded(guest);
Chris Wrenab41eec2016-01-04 18:01:27 -0500310 }
311 }
312
313 public void setComponentState(ComponentName component, boolean enabled) {
314 boolean previous = !mSnoozingForCurrentProfiles.contains(component);
315 if (previous == enabled) {
316 return;
317 }
318
319 if (enabled) {
320 mSnoozingForCurrentProfiles.remove(component);
321 } else {
322 mSnoozingForCurrentProfiles.add(component);
323 }
324
325 // State changed
326 if (DEBUG) {
327 Slog.d(TAG, ((enabled) ? "Enabling " : "Disabling ") + "component " +
328 component.flattenToShortString());
329 }
330
331 final int[] userIds = mUserProfiles.getCurrentProfileIds();
332 for (int userId : userIds) {
333 if (enabled) {
334 registerServiceLocked(component, userId);
335 } else {
336 unregisterServiceLocked(component, userId);
337 }
Chris Wren51017d02015-12-15 15:34:46 -0500338 }
339 }
340
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800341 public void setCategoryState(String category, boolean enabled) {
342 synchronized (mMutex) {
343 final Boolean previous = mCategoryEnabled.put(category, enabled);
344 if (!(previous == null || previous != enabled)) {
345 return;
346 }
347
348 // State changed
349 if (DEBUG) {
350 Slog.d(TAG, ((enabled) ? "Enabling " : "Disabling ") + "category " + category);
351 }
352
353 final int[] userIds = mUserProfiles.getCurrentProfileIds();
354 for (int userId : userIds) {
355 final Set<ComponentName> componentNames = queryPackageForServices(null,
356 userId, category);
357
358 // Disallow services not enabled in Settings
359 final ArraySet<ComponentName> userComponents =
360 loadComponentNamesFromSetting(mConfig.secureSettingName, userId);
361 if (userComponents == null) {
362 componentNames.clear();
363 } else {
364 componentNames.retainAll(userComponents);
365 }
366
367 if (DEBUG) {
368 Slog.d(TAG, "Components for category " + category + ": " + componentNames);
369 }
370 for (ComponentName c : componentNames) {
371 if (enabled) {
372 registerServiceLocked(c, userId);
373 } else {
374 unregisterServiceLocked(c, userId);
375 }
376 }
377 }
378
379 }
380 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200381
382 private void rebuildRestoredPackages() {
383 mRestoredPackages.clear();
Chris Wrenab41eec2016-01-04 18:01:27 -0500384 mSnoozingForCurrentProfiles.clear();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200385 String settingName = restoredSettingName(mConfig);
John Spurlock7340fc82014-04-24 18:50:12 -0400386 int[] userIds = mUserProfiles.getCurrentProfileIds();
387 final int N = userIds.length;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200388 for (int i = 0; i < N; ++i) {
389 ArraySet<ComponentName> names = loadComponentNamesFromSetting(settingName, userIds[i]);
390 if (names == null)
391 continue;
392 for (ComponentName name: names) {
393 mRestoredPackages.add(name.getPackageName());
394 }
John Spurlock7340fc82014-04-24 18:50:12 -0400395 }
396 }
397
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200398
Julia Reynoldsc279b992015-10-30 08:23:51 -0400399 protected ArraySet<ComponentName> loadComponentNamesFromSetting(String settingName,
400 int userId) {
Christopher Tate6597e342015-02-17 12:15:25 -0800401 final ContentResolver cr = mContext.getContentResolver();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200402 String settingValue = Settings.Secure.getStringForUser(
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800403 cr,
404 settingName,
405 userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200406 if (TextUtils.isEmpty(settingValue))
407 return null;
408 String[] restored = settingValue.split(ENABLED_SERVICES_SEPARATOR);
409 ArraySet<ComponentName> result = new ArraySet<>(restored.length);
410 for (int i = 0; i < restored.length; i++) {
411 ComponentName value = ComponentName.unflattenFromString(restored[i]);
412 if (null != value) {
413 result.add(value);
Christopher Tate6597e342015-02-17 12:15:25 -0800414 }
415 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200416 return result;
417 }
John Spurlock7340fc82014-04-24 18:50:12 -0400418
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200419 private void storeComponentsToSetting(Set<ComponentName> components,
420 String settingName,
421 int userId) {
422 String[] componentNames = null;
423 if (null != components) {
424 componentNames = new String[components.size()];
425 int index = 0;
426 for (ComponentName c: components) {
427 componentNames[index++] = c.flattenToString();
428 }
429 }
430 final String value = (componentNames == null) ? "" :
431 TextUtils.join(ENABLED_SERVICES_SEPARATOR, componentNames);
432 final ContentResolver cr = mContext.getContentResolver();
433 Settings.Secure.putStringForUser(
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800434 cr,
435 settingName,
436 value,
437 userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200438 }
439
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200440 /**
441 * Remove access for any services that no longer exist.
442 */
443 private void updateSettingsAccordingToInstalledServices() {
444 int[] userIds = mUserProfiles.getCurrentProfileIds();
445 final int N = userIds.length;
446 for (int i = 0; i < N; ++i) {
447 updateSettingsAccordingToInstalledServices(userIds[i]);
448 }
449 rebuildRestoredPackages();
450 }
451
Julia Reynoldsc279b992015-10-30 08:23:51 -0400452 protected Set<ComponentName> queryPackageForServices(String packageName, int userId) {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800453 return queryPackageForServices(packageName, userId, null);
454 }
455
456 protected Set<ComponentName> queryPackageForServices(String packageName, int userId,
457 String category) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200458 Set<ComponentName> installed = new ArraySet<>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200459 final PackageManager pm = mContext.getPackageManager();
Julia Reynoldsc279b992015-10-30 08:23:51 -0400460 Intent queryIntent = new Intent(mConfig.serviceInterface);
461 if (!TextUtils.isEmpty(packageName)) {
462 queryIntent.setPackage(packageName);
463 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800464 if (category != null) {
465 queryIntent.addCategory(category);
466 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200467 List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
Julia Reynoldsc279b992015-10-30 08:23:51 -0400468 queryIntent,
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200469 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
470 userId);
471 if (DEBUG)
472 Slog.v(TAG, mConfig.serviceInterface + " services: " + installedServices);
Julia Reynolds50f05142015-10-30 17:03:59 -0400473 if (installedServices != null) {
474 for (int i = 0, count = installedServices.size(); i < count; i++) {
475 ResolveInfo resolveInfo = installedServices.get(i);
476 ServiceInfo info = resolveInfo.serviceInfo;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200477
Julia Reynolds50f05142015-10-30 17:03:59 -0400478 ComponentName component = new ComponentName(info.packageName, info.name);
479 if (!mConfig.bindPermission.equals(info.permission)) {
480 Slog.w(TAG, "Skipping " + getCaption() + " service "
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800481 + info.packageName + "/" + info.name
482 + ": it does not require the permission "
483 + mConfig.bindPermission);
Julia Reynolds50f05142015-10-30 17:03:59 -0400484 continue;
485 }
486 installed.add(component);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200487 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200488 }
Julia Reynoldsc279b992015-10-30 08:23:51 -0400489 return installed;
490 }
491
492 private void updateSettingsAccordingToInstalledServices(int userId) {
493 boolean restoredChanged = false;
494 boolean currentChanged = false;
495 Set<ComponentName> restored =
496 loadComponentNamesFromSetting(restoredSettingName(mConfig), userId);
497 Set<ComponentName> current =
498 loadComponentNamesFromSetting(mConfig.secureSettingName, userId);
499 // Load all services for all packages.
500 Set<ComponentName> installed = queryPackageForServices(null, userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200501
502 ArraySet<ComponentName> retained = new ArraySet<>();
503
504 for (ComponentName component : installed) {
505 if (null != restored) {
506 boolean wasRestored = restored.remove(component);
507 if (wasRestored) {
508 // Freshly installed package has service that was mentioned in restored setting.
509 if (DEBUG)
510 Slog.v(TAG, "Restoring " + component + " for user " + userId);
511 restoredChanged = true;
512 currentChanged = true;
513 retained.add(component);
John Spurlock7340fc82014-04-24 18:50:12 -0400514 continue;
515 }
John Spurlock7340fc82014-04-24 18:50:12 -0400516 }
517
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200518 if (null != current) {
519 if (current.contains(component))
520 retained.add(component);
John Spurlock7340fc82014-04-24 18:50:12 -0400521 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200522 }
523
524 currentChanged |= ((current == null ? 0 : current.size()) != retained.size());
525
526 if (currentChanged) {
527 if (DEBUG) Slog.v(TAG, "List of " + getCaption() + " services was updated " + current);
528 storeComponentsToSetting(retained, mConfig.secureSettingName, userId);
529 }
530
531 if (restoredChanged) {
532 if (DEBUG) Slog.v(TAG,
533 "List of " + getCaption() + " restored services was updated " + restored);
534 storeComponentsToSetting(restored, restoredSettingName(mConfig), userId);
John Spurlock7340fc82014-04-24 18:50:12 -0400535 }
536 }
537
538 /**
539 * Called whenever packages change, the user switches, or the secure setting
540 * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
541 */
542 private void rebindServices() {
543 if (DEBUG) Slog.d(TAG, "rebindServices");
544 final int[] userIds = mUserProfiles.getCurrentProfileIds();
545 final int nUserIds = userIds.length;
546
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200547 final SparseArray<ArraySet<ComponentName>> componentsByUser = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400548
549 for (int i = 0; i < nUserIds; ++i) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200550 componentsByUser.put(userIds[i],
551 loadComponentNamesFromSetting(mConfig.secureSettingName, userIds[i]));
John Spurlock7340fc82014-04-24 18:50:12 -0400552 }
553
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500554 final ArrayList<ManagedServiceInfo> removableBoundServices = new ArrayList<>();
555 final SparseArray<Set<ComponentName>> toAdd = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400556
557 synchronized (mMutex) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500558 // Potentially unbind automatically bound services, retain system services.
Christoph Studer5d423842014-05-23 13:15:54 +0200559 for (ManagedServiceInfo service : mServices) {
Chris Wren51017d02015-12-15 15:34:46 -0500560 if (!service.isSystem && !service.isGuest(this)) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500561 removableBoundServices.add(service);
Christoph Studer5d423842014-05-23 13:15:54 +0200562 }
563 }
John Spurlock7340fc82014-04-24 18:50:12 -0400564
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200565 final ArraySet<ComponentName> newEnabled = new ArraySet<>();
566 final ArraySet<String> newPackages = new ArraySet<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400567
568 for (int i = 0; i < nUserIds; ++i) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200569 // decode the list of components
570 final ArraySet<ComponentName> userComponents = componentsByUser.get(userIds[i]);
571 if (null == userComponents) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500572 toAdd.put(userIds[i], new HashSet<ComponentName>());
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200573 continue;
574 }
575
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500576 final Set<ComponentName> add = new HashSet<>(userComponents);
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800577
578 // Remove components from disabled categories so that those services aren't run.
579 for (Entry<String, Boolean> e : mCategoryEnabled.entrySet()) {
580 if (!e.getValue()) {
581 Set<ComponentName> c = queryPackageForServices(null, userIds[i],
582 e.getKey());
583 add.removeAll(c);
584 }
585 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500586 add.removeAll(mSnoozingForCurrentProfiles);
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800587
John Spurlock7340fc82014-04-24 18:50:12 -0400588 toAdd.put(userIds[i], add);
589
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200590 newEnabled.addAll(userComponents);
John Spurlock7340fc82014-04-24 18:50:12 -0400591
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200592 for (int j = 0; j < userComponents.size(); j++) {
Chris Wren083094c2015-12-15 16:25:07 -0500593 final ComponentName component = userComponents.valueAt(j);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200594 newPackages.add(component.getPackageName());
John Spurlock7340fc82014-04-24 18:50:12 -0400595 }
596 }
597 mEnabledServicesForCurrentProfiles = newEnabled;
598 mEnabledServicesPackageNames = newPackages;
599 }
600
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500601 for (ManagedServiceInfo info : removableBoundServices) {
John Spurlock7340fc82014-04-24 18:50:12 -0400602 final ComponentName component = info.component;
603 final int oldUser = info.userid;
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500604 final Set<ComponentName> allowedComponents = toAdd.get(info.userid);
605 if (allowedComponents != null) {
606 if (allowedComponents.contains(component)) {
607 // Already bound, don't need to bind again.
608 allowedComponents.remove(component);
609 } else {
610 // No longer allowed to be bound.
611 Slog.v(TAG, "disabling " + getCaption() + " for user "
612 + oldUser + ": " + component);
613 unregisterService(component, oldUser);
614 }
615 }
John Spurlock7340fc82014-04-24 18:50:12 -0400616 }
617
618 for (int i = 0; i < nUserIds; ++i) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500619 final Set<ComponentName> add = toAdd.get(userIds[i]);
620 for (ComponentName component : add) {
John Spurlock7340fc82014-04-24 18:50:12 -0400621 Slog.v(TAG, "enabling " + getCaption() + " for user " + userIds[i] + ": "
622 + component);
623 registerService(component, userIds[i]);
624 }
625 }
Christoph Studerb53dfd42014-09-12 14:45:59 +0200626
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200627 mLastSeenProfileIds = userIds;
John Spurlock7340fc82014-04-24 18:50:12 -0400628 }
629
630 /**
631 * Version of registerService that takes the name of a service component to bind to.
632 */
633 private void registerService(final ComponentName name, final int userid) {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800634 synchronized (mMutex) {
635 registerServiceLocked(name, userid);
636 }
637 }
638
639 private void registerServiceLocked(final ComponentName name, final int userid) {
John Spurlock7340fc82014-04-24 18:50:12 -0400640 if (DEBUG) Slog.v(TAG, "registerService: " + name + " u=" + userid);
641
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800642 final String servicesBindingTag = name.toString() + "/" + userid;
643 if (mServicesBinding.contains(servicesBindingTag)) {
644 // stop registering this thing already! we're working on it
645 return;
646 }
647 mServicesBinding.add(servicesBindingTag);
John Spurlock7340fc82014-04-24 18:50:12 -0400648
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800649 final int N = mServices.size();
650 for (int i = N - 1; i >= 0; i--) {
651 final ManagedServiceInfo info = mServices.get(i);
652 if (name.equals(info.component)
653 && info.userid == userid) {
654 // cut old connections
655 if (DEBUG) Slog.v(TAG, " disconnecting old " + getCaption() + ": "
656 + info.service);
657 removeServiceLocked(i);
658 if (info.connection != null) {
659 mContext.unbindService(info.connection);
John Spurlock7340fc82014-04-24 18:50:12 -0400660 }
661 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800662 }
John Spurlock7340fc82014-04-24 18:50:12 -0400663
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800664 Intent intent = new Intent(mConfig.serviceInterface);
665 intent.setComponent(name);
John Spurlock7340fc82014-04-24 18:50:12 -0400666
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800667 intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel);
John Spurlock7340fc82014-04-24 18:50:12 -0400668
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800669 final PendingIntent pendingIntent = PendingIntent.getActivity(
670 mContext, 0, new Intent(mConfig.settingsAction), 0);
671 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
John Spurlock7340fc82014-04-24 18:50:12 -0400672
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800673 ApplicationInfo appInfo = null;
674 try {
675 appInfo = mContext.getPackageManager().getApplicationInfo(
676 name.getPackageName(), 0);
677 } catch (NameNotFoundException e) {
678 // Ignore if the package doesn't exist we won't be able to bind to the service.
679 }
680 final int targetSdkVersion =
681 appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE;
John Spurlock7340fc82014-04-24 18:50:12 -0400682
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800683 try {
684 if (DEBUG) Slog.v(TAG, "binding: " + intent);
685 ServiceConnection serviceConnection = new ServiceConnection() {
686 IInterface mService;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200687
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800688 @Override
689 public void onServiceConnected(ComponentName name, IBinder binder) {
690 boolean added = false;
691 ManagedServiceInfo info = null;
692 synchronized (mMutex) {
693 mServicesBinding.remove(servicesBindingTag);
694 try {
695 mService = asInterface(binder);
696 info = newServiceInfo(mService, name,
697 userid, false /*isSystem*/, this, targetSdkVersion);
698 binder.linkToDeath(info, 0);
699 added = mServices.add(info);
700 } catch (RemoteException e) {
701 // already dead
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200702 }
703 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800704 if (added) {
705 onServiceAdded(info);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200706 }
John Spurlock7340fc82014-04-24 18:50:12 -0400707 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800708
709 @Override
710 public void onServiceDisconnected(ComponentName name) {
711 Slog.v(TAG, getCaption() + " connection lost: " + name);
712 }
713 };
714 if (!mContext.bindServiceAsUser(intent,
715 serviceConnection,
716 Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE,
717 new UserHandle(userid))) {
718 mServicesBinding.remove(servicesBindingTag);
719 Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent);
John Spurlock7340fc82014-04-24 18:50:12 -0400720 return;
721 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800722 } catch (SecurityException ex) {
723 Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex);
724 return;
John Spurlock7340fc82014-04-24 18:50:12 -0400725 }
726 }
727
728 /**
729 * Remove a service for the given user by ComponentName
730 */
731 private void unregisterService(ComponentName name, int userid) {
732 synchronized (mMutex) {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800733 unregisterServiceLocked(name, userid);
734 }
735 }
736
737 private void unregisterServiceLocked(ComponentName name, int userid) {
738 final int N = mServices.size();
739 for (int i = N - 1; i >= 0; i--) {
740 final ManagedServiceInfo info = mServices.get(i);
741 if (name.equals(info.component)
742 && info.userid == userid) {
743 removeServiceLocked(i);
744 if (info.connection != null) {
745 try {
746 mContext.unbindService(info.connection);
747 } catch (IllegalArgumentException ex) {
748 // something happened to the service: we think we have a connection
749 // but it's bogus.
750 Slog.e(TAG, getCaption() + " " + name + " could not be unbound: " + ex);
John Spurlock7340fc82014-04-24 18:50:12 -0400751 }
752 }
753 }
754 }
755 }
756
757 /**
758 * Removes a service from the list but does not unbind
759 *
760 * @return the removed service.
761 */
762 private ManagedServiceInfo removeServiceImpl(IInterface service, final int userid) {
John Spurlocke77bb362014-04-26 10:24:59 -0400763 if (DEBUG) Slog.d(TAG, "removeServiceImpl service=" + service + " u=" + userid);
John Spurlock7340fc82014-04-24 18:50:12 -0400764 ManagedServiceInfo serviceInfo = null;
765 synchronized (mMutex) {
766 final int N = mServices.size();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200767 for (int i = N - 1; i >= 0; i--) {
John Spurlock7340fc82014-04-24 18:50:12 -0400768 final ManagedServiceInfo info = mServices.get(i);
769 if (info.service.asBinder() == service.asBinder()
770 && info.userid == userid) {
John Spurlocke77bb362014-04-26 10:24:59 -0400771 if (DEBUG) Slog.d(TAG, "Removing active service " + info.component);
772 serviceInfo = removeServiceLocked(i);
John Spurlock7340fc82014-04-24 18:50:12 -0400773 }
774 }
775 }
776 return serviceInfo;
777 }
778
John Spurlocke77bb362014-04-26 10:24:59 -0400779 private ManagedServiceInfo removeServiceLocked(int i) {
780 final ManagedServiceInfo info = mServices.remove(i);
781 onServiceRemovedLocked(info);
782 return info;
783 }
784
John Spurlock7340fc82014-04-24 18:50:12 -0400785 private void checkNotNull(IInterface service) {
786 if (service == null) {
787 throw new IllegalArgumentException(getCaption() + " must not be null");
788 }
789 }
790
Christoph Studer3e144d32014-05-22 16:48:40 +0200791 private ManagedServiceInfo registerServiceImpl(final IInterface service,
John Spurlock7340fc82014-04-24 18:50:12 -0400792 final ComponentName component, final int userid) {
Chris Wren51017d02015-12-15 15:34:46 -0500793 ManagedServiceInfo info = newServiceInfo(service, component, userid,
794 true /*isSystem*/, null /*connection*/, Build.VERSION_CODES.LOLLIPOP);
795 return registerServiceImpl(info);
796 }
797
798 private ManagedServiceInfo registerServiceImpl(ManagedServiceInfo info) {
John Spurlock7340fc82014-04-24 18:50:12 -0400799 synchronized (mMutex) {
800 try {
Chris Wren51017d02015-12-15 15:34:46 -0500801 info.service.asBinder().linkToDeath(info, 0);
John Spurlock7340fc82014-04-24 18:50:12 -0400802 mServices.add(info);
Christoph Studer3e144d32014-05-22 16:48:40 +0200803 return info;
John Spurlock7340fc82014-04-24 18:50:12 -0400804 } catch (RemoteException e) {
805 // already dead
806 }
807 }
Christoph Studer3e144d32014-05-22 16:48:40 +0200808 return null;
John Spurlock7340fc82014-04-24 18:50:12 -0400809 }
810
811 /**
812 * Removes a service from the list and unbinds.
813 */
814 private void unregisterServiceImpl(IInterface service, int userid) {
815 ManagedServiceInfo info = removeServiceImpl(service, userid);
Chris Wren51017d02015-12-15 15:34:46 -0500816 if (info != null && info.connection != null && !info.isGuest(this)) {
John Spurlock7340fc82014-04-24 18:50:12 -0400817 mContext.unbindService(info.connection);
818 }
819 }
820
821 private class SettingsObserver extends ContentObserver {
822 private final Uri mSecureSettingsUri = Settings.Secure.getUriFor(mConfig.secureSettingName);
823
824 private SettingsObserver(Handler handler) {
825 super(handler);
826 }
827
828 private void observe() {
829 ContentResolver resolver = mContext.getContentResolver();
830 resolver.registerContentObserver(mSecureSettingsUri,
831 false, this, UserHandle.USER_ALL);
832 update(null);
833 }
834
835 @Override
836 public void onChange(boolean selfChange, Uri uri) {
837 update(uri);
838 }
839
840 private void update(Uri uri) {
841 if (uri == null || mSecureSettingsUri.equals(uri)) {
Christoph Studerb53dfd42014-09-12 14:45:59 +0200842 if (DEBUG) Slog.d(TAG, "Setting changed: mSecureSettingsUri=" + mSecureSettingsUri +
843 " / uri=" + uri);
John Spurlock7340fc82014-04-24 18:50:12 -0400844 rebindServices();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200845 rebuildRestoredPackages();
John Spurlock7340fc82014-04-24 18:50:12 -0400846 }
847 }
848 }
849
850 public class ManagedServiceInfo implements IBinder.DeathRecipient {
851 public IInterface service;
852 public ComponentName component;
853 public int userid;
854 public boolean isSystem;
855 public ServiceConnection connection;
856 public int targetSdkVersion;
857
858 public ManagedServiceInfo(IInterface service, ComponentName component,
859 int userid, boolean isSystem, ServiceConnection connection, int targetSdkVersion) {
860 this.service = service;
861 this.component = component;
862 this.userid = userid;
863 this.isSystem = isSystem;
864 this.connection = connection;
865 this.targetSdkVersion = targetSdkVersion;
866 }
867
Chris Wren51017d02015-12-15 15:34:46 -0500868 public boolean isGuest(ManagedServices host) {
869 return ManagedServices.this != host;
870 }
871
Chris Wrenab41eec2016-01-04 18:01:27 -0500872 public ManagedServices getOwner() {
873 return ManagedServices.this;
874 }
875
John Spurlocke77bb362014-04-26 10:24:59 -0400876 @Override
877 public String toString() {
878 return new StringBuilder("ManagedServiceInfo[")
879 .append("component=").append(component)
880 .append(",userid=").append(userid)
881 .append(",isSystem=").append(isSystem)
882 .append(",targetSdkVersion=").append(targetSdkVersion)
883 .append(",connection=").append(connection == null ? null : "<connection>")
884 .append(",service=").append(service)
885 .append(']').toString();
886 }
887
John Spurlock7340fc82014-04-24 18:50:12 -0400888 public boolean enabledAndUserMatches(int nid) {
889 if (!isEnabledForCurrentProfiles()) {
890 return false;
891 }
892 if (this.userid == UserHandle.USER_ALL) return true;
893 if (nid == UserHandle.USER_ALL || nid == this.userid) return true;
894 return supportsProfiles() && mUserProfiles.isCurrentProfile(nid);
895 }
896
897 public boolean supportsProfiles() {
Dianne Hackborn955d8d62014-10-07 20:17:19 -0700898 return targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
John Spurlock7340fc82014-04-24 18:50:12 -0400899 }
900
901 @Override
902 public void binderDied() {
John Spurlocke77bb362014-04-26 10:24:59 -0400903 if (DEBUG) Slog.d(TAG, "binderDied");
John Spurlock7340fc82014-04-24 18:50:12 -0400904 // Remove the service, but don't unbind from the service. The system will bring the
905 // service back up, and the onServiceConnected handler will readd the service with the
906 // new binding. If this isn't a bound service, and is just a registered
907 // service, just removing it from the list is all we need to do anyway.
908 removeServiceImpl(this.service, this.userid);
909 }
910
911 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
912 public boolean isEnabledForCurrentProfiles() {
913 if (this.isSystem) return true;
914 if (this.connection == null) return false;
915 return mEnabledServicesForCurrentProfiles.contains(this.component);
916 }
917 }
918
Chris Wrenab41eec2016-01-04 18:01:27 -0500919 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
920 public boolean isComponentEnabledForCurrentProfiles(ComponentName component) {
921 return mEnabledServicesForCurrentProfiles.contains(component);
922 }
923
John Spurlock7340fc82014-04-24 18:50:12 -0400924 public static class UserProfiles {
925 // Profiles of the current user.
Ruben Brunke24b9a62016-02-16 21:38:24 -0800926 private final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400927
Ruben Brunke24b9a62016-02-16 21:38:24 -0800928 public void updateCache(@NonNull Context context) {
John Spurlock7340fc82014-04-24 18:50:12 -0400929 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
930 if (userManager != null) {
931 int currentUserId = ActivityManager.getCurrentUser();
932 List<UserInfo> profiles = userManager.getProfiles(currentUserId);
933 synchronized (mCurrentProfiles) {
934 mCurrentProfiles.clear();
935 for (UserInfo user : profiles) {
936 mCurrentProfiles.put(user.id, user);
937 }
938 }
939 }
940 }
941
942 public int[] getCurrentProfileIds() {
943 synchronized (mCurrentProfiles) {
944 int[] users = new int[mCurrentProfiles.size()];
945 final int N = mCurrentProfiles.size();
946 for (int i = 0; i < N; ++i) {
947 users[i] = mCurrentProfiles.keyAt(i);
948 }
949 return users;
950 }
951 }
952
953 public boolean isCurrentProfile(int userId) {
954 synchronized (mCurrentProfiles) {
955 return mCurrentProfiles.get(userId) != null;
956 }
957 }
958 }
959
Ruben Brunke24b9a62016-02-16 21:38:24 -0800960 public static class Config {
961 public String caption;
962 public String serviceInterface;
963 public String secureSettingName;
964 public String bindPermission;
965 public String settingsAction;
966 public int clientLabel;
John Spurlock7340fc82014-04-24 18:50:12 -0400967 }
968}