blob: e90fb3208d8580dc4db54e1b25945970dadfd46e [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
Makoto Onuki6f7362d92016-03-04 13:39:41 -080019import android.annotation.NonNull;
Makoto Onuki43204b82016-03-08 16:16:44 -080020import android.annotation.Nullable;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080021import android.annotation.UserIdInt;
Kenny Guy53fa4ec2014-04-29 14:24:18 +010022import android.app.AppGlobals;
Amith Yamasani4f582632014-02-19 14:31:52 -080023import android.content.ComponentName;
24import android.content.Context;
25import android.content.Intent;
Kenny Guy53fa4ec2014-04-29 14:24:18 +010026import android.content.pm.ActivityInfo;
Kenny Guy77242752016-01-15 13:29:06 +000027import android.content.pm.ApplicationInfo;
Amith Yamasani4f582632014-02-19 14:31:52 -080028import android.content.pm.ILauncherApps;
29import android.content.pm.IOnAppsChangedListener;
Kenny Guy53fa4ec2014-04-29 14:24:18 +010030import android.content.pm.IPackageManager;
Kenny Guy53fa4ec2014-04-29 14:24:18 +010031import android.content.pm.PackageInfo;
Jeff Sharkeycd654482016-01-08 17:42:11 -070032import android.content.pm.PackageManager;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080033import android.content.pm.PackageManager.NameNotFoundException;
Sunny Goyal6cbc2fe2015-11-24 09:34:20 -080034import android.content.pm.ParceledListSlice;
Amith Yamasani4f582632014-02-19 14:31:52 -080035import android.content.pm.ResolveInfo;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080036import android.content.pm.ShortcutInfo;
37import android.content.pm.ShortcutServiceInternal;
38import android.content.pm.ShortcutServiceInternal.ShortcutChangeListener;
Amith Yamasani4f582632014-02-19 14:31:52 -080039import android.content.pm.UserInfo;
40import android.graphics.Rect;
Kenny Guy466d2032014-07-23 12:23:35 +010041import android.net.Uri;
Amith Yamasani4f582632014-02-19 14:31:52 -080042import android.os.Binder;
43import android.os.Bundle;
44import android.os.IInterface;
Makoto Onuki55046222016-03-08 10:49:47 -080045import android.os.ParcelFileDescriptor;
Amith Yamasani4f582632014-02-19 14:31:52 -080046import android.os.RemoteCallbackList;
47import android.os.RemoteException;
48import android.os.UserHandle;
49import android.os.UserManager;
Kenny Guy466d2032014-07-23 12:23:35 +010050import android.provider.Settings;
Amith Yamasani932249d2014-05-01 14:39:35 -070051import android.util.Log;
Amith Yamasani4f582632014-02-19 14:31:52 -080052import android.util.Slog;
53
54import com.android.internal.content.PackageMonitor;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080055import com.android.internal.util.Preconditions;
56import com.android.server.LocalServices;
Amith Yamasanidf7db072014-06-01 10:41:13 -070057import com.android.server.SystemService;
Amith Yamasani4f582632014-02-19 14:31:52 -080058
Amith Yamasani4f582632014-02-19 14:31:52 -080059import java.util.List;
60
61/**
62 * Service that manages requests and callbacks for launchers that support
Makoto Onuki6f7362d92016-03-04 13:39:41 -080063 * managed profiles.
Amith Yamasani4f582632014-02-19 14:31:52 -080064 */
Amith Yamasanidf7db072014-06-01 10:41:13 -070065public class LauncherAppsService extends SystemService {
66
67 private final LauncherAppsImpl mLauncherAppsImpl;
Amith Yamasani4f582632014-02-19 14:31:52 -080068
69 public LauncherAppsService(Context context) {
Amith Yamasanidf7db072014-06-01 10:41:13 -070070 super(context);
71 mLauncherAppsImpl = new LauncherAppsImpl(context);
Amith Yamasani4f582632014-02-19 14:31:52 -080072 }
73
Amith Yamasani4f582632014-02-19 14:31:52 -080074 @Override
Amith Yamasanidf7db072014-06-01 10:41:13 -070075 public void onStart() {
76 publishBinderService(Context.LAUNCHER_APPS_SERVICE, mLauncherAppsImpl);
77 }
78
Makoto Onuki6f7362d92016-03-04 13:39:41 -080079 static class LauncherAppsImpl extends ILauncherApps.Stub {
Amith Yamasanidf7db072014-06-01 10:41:13 -070080 private static final boolean DEBUG = false;
81 private static final String TAG = "LauncherAppsService";
82 private final Context mContext;
83 private final PackageManager mPm;
84 private final UserManager mUm;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080085 private final ShortcutServiceInternal mShortcutServiceInternal;
Amith Yamasanidf7db072014-06-01 10:41:13 -070086 private final PackageCallbackList<IOnAppsChangedListener> mListeners
87 = new PackageCallbackList<IOnAppsChangedListener>();
88
Makoto Onuki6f7362d92016-03-04 13:39:41 -080089 private final MyPackageMonitor mPackageMonitor = new MyPackageMonitor();
Amith Yamasanidf7db072014-06-01 10:41:13 -070090
91 public LauncherAppsImpl(Context context) {
92 mContext = context;
93 mPm = mContext.getPackageManager();
94 mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Makoto Onuki6f7362d92016-03-04 13:39:41 -080095 mShortcutServiceInternal = Preconditions.checkNotNull(
96 LocalServices.getService(ShortcutServiceInternal.class));
97 mShortcutServiceInternal.addListener(mPackageMonitor);
Amith Yamasanidf7db072014-06-01 10:41:13 -070098 }
99
100 /*
101 * @see android.content.pm.ILauncherApps#addOnAppsChangedListener(
102 * android.content.pm.IOnAppsChangedListener)
103 */
104 @Override
105 public void addOnAppsChangedListener(IOnAppsChangedListener listener) throws RemoteException {
106 synchronized (mListeners) {
Amith Yamasani932249d2014-05-01 14:39:35 -0700107 if (DEBUG) {
Amith Yamasanidf7db072014-06-01 10:41:13 -0700108 Log.d(TAG, "Adding listener from " + Binder.getCallingUserHandle());
Amith Yamasani932249d2014-05-01 14:39:35 -0700109 }
Amith Yamasanidf7db072014-06-01 10:41:13 -0700110 if (mListeners.getRegisteredCallbackCount() == 0) {
111 if (DEBUG) {
112 Log.d(TAG, "Starting package monitoring");
113 }
114 startWatchingPackageBroadcasts();
115 }
116 mListeners.unregister(listener);
117 mListeners.register(listener, Binder.getCallingUserHandle());
Amith Yamasani4f582632014-02-19 14:31:52 -0800118 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800119 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800120
Amith Yamasanidf7db072014-06-01 10:41:13 -0700121 /*
122 * @see android.content.pm.ILauncherApps#removeOnAppsChangedListener(
123 * android.content.pm.IOnAppsChangedListener)
124 */
125 @Override
126 public void removeOnAppsChangedListener(IOnAppsChangedListener listener)
127 throws RemoteException {
128 synchronized (mListeners) {
129 if (DEBUG) {
130 Log.d(TAG, "Removing listener from " + Binder.getCallingUserHandle());
131 }
132 mListeners.unregister(listener);
133 if (mListeners.getRegisteredCallbackCount() == 0) {
134 stopWatchingPackageBroadcasts();
135 }
136 }
137 }
138
139 /**
140 * Register a receiver to watch for package broadcasts
141 */
142 private void startWatchingPackageBroadcasts() {
143 mPackageMonitor.register(mContext, null, UserHandle.ALL, true);
144 }
145
146 /**
147 * Unregister package broadcast receiver
148 */
149 private void stopWatchingPackageBroadcasts() {
Amith Yamasani932249d2014-05-01 14:39:35 -0700150 if (DEBUG) {
Amith Yamasanidf7db072014-06-01 10:41:13 -0700151 Log.d(TAG, "Stopped watching for packages");
Amith Yamasani932249d2014-05-01 14:39:35 -0700152 }
Amith Yamasanidf7db072014-06-01 10:41:13 -0700153 mPackageMonitor.unregister();
154 }
155
156 void checkCallbackCount() {
157 synchronized (mListeners) {
158 if (DEBUG) {
159 Log.d(TAG, "Callback count = " + mListeners.getRegisteredCallbackCount());
160 }
161 if (mListeners.getRegisteredCallbackCount() == 0) {
162 stopWatchingPackageBroadcasts();
163 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800164 }
165 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800166
Amith Yamasanidf7db072014-06-01 10:41:13 -0700167 /**
168 * Checks if the caller is in the same group as the userToCheck.
169 */
170 private void ensureInUserProfiles(UserHandle userToCheck, String message) {
171 final int callingUserId = UserHandle.getCallingUserId();
172 final int targetUserId = userToCheck.getIdentifier();
Amith Yamasani4f582632014-02-19 14:31:52 -0800173
Amith Yamasanidf7db072014-06-01 10:41:13 -0700174 if (targetUserId == callingUserId) return;
Amith Yamasani4f582632014-02-19 14:31:52 -0800175
Amith Yamasani932249d2014-05-01 14:39:35 -0700176 long ident = Binder.clearCallingIdentity();
177 try {
Amith Yamasanidf7db072014-06-01 10:41:13 -0700178 UserInfo callingUserInfo = mUm.getUserInfo(callingUserId);
179 UserInfo targetUserInfo = mUm.getUserInfo(targetUserId);
180 if (targetUserInfo == null
181 || targetUserInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID
182 || targetUserInfo.profileGroupId != callingUserInfo.profileGroupId) {
183 throw new SecurityException(message);
Amith Yamasani932249d2014-05-01 14:39:35 -0700184 }
185 } finally {
186 Binder.restoreCallingIdentity(ident);
187 }
188 }
189
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800190 private void verifyCallingPackage(String callingPackage) {
191 int packageUid = -1;
192 try {
193 packageUid = mPm.getPackageUid(callingPackage,
194 PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE
195 | PackageManager.MATCH_UNINSTALLED_PACKAGES);
196 } catch (NameNotFoundException e) {
197 Log.e(TAG, "Package not found: " + callingPackage);
198 }
199 if (packageUid != Binder.getCallingUid()) {
200 throw new SecurityException("Calling package name mismatch");
201 }
202 }
203
Amith Yamasanidf7db072014-06-01 10:41:13 -0700204 /**
205 * Checks if the user is enabled.
206 */
207 private boolean isUserEnabled(UserHandle user) {
208 long ident = Binder.clearCallingIdentity();
209 try {
210 UserInfo targetUserInfo = mUm.getUserInfo(user.getIdentifier());
211 return targetUserInfo != null && targetUserInfo.isEnabled();
212 } finally {
213 Binder.restoreCallingIdentity(ident);
Amith Yamasani4f582632014-02-19 14:31:52 -0800214 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800215 }
216
217 @Override
Sunny Goyal6cbc2fe2015-11-24 09:34:20 -0800218 public ParceledListSlice<ResolveInfo> getLauncherActivities(String packageName, UserHandle user)
Amith Yamasanidf7db072014-06-01 10:41:13 -0700219 throws RemoteException {
220 ensureInUserProfiles(user, "Cannot retrieve activities for unrelated profile " + user);
221 if (!isUserEnabled(user)) {
Sunny Goyal6cbc2fe2015-11-24 09:34:20 -0800222 return null;
Amith Yamasani4f582632014-02-19 14:31:52 -0800223 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800224
Amith Yamasanidf7db072014-06-01 10:41:13 -0700225 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
226 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
227 mainIntent.setPackage(packageName);
228 long ident = Binder.clearCallingIdentity();
229 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700230 List<ResolveInfo> apps = mPm.queryIntentActivitiesAsUser(mainIntent,
Kenny Guyb1b30262016-02-09 16:02:35 +0000231 PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE, user.getIdentifier());
Sunny Goyal6cbc2fe2015-11-24 09:34:20 -0800232 return new ParceledListSlice<>(apps);
Amith Yamasanidf7db072014-06-01 10:41:13 -0700233 } finally {
234 Binder.restoreCallingIdentity(ident);
235 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800236 }
237
238 @Override
Amith Yamasanidf7db072014-06-01 10:41:13 -0700239 public ResolveInfo resolveActivity(Intent intent, UserHandle user)
240 throws RemoteException {
241 ensureInUserProfiles(user, "Cannot resolve activity for unrelated profile " + user);
242 if (!isUserEnabled(user)) {
243 return null;
Amith Yamasani4f582632014-02-19 14:31:52 -0800244 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800245
Amith Yamasanidf7db072014-06-01 10:41:13 -0700246 long ident = Binder.clearCallingIdentity();
247 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700248 ResolveInfo app = mPm.resolveActivityAsUser(intent,
Kenny Guyb1b30262016-02-09 16:02:35 +0000249 PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE, user.getIdentifier());
Amith Yamasanidf7db072014-06-01 10:41:13 -0700250 return app;
251 } finally {
252 Binder.restoreCallingIdentity(ident);
253 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800254 }
255
256 @Override
Amith Yamasanidf7db072014-06-01 10:41:13 -0700257 public boolean isPackageEnabled(String packageName, UserHandle user)
258 throws RemoteException {
259 ensureInUserProfiles(user, "Cannot check package for unrelated profile " + user);
260 if (!isUserEnabled(user)) {
261 return false;
Amith Yamasani4f582632014-02-19 14:31:52 -0800262 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800263
Amith Yamasanidf7db072014-06-01 10:41:13 -0700264 long ident = Binder.clearCallingIdentity();
265 try {
266 IPackageManager pm = AppGlobals.getPackageManager();
Jeff Sharkeycd654482016-01-08 17:42:11 -0700267 PackageInfo info = pm.getPackageInfo(packageName,
Kenny Guyb1b30262016-02-09 16:02:35 +0000268 PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE, user.getIdentifier());
Amith Yamasanidf7db072014-06-01 10:41:13 -0700269 return info != null && info.applicationInfo.enabled;
270 } finally {
271 Binder.restoreCallingIdentity(ident);
272 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800273 }
274
275 @Override
Kenny Guy77242752016-01-15 13:29:06 +0000276 public ApplicationInfo getApplicationInfo(String packageName, int flags, UserHandle user)
277 throws RemoteException {
278 ensureInUserProfiles(user, "Cannot check package for unrelated profile " + user);
279 if (!isUserEnabled(user)) {
280 return null;
281 }
282
283 long ident = Binder.clearCallingIdentity();
284 try {
285 IPackageManager pm = AppGlobals.getPackageManager();
286 ApplicationInfo info = pm.getApplicationInfo(packageName, flags,
287 user.getIdentifier());
288 return info;
289 } finally {
290 Binder.restoreCallingIdentity(ident);
291 }
292 }
293
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800294 private void enforceShortcutPermission(UserHandle user) {
295 ensureInUserProfiles(user, "Cannot start activity for unrelated profile " + user);
296 // STOPSHIP Implement it
297 }
298
299 @Override
300 public ParceledListSlice getShortcuts(String callingPackage, long changedSince,
301 String packageName, ComponentName componentName, int flags, UserHandle user)
302 throws RemoteException {
303 enforceShortcutPermission(user);
304 verifyCallingPackage(callingPackage);
305
306 return new ParceledListSlice<>(
307 mShortcutServiceInternal.getShortcuts(callingPackage, changedSince, packageName,
308 componentName, flags, user.getIdentifier()));
309 }
310
311 @Override
312 public ParceledListSlice getShortcutInfo(String callingPackage, String packageName,
313 List<String> ids, UserHandle user) throws RemoteException {
314 enforceShortcutPermission(user);
315 verifyCallingPackage(callingPackage);
316
317 return new ParceledListSlice<>(
318 mShortcutServiceInternal.getShortcutInfo(callingPackage, packageName,
319 ids, user.getIdentifier()));
320 }
321
322 @Override
323 public void pinShortcuts(String callingPackage, String packageName, List<String> ids,
324 UserHandle user) throws RemoteException {
325 enforceShortcutPermission(user);
326 verifyCallingPackage(callingPackage);
327
328 mShortcutServiceInternal.pinShortcuts(callingPackage, packageName,
329 ids, user.getIdentifier());
330 }
331
332 @Override
Makoto Onuki55046222016-03-08 10:49:47 -0800333 public int getShortcutIconResId(String callingPackage, ShortcutInfo shortcut,
334 UserHandle user) {
335 enforceShortcutPermission(user);
336 verifyCallingPackage(callingPackage);
337
338 return mShortcutServiceInternal.getShortcutIconResId(callingPackage, shortcut,
339 user.getIdentifier());
340 }
341
342 @Override
343 public ParcelFileDescriptor getShortcutIconFd(String callingPackage, ShortcutInfo shortcut,
344 UserHandle user) {
345 enforceShortcutPermission(user);
346 verifyCallingPackage(callingPackage);
347
348 return mShortcutServiceInternal.getShortcutIconFd(callingPackage, shortcut,
349 user.getIdentifier());
350 }
351
352 @Override
Makoto Onuki43204b82016-03-08 16:16:44 -0800353 public boolean startShortcut(String callingPackage, String packageName, String shortcutId,
354 Rect sourceBounds, Bundle startActivityOptions, UserHandle user)
355 throws RemoteException {
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800356 enforceShortcutPermission(user);
357 verifyCallingPackage(callingPackage);
358
359 final Intent intent = mShortcutServiceInternal.createShortcutIntent(callingPackage,
Makoto Onuki43204b82016-03-08 16:16:44 -0800360 packageName, shortcutId, user.getIdentifier());
361 if (intent == null) {
362 return false;
363 }
364 // Note the target activity doesn't have to be exported.
365
366 intent.setSourceBounds(sourceBounds);
367 prepareIntentForLaunch(intent, sourceBounds);
368
369 final long ident = Binder.clearCallingIdentity();
370 try {
371 mContext.startActivityAsUser(intent, startActivityOptions, user);
372 } finally {
373 Binder.restoreCallingIdentity(ident);
374 }
375 return true;
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800376 }
377
Kenny Guy77242752016-01-15 13:29:06 +0000378 @Override
Amith Yamasanidf7db072014-06-01 10:41:13 -0700379 public boolean isActivityEnabled(ComponentName component, UserHandle user)
380 throws RemoteException {
381 ensureInUserProfiles(user, "Cannot check component for unrelated profile " + user);
382 if (!isUserEnabled(user)) {
383 return false;
Amith Yamasani4f582632014-02-19 14:31:52 -0800384 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800385
Amith Yamasanidf7db072014-06-01 10:41:13 -0700386 long ident = Binder.clearCallingIdentity();
387 try {
388 IPackageManager pm = AppGlobals.getPackageManager();
Jeff Sharkeycd654482016-01-08 17:42:11 -0700389 ActivityInfo info = pm.getActivityInfo(component,
Kenny Guyb1b30262016-02-09 16:02:35 +0000390 PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE, user.getIdentifier());
Winson Chungdcf21d152014-10-01 14:30:05 -0700391 return info != null;
Amith Yamasanidf7db072014-06-01 10:41:13 -0700392 } finally {
393 Binder.restoreCallingIdentity(ident);
394 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800395 }
396
Amith Yamasani4f582632014-02-19 14:31:52 -0800397 @Override
Amith Yamasanidf7db072014-06-01 10:41:13 -0700398 public void startActivityAsUser(ComponentName component, Rect sourceBounds,
399 Bundle opts, UserHandle user) throws RemoteException {
400 ensureInUserProfiles(user, "Cannot start activity for unrelated profile " + user);
401 if (!isUserEnabled(user)) {
402 throw new IllegalStateException("Cannot start activity for disabled profile " + user);
403 }
404
405 Intent launchIntent = new Intent(Intent.ACTION_MAIN);
406 launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Makoto Onuki43204b82016-03-08 16:16:44 -0800407 prepareIntentForLaunch(launchIntent, sourceBounds);
Kenny Guy2df18922014-07-17 13:01:25 +0100408 launchIntent.setPackage(component.getPackageName());
Amith Yamasanidf7db072014-06-01 10:41:13 -0700409
410 long ident = Binder.clearCallingIdentity();
411 try {
Kenny Guy2df18922014-07-17 13:01:25 +0100412 IPackageManager pm = AppGlobals.getPackageManager();
Jeff Sharkeycd654482016-01-08 17:42:11 -0700413 ActivityInfo info = pm.getActivityInfo(component,
Kenny Guyb1b30262016-02-09 16:02:35 +0000414 PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE, user.getIdentifier());
Kenny Guy2df18922014-07-17 13:01:25 +0100415 if (!info.exported) {
416 throw new SecurityException("Cannot launch non-exported components "
417 + component);
418 }
419
420 // Check that the component actually has Intent.CATEGORY_LAUCNCHER
421 // as calling startActivityAsUser ignores the category and just
422 // resolves based on the component if present.
423 List<ResolveInfo> apps = mPm.queryIntentActivitiesAsUser(launchIntent,
Kenny Guyb1b30262016-02-09 16:02:35 +0000424 PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE, user.getIdentifier());
Kenny Guy2df18922014-07-17 13:01:25 +0100425 final int size = apps.size();
426 for (int i = 0; i < size; ++i) {
427 ActivityInfo activityInfo = apps.get(i).activityInfo;
428 if (activityInfo.packageName.equals(component.getPackageName()) &&
429 activityInfo.name.equals(component.getClassName())) {
430 // Found an activity with category launcher that matches
431 // this component so ok to launch.
432 launchIntent.setComponent(component);
433 mContext.startActivityAsUser(launchIntent, opts, user);
434 return;
435 }
436 }
437 throw new SecurityException("Attempt to launch activity without "
438 + " category Intent.CATEGORY_LAUNCHER " + component);
Amith Yamasanidf7db072014-06-01 10:41:13 -0700439 } finally {
440 Binder.restoreCallingIdentity(ident);
441 }
442 }
443
Makoto Onuki43204b82016-03-08 16:16:44 -0800444 private void prepareIntentForLaunch(@NonNull Intent launchIntent,
445 @Nullable Rect sourceBounds) {
446 launchIntent.setSourceBounds(sourceBounds);
447 launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
448 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
449 }
450
Kenny Guy466d2032014-07-23 12:23:35 +0100451 @Override
452 public void showAppDetailsAsUser(ComponentName component, Rect sourceBounds,
453 Bundle opts, UserHandle user) throws RemoteException {
454 ensureInUserProfiles(user, "Cannot show app details for unrelated profile " + user);
455 if (!isUserEnabled(user)) {
456 throw new IllegalStateException("Cannot show app details for disabled profile "
457 + user);
458 }
459
460 long ident = Binder.clearCallingIdentity();
461 try {
462 String packageName = component.getPackageName();
463 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
464 Uri.fromParts("package", packageName, null));
Winson369aad02016-02-17 10:35:47 -0800465 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Kenny Guy466d2032014-07-23 12:23:35 +0100466 intent.setSourceBounds(sourceBounds);
467 mContext.startActivityAsUser(intent, opts, user);
468 } finally {
469 Binder.restoreCallingIdentity(ident);
470 }
471 }
472
473
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800474 private class MyPackageMonitor extends PackageMonitor implements ShortcutChangeListener {
Amith Yamasanidf7db072014-06-01 10:41:13 -0700475
476 /** Checks if user is a profile of or same as listeningUser.
477 * and the user is enabled. */
478 private boolean isEnabledProfileOf(UserHandle user, UserHandle listeningUser,
479 String debugMsg) {
480 if (user.getIdentifier() == listeningUser.getIdentifier()) {
481 if (DEBUG) Log.d(TAG, "Delivering msg to same user " + debugMsg);
482 return true;
483 }
484 long ident = Binder.clearCallingIdentity();
485 try {
486 UserInfo userInfo = mUm.getUserInfo(user.getIdentifier());
487 UserInfo listeningUserInfo = mUm.getUserInfo(listeningUser.getIdentifier());
488 if (userInfo == null || listeningUserInfo == null
489 || userInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID
490 || userInfo.profileGroupId != listeningUserInfo.profileGroupId
491 || !userInfo.isEnabled()) {
492 if (DEBUG) {
493 Log.d(TAG, "Not delivering msg from " + user + " to " + listeningUser + ":"
494 + debugMsg);
495 }
496 return false;
497 } else {
498 if (DEBUG) {
499 Log.d(TAG, "Delivering msg from " + user + " to " + listeningUser + ":"
500 + debugMsg);
501 }
502 return true;
503 }
504 } finally {
505 Binder.restoreCallingIdentity(ident);
506 }
507 }
508
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800509 // TODO Simplify with lambdas.
510
Amith Yamasanidf7db072014-06-01 10:41:13 -0700511 @Override
512 public void onPackageAdded(String packageName, int uid) {
513 UserHandle user = new UserHandle(getChangingUserId());
514 final int n = mListeners.beginBroadcast();
515 for (int i = 0; i < n; i++) {
516 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
517 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i);
518 if (!isEnabledProfileOf(user, listeningUser, "onPackageAdded")) continue;
519 try {
520 listener.onPackageAdded(user, packageName);
521 } catch (RemoteException re) {
522 Slog.d(TAG, "Callback failed ", re);
523 }
524 }
525 mListeners.finishBroadcast();
526
527 super.onPackageAdded(packageName, uid);
528 }
529
530 @Override
531 public void onPackageRemoved(String packageName, int uid) {
532 UserHandle user = new UserHandle(getChangingUserId());
533 final int n = mListeners.beginBroadcast();
534 for (int i = 0; i < n; i++) {
535 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
536 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i);
537 if (!isEnabledProfileOf(user, listeningUser, "onPackageRemoved")) continue;
538 try {
539 listener.onPackageRemoved(user, packageName);
540 } catch (RemoteException re) {
541 Slog.d(TAG, "Callback failed ", re);
542 }
543 }
544 mListeners.finishBroadcast();
545
546 super.onPackageRemoved(packageName, uid);
547 }
548
549 @Override
550 public void onPackageModified(String packageName) {
551 UserHandle user = new UserHandle(getChangingUserId());
552 final int n = mListeners.beginBroadcast();
553 for (int i = 0; i < n; i++) {
554 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
555 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i);
556 if (!isEnabledProfileOf(user, listeningUser, "onPackageModified")) continue;
557 try {
558 listener.onPackageChanged(user, packageName);
559 } catch (RemoteException re) {
560 Slog.d(TAG, "Callback failed ", re);
561 }
562 }
563 mListeners.finishBroadcast();
564
565 super.onPackageModified(packageName);
566 }
567
568 @Override
569 public void onPackagesAvailable(String[] packages) {
570 UserHandle user = new UserHandle(getChangingUserId());
571 final int n = mListeners.beginBroadcast();
572 for (int i = 0; i < n; i++) {
573 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
574 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i);
575 if (!isEnabledProfileOf(user, listeningUser, "onPackagesAvailable")) continue;
576 try {
577 listener.onPackagesAvailable(user, packages, isReplacing());
578 } catch (RemoteException re) {
579 Slog.d(TAG, "Callback failed ", re);
580 }
581 }
582 mListeners.finishBroadcast();
583
584 super.onPackagesAvailable(packages);
585 }
586
587 @Override
588 public void onPackagesUnavailable(String[] packages) {
589 UserHandle user = new UserHandle(getChangingUserId());
590 final int n = mListeners.beginBroadcast();
591 for (int i = 0; i < n; i++) {
592 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
593 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i);
594 if (!isEnabledProfileOf(user, listeningUser, "onPackagesUnavailable")) continue;
595 try {
596 listener.onPackagesUnavailable(user, packages, isReplacing());
597 } catch (RemoteException re) {
598 Slog.d(TAG, "Callback failed ", re);
599 }
600 }
601 mListeners.finishBroadcast();
602
603 super.onPackagesUnavailable(packages);
604 }
605
Kenny Guy77242752016-01-15 13:29:06 +0000606 @Override
607 public void onPackagesSuspended(String[] packages) {
608 UserHandle user = new UserHandle(getChangingUserId());
609 final int n = mListeners.beginBroadcast();
610 for (int i = 0; i < n; i++) {
611 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
612 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i);
613 if (!isEnabledProfileOf(user, listeningUser, "onPackagesSuspended")) continue;
614 try {
615 listener.onPackagesSuspended(user, packages);
616 } catch (RemoteException re) {
617 Slog.d(TAG, "Callback failed ", re);
618 }
619 }
620 mListeners.finishBroadcast();
621
622 super.onPackagesSuspended(packages);
623 }
624
625 @Override
626 public void onPackagesUnsuspended(String[] packages) {
627 UserHandle user = new UserHandle(getChangingUserId());
628 final int n = mListeners.beginBroadcast();
629 for (int i = 0; i < n; i++) {
630 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
631 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i);
632 if (!isEnabledProfileOf(user, listeningUser, "onPackagesUnsuspended")) continue;
633 try {
634 listener.onPackagesUnsuspended(user, packages);
635 } catch (RemoteException re) {
636 Slog.d(TAG, "Callback failed ", re);
637 }
638 }
639 mListeners.finishBroadcast();
640
641 super.onPackagesUnsuspended(packages);
642 }
643
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800644 @Override
645 public void onShortcutChanged(@NonNull String packageName,
646 @NonNull List<ShortcutInfo> shortcuts, @UserIdInt int userId) {
647 final UserHandle user = UserHandle.of(userId);
648
649 final int n = mListeners.beginBroadcast();
650 for (int i = 0; i < n; i++) {
651 IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
652 UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i);
653 if (!isEnabledProfileOf(user, listeningUser, "onShortcutChanged")) continue;
654 try {
655 listener.onShortcutChanged(user, packageName,
656 new ParceledListSlice<>(shortcuts));
657 } catch (RemoteException re) {
658 Slog.d(TAG, "Callback failed ", re);
659 }
660 }
661 mListeners.finishBroadcast();
662 }
Amith Yamasanidf7db072014-06-01 10:41:13 -0700663 }
664
665 class PackageCallbackList<T extends IInterface> extends RemoteCallbackList<T> {
666 @Override
667 public void onCallbackDied(T callback, Object cookie) {
668 checkCallbackCount();
669 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800670 }
671 }
Nicolas Prevotb6830192015-06-26 13:45:12 -0700672}