blob: 29e2e441dcffa7a0036cf13e8c897d6ab02b878e [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<>();
Chris Wrenab41eec2016-01-04 18:01:27 -0500100 // List of enabled packages that have nevertheless asked not to be run
101 private ArraySet<ComponentName> mSnoozingForCurrentProfiles = new ArraySet<>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200102
John Spurlock7340fc82014-04-24 18:50:12 -0400103
Christoph Studerb53dfd42014-09-12 14:45:59 +0200104 // Kept to de-dupe user change events (experienced after boot, when we receive a settings and a
105 // user change).
106 private int[] mLastSeenProfileIds;
107
Christopher Tate6597e342015-02-17 12:15:25 -0800108 private final BroadcastReceiver mRestoreReceiver;
109
John Spurlock7340fc82014-04-24 18:50:12 -0400110 public ManagedServices(Context context, Handler handler, Object mutex,
111 UserProfiles userProfiles) {
112 mContext = context;
113 mMutex = mutex;
114 mUserProfiles = userProfiles;
115 mConfig = getConfig();
116 mSettingsObserver = new SettingsObserver(handler);
Christopher Tate6597e342015-02-17 12:15:25 -0800117
118 mRestoreReceiver = new SettingRestoredReceiver();
119 IntentFilter filter = new IntentFilter(Intent.ACTION_SETTING_RESTORED);
120 context.registerReceiver(mRestoreReceiver, filter);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200121 rebuildRestoredPackages();
Christopher Tate6597e342015-02-17 12:15:25 -0800122 }
123
124 class SettingRestoredReceiver extends BroadcastReceiver {
125 @Override
126 public void onReceive(Context context, Intent intent) {
127 if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) {
128 String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400129 if (Objects.equals(element, mConfig.secureSettingName)
130 || Objects.equals(element, mConfig.secondarySettingName)) {
Christopher Tate6597e342015-02-17 12:15:25 -0800131 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.
Julia Reynolds6e839b02016-04-13 10:01:17 -0400190 public static String restoredSettingName(String setting) {
191 return setting + ":restored";
Christopher Tate6597e342015-02-17 12:15:25 -0800192 }
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);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400201 if (mConfig.secureSettingName.equals(element) ||
202 mConfig.secondarySettingName.equals(element)) {
Christopher Tate6597e342015-02-17 12:15:25 -0800203 if (element != null) {
Christopher Tate6597e342015-02-17 12:15:25 -0800204 Settings.Secure.putStringForUser(mContext.getContentResolver(),
Julia Reynolds6e839b02016-04-13 10:01:17 -0400205 restoredSettingName(element),
Christopher Tate6597e342015-02-17 12:15:25 -0800206 newValue,
207 userid);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400208 updateSettingsAccordingToInstalledServices(element, userid);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200209 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
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400240 rebindServices(false);
John Spurlock7340fc82014-04-24 18:50:12 -0400241 }
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 }
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400251 rebindServices(true);
Christoph Studerb53dfd42014-09-12 14:45:59 +0200252 }
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();
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400257 rebindServices(false);
Julia Reynoldsa3dcaff2016-02-03 15:04:05 -0500258 }
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);
Ruben Brunke24b9a62016-02-16 21:38:24 -0800304 if (!checkType(guest.service)) {
305 throw new IllegalArgumentException();
306 }
Chris Wren51017d02015-12-15 15:34:46 -0500307 if (registerServiceImpl(guest) != null) {
308 onServiceAdded(guest);
Chris Wrenab41eec2016-01-04 18:01:27 -0500309 }
310 }
311
312 public void setComponentState(ComponentName component, boolean enabled) {
313 boolean previous = !mSnoozingForCurrentProfiles.contains(component);
314 if (previous == enabled) {
315 return;
316 }
317
318 if (enabled) {
319 mSnoozingForCurrentProfiles.remove(component);
320 } else {
321 mSnoozingForCurrentProfiles.add(component);
322 }
323
324 // State changed
325 if (DEBUG) {
326 Slog.d(TAG, ((enabled) ? "Enabling " : "Disabling ") + "component " +
327 component.flattenToShortString());
328 }
329
Chris Wren0efdb882016-03-01 17:17:47 -0500330
331 synchronized (mMutex) {
332 final int[] userIds = mUserProfiles.getCurrentProfileIds();
333
334 for (int userId : userIds) {
335 if (enabled) {
336 registerServiceLocked(component, userId);
337 } else {
338 unregisterServiceLocked(component, userId);
339 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500340 }
Chris Wren51017d02015-12-15 15:34:46 -0500341 }
342 }
343
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200344 private void rebuildRestoredPackages() {
345 mRestoredPackages.clear();
Chris Wrenab41eec2016-01-04 18:01:27 -0500346 mSnoozingForCurrentProfiles.clear();
Julia Reynolds6e839b02016-04-13 10:01:17 -0400347 String secureSettingName = restoredSettingName(mConfig.secureSettingName);
348 String secondarySettingName = mConfig.secondarySettingName == null
349 ? null : restoredSettingName(mConfig.secondarySettingName);
John Spurlock7340fc82014-04-24 18:50:12 -0400350 int[] userIds = mUserProfiles.getCurrentProfileIds();
351 final int N = userIds.length;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200352 for (int i = 0; i < N; ++i) {
Julia Reynolds6e839b02016-04-13 10:01:17 -0400353 ArraySet<ComponentName> names =
354 loadComponentNamesFromSetting(secureSettingName, userIds[i]);
355 if (secondarySettingName != null) {
356 names.addAll(loadComponentNamesFromSetting(secondarySettingName, userIds[i]));
357 }
358 for (ComponentName name : names) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200359 mRestoredPackages.add(name.getPackageName());
360 }
John Spurlock7340fc82014-04-24 18:50:12 -0400361 }
362 }
363
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200364
Julia Reynolds6e839b02016-04-13 10:01:17 -0400365 protected @NonNull ArraySet<ComponentName> loadComponentNamesFromSetting(String settingName,
Julia Reynoldsc279b992015-10-30 08:23:51 -0400366 int userId) {
Christopher Tate6597e342015-02-17 12:15:25 -0800367 final ContentResolver cr = mContext.getContentResolver();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200368 String settingValue = Settings.Secure.getStringForUser(
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800369 cr,
370 settingName,
371 userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200372 if (TextUtils.isEmpty(settingValue))
Julia Reynolds6e839b02016-04-13 10:01:17 -0400373 return new ArraySet<>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200374 String[] restored = settingValue.split(ENABLED_SERVICES_SEPARATOR);
375 ArraySet<ComponentName> result = new ArraySet<>(restored.length);
376 for (int i = 0; i < restored.length; i++) {
377 ComponentName value = ComponentName.unflattenFromString(restored[i]);
378 if (null != value) {
379 result.add(value);
Christopher Tate6597e342015-02-17 12:15:25 -0800380 }
381 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200382 return result;
383 }
John Spurlock7340fc82014-04-24 18:50:12 -0400384
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200385 private void storeComponentsToSetting(Set<ComponentName> components,
386 String settingName,
387 int userId) {
388 String[] componentNames = null;
389 if (null != components) {
390 componentNames = new String[components.size()];
391 int index = 0;
392 for (ComponentName c: components) {
393 componentNames[index++] = c.flattenToString();
394 }
395 }
396 final String value = (componentNames == null) ? "" :
397 TextUtils.join(ENABLED_SERVICES_SEPARATOR, componentNames);
398 final ContentResolver cr = mContext.getContentResolver();
399 Settings.Secure.putStringForUser(
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800400 cr,
401 settingName,
402 value,
403 userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200404 }
405
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200406 /**
407 * Remove access for any services that no longer exist.
408 */
409 private void updateSettingsAccordingToInstalledServices() {
410 int[] userIds = mUserProfiles.getCurrentProfileIds();
411 final int N = userIds.length;
412 for (int i = 0; i < N; ++i) {
Julia Reynolds6e839b02016-04-13 10:01:17 -0400413 updateSettingsAccordingToInstalledServices(mConfig.secureSettingName, userIds[i]);
414 if (mConfig.secondarySettingName != null) {
415 updateSettingsAccordingToInstalledServices(
416 mConfig.secondarySettingName, userIds[i]);
417 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200418 }
419 rebuildRestoredPackages();
420 }
421
Julia Reynoldsc279b992015-10-30 08:23:51 -0400422 protected Set<ComponentName> queryPackageForServices(String packageName, int userId) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200423 Set<ComponentName> installed = new ArraySet<>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200424 final PackageManager pm = mContext.getPackageManager();
Julia Reynoldsc279b992015-10-30 08:23:51 -0400425 Intent queryIntent = new Intent(mConfig.serviceInterface);
426 if (!TextUtils.isEmpty(packageName)) {
427 queryIntent.setPackage(packageName);
428 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200429 List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
Julia Reynoldsc279b992015-10-30 08:23:51 -0400430 queryIntent,
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200431 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
432 userId);
433 if (DEBUG)
434 Slog.v(TAG, mConfig.serviceInterface + " services: " + installedServices);
Julia Reynolds50f05142015-10-30 17:03:59 -0400435 if (installedServices != null) {
436 for (int i = 0, count = installedServices.size(); i < count; i++) {
437 ResolveInfo resolveInfo = installedServices.get(i);
438 ServiceInfo info = resolveInfo.serviceInfo;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200439
Julia Reynolds50f05142015-10-30 17:03:59 -0400440 ComponentName component = new ComponentName(info.packageName, info.name);
441 if (!mConfig.bindPermission.equals(info.permission)) {
442 Slog.w(TAG, "Skipping " + getCaption() + " service "
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800443 + info.packageName + "/" + info.name
444 + ": it does not require the permission "
445 + mConfig.bindPermission);
Julia Reynolds50f05142015-10-30 17:03:59 -0400446 continue;
447 }
448 installed.add(component);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200449 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200450 }
Julia Reynoldsc279b992015-10-30 08:23:51 -0400451 return installed;
452 }
453
Julia Reynolds6e839b02016-04-13 10:01:17 -0400454 private void updateSettingsAccordingToInstalledServices(String setting, int userId) {
Julia Reynoldsc279b992015-10-30 08:23:51 -0400455 boolean restoredChanged = false;
456 boolean currentChanged = false;
457 Set<ComponentName> restored =
Julia Reynolds6e839b02016-04-13 10:01:17 -0400458 loadComponentNamesFromSetting(restoredSettingName(setting), userId);
Julia Reynoldsc279b992015-10-30 08:23:51 -0400459 Set<ComponentName> current =
Julia Reynolds6e839b02016-04-13 10:01:17 -0400460 loadComponentNamesFromSetting(setting, userId);
Julia Reynoldsc279b992015-10-30 08:23:51 -0400461 // Load all services for all packages.
462 Set<ComponentName> installed = queryPackageForServices(null, userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200463
464 ArraySet<ComponentName> retained = new ArraySet<>();
465
466 for (ComponentName component : installed) {
467 if (null != restored) {
468 boolean wasRestored = restored.remove(component);
469 if (wasRestored) {
470 // Freshly installed package has service that was mentioned in restored setting.
471 if (DEBUG)
472 Slog.v(TAG, "Restoring " + component + " for user " + userId);
473 restoredChanged = true;
474 currentChanged = true;
475 retained.add(component);
John Spurlock7340fc82014-04-24 18:50:12 -0400476 continue;
477 }
John Spurlock7340fc82014-04-24 18:50:12 -0400478 }
479
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200480 if (null != current) {
481 if (current.contains(component))
482 retained.add(component);
John Spurlock7340fc82014-04-24 18:50:12 -0400483 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200484 }
485
486 currentChanged |= ((current == null ? 0 : current.size()) != retained.size());
487
488 if (currentChanged) {
489 if (DEBUG) Slog.v(TAG, "List of " + getCaption() + " services was updated " + current);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400490 storeComponentsToSetting(retained, setting, userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200491 }
492
493 if (restoredChanged) {
494 if (DEBUG) Slog.v(TAG,
495 "List of " + getCaption() + " restored services was updated " + restored);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400496 storeComponentsToSetting(restored, restoredSettingName(setting), userId);
John Spurlock7340fc82014-04-24 18:50:12 -0400497 }
498 }
499
500 /**
501 * Called whenever packages change, the user switches, or the secure setting
502 * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
503 */
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400504 private void rebindServices(boolean forceRebind) {
John Spurlock7340fc82014-04-24 18:50:12 -0400505 if (DEBUG) Slog.d(TAG, "rebindServices");
506 final int[] userIds = mUserProfiles.getCurrentProfileIds();
507 final int nUserIds = userIds.length;
508
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200509 final SparseArray<ArraySet<ComponentName>> componentsByUser = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400510
511 for (int i = 0; i < nUserIds; ++i) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200512 componentsByUser.put(userIds[i],
513 loadComponentNamesFromSetting(mConfig.secureSettingName, userIds[i]));
Julia Reynolds6e839b02016-04-13 10:01:17 -0400514 if (mConfig.secondarySettingName != null) {
515 componentsByUser.get(userIds[i]).addAll(
516 loadComponentNamesFromSetting(mConfig.secondarySettingName, userIds[i]));
517 }
John Spurlock7340fc82014-04-24 18:50:12 -0400518 }
519
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500520 final ArrayList<ManagedServiceInfo> removableBoundServices = new ArrayList<>();
521 final SparseArray<Set<ComponentName>> toAdd = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400522
523 synchronized (mMutex) {
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400524 // Rebind to non-system services if user switched
Christoph Studer5d423842014-05-23 13:15:54 +0200525 for (ManagedServiceInfo service : mServices) {
Chris Wren51017d02015-12-15 15:34:46 -0500526 if (!service.isSystem && !service.isGuest(this)) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500527 removableBoundServices.add(service);
Christoph Studer5d423842014-05-23 13:15:54 +0200528 }
529 }
John Spurlock7340fc82014-04-24 18:50:12 -0400530
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400531 mEnabledServicesForCurrentProfiles.clear();
532 mEnabledServicesPackageNames.clear();
John Spurlock7340fc82014-04-24 18:50:12 -0400533
534 for (int i = 0; i < nUserIds; ++i) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200535 // decode the list of components
536 final ArraySet<ComponentName> userComponents = componentsByUser.get(userIds[i]);
537 if (null == userComponents) {
Julia Reynolds6e839b02016-04-13 10:01:17 -0400538 toAdd.put(userIds[i], new ArraySet<ComponentName>());
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200539 continue;
540 }
541
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500542 final Set<ComponentName> add = new HashSet<>(userComponents);
Chris Wrenab41eec2016-01-04 18:01:27 -0500543 add.removeAll(mSnoozingForCurrentProfiles);
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800544
John Spurlock7340fc82014-04-24 18:50:12 -0400545 toAdd.put(userIds[i], add);
546
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400547 mEnabledServicesForCurrentProfiles.addAll(userComponents);
John Spurlock7340fc82014-04-24 18:50:12 -0400548
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200549 for (int j = 0; j < userComponents.size(); j++) {
Chris Wren083094c2015-12-15 16:25:07 -0500550 final ComponentName component = userComponents.valueAt(j);
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400551 mEnabledServicesPackageNames.add(component.getPackageName());
John Spurlock7340fc82014-04-24 18:50:12 -0400552 }
553 }
John Spurlock7340fc82014-04-24 18:50:12 -0400554 }
555
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500556 for (ManagedServiceInfo info : removableBoundServices) {
John Spurlock7340fc82014-04-24 18:50:12 -0400557 final ComponentName component = info.component;
558 final int oldUser = info.userid;
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500559 final Set<ComponentName> allowedComponents = toAdd.get(info.userid);
560 if (allowedComponents != null) {
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400561 if (allowedComponents.contains(component) && !forceRebind) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500562 // Already bound, don't need to bind again.
563 allowedComponents.remove(component);
564 } else {
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400565 // No longer allowed to be bound, or must rebind.
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500566 Slog.v(TAG, "disabling " + getCaption() + " for user "
567 + oldUser + ": " + component);
568 unregisterService(component, oldUser);
569 }
570 }
John Spurlock7340fc82014-04-24 18:50:12 -0400571 }
572
573 for (int i = 0; i < nUserIds; ++i) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500574 final Set<ComponentName> add = toAdd.get(userIds[i]);
575 for (ComponentName component : add) {
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400576 Slog.v(TAG, "enabling " + getCaption() + " for " + userIds[i] + ": " + component);
John Spurlock7340fc82014-04-24 18:50:12 -0400577 registerService(component, userIds[i]);
578 }
579 }
Christoph Studerb53dfd42014-09-12 14:45:59 +0200580
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200581 mLastSeenProfileIds = userIds;
John Spurlock7340fc82014-04-24 18:50:12 -0400582 }
583
584 /**
585 * Version of registerService that takes the name of a service component to bind to.
586 */
587 private void registerService(final ComponentName name, final int userid) {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800588 synchronized (mMutex) {
589 registerServiceLocked(name, userid);
590 }
591 }
592
Chris Wren0efdb882016-03-01 17:17:47 -0500593 /**
594 * Inject a system service into the management list.
595 */
596 public void registerSystemService(final ComponentName name, final int userid) {
597 synchronized (mMutex) {
598 registerServiceLocked(name, userid, true /* isSystem */);
599 }
600 }
601
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800602 private void registerServiceLocked(final ComponentName name, final int userid) {
Chris Wren0efdb882016-03-01 17:17:47 -0500603 registerServiceLocked(name, userid, false /* isSystem */);
604 }
605
606 private void registerServiceLocked(final ComponentName name, final int userid,
607 final boolean isSystem) {
John Spurlock7340fc82014-04-24 18:50:12 -0400608 if (DEBUG) Slog.v(TAG, "registerService: " + name + " u=" + userid);
609
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800610 final String servicesBindingTag = name.toString() + "/" + userid;
611 if (mServicesBinding.contains(servicesBindingTag)) {
612 // stop registering this thing already! we're working on it
613 return;
614 }
615 mServicesBinding.add(servicesBindingTag);
John Spurlock7340fc82014-04-24 18:50:12 -0400616
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800617 final int N = mServices.size();
618 for (int i = N - 1; i >= 0; i--) {
619 final ManagedServiceInfo info = mServices.get(i);
620 if (name.equals(info.component)
621 && info.userid == userid) {
622 // cut old connections
623 if (DEBUG) Slog.v(TAG, " disconnecting old " + getCaption() + ": "
624 + info.service);
625 removeServiceLocked(i);
626 if (info.connection != null) {
627 mContext.unbindService(info.connection);
John Spurlock7340fc82014-04-24 18:50:12 -0400628 }
629 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800630 }
John Spurlock7340fc82014-04-24 18:50:12 -0400631
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800632 Intent intent = new Intent(mConfig.serviceInterface);
633 intent.setComponent(name);
John Spurlock7340fc82014-04-24 18:50:12 -0400634
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800635 intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel);
John Spurlock7340fc82014-04-24 18:50:12 -0400636
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800637 final PendingIntent pendingIntent = PendingIntent.getActivity(
638 mContext, 0, new Intent(mConfig.settingsAction), 0);
639 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
John Spurlock7340fc82014-04-24 18:50:12 -0400640
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800641 ApplicationInfo appInfo = null;
642 try {
643 appInfo = mContext.getPackageManager().getApplicationInfo(
644 name.getPackageName(), 0);
645 } catch (NameNotFoundException e) {
646 // Ignore if the package doesn't exist we won't be able to bind to the service.
647 }
648 final int targetSdkVersion =
649 appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE;
John Spurlock7340fc82014-04-24 18:50:12 -0400650
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800651 try {
652 if (DEBUG) Slog.v(TAG, "binding: " + intent);
653 ServiceConnection serviceConnection = new ServiceConnection() {
654 IInterface mService;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200655
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800656 @Override
657 public void onServiceConnected(ComponentName name, IBinder binder) {
658 boolean added = false;
659 ManagedServiceInfo info = null;
660 synchronized (mMutex) {
661 mServicesBinding.remove(servicesBindingTag);
662 try {
663 mService = asInterface(binder);
664 info = newServiceInfo(mService, name,
Chris Wren0efdb882016-03-01 17:17:47 -0500665 userid, isSystem, this, targetSdkVersion);
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800666 binder.linkToDeath(info, 0);
667 added = mServices.add(info);
668 } catch (RemoteException e) {
669 // already dead
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200670 }
671 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800672 if (added) {
673 onServiceAdded(info);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200674 }
John Spurlock7340fc82014-04-24 18:50:12 -0400675 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800676
677 @Override
678 public void onServiceDisconnected(ComponentName name) {
679 Slog.v(TAG, getCaption() + " connection lost: " + name);
680 }
681 };
682 if (!mContext.bindServiceAsUser(intent,
683 serviceConnection,
684 Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE,
685 new UserHandle(userid))) {
686 mServicesBinding.remove(servicesBindingTag);
687 Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent);
John Spurlock7340fc82014-04-24 18:50:12 -0400688 return;
689 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800690 } catch (SecurityException ex) {
691 Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex);
692 return;
John Spurlock7340fc82014-04-24 18:50:12 -0400693 }
694 }
695
696 /**
697 * Remove a service for the given user by ComponentName
698 */
699 private void unregisterService(ComponentName name, int userid) {
700 synchronized (mMutex) {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800701 unregisterServiceLocked(name, userid);
702 }
703 }
704
705 private void unregisterServiceLocked(ComponentName name, int userid) {
706 final int N = mServices.size();
707 for (int i = N - 1; i >= 0; i--) {
708 final ManagedServiceInfo info = mServices.get(i);
709 if (name.equals(info.component)
710 && info.userid == userid) {
711 removeServiceLocked(i);
712 if (info.connection != null) {
713 try {
714 mContext.unbindService(info.connection);
715 } catch (IllegalArgumentException ex) {
716 // something happened to the service: we think we have a connection
717 // but it's bogus.
718 Slog.e(TAG, getCaption() + " " + name + " could not be unbound: " + ex);
John Spurlock7340fc82014-04-24 18:50:12 -0400719 }
720 }
721 }
722 }
723 }
724
725 /**
726 * Removes a service from the list but does not unbind
727 *
728 * @return the removed service.
729 */
730 private ManagedServiceInfo removeServiceImpl(IInterface service, final int userid) {
John Spurlocke77bb362014-04-26 10:24:59 -0400731 if (DEBUG) Slog.d(TAG, "removeServiceImpl service=" + service + " u=" + userid);
John Spurlock7340fc82014-04-24 18:50:12 -0400732 ManagedServiceInfo serviceInfo = null;
733 synchronized (mMutex) {
734 final int N = mServices.size();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200735 for (int i = N - 1; i >= 0; i--) {
John Spurlock7340fc82014-04-24 18:50:12 -0400736 final ManagedServiceInfo info = mServices.get(i);
737 if (info.service.asBinder() == service.asBinder()
738 && info.userid == userid) {
John Spurlocke77bb362014-04-26 10:24:59 -0400739 if (DEBUG) Slog.d(TAG, "Removing active service " + info.component);
740 serviceInfo = removeServiceLocked(i);
John Spurlock7340fc82014-04-24 18:50:12 -0400741 }
742 }
743 }
744 return serviceInfo;
745 }
746
John Spurlocke77bb362014-04-26 10:24:59 -0400747 private ManagedServiceInfo removeServiceLocked(int i) {
748 final ManagedServiceInfo info = mServices.remove(i);
749 onServiceRemovedLocked(info);
750 return info;
751 }
752
John Spurlock7340fc82014-04-24 18:50:12 -0400753 private void checkNotNull(IInterface service) {
754 if (service == null) {
755 throw new IllegalArgumentException(getCaption() + " must not be null");
756 }
757 }
758
Christoph Studer3e144d32014-05-22 16:48:40 +0200759 private ManagedServiceInfo registerServiceImpl(final IInterface service,
John Spurlock7340fc82014-04-24 18:50:12 -0400760 final ComponentName component, final int userid) {
Chris Wren51017d02015-12-15 15:34:46 -0500761 ManagedServiceInfo info = newServiceInfo(service, component, userid,
762 true /*isSystem*/, null /*connection*/, Build.VERSION_CODES.LOLLIPOP);
763 return registerServiceImpl(info);
764 }
765
766 private ManagedServiceInfo registerServiceImpl(ManagedServiceInfo info) {
John Spurlock7340fc82014-04-24 18:50:12 -0400767 synchronized (mMutex) {
768 try {
Chris Wren51017d02015-12-15 15:34:46 -0500769 info.service.asBinder().linkToDeath(info, 0);
John Spurlock7340fc82014-04-24 18:50:12 -0400770 mServices.add(info);
Christoph Studer3e144d32014-05-22 16:48:40 +0200771 return info;
John Spurlock7340fc82014-04-24 18:50:12 -0400772 } catch (RemoteException e) {
773 // already dead
774 }
775 }
Christoph Studer3e144d32014-05-22 16:48:40 +0200776 return null;
John Spurlock7340fc82014-04-24 18:50:12 -0400777 }
778
779 /**
780 * Removes a service from the list and unbinds.
781 */
782 private void unregisterServiceImpl(IInterface service, int userid) {
783 ManagedServiceInfo info = removeServiceImpl(service, userid);
Chris Wren51017d02015-12-15 15:34:46 -0500784 if (info != null && info.connection != null && !info.isGuest(this)) {
John Spurlock7340fc82014-04-24 18:50:12 -0400785 mContext.unbindService(info.connection);
786 }
787 }
788
789 private class SettingsObserver extends ContentObserver {
790 private final Uri mSecureSettingsUri = Settings.Secure.getUriFor(mConfig.secureSettingName);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400791 private final Uri mSecondarySettingsUri;
John Spurlock7340fc82014-04-24 18:50:12 -0400792
793 private SettingsObserver(Handler handler) {
794 super(handler);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400795 if (mConfig.secondarySettingName != null) {
796 mSecondarySettingsUri = Settings.Secure.getUriFor(mConfig.secondarySettingName);
797 } else {
798 mSecondarySettingsUri = null;
799 }
John Spurlock7340fc82014-04-24 18:50:12 -0400800 }
801
802 private void observe() {
803 ContentResolver resolver = mContext.getContentResolver();
804 resolver.registerContentObserver(mSecureSettingsUri,
805 false, this, UserHandle.USER_ALL);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400806 if (mSecondarySettingsUri != null) {
807 resolver.registerContentObserver(mSecondarySettingsUri,
808 false, this, UserHandle.USER_ALL);
809 }
John Spurlock7340fc82014-04-24 18:50:12 -0400810 update(null);
811 }
812
813 @Override
814 public void onChange(boolean selfChange, Uri uri) {
815 update(uri);
816 }
817
818 private void update(Uri uri) {
Julia Reynolds6e839b02016-04-13 10:01:17 -0400819 if (uri == null || mSecureSettingsUri.equals(uri)
820 || uri.equals(mSecondarySettingsUri)) {
821 if (DEBUG) Slog.d(TAG, "Setting changed: uri=" + uri);
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400822 rebindServices(false);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200823 rebuildRestoredPackages();
John Spurlock7340fc82014-04-24 18:50:12 -0400824 }
825 }
826 }
827
828 public class ManagedServiceInfo implements IBinder.DeathRecipient {
829 public IInterface service;
830 public ComponentName component;
831 public int userid;
832 public boolean isSystem;
833 public ServiceConnection connection;
834 public int targetSdkVersion;
835
836 public ManagedServiceInfo(IInterface service, ComponentName component,
837 int userid, boolean isSystem, ServiceConnection connection, int targetSdkVersion) {
838 this.service = service;
839 this.component = component;
840 this.userid = userid;
841 this.isSystem = isSystem;
842 this.connection = connection;
843 this.targetSdkVersion = targetSdkVersion;
844 }
845
Chris Wren51017d02015-12-15 15:34:46 -0500846 public boolean isGuest(ManagedServices host) {
847 return ManagedServices.this != host;
848 }
849
Chris Wrenab41eec2016-01-04 18:01:27 -0500850 public ManagedServices getOwner() {
851 return ManagedServices.this;
852 }
853
John Spurlocke77bb362014-04-26 10:24:59 -0400854 @Override
855 public String toString() {
856 return new StringBuilder("ManagedServiceInfo[")
857 .append("component=").append(component)
858 .append(",userid=").append(userid)
859 .append(",isSystem=").append(isSystem)
860 .append(",targetSdkVersion=").append(targetSdkVersion)
861 .append(",connection=").append(connection == null ? null : "<connection>")
862 .append(",service=").append(service)
863 .append(']').toString();
864 }
865
John Spurlock7340fc82014-04-24 18:50:12 -0400866 public boolean enabledAndUserMatches(int nid) {
867 if (!isEnabledForCurrentProfiles()) {
868 return false;
869 }
870 if (this.userid == UserHandle.USER_ALL) return true;
Chris Wren0efdb882016-03-01 17:17:47 -0500871 if (this.userid == UserHandle.USER_SYSTEM) return true;
John Spurlock7340fc82014-04-24 18:50:12 -0400872 if (nid == UserHandle.USER_ALL || nid == this.userid) return true;
873 return supportsProfiles() && mUserProfiles.isCurrentProfile(nid);
874 }
875
876 public boolean supportsProfiles() {
Dianne Hackborn955d8d62014-10-07 20:17:19 -0700877 return targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
John Spurlock7340fc82014-04-24 18:50:12 -0400878 }
879
880 @Override
881 public void binderDied() {
John Spurlocke77bb362014-04-26 10:24:59 -0400882 if (DEBUG) Slog.d(TAG, "binderDied");
John Spurlock7340fc82014-04-24 18:50:12 -0400883 // Remove the service, but don't unbind from the service. The system will bring the
884 // service back up, and the onServiceConnected handler will readd the service with the
885 // new binding. If this isn't a bound service, and is just a registered
886 // service, just removing it from the list is all we need to do anyway.
887 removeServiceImpl(this.service, this.userid);
888 }
889
890 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
891 public boolean isEnabledForCurrentProfiles() {
892 if (this.isSystem) return true;
893 if (this.connection == null) return false;
894 return mEnabledServicesForCurrentProfiles.contains(this.component);
895 }
896 }
897
Chris Wrenab41eec2016-01-04 18:01:27 -0500898 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
899 public boolean isComponentEnabledForCurrentProfiles(ComponentName component) {
900 return mEnabledServicesForCurrentProfiles.contains(component);
901 }
902
John Spurlock7340fc82014-04-24 18:50:12 -0400903 public static class UserProfiles {
904 // Profiles of the current user.
Ruben Brunke24b9a62016-02-16 21:38:24 -0800905 private final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400906
Ruben Brunke24b9a62016-02-16 21:38:24 -0800907 public void updateCache(@NonNull Context context) {
John Spurlock7340fc82014-04-24 18:50:12 -0400908 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
909 if (userManager != null) {
910 int currentUserId = ActivityManager.getCurrentUser();
911 List<UserInfo> profiles = userManager.getProfiles(currentUserId);
912 synchronized (mCurrentProfiles) {
913 mCurrentProfiles.clear();
914 for (UserInfo user : profiles) {
915 mCurrentProfiles.put(user.id, user);
916 }
917 }
918 }
919 }
920
921 public int[] getCurrentProfileIds() {
922 synchronized (mCurrentProfiles) {
923 int[] users = new int[mCurrentProfiles.size()];
924 final int N = mCurrentProfiles.size();
925 for (int i = 0; i < N; ++i) {
926 users[i] = mCurrentProfiles.keyAt(i);
927 }
928 return users;
929 }
930 }
931
932 public boolean isCurrentProfile(int userId) {
933 synchronized (mCurrentProfiles) {
934 return mCurrentProfiles.get(userId) != null;
935 }
936 }
937 }
938
Ruben Brunke24b9a62016-02-16 21:38:24 -0800939 public static class Config {
940 public String caption;
941 public String serviceInterface;
942 public String secureSettingName;
Julia Reynolds6e839b02016-04-13 10:01:17 -0400943 public String secondarySettingName;
Ruben Brunke24b9a62016-02-16 21:38:24 -0800944 public String bindPermission;
945 public String settingsAction;
946 public int clientLabel;
John Spurlock7340fc82014-04-24 18:50:12 -0400947 }
948}