LauncherAppsService.java revision 99302b55c6a960c9078ad2c84ae9be3296bd32f3
1/* 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.pm; 18 19import android.annotation.NonNull; 20import android.annotation.UserIdInt; 21import android.app.ActivityManager; 22import android.app.ActivityManagerInternal; 23import android.app.AppGlobals; 24import android.app.PendingIntent; 25import android.content.ComponentName; 26import android.content.Context; 27import android.content.Intent; 28import android.content.IntentSender; 29import android.content.pm.ActivityInfo; 30import android.content.pm.ApplicationInfo; 31import android.content.pm.ILauncherApps; 32import android.content.pm.IOnAppsChangedListener; 33import android.content.pm.IPackageManager; 34import android.content.pm.LauncherApps.ShortcutQuery; 35import android.content.pm.PackageInfo; 36import android.content.pm.PackageManager; 37import android.content.pm.PackageManager.NameNotFoundException; 38import android.content.pm.ParceledListSlice; 39import android.content.pm.ResolveInfo; 40import android.content.pm.ShortcutInfo; 41import android.content.pm.ShortcutServiceInternal; 42import android.content.pm.ShortcutServiceInternal.ShortcutChangeListener; 43import android.content.pm.UserInfo; 44import android.graphics.Rect; 45import android.net.Uri; 46import android.os.Binder; 47import android.os.Bundle; 48import android.os.Handler; 49import android.os.IInterface; 50import android.os.ParcelFileDescriptor; 51import android.os.RemoteCallbackList; 52import android.os.RemoteException; 53import android.os.UserHandle; 54import android.os.UserManager; 55import android.provider.Settings; 56import android.util.Log; 57import android.util.Slog; 58 59import com.android.internal.annotations.VisibleForTesting; 60import com.android.internal.content.PackageMonitor; 61import com.android.internal.os.BackgroundThread; 62import com.android.internal.util.Preconditions; 63import com.android.server.LocalServices; 64import com.android.server.SystemService; 65 66import java.util.ArrayList; 67import java.util.Collections; 68import java.util.List; 69 70/** 71 * Service that manages requests and callbacks for launchers that support 72 * managed profiles. 73 */ 74public class LauncherAppsService extends SystemService { 75 76 private final LauncherAppsImpl mLauncherAppsImpl; 77 78 public LauncherAppsService(Context context) { 79 super(context); 80 mLauncherAppsImpl = new LauncherAppsImpl(context); 81 } 82 83 @Override 84 public void onStart() { 85 publishBinderService(Context.LAUNCHER_APPS_SERVICE, mLauncherAppsImpl); 86 } 87 88 static class BroadcastCookie { 89 public final UserHandle user; 90 public final String packageName; 91 92 BroadcastCookie(UserHandle userHandle, String packageName) { 93 this.user = userHandle; 94 this.packageName = packageName; 95 } 96 } 97 98 @VisibleForTesting 99 static class LauncherAppsImpl extends ILauncherApps.Stub { 100 private static final boolean DEBUG = false; 101 private static final String TAG = "LauncherAppsService"; 102 private final Context mContext; 103 private final PackageManager mPm; 104 private final UserManager mUm; 105 private final ActivityManagerInternal mActivityManagerInternal; 106 private final ShortcutServiceInternal mShortcutServiceInternal; 107 private final PackageCallbackList<IOnAppsChangedListener> mListeners 108 = new PackageCallbackList<IOnAppsChangedListener>(); 109 110 private final MyPackageMonitor mPackageMonitor = new MyPackageMonitor(); 111 112 private final Handler mCallbackHandler; 113 114 public LauncherAppsImpl(Context context) { 115 mContext = context; 116 mPm = mContext.getPackageManager(); 117 mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE); 118 mActivityManagerInternal = Preconditions.checkNotNull( 119 LocalServices.getService(ActivityManagerInternal.class)); 120 mShortcutServiceInternal = Preconditions.checkNotNull( 121 LocalServices.getService(ShortcutServiceInternal.class)); 122 mShortcutServiceInternal.addListener(mPackageMonitor); 123 mCallbackHandler = BackgroundThread.getHandler(); 124 } 125 126 @VisibleForTesting 127 int injectBinderCallingUid() { 128 return getCallingUid(); 129 } 130 131 final int injectCallingUserId() { 132 return UserHandle.getUserId(injectBinderCallingUid()); 133 } 134 135 @VisibleForTesting 136 long injectClearCallingIdentity() { 137 return Binder.clearCallingIdentity(); 138 } 139 140 // Injection point. 141 @VisibleForTesting 142 void injectRestoreCallingIdentity(long token) { 143 Binder.restoreCallingIdentity(token); 144 } 145 146 private int getCallingUserId() { 147 return UserHandle.getUserId(injectBinderCallingUid()); 148 } 149 150 /* 151 * @see android.content.pm.ILauncherApps#addOnAppsChangedListener( 152 * android.content.pm.IOnAppsChangedListener) 153 */ 154 @Override 155 public void addOnAppsChangedListener(String callingPackage, IOnAppsChangedListener listener) 156 throws RemoteException { 157 verifyCallingPackage(callingPackage); 158 synchronized (mListeners) { 159 if (DEBUG) { 160 Log.d(TAG, "Adding listener from " + Binder.getCallingUserHandle()); 161 } 162 if (mListeners.getRegisteredCallbackCount() == 0) { 163 if (DEBUG) { 164 Log.d(TAG, "Starting package monitoring"); 165 } 166 startWatchingPackageBroadcasts(); 167 } 168 mListeners.unregister(listener); 169 mListeners.register(listener, new BroadcastCookie(UserHandle.of(getCallingUserId()), 170 callingPackage)); 171 } 172 } 173 174 /* 175 * @see android.content.pm.ILauncherApps#removeOnAppsChangedListener( 176 * android.content.pm.IOnAppsChangedListener) 177 */ 178 @Override 179 public void removeOnAppsChangedListener(IOnAppsChangedListener listener) 180 throws RemoteException { 181 synchronized (mListeners) { 182 if (DEBUG) { 183 Log.d(TAG, "Removing listener from " + Binder.getCallingUserHandle()); 184 } 185 mListeners.unregister(listener); 186 if (mListeners.getRegisteredCallbackCount() == 0) { 187 stopWatchingPackageBroadcasts(); 188 } 189 } 190 } 191 192 /** 193 * Register a receiver to watch for package broadcasts 194 */ 195 private void startWatchingPackageBroadcasts() { 196 mPackageMonitor.register(mContext, UserHandle.ALL, true, mCallbackHandler); 197 } 198 199 /** 200 * Unregister package broadcast receiver 201 */ 202 private void stopWatchingPackageBroadcasts() { 203 if (DEBUG) { 204 Log.d(TAG, "Stopped watching for packages"); 205 } 206 mPackageMonitor.unregister(); 207 } 208 209 void checkCallbackCount() { 210 synchronized (mListeners) { 211 if (DEBUG) { 212 Log.d(TAG, "Callback count = " + mListeners.getRegisteredCallbackCount()); 213 } 214 if (mListeners.getRegisteredCallbackCount() == 0) { 215 stopWatchingPackageBroadcasts(); 216 } 217 } 218 } 219 220 /** See {@link #canAccessProfile(String, int, String)} */ 221 private boolean canAccessProfile( 222 String callingPackage, UserHandle targetUser, String message) { 223 return canAccessProfile(callingPackage, targetUser.getIdentifier(), message); 224 } 225 226 /** 227 * Checks if the calling user is in the same group as {@code targetUser}, and allowed 228 * to access it. 229 * 230 * @return TRUE if the calling user can access {@code targetUserId}. FALSE if not *but 231 * they're still in the same profile group*. 232 * 233 * @throws SecurityException if the calling user and {@code targetUser} are not in the same 234 * group. 235 */ 236 private boolean canAccessProfile(String callingPackage, int targetUserId, String message) { 237 final int callingUserId = injectCallingUserId(); 238 239 if (targetUserId == callingUserId) return true; 240 241 long ident = injectClearCallingIdentity(); 242 try { 243 UserInfo callingUserInfo = mUm.getUserInfo(callingUserId); 244 if (callingUserInfo.isManagedProfile()) { 245 246 // STOPSHIP Remove the whitelist. 247 if ("com.google.android.talk".equals(callingPackage) 248 || "com.google.android.quicksearchbox".equals(callingPackage) 249 || "com.google.android.googlequicksearchbox".equals(callingPackage) 250 ) { 251 return false; 252 } 253 // STOPSHIP Change it to 'e'. 254 Slog.wtfStack(TAG, message + " by " + callingPackage + " for another profile " 255 + targetUserId + " from " + callingUserId); 256 257 return false; 258 } 259 260 UserInfo targetUserInfo = mUm.getUserInfo(targetUserId); 261 if (targetUserInfo == null 262 || targetUserInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID 263 || targetUserInfo.profileGroupId != callingUserInfo.profileGroupId) { 264 throw new SecurityException(message + " for unrelated profile " + targetUserId); 265 } 266 } finally { 267 injectRestoreCallingIdentity(ident); 268 } 269 return true; 270 } 271 272 @VisibleForTesting // We override it in unit tests 273 void verifyCallingPackage(String callingPackage) { 274 int packageUid = -1; 275 try { 276 packageUid = mPm.getPackageUidAsUser(callingPackage, 277 PackageManager.MATCH_DIRECT_BOOT_AWARE 278 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE 279 | PackageManager.MATCH_UNINSTALLED_PACKAGES, 280 UserHandle.getUserId(getCallingUid())); 281 } catch (NameNotFoundException e) { 282 Log.e(TAG, "Package not found: " + callingPackage); 283 } 284 if (packageUid != Binder.getCallingUid()) { 285 throw new SecurityException("Calling package name mismatch"); 286 } 287 } 288 289 /** 290 * Checks if the user is enabled. 291 */ 292 private boolean isUserEnabled(UserHandle user) { 293 return isUserEnabled(user.getIdentifier()); 294 } 295 296 private boolean isUserEnabled(int userId) { 297 long ident = injectClearCallingIdentity(); 298 try { 299 UserInfo targetUserInfo = mUm.getUserInfo(userId); 300 return targetUserInfo != null && targetUserInfo.isEnabled(); 301 } finally { 302 injectRestoreCallingIdentity(ident); 303 } 304 } 305 306 @Override 307 public ParceledListSlice<ResolveInfo> getLauncherActivities(String callingPackage, 308 String packageName, UserHandle user) 309 throws RemoteException { 310 return queryActivitiesForUser(callingPackage, 311 new Intent(Intent.ACTION_MAIN) 312 .addCategory(Intent.CATEGORY_LAUNCHER) 313 .setPackage(packageName), 314 user); 315 } 316 317 @Override 318 public ActivityInfo resolveActivity( 319 String callingPackage, ComponentName component, UserHandle user) 320 throws RemoteException { 321 if (!canAccessProfile(callingPackage, user, "Cannot resolve activity")) { 322 return null; 323 } 324 if (!isUserEnabled(user)) { 325 return null; 326 } 327 328 long ident = Binder.clearCallingIdentity(); 329 try { 330 IPackageManager pm = AppGlobals.getPackageManager(); 331 return pm.getActivityInfo(component, 332 PackageManager.MATCH_DIRECT_BOOT_AWARE 333 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, 334 user.getIdentifier()); 335 } finally { 336 Binder.restoreCallingIdentity(ident); 337 } 338 } 339 340 @Override 341 public ParceledListSlice getShortcutConfigActivities( 342 String callingPackage, String packageName, UserHandle user) 343 throws RemoteException { 344 return queryActivitiesForUser(callingPackage, 345 new Intent(Intent.ACTION_CREATE_SHORTCUT).setPackage(packageName), user); 346 } 347 348 private ParceledListSlice<ResolveInfo> queryActivitiesForUser(String callingPackage, 349 Intent intent, UserHandle user) { 350 if (!canAccessProfile(callingPackage, user, "Cannot retrieve activities")) { 351 return null; 352 } 353 if (!isUserEnabled(user)) { 354 return null; 355 } 356 357 long ident = injectClearCallingIdentity(); 358 try { 359 List<ResolveInfo> apps = mPm.queryIntentActivitiesAsUser(intent, 360 PackageManager.MATCH_DIRECT_BOOT_AWARE 361 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, 362 user.getIdentifier()); 363 return new ParceledListSlice<>(apps); 364 } finally { 365 injectRestoreCallingIdentity(ident); 366 } 367 } 368 369 @Override 370 public IntentSender getShortcutConfigActivityIntent(String callingPackage, 371 ComponentName component, UserHandle user) throws RemoteException { 372 ensureShortcutPermission(callingPackage); 373 if (!canAccessProfile(callingPackage, user, "Cannot check package")) { 374 return null; 375 } 376 Preconditions.checkNotNull(component); 377 Preconditions.checkArgument(isUserEnabled(user), "User not enabled"); 378 379 // All right, create the sender. 380 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(component); 381 final long identity = Binder.clearCallingIdentity(); 382 try { 383 final PendingIntent pi = PendingIntent.getActivityAsUser( 384 mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT 385 | PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT, 386 null, user); 387 return pi == null ? null : pi.getIntentSender(); 388 } finally { 389 Binder.restoreCallingIdentity(identity); 390 } 391 } 392 393 @Override 394 public boolean isPackageEnabled(String callingPackage, String packageName, UserHandle user) 395 throws RemoteException { 396 if (!canAccessProfile(callingPackage, user, "Cannot check package")) { 397 return false; 398 } 399 if (!isUserEnabled(user)) { 400 return false; 401 } 402 403 long ident = Binder.clearCallingIdentity(); 404 try { 405 IPackageManager pm = AppGlobals.getPackageManager(); 406 PackageInfo info = pm.getPackageInfo(packageName, 407 PackageManager.MATCH_DIRECT_BOOT_AWARE 408 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, 409 user.getIdentifier()); 410 return info != null && info.applicationInfo.enabled; 411 } finally { 412 Binder.restoreCallingIdentity(ident); 413 } 414 } 415 416 @Override 417 public ApplicationInfo getApplicationInfo( 418 String callingPackage, String packageName, int flags, UserHandle user) 419 throws RemoteException { 420 if (!canAccessProfile(callingPackage, user, "Cannot check package")) { 421 return null; 422 } 423 if (!isUserEnabled(user)) { 424 return null; 425 } 426 427 long ident = Binder.clearCallingIdentity(); 428 try { 429 IPackageManager pm = AppGlobals.getPackageManager(); 430 ApplicationInfo info = pm.getApplicationInfo(packageName, flags, 431 user.getIdentifier()); 432 return info; 433 } finally { 434 Binder.restoreCallingIdentity(ident); 435 } 436 } 437 438 private void ensureShortcutPermission(@NonNull String callingPackage) { 439 verifyCallingPackage(callingPackage); 440 if (!mShortcutServiceInternal.hasShortcutHostPermission(getCallingUserId(), 441 callingPackage)) { 442 throw new SecurityException("Caller can't access shortcut information"); 443 } 444 } 445 446 @Override 447 public ParceledListSlice getShortcuts(String callingPackage, long changedSince, 448 String packageName, List shortcutIds, ComponentName componentName, int flags, 449 UserHandle targetUser) { 450 ensureShortcutPermission(callingPackage); 451 if (!canAccessProfile(callingPackage, targetUser, "Cannot get shortcuts") 452 || !isUserEnabled(targetUser)) { 453 return new ParceledListSlice<>(Collections.EMPTY_LIST); 454 } 455 if (shortcutIds != null && packageName == null) { 456 throw new IllegalArgumentException( 457 "To query by shortcut ID, package name must also be set"); 458 } 459 460 // TODO(b/29399275): Eclipse compiler requires explicit List<ShortcutInfo> cast below. 461 return new ParceledListSlice<>((List<ShortcutInfo>) 462 mShortcutServiceInternal.getShortcuts(getCallingUserId(), 463 callingPackage, changedSince, packageName, shortcutIds, 464 componentName, flags, targetUser.getIdentifier())); 465 } 466 467 @Override 468 public void pinShortcuts(String callingPackage, String packageName, List<String> ids, 469 UserHandle targetUser) { 470 ensureShortcutPermission(callingPackage); 471 if (!canAccessProfile(callingPackage, targetUser, "Cannot pin shortcuts")) { 472 return; 473 } 474 if (!isUserEnabled(targetUser)) { 475 throw new IllegalStateException("Cannot pin shortcuts for disabled profile " 476 + targetUser); 477 } 478 479 mShortcutServiceInternal.pinShortcuts(getCallingUserId(), 480 callingPackage, packageName, ids, targetUser.getIdentifier()); 481 } 482 483 @Override 484 public int getShortcutIconResId(String callingPackage, String packageName, String id, 485 int targetUserId) { 486 ensureShortcutPermission(callingPackage); 487 if (!canAccessProfile(callingPackage, targetUserId, "Cannot access shortcuts")) { 488 return 0; 489 } 490 if (!isUserEnabled(targetUserId)) { 491 return 0; 492 } 493 494 return mShortcutServiceInternal.getShortcutIconResId(getCallingUserId(), 495 callingPackage, packageName, id, targetUserId); 496 } 497 498 @Override 499 public ParcelFileDescriptor getShortcutIconFd(String callingPackage, 500 String packageName, String id, int targetUserId) { 501 ensureShortcutPermission(callingPackage); 502 if (!canAccessProfile(callingPackage, targetUserId, "Cannot access shortcuts")) { 503 return null; 504 } 505 if (!isUserEnabled(targetUserId)) { 506 return null; 507 } 508 509 return mShortcutServiceInternal.getShortcutIconFd(getCallingUserId(), 510 callingPackage, packageName, id, targetUserId); 511 } 512 513 @Override 514 public boolean hasShortcutHostPermission(String callingPackage) { 515 verifyCallingPackage(callingPackage); 516 return mShortcutServiceInternal.hasShortcutHostPermission(getCallingUserId(), 517 callingPackage); 518 } 519 520 @Override 521 public boolean startShortcut(String callingPackage, String packageName, String shortcutId, 522 Rect sourceBounds, Bundle startActivityOptions, int targetUserId) { 523 verifyCallingPackage(callingPackage); 524 if (!canAccessProfile(callingPackage, targetUserId, "Cannot start activity")) { 525 return false; 526 } 527 if (!isUserEnabled(targetUserId)) { 528 throw new IllegalStateException("Cannot start a shortcut for disabled profile " 529 + targetUserId); 530 } 531 532 // Even without the permission, pinned shortcuts are always launchable. 533 if (!mShortcutServiceInternal.isPinnedByCaller(getCallingUserId(), 534 callingPackage, packageName, shortcutId, targetUserId)) { 535 ensureShortcutPermission(callingPackage); 536 } 537 538 final Intent[] intents = mShortcutServiceInternal.createShortcutIntents( 539 getCallingUserId(), callingPackage, packageName, shortcutId, targetUserId); 540 if (intents == null || intents.length == 0) { 541 return false; 542 } 543 // Note the target activity doesn't have to be exported. 544 545 intents[0].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 546 intents[0].setSourceBounds(sourceBounds); 547 548 return startShortcutIntentsAsPublisher( 549 intents, packageName, startActivityOptions, targetUserId); 550 } 551 552 private boolean startShortcutIntentsAsPublisher(@NonNull Intent[] intents, 553 @NonNull String publisherPackage, Bundle startActivityOptions, int userId) { 554 final int code; 555 final long ident = injectClearCallingIdentity(); 556 try { 557 code = mActivityManagerInternal.startActivitiesAsPackage(publisherPackage, 558 userId, intents, startActivityOptions); 559 if (code >= ActivityManager.START_SUCCESS) { 560 return true; // Success 561 } else { 562 Log.e(TAG, "Couldn't start activity, code=" + code); 563 } 564 return code >= ActivityManager.START_SUCCESS; 565 } catch (SecurityException e) { 566 if (DEBUG) { 567 Slog.d(TAG, "SecurityException while launching intent", e); 568 } 569 return false; 570 } finally { 571 injectRestoreCallingIdentity(ident); 572 } 573 } 574 575 @Override 576 public boolean isActivityEnabled( 577 String callingPackage, ComponentName component, UserHandle user) 578 throws RemoteException { 579 if (!canAccessProfile(callingPackage , user, "Cannot check component")) { 580 return false; 581 } 582 if (!isUserEnabled(user)) { 583 return false; 584 } 585 586 long ident = Binder.clearCallingIdentity(); 587 try { 588 IPackageManager pm = AppGlobals.getPackageManager(); 589 ActivityInfo info = pm.getActivityInfo(component, 590 PackageManager.MATCH_DIRECT_BOOT_AWARE 591 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, 592 user.getIdentifier()); 593 return info != null; 594 } finally { 595 Binder.restoreCallingIdentity(ident); 596 } 597 } 598 599 @Override 600 public void startActivityAsUser(String callingPackage, 601 ComponentName component, Rect sourceBounds, 602 Bundle opts, UserHandle user) throws RemoteException { 603 if (!canAccessProfile(callingPackage, user, "Cannot start activity")) { 604 return; 605 } 606 if (!isUserEnabled(user)) { 607 throw new IllegalStateException("Cannot start activity for disabled profile " + user); 608 } 609 610 Intent launchIntent = new Intent(Intent.ACTION_MAIN); 611 launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); 612 launchIntent.setSourceBounds(sourceBounds); 613 launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK 614 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 615 launchIntent.setPackage(component.getPackageName()); 616 617 long ident = Binder.clearCallingIdentity(); 618 try { 619 IPackageManager pm = AppGlobals.getPackageManager(); 620 ActivityInfo info = pm.getActivityInfo(component, 621 PackageManager.MATCH_DIRECT_BOOT_AWARE 622 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, 623 user.getIdentifier()); 624 if (!info.exported) { 625 throw new SecurityException("Cannot launch non-exported components " 626 + component); 627 } 628 629 // Check that the component actually has Intent.CATEGORY_LAUCNCHER 630 // as calling startActivityAsUser ignores the category and just 631 // resolves based on the component if present. 632 List<ResolveInfo> apps = mPm.queryIntentActivitiesAsUser(launchIntent, 633 PackageManager.MATCH_DIRECT_BOOT_AWARE 634 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, 635 user.getIdentifier()); 636 final int size = apps.size(); 637 for (int i = 0; i < size; ++i) { 638 ActivityInfo activityInfo = apps.get(i).activityInfo; 639 if (activityInfo.packageName.equals(component.getPackageName()) && 640 activityInfo.name.equals(component.getClassName())) { 641 // Found an activity with category launcher that matches 642 // this component so ok to launch. 643 launchIntent.setComponent(component); 644 mContext.startActivityAsUser(launchIntent, opts, user); 645 return; 646 } 647 } 648 throw new SecurityException("Attempt to launch activity without " 649 + " category Intent.CATEGORY_LAUNCHER " + component); 650 } finally { 651 Binder.restoreCallingIdentity(ident); 652 } 653 } 654 655 @Override 656 public void showAppDetailsAsUser(String callingPackage, ComponentName component, 657 Rect sourceBounds, Bundle opts, UserHandle user) throws RemoteException { 658 if (!canAccessProfile(callingPackage, user, "Cannot show app details")) { 659 return; 660 } 661 if (!isUserEnabled(user)) { 662 throw new IllegalStateException("Cannot show app details for disabled profile " 663 + user); 664 } 665 666 long ident = Binder.clearCallingIdentity(); 667 try { 668 String packageName = component.getPackageName(); 669 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, 670 Uri.fromParts("package", packageName, null)); 671 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 672 intent.setSourceBounds(sourceBounds); 673 mContext.startActivityAsUser(intent, opts, user); 674 } finally { 675 Binder.restoreCallingIdentity(ident); 676 } 677 } 678 679 /** Checks if user is a profile of or same as listeningUser. 680 * and the user is enabled. */ 681 private boolean isEnabledProfileOf(UserHandle user, UserHandle listeningUser, 682 String debugMsg) { 683 if (user.getIdentifier() == listeningUser.getIdentifier()) { 684 if (DEBUG) Log.d(TAG, "Delivering msg to same user: " + debugMsg); 685 return true; 686 } 687 if (mUm.isManagedProfile(listeningUser.getIdentifier())) { 688 if (DEBUG) Log.d(TAG, "Managed profile can't see other profiles: " + debugMsg); 689 return false; 690 } 691 long ident = injectClearCallingIdentity(); 692 try { 693 UserInfo userInfo = mUm.getUserInfo(user.getIdentifier()); 694 UserInfo listeningUserInfo = mUm.getUserInfo(listeningUser.getIdentifier()); 695 if (userInfo == null || listeningUserInfo == null 696 || userInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID 697 || userInfo.profileGroupId != listeningUserInfo.profileGroupId 698 || !userInfo.isEnabled()) { 699 if (DEBUG) { 700 Log.d(TAG, "Not delivering msg from " + user + " to " + listeningUser + ":" 701 + debugMsg); 702 } 703 return false; 704 } else { 705 if (DEBUG) { 706 Log.d(TAG, "Delivering msg from " + user + " to " + listeningUser + ":" 707 + debugMsg); 708 } 709 return true; 710 } 711 } finally { 712 injectRestoreCallingIdentity(ident); 713 } 714 } 715 716 @VisibleForTesting 717 void postToPackageMonitorHandler(Runnable r) { 718 mCallbackHandler.post(r); 719 } 720 721 private class MyPackageMonitor extends PackageMonitor implements ShortcutChangeListener { 722 723 // TODO Simplify with lambdas. 724 725 @Override 726 public void onPackageAdded(String packageName, int uid) { 727 UserHandle user = new UserHandle(getChangingUserId()); 728 final int n = mListeners.beginBroadcast(); 729 try { 730 for (int i = 0; i < n; i++) { 731 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 732 BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); 733 if (!isEnabledProfileOf(user, cookie.user, "onPackageAdded")) continue; 734 try { 735 listener.onPackageAdded(user, packageName); 736 } catch (RemoteException re) { 737 Slog.d(TAG, "Callback failed ", re); 738 } 739 } 740 } finally { 741 mListeners.finishBroadcast(); 742 } 743 744 super.onPackageAdded(packageName, uid); 745 } 746 747 @Override 748 public void onPackageRemoved(String packageName, int uid) { 749 UserHandle user = new UserHandle(getChangingUserId()); 750 final int n = mListeners.beginBroadcast(); 751 try { 752 for (int i = 0; i < n; i++) { 753 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 754 BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); 755 if (!isEnabledProfileOf(user, cookie.user, "onPackageRemoved")) continue; 756 try { 757 listener.onPackageRemoved(user, packageName); 758 } catch (RemoteException re) { 759 Slog.d(TAG, "Callback failed ", re); 760 } 761 } 762 } finally { 763 mListeners.finishBroadcast(); 764 } 765 766 super.onPackageRemoved(packageName, uid); 767 } 768 769 @Override 770 public void onPackageModified(String packageName) { 771 UserHandle user = new UserHandle(getChangingUserId()); 772 final int n = mListeners.beginBroadcast(); 773 try { 774 for (int i = 0; i < n; i++) { 775 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 776 BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); 777 if (!isEnabledProfileOf(user, cookie.user, "onPackageModified")) continue; 778 try { 779 listener.onPackageChanged(user, packageName); 780 } catch (RemoteException re) { 781 Slog.d(TAG, "Callback failed ", re); 782 } 783 } 784 } finally { 785 mListeners.finishBroadcast(); 786 } 787 788 super.onPackageModified(packageName); 789 } 790 791 @Override 792 public void onPackagesAvailable(String[] packages) { 793 UserHandle user = new UserHandle(getChangingUserId()); 794 final int n = mListeners.beginBroadcast(); 795 try { 796 for (int i = 0; i < n; i++) { 797 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 798 BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); 799 if (!isEnabledProfileOf(user, cookie.user, "onPackagesAvailable")) continue; 800 try { 801 listener.onPackagesAvailable(user, packages, isReplacing()); 802 } catch (RemoteException re) { 803 Slog.d(TAG, "Callback failed ", re); 804 } 805 } 806 } finally { 807 mListeners.finishBroadcast(); 808 } 809 810 super.onPackagesAvailable(packages); 811 } 812 813 @Override 814 public void onPackagesUnavailable(String[] packages) { 815 UserHandle user = new UserHandle(getChangingUserId()); 816 final int n = mListeners.beginBroadcast(); 817 try { 818 for (int i = 0; i < n; i++) { 819 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 820 BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); 821 if (!isEnabledProfileOf(user, cookie.user, "onPackagesUnavailable")) continue; 822 try { 823 listener.onPackagesUnavailable(user, packages, isReplacing()); 824 } catch (RemoteException re) { 825 Slog.d(TAG, "Callback failed ", re); 826 } 827 } 828 } finally { 829 mListeners.finishBroadcast(); 830 } 831 832 super.onPackagesUnavailable(packages); 833 } 834 835 @Override 836 public void onPackagesSuspended(String[] packages) { 837 UserHandle user = new UserHandle(getChangingUserId()); 838 final int n = mListeners.beginBroadcast(); 839 try { 840 for (int i = 0; i < n; i++) { 841 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 842 BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); 843 if (!isEnabledProfileOf(user, cookie.user, "onPackagesSuspended")) continue; 844 try { 845 listener.onPackagesSuspended(user, packages); 846 } catch (RemoteException re) { 847 Slog.d(TAG, "Callback failed ", re); 848 } 849 } 850 } finally { 851 mListeners.finishBroadcast(); 852 } 853 854 super.onPackagesSuspended(packages); 855 } 856 857 @Override 858 public void onPackagesUnsuspended(String[] packages) { 859 UserHandle user = new UserHandle(getChangingUserId()); 860 final int n = mListeners.beginBroadcast(); 861 try { 862 for (int i = 0; i < n; i++) { 863 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 864 BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); 865 if (!isEnabledProfileOf(user, cookie.user, "onPackagesUnsuspended")) continue; 866 try { 867 listener.onPackagesUnsuspended(user, packages); 868 } catch (RemoteException re) { 869 Slog.d(TAG, "Callback failed ", re); 870 } 871 } 872 } finally { 873 mListeners.finishBroadcast(); 874 } 875 876 super.onPackagesUnsuspended(packages); 877 } 878 879 @Override 880 public void onShortcutChanged(@NonNull String packageName, 881 @UserIdInt int userId) { 882 postToPackageMonitorHandler(() -> onShortcutChangedInner(packageName, userId)); 883 } 884 885 private void onShortcutChangedInner(@NonNull String packageName, 886 @UserIdInt int userId) { 887 final int n = mListeners.beginBroadcast(); 888 try { 889 final UserHandle user = UserHandle.of(userId); 890 891 for (int i = 0; i < n; i++) { 892 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 893 BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); 894 if (!isEnabledProfileOf(user, cookie.user, "onShortcutChanged")) continue; 895 896 final int launcherUserId = cookie.user.getIdentifier(); 897 898 // Make sure the caller has the permission. 899 if (!mShortcutServiceInternal.hasShortcutHostPermission( 900 launcherUserId, cookie.packageName)) { 901 continue; 902 } 903 // Each launcher has a different set of pinned shortcuts, so we need to do a 904 // query in here. 905 // (As of now, only one launcher has the permission at a time, so it's bit 906 // moot, but we may change the permission model eventually.) 907 final List<ShortcutInfo> list = 908 mShortcutServiceInternal.getShortcuts(launcherUserId, 909 cookie.packageName, 910 /* changedSince= */ 0, packageName, /* shortcutIds=*/ null, 911 /* component= */ null, 912 ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY 913 | ShortcutQuery.FLAG_GET_ALL_KINDS 914 , userId); 915 try { 916 listener.onShortcutChanged(user, packageName, 917 new ParceledListSlice<>(list)); 918 } catch (RemoteException re) { 919 Slog.d(TAG, "Callback failed ", re); 920 } 921 } 922 } catch (RuntimeException e) { 923 // When the user is locked we get IllegalState, so just catch all. 924 Log.w(TAG, e.getMessage(), e); 925 } finally { 926 mListeners.finishBroadcast(); 927 } 928 } 929 } 930 931 class PackageCallbackList<T extends IInterface> extends RemoteCallbackList<T> { 932 @Override 933 public void onCallbackDied(T callback, Object cookie) { 934 checkCallbackCount(); 935 } 936 } 937 } 938} 939