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 | |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
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; |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 33 | import android.os.Bundle; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 34 | import android.os.Handler; |
| 35 | import android.os.IBinder; |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 36 | import android.os.Looper; |
Soonil Nagarkar | 192710b | 2019-01-24 17:42:49 -0800 | [diff] [blame] | 37 | import android.os.RemoteException; |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 38 | import android.os.UserHandle; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 39 | import android.util.Log; |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 40 | import android.util.Slog; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 41 | |
| 42 | import com.android.internal.content.PackageMonitor; |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 43 | import com.android.internal.util.Preconditions; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 44 | |
| 45 | import java.util.ArrayList; |
| 46 | import java.util.Arrays; |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 47 | import java.util.Collections; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 48 | import java.util.HashSet; |
| 49 | import java.util.List; |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 50 | import java.util.Objects; |
Soonil Nagarkar | 192710b | 2019-01-24 17:42:49 -0800 | [diff] [blame] | 51 | import java.util.concurrent.Callable; |
| 52 | import java.util.concurrent.ExecutionException; |
| 53 | import java.util.concurrent.FutureTask; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Find the best Service, and bind to it. |
| 57 | * Handles run-time package changes. |
| 58 | */ |
| 59 | public class ServiceWatcher implements ServiceConnection { |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 60 | |
| 61 | private static final String TAG = "ServiceWatcher"; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 62 | private static final boolean D = false; |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 63 | |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 64 | public static final String EXTRA_SERVICE_VERSION = "serviceVersion"; |
Victoria Lease | 03cdd3d | 2013-02-01 15:15:54 -0800 | [diff] [blame] | 65 | public static final String EXTRA_SERVICE_IS_MULTIUSER = "serviceIsMultiuser"; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 66 | |
Soonil Nagarkar | 192710b | 2019-01-24 17:42:49 -0800 | [diff] [blame] | 67 | |
| 68 | /** Function to run on binder interface. */ |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 69 | public interface BinderRunner { |
Soonil Nagarkar | 192710b | 2019-01-24 17:42:49 -0800 | [diff] [blame] | 70 | /** Called to run client code with the binder. */ |
| 71 | void run(IBinder binder) throws RemoteException; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Function to run on binder interface. |
| 76 | * @param <T> Type to return. |
| 77 | */ |
| 78 | public interface BlockingBinderRunner<T> { |
| 79 | /** Called to run client code with the binder. */ |
| 80 | T run(IBinder binder) throws RemoteException; |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 81 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 82 | |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 83 | public static ArrayList<HashSet<Signature>> getSignatureSets(Context context, |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 84 | String... packageNames) { |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 85 | PackageManager pm = context.getPackageManager(); |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 86 | |
| 87 | ArrayList<HashSet<Signature>> signatureSets = new ArrayList<>(packageNames.length); |
| 88 | for (String packageName : packageNames) { |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 89 | try { |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 90 | Signature[] signatures = pm.getPackageInfo(packageName, |
| 91 | PackageManager.MATCH_SYSTEM_ONLY |
| 92 | | PackageManager.GET_SIGNATURES).signatures; |
| 93 | |
| 94 | HashSet<Signature> set = new HashSet<>(); |
| 95 | Collections.addAll(set, signatures); |
| 96 | signatureSets.add(set); |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 97 | } catch (NameNotFoundException e) { |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 98 | Log.w(TAG, packageName + " not found"); |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 99 | } |
| 100 | } |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 101 | return signatureSets; |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 104 | /** Checks if signatures match. */ |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 105 | public static boolean isSignatureMatch(Signature[] signatures, |
| 106 | List<HashSet<Signature>> sigSets) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 107 | if (signatures == null) return false; |
| 108 | |
| 109 | // build hashset of input to test against |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 110 | HashSet<Signature> inputSet = new HashSet<>(); |
| 111 | Collections.addAll(inputSet, signatures); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 112 | |
| 113 | // test input against each of the signature sets |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 114 | for (HashSet<Signature> referenceSet : sigSets) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 115 | if (referenceSet.equals(inputSet)) { |
| 116 | return true; |
| 117 | } |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 122 | private final Context mContext; |
| 123 | private final String mTag; |
| 124 | private final String mAction; |
| 125 | private final String mServicePackageName; |
| 126 | private final List<HashSet<Signature>> mSignatureSets; |
| 127 | |
| 128 | private final Handler mHandler; |
| 129 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 130 | // read/write from handler thread |
Soonil Nagarkar | 192710b | 2019-01-24 17:42:49 -0800 | [diff] [blame] | 131 | private IBinder mBestService; |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 132 | private int mCurrentUserId; |
| 133 | |
| 134 | // read from any thread, write from handler thread |
| 135 | private volatile ComponentName mBestComponent; |
| 136 | private volatile int mBestVersion; |
| 137 | private volatile int mBestUserId; |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 138 | |
| 139 | public ServiceWatcher(Context context, String logTag, String action, |
| 140 | int overlaySwitchResId, int defaultServicePackageNameResId, |
| 141 | int initialPackageNamesResId, Handler handler) { |
| 142 | Resources resources = context.getResources(); |
| 143 | |
| 144 | mContext = context; |
| 145 | mTag = logTag; |
| 146 | mAction = action; |
| 147 | |
| 148 | boolean enableOverlay = resources.getBoolean(overlaySwitchResId); |
| 149 | if (enableOverlay) { |
| 150 | String[] pkgs = resources.getStringArray(initialPackageNamesResId); |
| 151 | mServicePackageName = null; |
| 152 | mSignatureSets = getSignatureSets(context, pkgs); |
| 153 | if (D) Log.d(mTag, "Overlay enabled, packages=" + Arrays.toString(pkgs)); |
| 154 | } else { |
| 155 | mServicePackageName = resources.getString(defaultServicePackageNameResId); |
| 156 | mSignatureSets = getSignatureSets(context, mServicePackageName); |
| 157 | if (D) Log.d(mTag, "Overlay disabled, default package=" + mServicePackageName); |
| 158 | } |
| 159 | |
| 160 | mHandler = handler; |
| 161 | |
| 162 | mBestComponent = null; |
| 163 | mBestVersion = Integer.MIN_VALUE; |
| 164 | mBestUserId = UserHandle.USER_NULL; |
| 165 | |
| 166 | mBestService = null; |
Jeff Hamilton | fbadb69 | 2012-10-05 14:21:58 -0500 | [diff] [blame] | 167 | } |
| 168 | |
Soonil Nagarkar | 192710b | 2019-01-24 17:42:49 -0800 | [diff] [blame] | 169 | protected void onBind() {} |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 170 | |
Soonil Nagarkar | 192710b | 2019-01-24 17:42:49 -0800 | [diff] [blame] | 171 | protected void onUnbind() {} |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 172 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Start this watcher, including binding to the current best match and |
| 175 | * re-binding to any better matches down the road. |
| 176 | * <p> |
| 177 | * Note that if there are no matching encryption-aware services, we may not |
| 178 | * bind to a real service until after the current user is unlocked. |
| 179 | * |
| 180 | * @return {@code true} if a potential service implementation was found. |
| 181 | */ |
| 182 | public final boolean start() { |
| 183 | // if we have to return false, do it before registering anything |
| 184 | if (isServiceMissing()) return false; |
| 185 | |
| 186 | // listen for relevant package changes if service overlay is enabled on handler |
| 187 | if (mServicePackageName == null) { |
| 188 | new PackageMonitor() { |
| 189 | @Override |
| 190 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 191 | bindBestPackage(Objects.equals(packageName, getCurrentPackageName())); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 192 | } |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 193 | |
| 194 | @Override |
| 195 | public void onPackageAdded(String packageName, int uid) { |
| 196 | bindBestPackage(Objects.equals(packageName, getCurrentPackageName())); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void onPackageRemoved(String packageName, int uid) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 201 | bindBestPackage(Objects.equals(packageName, getCurrentPackageName())); |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public boolean onPackageChanged(String packageName, int uid, String[] components) { |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 206 | bindBestPackage(Objects.equals(packageName, getCurrentPackageName())); |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 207 | return super.onPackageChanged(packageName, uid, components); |
| 208 | } |
| 209 | }.register(mContext, UserHandle.ALL, true, mHandler); |
| 210 | } |
| 211 | |
| 212 | // listen for user change on handler |
| 213 | IntentFilter intentFilter = new IntentFilter(); |
| 214 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
| 215 | intentFilter.addAction(Intent.ACTION_USER_UNLOCKED); |
| 216 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
| 217 | @Override |
| 218 | public void onReceive(Context context, Intent intent) { |
| 219 | final String action = intent.getAction(); |
| 220 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 221 | UserHandle.USER_NULL); |
| 222 | if (Intent.ACTION_USER_SWITCHED.equals(action)) { |
| 223 | mCurrentUserId = userId; |
| 224 | bindBestPackage(false); |
| 225 | } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) { |
| 226 | if (userId == mCurrentUserId) { |
| 227 | bindBestPackage(false); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | }, UserHandle.ALL, intentFilter, null, mHandler); |
| 232 | |
| 233 | mCurrentUserId = ActivityManager.getCurrentUser(); |
| 234 | |
| 235 | mHandler.post(() -> bindBestPackage(false)); |
| 236 | return true; |
| 237 | } |
| 238 | |
Soonil Nagarkar | 1575a04 | 2018-10-24 17:54:54 -0700 | [diff] [blame] | 239 | /** Returns the name of the currently connected package or null. */ |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 240 | @Nullable |
| 241 | public String getCurrentPackageName() { |
| 242 | ComponentName bestComponent = mBestComponent; |
| 243 | return bestComponent == null ? null : bestComponent.getPackageName(); |
| 244 | } |
| 245 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 246 | private boolean isServiceMissing() { |
| 247 | return mContext.getPackageManager().queryIntentServicesAsUser(new Intent(mAction), |
| 248 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 249 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
| 250 | UserHandle.USER_SYSTEM).isEmpty(); |
| 251 | } |
| 252 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 253 | private void bindBestPackage(boolean forceRebind) { |
| 254 | Preconditions.checkState(Looper.myLooper() == mHandler.getLooper()); |
| 255 | |
| 256 | Intent intent = new Intent(mAction); |
| 257 | if (mServicePackageName != null) { |
| 258 | intent.setPackage(mServicePackageName); |
| 259 | } |
| 260 | |
| 261 | List<ResolveInfo> rInfos = mContext.getPackageManager().queryIntentServicesAsUser(intent, |
| 262 | PackageManager.GET_META_DATA | PackageManager.MATCH_DIRECT_BOOT_AUTO, |
| 263 | mCurrentUserId); |
| 264 | if (rInfos == null) { |
| 265 | rInfos = Collections.emptyList(); |
| 266 | } |
| 267 | |
| 268 | ComponentName bestComponent = null; |
| 269 | int bestVersion = Integer.MIN_VALUE; |
| 270 | boolean bestIsMultiuser = false; |
| 271 | |
| 272 | for (ResolveInfo rInfo : rInfos) { |
| 273 | ComponentName component = rInfo.serviceInfo.getComponentName(); |
| 274 | String packageName = component.getPackageName(); |
| 275 | |
| 276 | // check signature |
| 277 | try { |
| 278 | PackageInfo pInfo = mContext.getPackageManager().getPackageInfo(packageName, |
| 279 | PackageManager.GET_SIGNATURES |
| 280 | | PackageManager.MATCH_DIRECT_BOOT_AUTO); |
| 281 | if (!isSignatureMatch(pInfo.signatures, mSignatureSets)) { |
| 282 | Log.w(mTag, packageName + " resolves service " + mAction |
| 283 | + ", but has wrong signature, ignoring"); |
| 284 | continue; |
| 285 | } |
| 286 | } catch (NameNotFoundException e) { |
| 287 | Log.wtf(mTag, e); |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | // check metadata |
| 292 | Bundle metadata = rInfo.serviceInfo.metaData; |
| 293 | int version = Integer.MIN_VALUE; |
| 294 | boolean isMultiuser = false; |
| 295 | if (metadata != null) { |
| 296 | version = metadata.getInt(EXTRA_SERVICE_VERSION, Integer.MIN_VALUE); |
| 297 | isMultiuser = metadata.getBoolean(EXTRA_SERVICE_IS_MULTIUSER, false); |
| 298 | } |
| 299 | |
| 300 | if (version > bestVersion) { |
| 301 | bestComponent = component; |
| 302 | bestVersion = version; |
| 303 | bestIsMultiuser = isMultiuser; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | if (D) { |
| 308 | Log.d(mTag, String.format("bindBestPackage for %s : %s found %d, %s", mAction, |
| 309 | (mServicePackageName == null ? "" |
| 310 | : "(" + mServicePackageName + ") "), rInfos.size(), |
| 311 | (bestComponent == null ? "no new best component" |
| 312 | : "new best component: " + bestComponent))); |
| 313 | } |
| 314 | |
| 315 | if (bestComponent == null) { |
| 316 | Slog.w(mTag, "Odd, no component found for service " + mAction); |
| 317 | unbind(); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | int userId = bestIsMultiuser ? UserHandle.USER_SYSTEM : mCurrentUserId; |
| 322 | boolean alreadyBound = Objects.equals(bestComponent, mBestComponent) |
| 323 | && bestVersion == mBestVersion && userId == mBestUserId; |
| 324 | if (forceRebind || !alreadyBound) { |
| 325 | unbind(); |
| 326 | bind(bestComponent, bestVersion, userId); |
| 327 | } |
| 328 | } |
| 329 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 330 | private void bind(ComponentName component, int version, int userId) { |
| 331 | Preconditions.checkState(Looper.myLooper() == mHandler.getLooper()); |
| 332 | |
| 333 | Intent intent = new Intent(mAction); |
| 334 | intent.setComponent(component); |
| 335 | |
| 336 | mBestComponent = component; |
| 337 | mBestVersion = version; |
| 338 | mBestUserId = userId; |
| 339 | |
| 340 | if (D) Log.d(mTag, "binding " + component + " (v" + version + ") (u" + userId + ")"); |
| 341 | mContext.bindServiceAsUser(intent, this, |
| 342 | Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND | Context.BIND_NOT_VISIBLE, |
| 343 | UserHandle.of(userId)); |
| 344 | } |
| 345 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 346 | private void unbind() { |
| 347 | Preconditions.checkState(Looper.myLooper() == mHandler.getLooper()); |
| 348 | |
| 349 | if (mBestComponent != null) { |
| 350 | if (D) Log.d(mTag, "unbinding " + mBestComponent); |
| 351 | mContext.unbindService(this); |
| 352 | } |
| 353 | |
| 354 | mBestComponent = null; |
| 355 | mBestVersion = Integer.MIN_VALUE; |
| 356 | mBestUserId = UserHandle.USER_NULL; |
| 357 | } |
| 358 | |
Soonil Nagarkar | 192710b | 2019-01-24 17:42:49 -0800 | [diff] [blame] | 359 | /** |
| 360 | * Runs the given function asynchronously if currently connected. Suppresses any RemoteException |
| 361 | * thrown during execution. |
| 362 | */ |
| 363 | public final void runOnBinder(BinderRunner runner) { |
| 364 | runOnHandler(() -> { |
| 365 | if (mBestService == null) { |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | try { |
| 370 | runner.run(mBestService); |
| 371 | } catch (RuntimeException e) { |
| 372 | // the code being run is privileged, but may be outside the system server, and thus |
| 373 | // we cannot allow runtime exceptions to crash the system server |
| 374 | Log.e(TAG, "exception while while running " + runner + " on " + mBestService |
| 375 | + " from " + this, e); |
| 376 | } catch (RemoteException e) { |
| 377 | // do nothing |
| 378 | } |
| 379 | }); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Runs the given function synchronously if currently connected, and returns the default value |
| 384 | * if not currently connected or if any exception is thrown. |
| 385 | */ |
| 386 | public final <T> T runOnBinderBlocking(BlockingBinderRunner<T> runner, T defaultValue) { |
| 387 | try { |
| 388 | return runOnHandlerBlocking(() -> { |
| 389 | if (mBestService == null) { |
| 390 | return defaultValue; |
| 391 | } |
| 392 | |
| 393 | try { |
| 394 | return runner.run(mBestService); |
| 395 | } catch (RemoteException e) { |
| 396 | return defaultValue; |
| 397 | } |
| 398 | }); |
| 399 | } catch (InterruptedException e) { |
| 400 | return defaultValue; |
| 401 | } |
| 402 | } |
| 403 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 404 | @Override |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 405 | public final void onServiceConnected(ComponentName component, IBinder binder) { |
Soonil Nagarkar | 192710b | 2019-01-24 17:42:49 -0800 | [diff] [blame] | 406 | runOnHandler(() -> { |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 407 | if (D) Log.d(mTag, component + " connected"); |
Soonil Nagarkar | 192710b | 2019-01-24 17:42:49 -0800 | [diff] [blame] | 408 | mBestService = binder; |
| 409 | onBind(); |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 410 | }); |
| 411 | } |
| 412 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 413 | @Override |
| 414 | public final void onServiceDisconnected(ComponentName component) { |
Soonil Nagarkar | 192710b | 2019-01-24 17:42:49 -0800 | [diff] [blame] | 415 | runOnHandler(() -> { |
Jeff Sharkey | 58482c55 | 2016-02-08 17:49:17 -0700 | [diff] [blame] | 416 | if (D) Log.d(mTag, component + " disconnected"); |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 417 | mBestService = null; |
Soonil Nagarkar | 192710b | 2019-01-24 17:42:49 -0800 | [diff] [blame] | 418 | onUnbind(); |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 419 | }); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 422 | @Override |
| 423 | public String toString() { |
| 424 | ComponentName bestComponent = mBestComponent; |
| 425 | return bestComponent == null ? "null" : bestComponent.toShortString() + "@" + mBestVersion; |
Victoria Lease | b711d57 | 2012-10-02 13:14:11 -0700 | [diff] [blame] | 426 | } |
Soonil Nagarkar | 192710b | 2019-01-24 17:42:49 -0800 | [diff] [blame] | 427 | |
| 428 | private void runOnHandler(Runnable r) { |
| 429 | if (Looper.myLooper() == mHandler.getLooper()) { |
| 430 | r.run(); |
| 431 | } else { |
| 432 | mHandler.post(r); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | private <T> T runOnHandlerBlocking(Callable<T> c) throws InterruptedException { |
| 437 | if (Looper.myLooper() == mHandler.getLooper()) { |
| 438 | try { |
| 439 | return c.call(); |
| 440 | } catch (Exception e) { |
| 441 | // Function cannot throw exception, this should never happen |
| 442 | throw new IllegalStateException(e); |
| 443 | } |
| 444 | } else { |
| 445 | FutureTask<T> task = new FutureTask<>(c); |
| 446 | mHandler.post(task); |
| 447 | try { |
| 448 | return task.get(); |
| 449 | } catch (ExecutionException e) { |
| 450 | // Function cannot throw exception, this should never happen |
| 451 | throw new IllegalStateException(e); |
| 452 | } |
| 453 | } |
| 454 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 455 | } |