blob: 11cc52df180d69232082264d5a8e511115f78c2f [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
Felipe Lemea1b79bf2016-05-24 13:06:54 -070019import static android.content.Context.BIND_ALLOW_WHITELIST_MANAGEMENT;
20import static android.content.Context.BIND_AUTO_CREATE;
21import static android.content.Context.BIND_FOREGROUND_SERVICE;
22
Ruben Brunke24b9a62016-02-16 21:38:24 -080023import android.annotation.NonNull;
John Spurlock7340fc82014-04-24 18:50:12 -040024import android.app.ActivityManager;
25import android.app.PendingIntent;
Christopher Tate6597e342015-02-17 12:15:25 -080026import android.content.BroadcastReceiver;
John Spurlock7340fc82014-04-24 18:50:12 -040027import android.content.ComponentName;
28import android.content.ContentResolver;
29import android.content.Context;
30import android.content.Intent;
Christopher Tate6597e342015-02-17 12:15:25 -080031import android.content.IntentFilter;
John Spurlock7340fc82014-04-24 18:50:12 -040032import android.content.ServiceConnection;
33import android.content.pm.ApplicationInfo;
Julia Reynoldsa75c7522017-03-21 17:34:25 -040034import android.content.pm.IPackageManager;
John Spurlock7340fc82014-04-24 18:50:12 -040035import android.content.pm.PackageManager;
36import android.content.pm.PackageManager.NameNotFoundException;
37import android.content.pm.ResolveInfo;
38import android.content.pm.ServiceInfo;
39import android.content.pm.UserInfo;
40import android.database.ContentObserver;
41import android.net.Uri;
42import android.os.Build;
43import android.os.Handler;
44import android.os.IBinder;
45import android.os.IInterface;
46import android.os.RemoteException;
Julia Reynoldsa75c7522017-03-21 17:34:25 -040047import android.os.ServiceManager;
John Spurlock7340fc82014-04-24 18:50:12 -040048import android.os.UserHandle;
49import android.os.UserManager;
50import android.provider.Settings;
51import android.text.TextUtils;
52import android.util.ArraySet;
John Spurlock4db0d982014-08-13 09:19:03 -040053import android.util.Log;
John Spurlock7340fc82014-04-24 18:50:12 -040054import android.util.Slog;
55import android.util.SparseArray;
56
John Spurlock25e2d242014-06-27 13:58:23 -040057import com.android.server.notification.NotificationManagerService.DumpFilter;
58
John Spurlock7340fc82014-04-24 18:50:12 -040059import java.io.PrintWriter;
60import java.util.ArrayList;
John Spurlocke77bb362014-04-26 10:24:59 -040061import java.util.Arrays;
Julia Reynolds9a86cc02016-02-10 15:38:15 -050062import java.util.HashSet;
John Spurlock7340fc82014-04-24 18:50:12 -040063import java.util.List;
Christopher Tate6597e342015-02-17 12:15:25 -080064import java.util.Objects;
John Spurlock7340fc82014-04-24 18:50:12 -040065import java.util.Set;
66
67/**
68 * Manages the lifecycle of application-provided services bound by system server.
69 *
70 * Services managed by this helper must have:
71 * - An associated system settings value with a list of enabled component names.
72 * - A well-known action for services to use in their intent-filter.
73 * - A system permission for services to require in order to ensure system has exclusive binding.
74 * - A settings page for user configuration of enabled services, and associated intent action.
75 * - A remote interface definition (aidl) provided by the service used for communication.
76 */
77abstract public class ManagedServices {
78 protected final String TAG = getClass().getSimpleName();
John Spurlock4db0d982014-08-13 09:19:03 -040079 protected final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
John Spurlock7340fc82014-04-24 18:50:12 -040080
Julia Reynoldsc279b992015-10-30 08:23:51 -040081 protected static final String ENABLED_SERVICES_SEPARATOR = ":";
John Spurlock7340fc82014-04-24 18:50:12 -040082
John Spurlockaf8d6c42014-05-07 17:49:08 -040083 protected final Context mContext;
John Spurlocke77bb362014-04-26 10:24:59 -040084 protected final Object mMutex;
John Spurlock7340fc82014-04-24 18:50:12 -040085 private final UserProfiles mUserProfiles;
86 private final SettingsObserver mSettingsObserver;
Julia Reynoldsa75c7522017-03-21 17:34:25 -040087 private final IPackageManager mPm;
John Spurlock7340fc82014-04-24 18:50:12 -040088 private final Config mConfig;
Christopher Tate6597e342015-02-17 12:15:25 -080089 private ArraySet<String> mRestored;
John Spurlock7340fc82014-04-24 18:50:12 -040090
91 // contains connections to all connected services, including app services
92 // and system services
93 protected final ArrayList<ManagedServiceInfo> mServices = new ArrayList<ManagedServiceInfo>();
94 // things that will be put into mServices as soon as they're ready
95 private final ArrayList<String> mServicesBinding = new ArrayList<String>();
Chris Wrenab41eec2016-01-04 18:01:27 -050096 // lists the component names of all enabled (and therefore potentially connected)
John Spurlock7340fc82014-04-24 18:50:12 -040097 // app services for current profiles.
98 private ArraySet<ComponentName> mEnabledServicesForCurrentProfiles
99 = new ArraySet<ComponentName>();
100 // Just the packages from mEnabledServicesForCurrentProfiles
101 private ArraySet<String> mEnabledServicesPackageNames = new ArraySet<String>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200102 // List of packages in restored setting across all mUserProfiles, for quick
103 // filtering upon package updates.
104 private ArraySet<String> mRestoredPackages = new ArraySet<>();
Chris Wrenab41eec2016-01-04 18:01:27 -0500105 // List of enabled packages that have nevertheless asked not to be run
106 private ArraySet<ComponentName> mSnoozingForCurrentProfiles = new ArraySet<>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200107
John Spurlock7340fc82014-04-24 18:50:12 -0400108
Christoph Studerb53dfd42014-09-12 14:45:59 +0200109 // Kept to de-dupe user change events (experienced after boot, when we receive a settings and a
110 // user change).
111 private int[] mLastSeenProfileIds;
112
Christopher Tate6597e342015-02-17 12:15:25 -0800113 private final BroadcastReceiver mRestoreReceiver;
114
John Spurlock7340fc82014-04-24 18:50:12 -0400115 public ManagedServices(Context context, Handler handler, Object mutex,
116 UserProfiles userProfiles) {
117 mContext = context;
118 mMutex = mutex;
119 mUserProfiles = userProfiles;
Julia Reynoldsa75c7522017-03-21 17:34:25 -0400120 mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
John Spurlock7340fc82014-04-24 18:50:12 -0400121 mConfig = getConfig();
122 mSettingsObserver = new SettingsObserver(handler);
Christopher Tate6597e342015-02-17 12:15:25 -0800123
124 mRestoreReceiver = new SettingRestoredReceiver();
125 IntentFilter filter = new IntentFilter(Intent.ACTION_SETTING_RESTORED);
126 context.registerReceiver(mRestoreReceiver, filter);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200127 rebuildRestoredPackages();
Christopher Tate6597e342015-02-17 12:15:25 -0800128 }
129
130 class SettingRestoredReceiver extends BroadcastReceiver {
131 @Override
132 public void onReceive(Context context, Intent intent) {
133 if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) {
134 String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400135 if (Objects.equals(element, mConfig.secureSettingName)
136 || Objects.equals(element, mConfig.secondarySettingName)) {
Christopher Tate6597e342015-02-17 12:15:25 -0800137 String prevValue = intent.getStringExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE);
138 String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE);
139 settingRestored(element, prevValue, newValue, getSendingUserId());
140 }
141 }
142 }
John Spurlock7340fc82014-04-24 18:50:12 -0400143 }
144
145 abstract protected Config getConfig();
146
147 private String getCaption() {
148 return mConfig.caption;
149 }
150
151 abstract protected IInterface asInterface(IBinder binder);
152
Chris Wren51017d02015-12-15 15:34:46 -0500153 abstract protected boolean checkType(IInterface service);
154
John Spurlock3b98b3f2014-05-01 09:08:48 -0400155 abstract protected void onServiceAdded(ManagedServiceInfo info);
John Spurlock7340fc82014-04-24 18:50:12 -0400156
John Spurlocke77bb362014-04-26 10:24:59 -0400157 protected void onServiceRemovedLocked(ManagedServiceInfo removed) { }
158
John Spurlock7340fc82014-04-24 18:50:12 -0400159 private ManagedServiceInfo newServiceInfo(IInterface service,
160 ComponentName component, int userid, boolean isSystem, ServiceConnection connection,
161 int targetSdkVersion) {
162 return new ManagedServiceInfo(service, component, userid, isSystem, connection,
163 targetSdkVersion);
164 }
165
166 public void onBootPhaseAppsCanStart() {
167 mSettingsObserver.observe();
168 }
169
John Spurlock25e2d242014-06-27 13:58:23 -0400170 public void dump(PrintWriter pw, DumpFilter filter) {
John Spurlocke77bb362014-04-26 10:24:59 -0400171 pw.println(" All " + getCaption() + "s (" + mEnabledServicesForCurrentProfiles.size()
John Spurlock7340fc82014-04-24 18:50:12 -0400172 + ") enabled for current profiles:");
173 for (ComponentName cmpt : mEnabledServicesForCurrentProfiles) {
John Spurlock25e2d242014-06-27 13:58:23 -0400174 if (filter != null && !filter.matches(cmpt)) continue;
John Spurlocke77bb362014-04-26 10:24:59 -0400175 pw.println(" " + cmpt);
John Spurlock7340fc82014-04-24 18:50:12 -0400176 }
177
John Spurlocke77bb362014-04-26 10:24:59 -0400178 pw.println(" Live " + getCaption() + "s (" + mServices.size() + "):");
John Spurlock7340fc82014-04-24 18:50:12 -0400179 for (ManagedServiceInfo info : mServices) {
John Spurlock25e2d242014-06-27 13:58:23 -0400180 if (filter != null && !filter.matches(info.component)) continue;
John Spurlocke77bb362014-04-26 10:24:59 -0400181 pw.println(" " + info.component
John Spurlock7340fc82014-04-24 18:50:12 -0400182 + " (user " + info.userid + "): " + info.service
Chris Wren51017d02015-12-15 15:34:46 -0500183 + (info.isSystem?" SYSTEM":"")
184 + (info.isGuest(this)?" GUEST":""));
John Spurlock7340fc82014-04-24 18:50:12 -0400185 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500186
187 pw.println(" Snoozed " + getCaption() + "s (" +
188 mSnoozingForCurrentProfiles.size() + "):");
189 for (ComponentName name : mSnoozingForCurrentProfiles) {
190 pw.println(" " + name.flattenToShortString());
191 }
John Spurlock7340fc82014-04-24 18:50:12 -0400192 }
193
Christopher Tate6597e342015-02-17 12:15:25 -0800194 // By convention, restored settings are replicated to another settings
195 // entry, named similarly but with a disambiguation suffix.
Julia Reynolds6e839b02016-04-13 10:01:17 -0400196 public static String restoredSettingName(String setting) {
197 return setting + ":restored";
Christopher Tate6597e342015-02-17 12:15:25 -0800198 }
199
200 // The OS has done a restore of this service's saved state. We clone it to the
201 // 'restored' reserve, and then once we return and the actual write to settings is
202 // performed, our observer will do the work of maintaining the restored vs live
203 // settings data.
204 public void settingRestored(String element, String oldValue, String newValue, int userid) {
205 if (DEBUG) Slog.d(TAG, "Restored managed service setting: " + element
206 + " ovalue=" + oldValue + " nvalue=" + newValue);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400207 if (mConfig.secureSettingName.equals(element) ||
208 mConfig.secondarySettingName.equals(element)) {
Christopher Tate6597e342015-02-17 12:15:25 -0800209 if (element != null) {
Christopher Tate6597e342015-02-17 12:15:25 -0800210 Settings.Secure.putStringForUser(mContext.getContentResolver(),
Julia Reynolds6e839b02016-04-13 10:01:17 -0400211 restoredSettingName(element),
Christopher Tate6597e342015-02-17 12:15:25 -0800212 newValue,
213 userid);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400214 updateSettingsAccordingToInstalledServices(element, userid);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200215 rebuildRestoredPackages();
Christopher Tate6597e342015-02-17 12:15:25 -0800216 }
217 }
218 }
219
John Spurlock80774932015-05-07 17:38:50 -0400220 public boolean isComponentEnabledForPackage(String pkg) {
221 return mEnabledServicesPackageNames.contains(pkg);
222 }
223
Julia Reynolds6434eb22016-08-08 17:19:26 -0400224 public void onPackagesChanged(boolean removingPackage, String[] pkgList) {
225 if (DEBUG) Slog.d(TAG, "onPackagesChanged removingPackage=" + removingPackage
John Spurlocke77bb362014-04-26 10:24:59 -0400226 + " pkgList=" + (pkgList == null ? null : Arrays.asList(pkgList))
227 + " mEnabledServicesPackageNames=" + mEnabledServicesPackageNames);
John Spurlock7340fc82014-04-24 18:50:12 -0400228 boolean anyServicesInvolved = false;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200229
John Spurlock7340fc82014-04-24 18:50:12 -0400230 if (pkgList != null && (pkgList.length > 0)) {
231 for (String pkgName : pkgList) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200232 if (mEnabledServicesPackageNames.contains(pkgName) ||
233 mRestoredPackages.contains(pkgName)) {
John Spurlock7340fc82014-04-24 18:50:12 -0400234 anyServicesInvolved = true;
235 }
236 }
237 }
238
239 if (anyServicesInvolved) {
240 // if we're not replacing a package, clean up orphaned bits
Julia Reynolds6434eb22016-08-08 17:19:26 -0400241 if (removingPackage) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200242 updateSettingsAccordingToInstalledServices();
243 rebuildRestoredPackages();
John Spurlock7340fc82014-04-24 18:50:12 -0400244 }
245 // make sure we're still bound to any of our services who may have just upgraded
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400246 rebindServices(false);
John Spurlock7340fc82014-04-24 18:50:12 -0400247 }
248 }
249
John Spurlock1b8b22b2015-05-20 09:47:13 -0400250 public void onUserSwitched(int user) {
251 if (DEBUG) Slog.d(TAG, "onUserSwitched u=" + user);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200252 rebuildRestoredPackages();
Christoph Studerb53dfd42014-09-12 14:45:59 +0200253 if (Arrays.equals(mLastSeenProfileIds, mUserProfiles.getCurrentProfileIds())) {
254 if (DEBUG) Slog.d(TAG, "Current profile IDs didn't change, skipping rebindServices().");
255 return;
256 }
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400257 rebindServices(true);
Christoph Studerb53dfd42014-09-12 14:45:59 +0200258 }
259
Julia Reynoldsa3dcaff2016-02-03 15:04:05 -0500260 public void onUserUnlocked(int user) {
261 if (DEBUG) Slog.d(TAG, "onUserUnlocked u=" + user);
262 rebuildRestoredPackages();
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400263 rebindServices(false);
Julia Reynoldsa3dcaff2016-02-03 15:04:05 -0500264 }
265
Chris Wrenab41eec2016-01-04 18:01:27 -0500266 public ManagedServiceInfo getServiceFromTokenLocked(IInterface service) {
267 if (service == null) {
268 return null;
269 }
John Spurlock7340fc82014-04-24 18:50:12 -0400270 final IBinder token = service.asBinder();
271 final int N = mServices.size();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200272 for (int i = 0; i < N; i++) {
John Spurlock7340fc82014-04-24 18:50:12 -0400273 final ManagedServiceInfo info = mServices.get(i);
274 if (info.service.asBinder() == token) return info;
275 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500276 return null;
277 }
278
279 public ManagedServiceInfo checkServiceTokenLocked(IInterface service) {
280 checkNotNull(service);
281 ManagedServiceInfo info = getServiceFromTokenLocked(service);
282 if (info != null) {
283 return info;
284 }
John Spurlock7340fc82014-04-24 18:50:12 -0400285 throw new SecurityException("Disallowed call from unknown " + getCaption() + ": "
286 + service);
287 }
288
289 public void unregisterService(IInterface service, int userid) {
290 checkNotNull(service);
291 // no need to check permissions; if your service binder is in the list,
292 // that's proof that you had permission to add it in the first place
293 unregisterServiceImpl(service, userid);
294 }
295
296 public void registerService(IInterface service, ComponentName component, int userid) {
297 checkNotNull(service);
Christoph Studer3e144d32014-05-22 16:48:40 +0200298 ManagedServiceInfo info = registerServiceImpl(service, component, userid);
299 if (info != null) {
300 onServiceAdded(info);
301 }
John Spurlock7340fc82014-04-24 18:50:12 -0400302 }
303
Chris Wren51017d02015-12-15 15:34:46 -0500304 /**
305 * Add a service to our callbacks. The lifecycle of this service is managed externally,
306 * but unlike a system service, it should not be considered privledged.
307 * */
308 public void registerGuestService(ManagedServiceInfo guest) {
309 checkNotNull(guest.service);
Ruben Brunke24b9a62016-02-16 21:38:24 -0800310 if (!checkType(guest.service)) {
311 throw new IllegalArgumentException();
312 }
Chris Wren51017d02015-12-15 15:34:46 -0500313 if (registerServiceImpl(guest) != null) {
314 onServiceAdded(guest);
Chris Wrenab41eec2016-01-04 18:01:27 -0500315 }
316 }
317
318 public void setComponentState(ComponentName component, boolean enabled) {
319 boolean previous = !mSnoozingForCurrentProfiles.contains(component);
320 if (previous == enabled) {
321 return;
322 }
323
324 if (enabled) {
325 mSnoozingForCurrentProfiles.remove(component);
326 } else {
327 mSnoozingForCurrentProfiles.add(component);
328 }
329
330 // State changed
331 if (DEBUG) {
332 Slog.d(TAG, ((enabled) ? "Enabling " : "Disabling ") + "component " +
333 component.flattenToShortString());
334 }
335
Chris Wren0efdb882016-03-01 17:17:47 -0500336
337 synchronized (mMutex) {
338 final int[] userIds = mUserProfiles.getCurrentProfileIds();
339
340 for (int userId : userIds) {
341 if (enabled) {
342 registerServiceLocked(component, userId);
343 } else {
344 unregisterServiceLocked(component, userId);
345 }
Chris Wrenab41eec2016-01-04 18:01:27 -0500346 }
Chris Wren51017d02015-12-15 15:34:46 -0500347 }
348 }
349
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200350 private void rebuildRestoredPackages() {
351 mRestoredPackages.clear();
Chris Wrenab41eec2016-01-04 18:01:27 -0500352 mSnoozingForCurrentProfiles.clear();
Julia Reynolds6e839b02016-04-13 10:01:17 -0400353 String secureSettingName = restoredSettingName(mConfig.secureSettingName);
354 String secondarySettingName = mConfig.secondarySettingName == null
355 ? null : restoredSettingName(mConfig.secondarySettingName);
John Spurlock7340fc82014-04-24 18:50:12 -0400356 int[] userIds = mUserProfiles.getCurrentProfileIds();
357 final int N = userIds.length;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200358 for (int i = 0; i < N; ++i) {
Julia Reynolds6e839b02016-04-13 10:01:17 -0400359 ArraySet<ComponentName> names =
360 loadComponentNamesFromSetting(secureSettingName, userIds[i]);
361 if (secondarySettingName != null) {
362 names.addAll(loadComponentNamesFromSetting(secondarySettingName, userIds[i]));
363 }
364 for (ComponentName name : names) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200365 mRestoredPackages.add(name.getPackageName());
366 }
John Spurlock7340fc82014-04-24 18:50:12 -0400367 }
368 }
369
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200370
Julia Reynolds6e839b02016-04-13 10:01:17 -0400371 protected @NonNull ArraySet<ComponentName> loadComponentNamesFromSetting(String settingName,
Julia Reynoldsc279b992015-10-30 08:23:51 -0400372 int userId) {
Christopher Tate6597e342015-02-17 12:15:25 -0800373 final ContentResolver cr = mContext.getContentResolver();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200374 String settingValue = Settings.Secure.getStringForUser(
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800375 cr,
376 settingName,
377 userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200378 if (TextUtils.isEmpty(settingValue))
Julia Reynolds6e839b02016-04-13 10:01:17 -0400379 return new ArraySet<>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200380 String[] restored = settingValue.split(ENABLED_SERVICES_SEPARATOR);
381 ArraySet<ComponentName> result = new ArraySet<>(restored.length);
382 for (int i = 0; i < restored.length; i++) {
383 ComponentName value = ComponentName.unflattenFromString(restored[i]);
384 if (null != value) {
385 result.add(value);
Christopher Tate6597e342015-02-17 12:15:25 -0800386 }
387 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200388 return result;
389 }
John Spurlock7340fc82014-04-24 18:50:12 -0400390
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200391 private void storeComponentsToSetting(Set<ComponentName> components,
392 String settingName,
393 int userId) {
394 String[] componentNames = null;
395 if (null != components) {
396 componentNames = new String[components.size()];
397 int index = 0;
398 for (ComponentName c: components) {
399 componentNames[index++] = c.flattenToString();
400 }
401 }
402 final String value = (componentNames == null) ? "" :
403 TextUtils.join(ENABLED_SERVICES_SEPARATOR, componentNames);
404 final ContentResolver cr = mContext.getContentResolver();
405 Settings.Secure.putStringForUser(
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800406 cr,
407 settingName,
408 value,
409 userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200410 }
411
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200412 /**
413 * Remove access for any services that no longer exist.
414 */
415 private void updateSettingsAccordingToInstalledServices() {
416 int[] userIds = mUserProfiles.getCurrentProfileIds();
417 final int N = userIds.length;
418 for (int i = 0; i < N; ++i) {
Julia Reynolds6e839b02016-04-13 10:01:17 -0400419 updateSettingsAccordingToInstalledServices(mConfig.secureSettingName, userIds[i]);
420 if (mConfig.secondarySettingName != null) {
421 updateSettingsAccordingToInstalledServices(
422 mConfig.secondarySettingName, userIds[i]);
423 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200424 }
425 rebuildRestoredPackages();
426 }
427
Julia Reynoldsc279b992015-10-30 08:23:51 -0400428 protected Set<ComponentName> queryPackageForServices(String packageName, int userId) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200429 Set<ComponentName> installed = new ArraySet<>();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200430 final PackageManager pm = mContext.getPackageManager();
Julia Reynoldsc279b992015-10-30 08:23:51 -0400431 Intent queryIntent = new Intent(mConfig.serviceInterface);
432 if (!TextUtils.isEmpty(packageName)) {
433 queryIntent.setPackage(packageName);
434 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200435 List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
Julia Reynoldsc279b992015-10-30 08:23:51 -0400436 queryIntent,
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200437 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
438 userId);
439 if (DEBUG)
440 Slog.v(TAG, mConfig.serviceInterface + " services: " + installedServices);
Julia Reynolds50f05142015-10-30 17:03:59 -0400441 if (installedServices != null) {
442 for (int i = 0, count = installedServices.size(); i < count; i++) {
443 ResolveInfo resolveInfo = installedServices.get(i);
444 ServiceInfo info = resolveInfo.serviceInfo;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200445
Julia Reynolds50f05142015-10-30 17:03:59 -0400446 ComponentName component = new ComponentName(info.packageName, info.name);
447 if (!mConfig.bindPermission.equals(info.permission)) {
448 Slog.w(TAG, "Skipping " + getCaption() + " service "
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800449 + info.packageName + "/" + info.name
450 + ": it does not require the permission "
451 + mConfig.bindPermission);
Julia Reynolds50f05142015-10-30 17:03:59 -0400452 continue;
453 }
454 installed.add(component);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200455 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200456 }
Julia Reynoldsc279b992015-10-30 08:23:51 -0400457 return installed;
458 }
459
Julia Reynolds6e839b02016-04-13 10:01:17 -0400460 private void updateSettingsAccordingToInstalledServices(String setting, int userId) {
Julia Reynoldsc279b992015-10-30 08:23:51 -0400461 boolean restoredChanged = false;
462 boolean currentChanged = false;
463 Set<ComponentName> restored =
Julia Reynolds6e839b02016-04-13 10:01:17 -0400464 loadComponentNamesFromSetting(restoredSettingName(setting), userId);
Julia Reynoldsc279b992015-10-30 08:23:51 -0400465 Set<ComponentName> current =
Julia Reynolds6e839b02016-04-13 10:01:17 -0400466 loadComponentNamesFromSetting(setting, userId);
Julia Reynoldsc279b992015-10-30 08:23:51 -0400467 // Load all services for all packages.
468 Set<ComponentName> installed = queryPackageForServices(null, userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200469
470 ArraySet<ComponentName> retained = new ArraySet<>();
471
472 for (ComponentName component : installed) {
473 if (null != restored) {
474 boolean wasRestored = restored.remove(component);
475 if (wasRestored) {
476 // Freshly installed package has service that was mentioned in restored setting.
477 if (DEBUG)
478 Slog.v(TAG, "Restoring " + component + " for user " + userId);
479 restoredChanged = true;
480 currentChanged = true;
481 retained.add(component);
John Spurlock7340fc82014-04-24 18:50:12 -0400482 continue;
483 }
John Spurlock7340fc82014-04-24 18:50:12 -0400484 }
485
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200486 if (null != current) {
487 if (current.contains(component))
488 retained.add(component);
John Spurlock7340fc82014-04-24 18:50:12 -0400489 }
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200490 }
491
492 currentChanged |= ((current == null ? 0 : current.size()) != retained.size());
493
494 if (currentChanged) {
495 if (DEBUG) Slog.v(TAG, "List of " + getCaption() + " services was updated " + current);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400496 storeComponentsToSetting(retained, setting, userId);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200497 }
498
499 if (restoredChanged) {
500 if (DEBUG) Slog.v(TAG,
501 "List of " + getCaption() + " restored services was updated " + restored);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400502 storeComponentsToSetting(restored, restoredSettingName(setting), userId);
John Spurlock7340fc82014-04-24 18:50:12 -0400503 }
504 }
505
506 /**
507 * Called whenever packages change, the user switches, or the secure setting
508 * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
509 */
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400510 private void rebindServices(boolean forceRebind) {
John Spurlock7340fc82014-04-24 18:50:12 -0400511 if (DEBUG) Slog.d(TAG, "rebindServices");
512 final int[] userIds = mUserProfiles.getCurrentProfileIds();
513 final int nUserIds = userIds.length;
514
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200515 final SparseArray<ArraySet<ComponentName>> componentsByUser = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400516
517 for (int i = 0; i < nUserIds; ++i) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200518 componentsByUser.put(userIds[i],
519 loadComponentNamesFromSetting(mConfig.secureSettingName, userIds[i]));
Julia Reynolds6e839b02016-04-13 10:01:17 -0400520 if (mConfig.secondarySettingName != null) {
521 componentsByUser.get(userIds[i]).addAll(
522 loadComponentNamesFromSetting(mConfig.secondarySettingName, userIds[i]));
523 }
John Spurlock7340fc82014-04-24 18:50:12 -0400524 }
525
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500526 final ArrayList<ManagedServiceInfo> removableBoundServices = new ArrayList<>();
527 final SparseArray<Set<ComponentName>> toAdd = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400528
529 synchronized (mMutex) {
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400530 // Rebind to non-system services if user switched
Christoph Studer5d423842014-05-23 13:15:54 +0200531 for (ManagedServiceInfo service : mServices) {
Chris Wren51017d02015-12-15 15:34:46 -0500532 if (!service.isSystem && !service.isGuest(this)) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500533 removableBoundServices.add(service);
Christoph Studer5d423842014-05-23 13:15:54 +0200534 }
535 }
John Spurlock7340fc82014-04-24 18:50:12 -0400536
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400537 mEnabledServicesForCurrentProfiles.clear();
538 mEnabledServicesPackageNames.clear();
John Spurlock7340fc82014-04-24 18:50:12 -0400539
540 for (int i = 0; i < nUserIds; ++i) {
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200541 // decode the list of components
542 final ArraySet<ComponentName> userComponents = componentsByUser.get(userIds[i]);
543 if (null == userComponents) {
Julia Reynolds6e839b02016-04-13 10:01:17 -0400544 toAdd.put(userIds[i], new ArraySet<ComponentName>());
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200545 continue;
546 }
547
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500548 final Set<ComponentName> add = new HashSet<>(userComponents);
Chris Wrenab41eec2016-01-04 18:01:27 -0500549 add.removeAll(mSnoozingForCurrentProfiles);
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800550
John Spurlock7340fc82014-04-24 18:50:12 -0400551 toAdd.put(userIds[i], add);
552
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400553 mEnabledServicesForCurrentProfiles.addAll(userComponents);
John Spurlock7340fc82014-04-24 18:50:12 -0400554
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200555 for (int j = 0; j < userComponents.size(); j++) {
Chris Wren083094c2015-12-15 16:25:07 -0500556 final ComponentName component = userComponents.valueAt(j);
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400557 mEnabledServicesPackageNames.add(component.getPackageName());
John Spurlock7340fc82014-04-24 18:50:12 -0400558 }
559 }
John Spurlock7340fc82014-04-24 18:50:12 -0400560 }
561
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500562 for (ManagedServiceInfo info : removableBoundServices) {
John Spurlock7340fc82014-04-24 18:50:12 -0400563 final ComponentName component = info.component;
564 final int oldUser = info.userid;
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500565 final Set<ComponentName> allowedComponents = toAdd.get(info.userid);
566 if (allowedComponents != null) {
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400567 if (allowedComponents.contains(component) && !forceRebind) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500568 // Already bound, don't need to bind again.
569 allowedComponents.remove(component);
570 } else {
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400571 // No longer allowed to be bound, or must rebind.
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500572 Slog.v(TAG, "disabling " + getCaption() + " for user "
573 + oldUser + ": " + component);
574 unregisterService(component, oldUser);
575 }
576 }
John Spurlock7340fc82014-04-24 18:50:12 -0400577 }
578
579 for (int i = 0; i < nUserIds; ++i) {
Julia Reynolds9a86cc02016-02-10 15:38:15 -0500580 final Set<ComponentName> add = toAdd.get(userIds[i]);
581 for (ComponentName component : add) {
Julia Reynoldsa75c7522017-03-21 17:34:25 -0400582 try {
583 ServiceInfo info = mPm.getServiceInfo(component,
584 PackageManager.MATCH_DIRECT_BOOT_AWARE
585 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userIds[i]);
586 if (!mConfig.bindPermission.equals(info.permission)) {
587 Slog.w(TAG, "Skipping " + getCaption() + " service " + component
588 + ": it does not require the permission " + mConfig.bindPermission);
589 continue;
590 }
591 Slog.v(TAG,
592 "enabling " + getCaption() + " for " + userIds[i] + ": " + component);
593 registerService(component, userIds[i]);
594 } catch (RemoteException e) {
595 e.rethrowFromSystemServer();
596 }
John Spurlock7340fc82014-04-24 18:50:12 -0400597 }
598 }
Christoph Studerb53dfd42014-09-12 14:45:59 +0200599
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200600 mLastSeenProfileIds = userIds;
John Spurlock7340fc82014-04-24 18:50:12 -0400601 }
602
603 /**
604 * Version of registerService that takes the name of a service component to bind to.
605 */
606 private void registerService(final ComponentName name, final int userid) {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800607 synchronized (mMutex) {
608 registerServiceLocked(name, userid);
609 }
610 }
611
Chris Wren0efdb882016-03-01 17:17:47 -0500612 /**
613 * Inject a system service into the management list.
614 */
615 public void registerSystemService(final ComponentName name, final int userid) {
616 synchronized (mMutex) {
617 registerServiceLocked(name, userid, true /* isSystem */);
618 }
619 }
620
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800621 private void registerServiceLocked(final ComponentName name, final int userid) {
Chris Wren0efdb882016-03-01 17:17:47 -0500622 registerServiceLocked(name, userid, false /* isSystem */);
623 }
624
625 private void registerServiceLocked(final ComponentName name, final int userid,
626 final boolean isSystem) {
John Spurlock7340fc82014-04-24 18:50:12 -0400627 if (DEBUG) Slog.v(TAG, "registerService: " + name + " u=" + userid);
628
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800629 final String servicesBindingTag = name.toString() + "/" + userid;
630 if (mServicesBinding.contains(servicesBindingTag)) {
631 // stop registering this thing already! we're working on it
632 return;
633 }
634 mServicesBinding.add(servicesBindingTag);
John Spurlock7340fc82014-04-24 18:50:12 -0400635
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800636 final int N = mServices.size();
637 for (int i = N - 1; i >= 0; i--) {
638 final ManagedServiceInfo info = mServices.get(i);
639 if (name.equals(info.component)
640 && info.userid == userid) {
641 // cut old connections
642 if (DEBUG) Slog.v(TAG, " disconnecting old " + getCaption() + ": "
643 + info.service);
644 removeServiceLocked(i);
645 if (info.connection != null) {
646 mContext.unbindService(info.connection);
John Spurlock7340fc82014-04-24 18:50:12 -0400647 }
648 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800649 }
John Spurlock7340fc82014-04-24 18:50:12 -0400650
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800651 Intent intent = new Intent(mConfig.serviceInterface);
652 intent.setComponent(name);
John Spurlock7340fc82014-04-24 18:50:12 -0400653
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800654 intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel);
John Spurlock7340fc82014-04-24 18:50:12 -0400655
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800656 final PendingIntent pendingIntent = PendingIntent.getActivity(
657 mContext, 0, new Intent(mConfig.settingsAction), 0);
658 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
John Spurlock7340fc82014-04-24 18:50:12 -0400659
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800660 ApplicationInfo appInfo = null;
661 try {
662 appInfo = mContext.getPackageManager().getApplicationInfo(
663 name.getPackageName(), 0);
664 } catch (NameNotFoundException e) {
665 // Ignore if the package doesn't exist we won't be able to bind to the service.
666 }
667 final int targetSdkVersion =
668 appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE;
John Spurlock7340fc82014-04-24 18:50:12 -0400669
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800670 try {
671 if (DEBUG) Slog.v(TAG, "binding: " + intent);
672 ServiceConnection serviceConnection = new ServiceConnection() {
673 IInterface mService;
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200674
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800675 @Override
676 public void onServiceConnected(ComponentName name, IBinder binder) {
677 boolean added = false;
678 ManagedServiceInfo info = null;
679 synchronized (mMutex) {
680 mServicesBinding.remove(servicesBindingTag);
681 try {
682 mService = asInterface(binder);
683 info = newServiceInfo(mService, name,
Chris Wren0efdb882016-03-01 17:17:47 -0500684 userid, isSystem, this, targetSdkVersion);
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800685 binder.linkToDeath(info, 0);
686 added = mServices.add(info);
687 } catch (RemoteException e) {
688 // already dead
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200689 }
690 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800691 if (added) {
692 onServiceAdded(info);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200693 }
John Spurlock7340fc82014-04-24 18:50:12 -0400694 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800695
696 @Override
697 public void onServiceDisconnected(ComponentName name) {
698 Slog.v(TAG, getCaption() + " connection lost: " + name);
699 }
700 };
701 if (!mContext.bindServiceAsUser(intent,
702 serviceConnection,
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700703 BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE | BIND_ALLOW_WHITELIST_MANAGEMENT,
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800704 new UserHandle(userid))) {
705 mServicesBinding.remove(servicesBindingTag);
706 Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent);
John Spurlock7340fc82014-04-24 18:50:12 -0400707 return;
708 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800709 } catch (SecurityException ex) {
710 Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex);
711 return;
John Spurlock7340fc82014-04-24 18:50:12 -0400712 }
713 }
714
715 /**
716 * Remove a service for the given user by ComponentName
717 */
718 private void unregisterService(ComponentName name, int userid) {
719 synchronized (mMutex) {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -0800720 unregisterServiceLocked(name, userid);
721 }
722 }
723
724 private void unregisterServiceLocked(ComponentName name, int userid) {
725 final int N = mServices.size();
726 for (int i = N - 1; i >= 0; i--) {
727 final ManagedServiceInfo info = mServices.get(i);
728 if (name.equals(info.component)
729 && info.userid == userid) {
730 removeServiceLocked(i);
731 if (info.connection != null) {
732 try {
733 mContext.unbindService(info.connection);
734 } catch (IllegalArgumentException ex) {
735 // something happened to the service: we think we have a connection
736 // but it's bogus.
737 Slog.e(TAG, getCaption() + " " + name + " could not be unbound: " + ex);
John Spurlock7340fc82014-04-24 18:50:12 -0400738 }
739 }
740 }
741 }
742 }
743
744 /**
745 * Removes a service from the list but does not unbind
746 *
747 * @return the removed service.
748 */
749 private ManagedServiceInfo removeServiceImpl(IInterface service, final int userid) {
John Spurlocke77bb362014-04-26 10:24:59 -0400750 if (DEBUG) Slog.d(TAG, "removeServiceImpl service=" + service + " u=" + userid);
John Spurlock7340fc82014-04-24 18:50:12 -0400751 ManagedServiceInfo serviceInfo = null;
752 synchronized (mMutex) {
753 final int N = mServices.size();
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200754 for (int i = N - 1; i >= 0; i--) {
John Spurlock7340fc82014-04-24 18:50:12 -0400755 final ManagedServiceInfo info = mServices.get(i);
756 if (info.service.asBinder() == service.asBinder()
757 && info.userid == userid) {
John Spurlocke77bb362014-04-26 10:24:59 -0400758 if (DEBUG) Slog.d(TAG, "Removing active service " + info.component);
759 serviceInfo = removeServiceLocked(i);
John Spurlock7340fc82014-04-24 18:50:12 -0400760 }
761 }
762 }
763 return serviceInfo;
764 }
765
John Spurlocke77bb362014-04-26 10:24:59 -0400766 private ManagedServiceInfo removeServiceLocked(int i) {
767 final ManagedServiceInfo info = mServices.remove(i);
768 onServiceRemovedLocked(info);
769 return info;
770 }
771
John Spurlock7340fc82014-04-24 18:50:12 -0400772 private void checkNotNull(IInterface service) {
773 if (service == null) {
774 throw new IllegalArgumentException(getCaption() + " must not be null");
775 }
776 }
777
Christoph Studer3e144d32014-05-22 16:48:40 +0200778 private ManagedServiceInfo registerServiceImpl(final IInterface service,
John Spurlock7340fc82014-04-24 18:50:12 -0400779 final ComponentName component, final int userid) {
Chris Wren51017d02015-12-15 15:34:46 -0500780 ManagedServiceInfo info = newServiceInfo(service, component, userid,
781 true /*isSystem*/, null /*connection*/, Build.VERSION_CODES.LOLLIPOP);
782 return registerServiceImpl(info);
783 }
784
785 private ManagedServiceInfo registerServiceImpl(ManagedServiceInfo info) {
John Spurlock7340fc82014-04-24 18:50:12 -0400786 synchronized (mMutex) {
787 try {
Chris Wren51017d02015-12-15 15:34:46 -0500788 info.service.asBinder().linkToDeath(info, 0);
John Spurlock7340fc82014-04-24 18:50:12 -0400789 mServices.add(info);
Christoph Studer3e144d32014-05-22 16:48:40 +0200790 return info;
John Spurlock7340fc82014-04-24 18:50:12 -0400791 } catch (RemoteException e) {
792 // already dead
793 }
794 }
Christoph Studer3e144d32014-05-22 16:48:40 +0200795 return null;
John Spurlock7340fc82014-04-24 18:50:12 -0400796 }
797
798 /**
799 * Removes a service from the list and unbinds.
800 */
801 private void unregisterServiceImpl(IInterface service, int userid) {
802 ManagedServiceInfo info = removeServiceImpl(service, userid);
Chris Wren51017d02015-12-15 15:34:46 -0500803 if (info != null && info.connection != null && !info.isGuest(this)) {
John Spurlock7340fc82014-04-24 18:50:12 -0400804 mContext.unbindService(info.connection);
805 }
806 }
807
808 private class SettingsObserver extends ContentObserver {
809 private final Uri mSecureSettingsUri = Settings.Secure.getUriFor(mConfig.secureSettingName);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400810 private final Uri mSecondarySettingsUri;
John Spurlock7340fc82014-04-24 18:50:12 -0400811
812 private SettingsObserver(Handler handler) {
813 super(handler);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400814 if (mConfig.secondarySettingName != null) {
815 mSecondarySettingsUri = Settings.Secure.getUriFor(mConfig.secondarySettingName);
816 } else {
817 mSecondarySettingsUri = null;
818 }
John Spurlock7340fc82014-04-24 18:50:12 -0400819 }
820
821 private void observe() {
822 ContentResolver resolver = mContext.getContentResolver();
823 resolver.registerContentObserver(mSecureSettingsUri,
824 false, this, UserHandle.USER_ALL);
Julia Reynolds6e839b02016-04-13 10:01:17 -0400825 if (mSecondarySettingsUri != null) {
826 resolver.registerContentObserver(mSecondarySettingsUri,
827 false, this, UserHandle.USER_ALL);
828 }
John Spurlock7340fc82014-04-24 18:50:12 -0400829 update(null);
830 }
831
832 @Override
833 public void onChange(boolean selfChange, Uri uri) {
834 update(uri);
835 }
836
837 private void update(Uri uri) {
Julia Reynolds6e839b02016-04-13 10:01:17 -0400838 if (uri == null || mSecureSettingsUri.equals(uri)
839 || uri.equals(mSecondarySettingsUri)) {
840 if (DEBUG) Slog.d(TAG, "Setting changed: uri=" + uri);
Julia Reynolds1c9bd422016-03-15 09:25:56 -0400841 rebindServices(false);
Denis Kuznetsov76c02682015-09-17 19:57:00 +0200842 rebuildRestoredPackages();
John Spurlock7340fc82014-04-24 18:50:12 -0400843 }
844 }
845 }
846
847 public class ManagedServiceInfo implements IBinder.DeathRecipient {
848 public IInterface service;
849 public ComponentName component;
850 public int userid;
851 public boolean isSystem;
852 public ServiceConnection connection;
853 public int targetSdkVersion;
854
855 public ManagedServiceInfo(IInterface service, ComponentName component,
856 int userid, boolean isSystem, ServiceConnection connection, int targetSdkVersion) {
857 this.service = service;
858 this.component = component;
859 this.userid = userid;
860 this.isSystem = isSystem;
861 this.connection = connection;
862 this.targetSdkVersion = targetSdkVersion;
863 }
864
Chris Wren51017d02015-12-15 15:34:46 -0500865 public boolean isGuest(ManagedServices host) {
866 return ManagedServices.this != host;
867 }
868
Chris Wrenab41eec2016-01-04 18:01:27 -0500869 public ManagedServices getOwner() {
870 return ManagedServices.this;
871 }
872
John Spurlocke77bb362014-04-26 10:24:59 -0400873 @Override
874 public String toString() {
875 return new StringBuilder("ManagedServiceInfo[")
876 .append("component=").append(component)
877 .append(",userid=").append(userid)
878 .append(",isSystem=").append(isSystem)
879 .append(",targetSdkVersion=").append(targetSdkVersion)
880 .append(",connection=").append(connection == null ? null : "<connection>")
881 .append(",service=").append(service)
882 .append(']').toString();
883 }
884
John Spurlock7340fc82014-04-24 18:50:12 -0400885 public boolean enabledAndUserMatches(int nid) {
886 if (!isEnabledForCurrentProfiles()) {
887 return false;
888 }
889 if (this.userid == UserHandle.USER_ALL) return true;
Chris Wren0c4eeb42016-05-12 13:21:08 -0400890 if (this.isSystem) return true;
John Spurlock7340fc82014-04-24 18:50:12 -0400891 if (nid == UserHandle.USER_ALL || nid == this.userid) return true;
892 return supportsProfiles() && mUserProfiles.isCurrentProfile(nid);
893 }
894
895 public boolean supportsProfiles() {
Dianne Hackborn955d8d62014-10-07 20:17:19 -0700896 return targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
John Spurlock7340fc82014-04-24 18:50:12 -0400897 }
898
899 @Override
900 public void binderDied() {
John Spurlocke77bb362014-04-26 10:24:59 -0400901 if (DEBUG) Slog.d(TAG, "binderDied");
John Spurlock7340fc82014-04-24 18:50:12 -0400902 // Remove the service, but don't unbind from the service. The system will bring the
903 // service back up, and the onServiceConnected handler will readd the service with the
904 // new binding. If this isn't a bound service, and is just a registered
905 // service, just removing it from the list is all we need to do anyway.
906 removeServiceImpl(this.service, this.userid);
907 }
908
909 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
910 public boolean isEnabledForCurrentProfiles() {
911 if (this.isSystem) return true;
912 if (this.connection == null) return false;
913 return mEnabledServicesForCurrentProfiles.contains(this.component);
914 }
915 }
916
Chris Wrenab41eec2016-01-04 18:01:27 -0500917 /** convenience method for looking in mEnabledServicesForCurrentProfiles */
918 public boolean isComponentEnabledForCurrentProfiles(ComponentName component) {
919 return mEnabledServicesForCurrentProfiles.contains(component);
920 }
921
John Spurlock7340fc82014-04-24 18:50:12 -0400922 public static class UserProfiles {
923 // Profiles of the current user.
Ruben Brunke24b9a62016-02-16 21:38:24 -0800924 private final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<>();
John Spurlock7340fc82014-04-24 18:50:12 -0400925
Ruben Brunke24b9a62016-02-16 21:38:24 -0800926 public void updateCache(@NonNull Context context) {
John Spurlock7340fc82014-04-24 18:50:12 -0400927 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
928 if (userManager != null) {
929 int currentUserId = ActivityManager.getCurrentUser();
930 List<UserInfo> profiles = userManager.getProfiles(currentUserId);
931 synchronized (mCurrentProfiles) {
932 mCurrentProfiles.clear();
933 for (UserInfo user : profiles) {
934 mCurrentProfiles.put(user.id, user);
935 }
936 }
937 }
938 }
939
940 public int[] getCurrentProfileIds() {
941 synchronized (mCurrentProfiles) {
942 int[] users = new int[mCurrentProfiles.size()];
943 final int N = mCurrentProfiles.size();
944 for (int i = 0; i < N; ++i) {
945 users[i] = mCurrentProfiles.keyAt(i);
946 }
947 return users;
948 }
949 }
950
951 public boolean isCurrentProfile(int userId) {
952 synchronized (mCurrentProfiles) {
953 return mCurrentProfiles.get(userId) != null;
954 }
955 }
956 }
957
Ruben Brunke24b9a62016-02-16 21:38:24 -0800958 public static class Config {
959 public String caption;
960 public String serviceInterface;
961 public String secureSettingName;
Julia Reynolds6e839b02016-04-13 10:01:17 -0400962 public String secondarySettingName;
Ruben Brunke24b9a62016-02-16 21:38:24 -0800963 public String bindPermission;
964 public String settingsAction;
965 public int clientLabel;
John Spurlock7340fc82014-04-24 18:50:12 -0400966 }
967}