blob: d787919b01ab33ca6560c09e65d32efde53be6a8 [file] [log] [blame]
Amith Yamasani4f582632014-02-19 14:31:52 -08001/*
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
Kenny Guy53fa4ec2014-04-29 14:24:18 +010019import android.app.AppGlobals;
Amith Yamasani4f582632014-02-19 14:31:52 -080020import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
Kenny Guy53fa4ec2014-04-29 14:24:18 +010023import android.content.pm.ActivityInfo;
Amith Yamasani4f582632014-02-19 14:31:52 -080024import android.content.pm.ILauncherApps;
25import android.content.pm.IOnAppsChangedListener;
Kenny Guy53fa4ec2014-04-29 14:24:18 +010026import android.content.pm.IPackageManager;
Amith Yamasani4f582632014-02-19 14:31:52 -080027import android.content.pm.PackageManager;
Kenny Guy53fa4ec2014-04-29 14:24:18 +010028import android.content.pm.PackageInfo;
Amith Yamasani4f582632014-02-19 14:31:52 -080029import android.content.pm.ResolveInfo;
30import android.content.pm.UserInfo;
31import android.graphics.Rect;
Kenny Guy466d2032014-07-23 12:23:35 +010032import android.net.Uri;
Amith Yamasani4f582632014-02-19 14:31:52 -080033import 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;
Kenny Guy466d2032014-07-23 12:23:35 +010040import android.provider.Settings;
Amith Yamasani932249d2014-05-01 14:39:35 -070041import android.util.Log;
Amith Yamasani4f582632014-02-19 14:31:52 -080042import android.util.Slog;
43
44import com.android.internal.content.PackageMonitor;
Amith Yamasanidf7db072014-06-01 10:41:13 -070045import com.android.server.SystemService;
Amith Yamasani4f582632014-02-19 14:31:52 -080046
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 */
Amith Yamasani4f582632014-02-19 14:31:52 -080054
Amith Yamasanidf7db072014-06-01 10:41:13 -070055public class LauncherAppsService extends SystemService {
56
57 private final LauncherAppsImpl mLauncherAppsImpl;
Amith Yamasani4f582632014-02-19 14:31:52 -080058
59 public LauncherAppsService(Context context) {
Amith Yamasanidf7db072014-06-01 10:41:13 -070060 super(context);
61 mLauncherAppsImpl = new LauncherAppsImpl(context);
Amith Yamasani4f582632014-02-19 14:31:52 -080062 }
63
Amith Yamasani4f582632014-02-19 14:31:52 -080064 @Override
Amith Yamasanidf7db072014-06-01 10:41:13 -070065 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) {
Amith Yamasani932249d2014-05-01 14:39:35 -070093 if (DEBUG) {
Amith Yamasanidf7db072014-06-01 10:41:13 -070094 Log.d(TAG, "Adding listener from " + Binder.getCallingUserHandle());
Amith Yamasani932249d2014-05-01 14:39:35 -070095 }
Amith Yamasanidf7db072014-06-01 10:41:13 -070096 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());
Amith Yamasani4f582632014-02-19 14:31:52 -0800104 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800105 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800106
Amith Yamasanidf7db072014-06-01 10:41:13 -0700107 /*
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() {
Amith Yamasani932249d2014-05-01 14:39:35 -0700136 if (DEBUG) {
Amith Yamasanidf7db072014-06-01 10:41:13 -0700137 Log.d(TAG, "Stopped watching for packages");
Amith Yamasani932249d2014-05-01 14:39:35 -0700138 }
Amith Yamasanidf7db072014-06-01 10:41:13 -0700139 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 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800150 }
151 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800152
Amith Yamasanidf7db072014-06-01 10:41:13 -0700153 /**
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();
Amith Yamasani4f582632014-02-19 14:31:52 -0800159
Amith Yamasanidf7db072014-06-01 10:41:13 -0700160 if (targetUserId == callingUserId) return;
Amith Yamasani4f582632014-02-19 14:31:52 -0800161
Amith Yamasani932249d2014-05-01 14:39:35 -0700162 long ident = Binder.clearCallingIdentity();
163 try {
Amith Yamasanidf7db072014-06-01 10:41:13 -0700164 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);
Amith Yamasani932249d2014-05-01 14:39:35 -0700170 }
171 } finally {
172 Binder.restoreCallingIdentity(ident);
173 }
174 }
175
Amith Yamasanidf7db072014-06-01 10:41:13 -0700176 /**
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);
Amith Yamasani4f582632014-02-19 14:31:52 -0800186 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800187 }
188
189 @Override
Amith Yamasanidf7db072014-06-01 10:41:13 -0700190 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>();
Amith Yamasani4f582632014-02-19 14:31:52 -0800195 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800196
Amith Yamasanidf7db072014-06-01 10:41:13 -0700197 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 {
Alexandra Gherghinac7884872014-09-09 10:21:11 +0100202 List<ResolveInfo> apps = mPm.queryIntentActivitiesAsUser(mainIntent, 0 /* flags */,
Amith Yamasanidf7db072014-06-01 10:41:13 -0700203 user.getIdentifier());
204 return apps;
205 } finally {
206 Binder.restoreCallingIdentity(ident);
207 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800208 }
209
210 @Override
Amith Yamasanidf7db072014-06-01 10:41:13 -0700211 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;
Amith Yamasani4f582632014-02-19 14:31:52 -0800216 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800217
Amith Yamasanidf7db072014-06-01 10:41:13 -0700218 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 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800225 }
226
227 @Override
Amith Yamasanidf7db072014-06-01 10:41:13 -0700228 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;
Amith Yamasani4f582632014-02-19 14:31:52 -0800233 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800234
Amith Yamasanidf7db072014-06-01 10:41:13 -0700235 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 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800243 }
244
245 @Override
Amith Yamasanidf7db072014-06-01 10:41:13 -0700246 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;
Amith Yamasani4f582632014-02-19 14:31:52 -0800251 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800252
Amith Yamasanidf7db072014-06-01 10:41:13 -0700253 long ident = Binder.clearCallingIdentity();
254 try {
255 IPackageManager pm = AppGlobals.getPackageManager();
256 ActivityInfo info = pm.getActivityInfo(component, 0, user.getIdentifier());
Winson Chungdcf21d152014-10-01 14:30:05 -0700257 return info != null;
Amith Yamasanidf7db072014-06-01 10:41:13 -0700258 } finally {
259 Binder.restoreCallingIdentity(ident);
260 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800261 }
262
Amith Yamasani4f582632014-02-19 14:31:52 -0800263 @Override
Amith Yamasanidf7db072014-06-01 10:41:13 -0700264 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);
Amith Yamasanidf7db072014-06-01 10:41:13 -0700273 launchIntent.setSourceBounds(sourceBounds);
274 launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Kenny Guy2df18922014-07-17 13:01:25 +0100275 launchIntent.setPackage(component.getPackageName());
Amith Yamasanidf7db072014-06-01 10:41:13 -0700276
277 long ident = Binder.clearCallingIdentity();
278 try {
Kenny Guy2df18922014-07-17 13:01:25 +0100279 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,
Alexandra Gherghinac7884872014-09-09 10:21:11 +0100290 0 /* flags */, user.getIdentifier());
Kenny Guy2df18922014-07-17 13:01:25 +0100291 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);
Amith Yamasanidf7db072014-06-01 10:41:13 -0700305 } finally {
306 Binder.restoreCallingIdentity(ident);
307 }
308 }
309
Kenny Guy466d2032014-07-23 12:23:35 +0100310 @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
Amith Yamasanidf7db072014-06-01 10:41:13 -0700334 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 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800471 }
472 }
Amith Yamasanidf7db072014-06-01 10:41:13 -0700473}