LauncherAppsService.java revision 466d203c4ff032477d9a6bdb077ce3cd9b4fe070
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.app.AppGlobals; 20import android.content.ComponentName; 21import android.content.Context; 22import android.content.Intent; 23import android.content.pm.ActivityInfo; 24import android.content.pm.ILauncherApps; 25import android.content.pm.IOnAppsChangedListener; 26import android.content.pm.IPackageManager; 27import android.content.pm.PackageManager; 28import android.content.pm.PackageInfo; 29import android.content.pm.ResolveInfo; 30import android.content.pm.UserInfo; 31import android.graphics.Rect; 32import android.net.Uri; 33import android.os.Binder; 34import android.os.Bundle; 35import android.os.IInterface; 36import android.os.RemoteCallbackList; 37import android.os.RemoteException; 38import android.os.UserHandle; 39import android.os.UserManager; 40import android.provider.Settings; 41import android.util.Log; 42import android.util.Slog; 43 44import com.android.internal.content.PackageMonitor; 45import com.android.server.SystemService; 46 47import java.util.ArrayList; 48import java.util.List; 49 50/** 51 * Service that manages requests and callbacks for launchers that support 52 * managed profiles. 53 */ 54 55public class LauncherAppsService extends SystemService { 56 57 private final LauncherAppsImpl mLauncherAppsImpl; 58 59 public LauncherAppsService(Context context) { 60 super(context); 61 mLauncherAppsImpl = new LauncherAppsImpl(context); 62 } 63 64 @Override 65 public void onStart() { 66 publishBinderService(Context.LAUNCHER_APPS_SERVICE, mLauncherAppsImpl); 67 } 68 69 class LauncherAppsImpl extends ILauncherApps.Stub { 70 private static final boolean DEBUG = false; 71 private static final String TAG = "LauncherAppsService"; 72 private final Context mContext; 73 private final PackageManager mPm; 74 private final UserManager mUm; 75 private final PackageCallbackList<IOnAppsChangedListener> mListeners 76 = new PackageCallbackList<IOnAppsChangedListener>(); 77 78 private MyPackageMonitor mPackageMonitor = new MyPackageMonitor(); 79 80 public LauncherAppsImpl(Context context) { 81 mContext = context; 82 mPm = mContext.getPackageManager(); 83 mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE); 84 } 85 86 /* 87 * @see android.content.pm.ILauncherApps#addOnAppsChangedListener( 88 * android.content.pm.IOnAppsChangedListener) 89 */ 90 @Override 91 public void addOnAppsChangedListener(IOnAppsChangedListener listener) throws RemoteException { 92 synchronized (mListeners) { 93 if (DEBUG) { 94 Log.d(TAG, "Adding listener from " + Binder.getCallingUserHandle()); 95 } 96 if (mListeners.getRegisteredCallbackCount() == 0) { 97 if (DEBUG) { 98 Log.d(TAG, "Starting package monitoring"); 99 } 100 startWatchingPackageBroadcasts(); 101 } 102 mListeners.unregister(listener); 103 mListeners.register(listener, Binder.getCallingUserHandle()); 104 } 105 } 106 107 /* 108 * @see android.content.pm.ILauncherApps#removeOnAppsChangedListener( 109 * android.content.pm.IOnAppsChangedListener) 110 */ 111 @Override 112 public void removeOnAppsChangedListener(IOnAppsChangedListener listener) 113 throws RemoteException { 114 synchronized (mListeners) { 115 if (DEBUG) { 116 Log.d(TAG, "Removing listener from " + Binder.getCallingUserHandle()); 117 } 118 mListeners.unregister(listener); 119 if (mListeners.getRegisteredCallbackCount() == 0) { 120 stopWatchingPackageBroadcasts(); 121 } 122 } 123 } 124 125 /** 126 * Register a receiver to watch for package broadcasts 127 */ 128 private void startWatchingPackageBroadcasts() { 129 mPackageMonitor.register(mContext, null, UserHandle.ALL, true); 130 } 131 132 /** 133 * Unregister package broadcast receiver 134 */ 135 private void stopWatchingPackageBroadcasts() { 136 if (DEBUG) { 137 Log.d(TAG, "Stopped watching for packages"); 138 } 139 mPackageMonitor.unregister(); 140 } 141 142 void checkCallbackCount() { 143 synchronized (mListeners) { 144 if (DEBUG) { 145 Log.d(TAG, "Callback count = " + mListeners.getRegisteredCallbackCount()); 146 } 147 if (mListeners.getRegisteredCallbackCount() == 0) { 148 stopWatchingPackageBroadcasts(); 149 } 150 } 151 } 152 153 /** 154 * Checks if the caller is in the same group as the userToCheck. 155 */ 156 private void ensureInUserProfiles(UserHandle userToCheck, String message) { 157 final int callingUserId = UserHandle.getCallingUserId(); 158 final int targetUserId = userToCheck.getIdentifier(); 159 160 if (targetUserId == callingUserId) return; 161 162 long ident = Binder.clearCallingIdentity(); 163 try { 164 UserInfo callingUserInfo = mUm.getUserInfo(callingUserId); 165 UserInfo targetUserInfo = mUm.getUserInfo(targetUserId); 166 if (targetUserInfo == null 167 || targetUserInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID 168 || targetUserInfo.profileGroupId != callingUserInfo.profileGroupId) { 169 throw new SecurityException(message); 170 } 171 } finally { 172 Binder.restoreCallingIdentity(ident); 173 } 174 } 175 176 /** 177 * Checks if the user is enabled. 178 */ 179 private boolean isUserEnabled(UserHandle user) { 180 long ident = Binder.clearCallingIdentity(); 181 try { 182 UserInfo targetUserInfo = mUm.getUserInfo(user.getIdentifier()); 183 return targetUserInfo != null && targetUserInfo.isEnabled(); 184 } finally { 185 Binder.restoreCallingIdentity(ident); 186 } 187 } 188 189 @Override 190 public List<ResolveInfo> getLauncherActivities(String packageName, UserHandle user) 191 throws RemoteException { 192 ensureInUserProfiles(user, "Cannot retrieve activities for unrelated profile " + user); 193 if (!isUserEnabled(user)) { 194 return new ArrayList<ResolveInfo>(); 195 } 196 197 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 198 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 199 mainIntent.setPackage(packageName); 200 long ident = Binder.clearCallingIdentity(); 201 try { 202 List<ResolveInfo> apps = mPm.queryIntentActivitiesAsUser(mainIntent, 203 PackageManager.NO_CROSS_PROFILE, // We only want the apps for this user 204 user.getIdentifier()); 205 return apps; 206 } finally { 207 Binder.restoreCallingIdentity(ident); 208 } 209 } 210 211 @Override 212 public ResolveInfo resolveActivity(Intent intent, UserHandle user) 213 throws RemoteException { 214 ensureInUserProfiles(user, "Cannot resolve activity for unrelated profile " + user); 215 if (!isUserEnabled(user)) { 216 return null; 217 } 218 219 long ident = Binder.clearCallingIdentity(); 220 try { 221 ResolveInfo app = mPm.resolveActivityAsUser(intent, 0, user.getIdentifier()); 222 return app; 223 } finally { 224 Binder.restoreCallingIdentity(ident); 225 } 226 } 227 228 @Override 229 public boolean isPackageEnabled(String packageName, UserHandle user) 230 throws RemoteException { 231 ensureInUserProfiles(user, "Cannot check package for unrelated profile " + user); 232 if (!isUserEnabled(user)) { 233 return false; 234 } 235 236 long ident = Binder.clearCallingIdentity(); 237 try { 238 IPackageManager pm = AppGlobals.getPackageManager(); 239 PackageInfo info = pm.getPackageInfo(packageName, 0, user.getIdentifier()); 240 return info != null && info.applicationInfo.enabled; 241 } finally { 242 Binder.restoreCallingIdentity(ident); 243 } 244 } 245 246 @Override 247 public boolean isActivityEnabled(ComponentName component, UserHandle user) 248 throws RemoteException { 249 ensureInUserProfiles(user, "Cannot check component for unrelated profile " + user); 250 if (!isUserEnabled(user)) { 251 return false; 252 } 253 254 long ident = Binder.clearCallingIdentity(); 255 try { 256 IPackageManager pm = AppGlobals.getPackageManager(); 257 ActivityInfo info = pm.getActivityInfo(component, 0, user.getIdentifier()); 258 return info != null && info.isEnabled(); 259 } finally { 260 Binder.restoreCallingIdentity(ident); 261 } 262 } 263 264 @Override 265 public void startActivityAsUser(ComponentName component, Rect sourceBounds, 266 Bundle opts, UserHandle user) throws RemoteException { 267 ensureInUserProfiles(user, "Cannot start activity for unrelated profile " + user); 268 if (!isUserEnabled(user)) { 269 throw new IllegalStateException("Cannot start activity for disabled profile " + user); 270 } 271 272 Intent launchIntent = new Intent(Intent.ACTION_MAIN); 273 launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); 274 launchIntent.setSourceBounds(sourceBounds); 275 launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 276 launchIntent.setPackage(component.getPackageName()); 277 278 long ident = Binder.clearCallingIdentity(); 279 try { 280 IPackageManager pm = AppGlobals.getPackageManager(); 281 ActivityInfo info = pm.getActivityInfo(component, 0, user.getIdentifier()); 282 if (!info.exported) { 283 throw new SecurityException("Cannot launch non-exported components " 284 + component); 285 } 286 287 // Check that the component actually has Intent.CATEGORY_LAUCNCHER 288 // as calling startActivityAsUser ignores the category and just 289 // resolves based on the component if present. 290 List<ResolveInfo> apps = mPm.queryIntentActivitiesAsUser(launchIntent, 291 PackageManager.NO_CROSS_PROFILE, // We only want the apps for this user 292 user.getIdentifier()); 293 final int size = apps.size(); 294 for (int i = 0; i < size; ++i) { 295 ActivityInfo activityInfo = apps.get(i).activityInfo; 296 if (activityInfo.packageName.equals(component.getPackageName()) && 297 activityInfo.name.equals(component.getClassName())) { 298 // Found an activity with category launcher that matches 299 // this component so ok to launch. 300 launchIntent.setComponent(component); 301 mContext.startActivityAsUser(launchIntent, opts, user); 302 return; 303 } 304 } 305 throw new SecurityException("Attempt to launch activity without " 306 + " category Intent.CATEGORY_LAUNCHER " + component); 307 } finally { 308 Binder.restoreCallingIdentity(ident); 309 } 310 } 311 312 @Override 313 public void showAppDetailsAsUser(ComponentName component, Rect sourceBounds, 314 Bundle opts, UserHandle user) throws RemoteException { 315 ensureInUserProfiles(user, "Cannot show app details for unrelated profile " + user); 316 if (!isUserEnabled(user)) { 317 throw new IllegalStateException("Cannot show app details for disabled profile " 318 + user); 319 } 320 321 long ident = Binder.clearCallingIdentity(); 322 try { 323 String packageName = component.getPackageName(); 324 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, 325 Uri.fromParts("package", packageName, null)); 326 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | 327 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 328 intent.setSourceBounds(sourceBounds); 329 mContext.startActivityAsUser(intent, opts, user); 330 } finally { 331 Binder.restoreCallingIdentity(ident); 332 } 333 } 334 335 336 private class MyPackageMonitor extends PackageMonitor { 337 338 /** Checks if user is a profile of or same as listeningUser. 339 * and the user is enabled. */ 340 private boolean isEnabledProfileOf(UserHandle user, UserHandle listeningUser, 341 String debugMsg) { 342 if (user.getIdentifier() == listeningUser.getIdentifier()) { 343 if (DEBUG) Log.d(TAG, "Delivering msg to same user " + debugMsg); 344 return true; 345 } 346 long ident = Binder.clearCallingIdentity(); 347 try { 348 UserInfo userInfo = mUm.getUserInfo(user.getIdentifier()); 349 UserInfo listeningUserInfo = mUm.getUserInfo(listeningUser.getIdentifier()); 350 if (userInfo == null || listeningUserInfo == null 351 || userInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID 352 || userInfo.profileGroupId != listeningUserInfo.profileGroupId 353 || !userInfo.isEnabled()) { 354 if (DEBUG) { 355 Log.d(TAG, "Not delivering msg from " + user + " to " + listeningUser + ":" 356 + debugMsg); 357 } 358 return false; 359 } else { 360 if (DEBUG) { 361 Log.d(TAG, "Delivering msg from " + user + " to " + listeningUser + ":" 362 + debugMsg); 363 } 364 return true; 365 } 366 } finally { 367 Binder.restoreCallingIdentity(ident); 368 } 369 } 370 371 @Override 372 public void onPackageAdded(String packageName, int uid) { 373 UserHandle user = new UserHandle(getChangingUserId()); 374 final int n = mListeners.beginBroadcast(); 375 for (int i = 0; i < n; i++) { 376 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 377 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); 378 if (!isEnabledProfileOf(user, listeningUser, "onPackageAdded")) continue; 379 try { 380 listener.onPackageAdded(user, packageName); 381 } catch (RemoteException re) { 382 Slog.d(TAG, "Callback failed ", re); 383 } 384 } 385 mListeners.finishBroadcast(); 386 387 super.onPackageAdded(packageName, uid); 388 } 389 390 @Override 391 public void onPackageRemoved(String packageName, int uid) { 392 UserHandle user = new UserHandle(getChangingUserId()); 393 final int n = mListeners.beginBroadcast(); 394 for (int i = 0; i < n; i++) { 395 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 396 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); 397 if (!isEnabledProfileOf(user, listeningUser, "onPackageRemoved")) continue; 398 try { 399 listener.onPackageRemoved(user, packageName); 400 } catch (RemoteException re) { 401 Slog.d(TAG, "Callback failed ", re); 402 } 403 } 404 mListeners.finishBroadcast(); 405 406 super.onPackageRemoved(packageName, uid); 407 } 408 409 @Override 410 public void onPackageModified(String packageName) { 411 UserHandle user = new UserHandle(getChangingUserId()); 412 final int n = mListeners.beginBroadcast(); 413 for (int i = 0; i < n; i++) { 414 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 415 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); 416 if (!isEnabledProfileOf(user, listeningUser, "onPackageModified")) continue; 417 try { 418 listener.onPackageChanged(user, packageName); 419 } catch (RemoteException re) { 420 Slog.d(TAG, "Callback failed ", re); 421 } 422 } 423 mListeners.finishBroadcast(); 424 425 super.onPackageModified(packageName); 426 } 427 428 @Override 429 public void onPackagesAvailable(String[] packages) { 430 UserHandle user = new UserHandle(getChangingUserId()); 431 final int n = mListeners.beginBroadcast(); 432 for (int i = 0; i < n; i++) { 433 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 434 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); 435 if (!isEnabledProfileOf(user, listeningUser, "onPackagesAvailable")) continue; 436 try { 437 listener.onPackagesAvailable(user, packages, isReplacing()); 438 } catch (RemoteException re) { 439 Slog.d(TAG, "Callback failed ", re); 440 } 441 } 442 mListeners.finishBroadcast(); 443 444 super.onPackagesAvailable(packages); 445 } 446 447 @Override 448 public void onPackagesUnavailable(String[] packages) { 449 UserHandle user = new UserHandle(getChangingUserId()); 450 final int n = mListeners.beginBroadcast(); 451 for (int i = 0; i < n; i++) { 452 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 453 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); 454 if (!isEnabledProfileOf(user, listeningUser, "onPackagesUnavailable")) continue; 455 try { 456 listener.onPackagesUnavailable(user, packages, isReplacing()); 457 } catch (RemoteException re) { 458 Slog.d(TAG, "Callback failed ", re); 459 } 460 } 461 mListeners.finishBroadcast(); 462 463 super.onPackagesUnavailable(packages); 464 } 465 466 } 467 468 class PackageCallbackList<T extends IInterface> extends RemoteCallbackList<T> { 469 @Override 470 public void onCallbackDied(T callback, Object cookie) { 471 checkCallbackCount(); 472 } 473 } 474 } 475}