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 17 package com.android.server.pm; 18 19 import android.app.AppGlobals; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.ActivityInfo; 24 import android.content.pm.ILauncherApps; 25 import android.content.pm.IOnAppsChangedListener; 26 import android.content.pm.IPackageManager; 27 import android.content.pm.PackageManager; 28 import android.content.pm.PackageInfo; 29 import android.content.pm.ResolveInfo; 30 import android.content.pm.UserInfo; 31 import android.graphics.Rect; 32 import android.net.Uri; 33 import android.os.Binder; 34 import android.os.Bundle; 35 import android.os.IInterface; 36 import android.os.RemoteCallbackList; 37 import android.os.RemoteException; 38 import android.os.UserHandle; 39 import android.os.UserManager; 40 import android.provider.Settings; 41 import android.util.Log; 42 import android.util.Slog; 43 44 import com.android.internal.content.PackageMonitor; 45 import com.android.server.SystemService; 46 47 import java.util.ArrayList; 48 import java.util.List; 49 50 /** 51 * Service that manages requests and callbacks for launchers that support 52 * managed profiles. 53 */ 54 55 public 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, 0 /* flags */, 203 user.getIdentifier()); 204 return apps; 205 } finally { 206 Binder.restoreCallingIdentity(ident); 207 } 208 } 209 210 @Override 211 public ResolveInfo resolveActivity(Intent intent, UserHandle user) 212 throws RemoteException { 213 ensureInUserProfiles(user, "Cannot resolve activity for unrelated profile " + user); 214 if (!isUserEnabled(user)) { 215 return null; 216 } 217 218 long ident = Binder.clearCallingIdentity(); 219 try { 220 ResolveInfo app = mPm.resolveActivityAsUser(intent, 0, user.getIdentifier()); 221 return app; 222 } finally { 223 Binder.restoreCallingIdentity(ident); 224 } 225 } 226 227 @Override 228 public boolean isPackageEnabled(String packageName, UserHandle user) 229 throws RemoteException { 230 ensureInUserProfiles(user, "Cannot check package for unrelated profile " + user); 231 if (!isUserEnabled(user)) { 232 return false; 233 } 234 235 long ident = Binder.clearCallingIdentity(); 236 try { 237 IPackageManager pm = AppGlobals.getPackageManager(); 238 PackageInfo info = pm.getPackageInfo(packageName, 0, user.getIdentifier()); 239 return info != null && info.applicationInfo.enabled; 240 } finally { 241 Binder.restoreCallingIdentity(ident); 242 } 243 } 244 245 @Override 246 public boolean isActivityEnabled(ComponentName component, UserHandle user) 247 throws RemoteException { 248 ensureInUserProfiles(user, "Cannot check component for unrelated profile " + user); 249 if (!isUserEnabled(user)) { 250 return false; 251 } 252 253 long ident = Binder.clearCallingIdentity(); 254 try { 255 IPackageManager pm = AppGlobals.getPackageManager(); 256 ActivityInfo info = pm.getActivityInfo(component, 0, user.getIdentifier()); 257 return info != null; 258 } finally { 259 Binder.restoreCallingIdentity(ident); 260 } 261 } 262 263 @Override 264 public void startActivityAsUser(ComponentName component, Rect sourceBounds, 265 Bundle opts, UserHandle user) throws RemoteException { 266 ensureInUserProfiles(user, "Cannot start activity for unrelated profile " + user); 267 if (!isUserEnabled(user)) { 268 throw new IllegalStateException("Cannot start activity for disabled profile " + user); 269 } 270 271 Intent launchIntent = new Intent(Intent.ACTION_MAIN); 272 launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); 273 launchIntent.setSourceBounds(sourceBounds); 274 launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 275 launchIntent.setPackage(component.getPackageName()); 276 277 long ident = Binder.clearCallingIdentity(); 278 try { 279 IPackageManager pm = AppGlobals.getPackageManager(); 280 ActivityInfo info = pm.getActivityInfo(component, 0, user.getIdentifier()); 281 if (!info.exported) { 282 throw new SecurityException("Cannot launch non-exported components " 283 + component); 284 } 285 286 // Check that the component actually has Intent.CATEGORY_LAUCNCHER 287 // as calling startActivityAsUser ignores the category and just 288 // resolves based on the component if present. 289 List<ResolveInfo> apps = mPm.queryIntentActivitiesAsUser(launchIntent, 290 0 /* flags */, user.getIdentifier()); 291 final int size = apps.size(); 292 for (int i = 0; i < size; ++i) { 293 ActivityInfo activityInfo = apps.get(i).activityInfo; 294 if (activityInfo.packageName.equals(component.getPackageName()) && 295 activityInfo.name.equals(component.getClassName())) { 296 // Found an activity with category launcher that matches 297 // this component so ok to launch. 298 launchIntent.setComponent(component); 299 mContext.startActivityAsUser(launchIntent, opts, user); 300 return; 301 } 302 } 303 throw new SecurityException("Attempt to launch activity without " 304 + " category Intent.CATEGORY_LAUNCHER " + component); 305 } finally { 306 Binder.restoreCallingIdentity(ident); 307 } 308 } 309 310 @Override 311 public void showAppDetailsAsUser(ComponentName component, Rect sourceBounds, 312 Bundle opts, UserHandle user) throws RemoteException { 313 ensureInUserProfiles(user, "Cannot show app details for unrelated profile " + user); 314 if (!isUserEnabled(user)) { 315 throw new IllegalStateException("Cannot show app details for disabled profile " 316 + user); 317 } 318 319 long ident = Binder.clearCallingIdentity(); 320 try { 321 String packageName = component.getPackageName(); 322 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, 323 Uri.fromParts("package", packageName, null)); 324 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | 325 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 326 intent.setSourceBounds(sourceBounds); 327 mContext.startActivityAsUser(intent, opts, user); 328 } finally { 329 Binder.restoreCallingIdentity(ident); 330 } 331 } 332 333 334 private class MyPackageMonitor extends PackageMonitor { 335 336 /** Checks if user is a profile of or same as listeningUser. 337 * and the user is enabled. */ 338 private boolean isEnabledProfileOf(UserHandle user, UserHandle listeningUser, 339 String debugMsg) { 340 if (user.getIdentifier() == listeningUser.getIdentifier()) { 341 if (DEBUG) Log.d(TAG, "Delivering msg to same user " + debugMsg); 342 return true; 343 } 344 long ident = Binder.clearCallingIdentity(); 345 try { 346 UserInfo userInfo = mUm.getUserInfo(user.getIdentifier()); 347 UserInfo listeningUserInfo = mUm.getUserInfo(listeningUser.getIdentifier()); 348 if (userInfo == null || listeningUserInfo == null 349 || userInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID 350 || userInfo.profileGroupId != listeningUserInfo.profileGroupId 351 || !userInfo.isEnabled()) { 352 if (DEBUG) { 353 Log.d(TAG, "Not delivering msg from " + user + " to " + listeningUser + ":" 354 + debugMsg); 355 } 356 return false; 357 } else { 358 if (DEBUG) { 359 Log.d(TAG, "Delivering msg from " + user + " to " + listeningUser + ":" 360 + debugMsg); 361 } 362 return true; 363 } 364 } finally { 365 Binder.restoreCallingIdentity(ident); 366 } 367 } 368 369 @Override 370 public void onPackageAdded(String packageName, int uid) { 371 UserHandle user = new UserHandle(getChangingUserId()); 372 final int n = mListeners.beginBroadcast(); 373 for (int i = 0; i < n; i++) { 374 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 375 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); 376 if (!isEnabledProfileOf(user, listeningUser, "onPackageAdded")) continue; 377 try { 378 listener.onPackageAdded(user, packageName); 379 } catch (RemoteException re) { 380 Slog.d(TAG, "Callback failed ", re); 381 } 382 } 383 mListeners.finishBroadcast(); 384 385 super.onPackageAdded(packageName, uid); 386 } 387 388 @Override 389 public void onPackageRemoved(String packageName, int uid) { 390 UserHandle user = new UserHandle(getChangingUserId()); 391 final int n = mListeners.beginBroadcast(); 392 for (int i = 0; i < n; i++) { 393 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 394 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); 395 if (!isEnabledProfileOf(user, listeningUser, "onPackageRemoved")) continue; 396 try { 397 listener.onPackageRemoved(user, packageName); 398 } catch (RemoteException re) { 399 Slog.d(TAG, "Callback failed ", re); 400 } 401 } 402 mListeners.finishBroadcast(); 403 404 super.onPackageRemoved(packageName, uid); 405 } 406 407 @Override 408 public void onPackageModified(String packageName) { 409 UserHandle user = new UserHandle(getChangingUserId()); 410 final int n = mListeners.beginBroadcast(); 411 for (int i = 0; i < n; i++) { 412 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 413 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); 414 if (!isEnabledProfileOf(user, listeningUser, "onPackageModified")) continue; 415 try { 416 listener.onPackageChanged(user, packageName); 417 } catch (RemoteException re) { 418 Slog.d(TAG, "Callback failed ", re); 419 } 420 } 421 mListeners.finishBroadcast(); 422 423 super.onPackageModified(packageName); 424 } 425 426 @Override 427 public void onPackagesAvailable(String[] packages) { 428 UserHandle user = new UserHandle(getChangingUserId()); 429 final int n = mListeners.beginBroadcast(); 430 for (int i = 0; i < n; i++) { 431 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 432 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); 433 if (!isEnabledProfileOf(user, listeningUser, "onPackagesAvailable")) continue; 434 try { 435 listener.onPackagesAvailable(user, packages, isReplacing()); 436 } catch (RemoteException re) { 437 Slog.d(TAG, "Callback failed ", re); 438 } 439 } 440 mListeners.finishBroadcast(); 441 442 super.onPackagesAvailable(packages); 443 } 444 445 @Override 446 public void onPackagesUnavailable(String[] packages) { 447 UserHandle user = new UserHandle(getChangingUserId()); 448 final int n = mListeners.beginBroadcast(); 449 for (int i = 0; i < n; i++) { 450 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); 451 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); 452 if (!isEnabledProfileOf(user, listeningUser, "onPackagesUnavailable")) continue; 453 try { 454 listener.onPackagesUnavailable(user, packages, isReplacing()); 455 } catch (RemoteException re) { 456 Slog.d(TAG, "Callback failed ", re); 457 } 458 } 459 mListeners.finishBroadcast(); 460 461 super.onPackagesUnavailable(packages); 462 } 463 464 } 465 466 class PackageCallbackList<T extends IInterface> extends RemoteCallbackList<T> { 467 @Override 468 public void onCallbackDied(T callback, Object cookie) { 469 checkCallbackCount(); 470 } 471 } 472 } 473 }