|
31 | 31 | import androidx.annotation.AnyThread; |
32 | 32 | import androidx.annotation.MainThread; |
33 | 33 | import com.google.common.annotations.VisibleForTesting; |
| 34 | +import com.google.common.base.VerifyException; |
34 | 35 | import com.google.errorprone.annotations.concurrent.GuardedBy; |
35 | 36 | import io.grpc.Status; |
36 | 37 | import io.grpc.StatusException; |
37 | 38 | import io.grpc.binder.BinderChannelCredentials; |
38 | | -import java.lang.reflect.Method; |
39 | 39 | import java.util.concurrent.Executor; |
40 | 40 | import java.util.logging.Level; |
41 | 41 | import java.util.logging.Logger; |
@@ -257,39 +257,32 @@ private static int getIdentifier(UserHandle userHandle) throws ReflectiveOperati |
257 | 257 | } |
258 | 258 |
|
259 | 259 | private static ResolveInfo resolveServiceAsUser( |
260 | | - PackageManager packageManager, Intent bindIntent, int flags, UserHandle targetUserHandle) |
261 | | - throws ReflectiveOperationException { |
262 | | - Method resolveService; |
263 | | - Object[] args; |
264 | | - if (targetUserHandle == null) { |
265 | | - resolveService = |
266 | | - packageManager.getClass().getMethod("resolveService", Intent.class, int.class); |
267 | | - args = new Object[] {bindIntent, flags}; |
268 | | - } else { |
269 | | - resolveService = |
| 260 | + PackageManager packageManager, Intent intent, int flags, UserHandle targetUserHandle) { |
| 261 | + try { |
| 262 | + return (ResolveInfo) |
270 | 263 | packageManager |
271 | 264 | .getClass() |
272 | | - .getMethod("resolveServiceAsUser", Intent.class, int.class, int.class); |
273 | | - args = new Object[] {bindIntent, flags, getIdentifier(targetUserHandle)}; |
| 265 | + .getMethod("resolveServiceAsUser", Intent.class, int.class, int.class) |
| 266 | + .invoke(packageManager, intent, flags, getIdentifier(targetUserHandle)); |
| 267 | + } catch (ReflectiveOperationException e) { |
| 268 | + throw new VerifyException(e); |
274 | 269 | } |
275 | | - return (ResolveInfo) resolveService.invoke(packageManager, args); |
276 | 270 | } |
277 | 271 |
|
278 | 272 | @AnyThread |
279 | 273 | public ServiceInfo resolve() throws StatusException { |
280 | 274 | checkState(sourceContext != null); |
281 | | - try { |
282 | | - ResolveInfo resolveInfo = |
283 | | - resolveServiceAsUser(sourceContext.getPackageManager(), bindIntent, 0, targetUserHandle); |
284 | | - if (resolveInfo == null) { |
285 | | - throw Status.UNIMPLEMENTED // Same code as when bindService() returns false. |
286 | | - .withDescription("resolveService(" + bindIntent + ") returned null") |
287 | | - .asException(); |
288 | | - } |
289 | | - return resolveInfo.serviceInfo; |
290 | | - } catch (ReflectiveOperationException e) { |
291 | | - throw Status.fromThrowable(e).asRuntimeException(); |
| 275 | + PackageManager packageManager = sourceContext.getPackageManager(); |
| 276 | + ResolveInfo resolveInfo = |
| 277 | + targetUserHandle != null |
| 278 | + ? resolveServiceAsUser(packageManager, bindIntent, 0, targetUserHandle) |
| 279 | + : packageManager.resolveService(bindIntent, 0); |
| 280 | + if (resolveInfo == null) { |
| 281 | + throw Status.UNIMPLEMENTED // Same status code as when bindService() returns false. |
| 282 | + .withDescription("resolveService(" + bindIntent + ") returned null") |
| 283 | + .asException(); |
292 | 284 | } |
| 285 | + return resolveInfo.serviceInfo; |
293 | 286 | } |
294 | 287 |
|
295 | 288 | @MainThread |
|
0 commit comments