Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| 17 | package com.android.server; |
| 18 | |
Lifu Tang | 66f761c | 2017-11-13 15:51:54 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 25 | import android.content.IntentFilter; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 26 | import android.content.ServiceConnection; |
| 27 | import android.content.pm.PackageInfo; |
| 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.pm.PackageManager.NameNotFoundException; |
| 30 | import android.content.pm.ResolveInfo; |
| 31 | import android.content.pm.Signature; |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 32 | import android.content.res.Resources; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 33 | import android.os.Handler; |
| 34 | import android.os.IBinder; |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 35 | import android.os.UserHandle; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 36 | import android.util.Log; |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 37 | import android.util.Slog; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 38 | |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 39 | import com.android.internal.annotations.GuardedBy; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 40 | import com.android.internal.content.PackageMonitor; |
| 41 | |
| 42 | import java.util.ArrayList; |
| 43 | import java.util.Arrays; |
| 44 | import java.util.HashSet; |
| 45 | import java.util.List; |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 46 | import java.util.Objects; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Find the best Service, and bind to it. |
| 50 | * Handles run-time package changes. |
| 51 | */ |
| 52 | public class ServiceWatcher implements ServiceConnection { |
| 53 | private static final boolean D = false; |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 54 | public static final String EXTRA_SERVICE_VERSION = "serviceVersion"; |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 55 | public static final String EXTRA_SERVICE_IS_MULTIUSER = "serviceIsMultiuser"; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 56 | |
| 57 | private final String mTag; |
| 58 | private final Context mContext; |
| 59 | private final PackageManager mPm; |
| 60 | private final List<HashSet<Signature>> mSignatureSets; |
| 61 | private final String mAction; |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * If mServicePackageName is not null, only this package will be searched for the service that |
| 65 | * implements mAction. When null, all packages in the system that matches one of the signature |
| 66 | * in mSignatureSets are searched. |
| 67 | */ |
| 68 | private final String mServicePackageName; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 69 | private final Runnable mNewServiceWork; |
| 70 | private final Handler mHandler; |
| 71 | |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 72 | private final Object mLock = new Object(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 73 | |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 74 | @GuardedBy("mLock") |
| 75 | private int mCurrentUserId = UserHandle.USER_SYSTEM; |
| 76 | |
| 77 | @GuardedBy("mLock") |
| 78 | private IBinder mBoundService; |
| 79 | @GuardedBy("mLock") |
| 80 | private ComponentName mBoundComponent; |
| 81 | @GuardedBy("mLock") |
| 82 | private String mBoundPackageName; |
| 83 | @GuardedBy("mLock") |
| 84 | private int mBoundVersion = Integer.MIN_VALUE; |
| 85 | @GuardedBy("mLock") |
| 86 | private int mBoundUserId = UserHandle.USER_NULL; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 87 | |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 88 | public static ArrayList<HashSet<Signature>> getSignatureSets(Context context, |
| 89 | List<String> initialPackageNames) { |
| 90 | PackageManager pm = context.getPackageManager(); |
| 91 | ArrayList<HashSet<Signature>> sigSets = new ArrayList<HashSet<Signature>>(); |
| 92 | for (int i = 0, size = initialPackageNames.size(); i < size; i++) { |
| 93 | String pkg = initialPackageNames.get(i); |
| 94 | try { |
| 95 | HashSet<Signature> set = new HashSet<Signature>(); |
Jeff Sharkey | c7bacab | 2016-02-09 15:56:11 -0700 | [diff] [blame] | 96 | Signature[] sigs = pm.getPackageInfo(pkg, PackageManager.MATCH_SYSTEM_ONLY |
| 97 | | PackageManager.GET_SIGNATURES).signatures; |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 98 | set.addAll(Arrays.asList(sigs)); |
| 99 | sigSets.add(set); |
| 100 | } catch (NameNotFoundException e) { |
| 101 | Log.w("ServiceWatcher", pkg + " not found"); |
| 102 | } |
| 103 | } |
| 104 | return sigSets; |
| 105 | } |
| 106 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 107 | public ServiceWatcher(Context context, String logTag, String action, |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 108 | int overlaySwitchResId, int defaultServicePackageNameResId, |
| 109 | int initialPackageNamesResId, Runnable newServiceWork, |
| 110 | Handler handler) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 111 | mContext = context; |
| 112 | mTag = logTag; |
| 113 | mAction = action; |
| 114 | mPm = mContext.getPackageManager(); |
| 115 | mNewServiceWork = newServiceWork; |
| 116 | mHandler = handler; |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 117 | Resources resources = context.getResources(); |
| 118 | |
| 119 | // Whether to enable service overlay. |
| 120 | boolean enableOverlay = resources.getBoolean(overlaySwitchResId); |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 121 | ArrayList<String> initialPackageNames = new ArrayList<String>(); |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 122 | if (enableOverlay) { |
| 123 | // A list of package names used to create the signatures. |
| 124 | String[] pkgs = resources.getStringArray(initialPackageNamesResId); |
| 125 | if (pkgs != null) initialPackageNames.addAll(Arrays.asList(pkgs)); |
| 126 | mServicePackageName = null; |
| 127 | if (D) Log.d(mTag, "Overlay enabled, packages=" + Arrays.toString(pkgs)); |
| 128 | } else { |
| 129 | // The default package name that is searched for service implementation when overlay is |
| 130 | // disabled. |
| 131 | String servicePackageName = resources.getString(defaultServicePackageNameResId); |
| 132 | if (servicePackageName != null) initialPackageNames.add(servicePackageName); |
| 133 | mServicePackageName = servicePackageName; |
| 134 | if (D) Log.d(mTag, "Overlay disabled, default package=" + servicePackageName); |
| 135 | } |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 136 | mSignatureSets = getSignatureSets(context, initialPackageNames); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Start this watcher, including binding to the current best match and |
| 141 | * re-binding to any better matches down the road. |
| 142 | * <p> |
| 143 | * Note that if there are no matching encryption-aware services, we may not |
| 144 | * bind to a real service until after the current user is unlocked. |
Jeff Sharkey | 05ed25c | 2016-09-20 17:20:17 -0600 | [diff] [blame] | 145 | * |
| 146 | * @returns {@code true} if a potential service implementation was found. |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 147 | */ |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 148 | public boolean start() { |
Jeff Sharkey | 05ed25c | 2016-09-20 17:20:17 -0600 | [diff] [blame] | 149 | if (isServiceMissing()) return false; |
| 150 | |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 151 | synchronized (mLock) { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 152 | bindBestPackageLocked(mServicePackageName, false); |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 153 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 154 | |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 155 | // listen for user change |
| 156 | IntentFilter intentFilter = new IntentFilter(); |
| 157 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 158 | intentFilter.addAction(Intent.ACTION_USER_UNLOCKED); |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 159 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
| 160 | @Override |
| 161 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 162 | final String action = intent.getAction(); |
| 163 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 164 | UserHandle.USER_NULL); |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 165 | if (Intent.ACTION_USER_SWITCHED.equals(action)) { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 166 | switchUser(userId); |
| 167 | } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) { |
| 168 | unlockUser(userId); |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | }, UserHandle.ALL, intentFilter, null, mHandler); |
| 172 | |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 173 | // listen for relevant package changes if service overlay is enabled. |
| 174 | if (mServicePackageName == null) { |
| 175 | mPackageMonitor.register(mContext, null, UserHandle.ALL, true); |
| 176 | } |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 177 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
| 181 | /** |
Jeff Sharkey | 05ed25c | 2016-09-20 17:20:17 -0600 | [diff] [blame] | 182 | * Check if any instance of this service is present on the device, |
| 183 | * regardless of it being encryption-aware or not. |
| 184 | */ |
| 185 | private boolean isServiceMissing() { |
| 186 | final Intent intent = new Intent(mAction); |
| 187 | final int flags = PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 188 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE; |
| 189 | return mPm.queryIntentServicesAsUser(intent, flags, mCurrentUserId).isEmpty(); |
| 190 | } |
| 191 | |
| 192 | /** |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 193 | * Searches and binds to the best package, or do nothing if the best package |
| 194 | * is already bound, unless force rebinding is requested. |
| 195 | * |
| 196 | * @param justCheckThisPackage Only consider this package, or consider all |
| 197 | * packages if it is {@code null}. |
| 198 | * @param forceRebind Force a rebinding to the best package if it's already |
| 199 | * bound. |
Jeff Sharkey | 05ed25c | 2016-09-20 17:20:17 -0600 | [diff] [blame] | 200 | * @returns {@code true} if a valid package was found to bind to. |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 201 | */ |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 202 | private boolean bindBestPackageLocked(String justCheckThisPackage, boolean forceRebind) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 203 | Intent intent = new Intent(mAction); |
| 204 | if (justCheckThisPackage != null) { |
| 205 | intent.setPackage(justCheckThisPackage); |
| 206 | } |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 207 | final List<ResolveInfo> rInfos = mPm.queryIntentServicesAsUser(intent, |
| 208 | PackageManager.GET_META_DATA | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 209 | mCurrentUserId); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 210 | int bestVersion = Integer.MIN_VALUE; |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 211 | ComponentName bestComponent = null; |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 212 | boolean bestIsMultiuser = false; |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 213 | if (rInfos != null) { |
| 214 | for (ResolveInfo rInfo : rInfos) { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 215 | final ComponentName component = rInfo.serviceInfo.getComponentName(); |
| 216 | final String packageName = component.getPackageName(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 217 | |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 218 | // check signature |
| 219 | try { |
| 220 | PackageInfo pInfo; |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 221 | pInfo = mPm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES |
| 222 | | PackageManager.MATCH_DEBUG_TRIAGED_MISSING); |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 223 | if (!isSignatureMatch(pInfo.signatures)) { |
| 224 | Log.w(mTag, packageName + " resolves service " + mAction |
| 225 | + ", but has wrong signature, ignoring"); |
| 226 | continue; |
| 227 | } |
| 228 | } catch (NameNotFoundException e) { |
| 229 | Log.wtf(mTag, e); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 230 | continue; |
| 231 | } |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 232 | |
| 233 | // check metadata |
| 234 | int version = Integer.MIN_VALUE; |
| 235 | boolean isMultiuser = false; |
| 236 | if (rInfo.serviceInfo.metaData != null) { |
| 237 | version = rInfo.serviceInfo.metaData.getInt( |
| 238 | EXTRA_SERVICE_VERSION, Integer.MIN_VALUE); |
| 239 | isMultiuser = rInfo.serviceInfo.metaData.getBoolean(EXTRA_SERVICE_IS_MULTIUSER); |
| 240 | } |
| 241 | |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 242 | if (version > bestVersion) { |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 243 | bestVersion = version; |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 244 | bestComponent = component; |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 245 | bestIsMultiuser = isMultiuser; |
| 246 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 249 | if (D) { |
| 250 | Log.d(mTag, String.format("bindBestPackage for %s : %s found %d, %s", mAction, |
| 251 | (justCheckThisPackage == null ? "" |
| 252 | : "(" + justCheckThisPackage + ") "), rInfos.size(), |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 253 | (bestComponent == null ? "no new best component" |
| 254 | : "new best component: " + bestComponent))); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 255 | } |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 256 | } else { |
| 257 | if (D) Log.d(mTag, "Unable to query intent services for action: " + mAction); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 258 | } |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 259 | |
| 260 | if (bestComponent == null) { |
| 261 | Slog.w(mTag, "Odd, no component found for service " + mAction); |
| 262 | unbindLocked(); |
| 263 | return false; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 264 | } |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 265 | |
| 266 | final int userId = bestIsMultiuser ? UserHandle.USER_SYSTEM : mCurrentUserId; |
| 267 | final boolean alreadyBound = Objects.equals(bestComponent, mBoundComponent) |
| 268 | && bestVersion == mBoundVersion && userId == mBoundUserId; |
| 269 | if (forceRebind || !alreadyBound) { |
| 270 | unbindLocked(); |
| 271 | bindToPackageLocked(bestComponent, bestVersion, userId); |
| 272 | } |
| 273 | return true; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 276 | private void unbindLocked() { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 277 | ComponentName component; |
| 278 | component = mBoundComponent; |
| 279 | mBoundComponent = null; |
| 280 | mBoundPackageName = null; |
| 281 | mBoundVersion = Integer.MIN_VALUE; |
| 282 | mBoundUserId = UserHandle.USER_NULL; |
| 283 | if (component != null) { |
| 284 | if (D) Log.d(mTag, "unbinding " + component); |
Lifu Tang | eb298df | 2017-11-22 12:13:12 -0800 | [diff] [blame] | 285 | mBoundService = null; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 286 | mContext.unbindService(this); |
| 287 | } |
| 288 | } |
| 289 | |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 290 | private void bindToPackageLocked(ComponentName component, int version, int userId) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 291 | Intent intent = new Intent(mAction); |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 292 | intent.setComponent(component); |
| 293 | mBoundComponent = component; |
| 294 | mBoundPackageName = component.getPackageName(); |
| 295 | mBoundVersion = version; |
| 296 | mBoundUserId = userId; |
| 297 | if (D) Log.d(mTag, "binding " + component + " (v" + version + ") (u" + userId + ")"); |
| 298 | mContext.bindServiceAsUser(intent, this, |
| 299 | Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND | Context.BIND_NOT_VISIBLE, |
| 300 | new UserHandle(userId)); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 303 | public static boolean isSignatureMatch(Signature[] signatures, |
| 304 | List<HashSet<Signature>> sigSets) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 305 | if (signatures == null) return false; |
| 306 | |
| 307 | // build hashset of input to test against |
| 308 | HashSet<Signature> inputSet = new HashSet<Signature>(); |
| 309 | for (Signature s : signatures) { |
| 310 | inputSet.add(s); |
| 311 | } |
| 312 | |
| 313 | // test input against each of the signature sets |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 314 | for (HashSet<Signature> referenceSet : sigSets) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 315 | if (referenceSet.equals(inputSet)) { |
| 316 | return true; |
| 317 | } |
| 318 | } |
| 319 | return false; |
| 320 | } |
| 321 | |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 322 | private boolean isSignatureMatch(Signature[] signatures) { |
| 323 | return isSignatureMatch(signatures, mSignatureSets); |
| 324 | } |
| 325 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 326 | private final PackageMonitor mPackageMonitor = new PackageMonitor() { |
| 327 | /** |
| 328 | * Called when package has been reinstalled |
| 329 | */ |
| 330 | @Override |
| 331 | public void onPackageUpdateFinished(String packageName, int uid) { |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 332 | synchronized (mLock) { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 333 | final boolean forceRebind = Objects.equals(packageName, mBoundPackageName); |
| 334 | bindBestPackageLocked(null, forceRebind); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 335 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public void onPackageAdded(String packageName, int uid) { |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 340 | synchronized (mLock) { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 341 | final boolean forceRebind = Objects.equals(packageName, mBoundPackageName); |
| 342 | bindBestPackageLocked(null, forceRebind); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 343 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public void onPackageRemoved(String packageName, int uid) { |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 348 | synchronized (mLock) { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 349 | final boolean forceRebind = Objects.equals(packageName, mBoundPackageName); |
| 350 | bindBestPackageLocked(null, forceRebind); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 353 | |
| 354 | @Override |
| 355 | public boolean onPackageChanged(String packageName, int uid, String[] components) { |
| 356 | synchronized (mLock) { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 357 | final boolean forceRebind = Objects.equals(packageName, mBoundPackageName); |
| 358 | bindBestPackageLocked(null, forceRebind); |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 359 | } |
| 360 | return super.onPackageChanged(packageName, uid, components); |
| 361 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | @Override |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 365 | public void onServiceConnected(ComponentName component, IBinder binder) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 366 | synchronized (mLock) { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 367 | if (component.equals(mBoundComponent)) { |
| 368 | if (D) Log.d(mTag, component + " connected"); |
| 369 | mBoundService = binder; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 370 | if (mHandler !=null && mNewServiceWork != null) { |
| 371 | mHandler.post(mNewServiceWork); |
| 372 | } |
| 373 | } else { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 374 | Log.w(mTag, "unexpected onServiceConnected: " + component); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | @Override |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 380 | public void onServiceDisconnected(ComponentName component) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 381 | synchronized (mLock) { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 382 | if (D) Log.d(mTag, component + " disconnected"); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 383 | |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 384 | if (component.equals(mBoundComponent)) { |
| 385 | mBoundService = null; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 390 | public @Nullable String getBestPackageName() { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 391 | synchronized (mLock) { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 392 | return mBoundPackageName; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
| 396 | public int getBestVersion() { |
| 397 | synchronized (mLock) { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 398 | return mBoundVersion; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Lifu Tang | 66f761c | 2017-11-13 15:51:54 -0800 | [diff] [blame] | 402 | /** |
| 403 | * The runner that runs on the binder retrieved from {@link ServiceWatcher}. |
| 404 | */ |
| 405 | public interface BinderRunner { |
| 406 | /** |
| 407 | * Runs on the retrieved binder. |
| 408 | * @param binder the binder retrieved from the {@link ServiceWatcher}. |
| 409 | */ |
| 410 | public void run(@NonNull IBinder binder); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Retrieves the binder from {@link ServiceWatcher} and runs it. |
| 415 | * @return whether a valid service exists. |
| 416 | */ |
| 417 | public boolean runOnBinder(@NonNull BinderRunner runner) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 418 | synchronized (mLock) { |
Lifu Tang | 66f761c | 2017-11-13 15:51:54 -0800 | [diff] [blame] | 419 | if (mBoundService == null) { |
| 420 | return false; |
| 421 | } else { |
| 422 | runner.run(mBoundService); |
| 423 | return true; |
| 424 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 427 | |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 428 | public void switchUser(int userId) { |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 429 | synchronized (mLock) { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 430 | mCurrentUserId = userId; |
| 431 | bindBestPackageLocked(mServicePackageName, false); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | public void unlockUser(int userId) { |
| 436 | synchronized (mLock) { |
| 437 | if (userId == mCurrentUserId) { |
| 438 | bindBestPackageLocked(mServicePackageName, false); |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 439 | } |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 442 | } |