2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.content.pm;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.ILauncherApps;
23 import android.content.pm.IOnAppsChangedListener;
24 import android.content.pm.PackageManager.NameNotFoundException;
25 import android.graphics.Rect;
26 import android.os.Bundle;
27 import android.os.Handler;
28 import android.os.Looper;
29 import android.os.Message;
30 import android.os.RemoteException;
31 import android.os.UserHandle;
32 import android.os.UserManager;
33 import android.util.Log;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
40 * Class for retrieving a list of launchable activities for the current user and any associated
41 * managed profiles. This is mainly for use by launchers. Apps can be queried for each user profile.
42 * Since the PackageManager will not deliver package broadcasts for other profiles, you can register
43 * for package changes here.
45 * To watch for managed profiles being added or removed, register for the following broadcasts:
46 * {@link Intent#ACTION_MANAGED_PROFILE_ADDED} and {@link Intent#ACTION_MANAGED_PROFILE_REMOVED}.
48 * You can retrieve the list of profiles associated with this user with
49 * {@link UserManager#getUserProfiles()}.
51 public class LauncherApps {
53 static final String TAG = "LauncherApps";
54 static final boolean DEBUG = false;
56 private Context mContext;
57 private ILauncherApps mService;
58 private PackageManager mPm;
60 private List<CallbackMessageHandler> mCallbacks
61 = new ArrayList<CallbackMessageHandler>();
64 * Callbacks for package changes to this and related managed profiles.
66 public static abstract class Callback {
68 * Indicates that a package was removed from the specified profile.
70 * If a package is removed while being updated onPackageChanged will be
73 * @param packageName The name of the package that was removed.
74 * @param user The UserHandle of the profile that generated the change.
76 abstract public void onPackageRemoved(String packageName, UserHandle user);
79 * Indicates that a package was added to the specified profile.
81 * If a package is added while being updated then onPackageChanged will be
84 * @param packageName The name of the package that was added.
85 * @param user The UserHandle of the profile that generated the change.
87 abstract public void onPackageAdded(String packageName, UserHandle user);
90 * Indicates that a package was modified in the specified profile.
91 * This can happen, for example, when the package is updated or when
92 * one or more components are enabled or disabled.
94 * @param packageName The name of the package that has changed.
95 * @param user The UserHandle of the profile that generated the change.
97 abstract public void onPackageChanged(String packageName, UserHandle user);
100 * Indicates that one or more packages have become available. For
101 * example, this can happen when a removable storage card has
104 * @param packageNames The names of the packages that have become
106 * @param user The UserHandle of the profile that generated the change.
107 * @param replacing Indicates whether these packages are replacing
110 abstract public void onPackagesAvailable(String[] packageNames, UserHandle user,
114 * Indicates that one or more packages have become unavailable. For
115 * example, this can happen when a removable storage card has been
118 * @param packageNames The names of the packages that have become
120 * @param user The UserHandle of the profile that generated the change.
121 * @param replacing Indicates whether the packages are about to be
122 * replaced with new versions.
124 abstract public void onPackagesUnavailable(String[] packageNames, UserHandle user,
129 public LauncherApps(Context context, ILauncherApps service) {
132 mPm = context.getPackageManager();
136 * Retrieves a list of launchable activities that match {@link Intent#ACTION_MAIN} and
137 * {@link Intent#CATEGORY_LAUNCHER}, for a specified user.
139 * @param packageName The specific package to query. If null, it checks all installed packages
141 * @param user The UserHandle of the profile.
142 * @return List of launchable activities. Can be an empty list but will not be null.
144 public List<LauncherActivityInfo> getActivityList(String packageName, UserHandle user) {
145 List<ResolveInfo> activities = null;
147 activities = mService.getLauncherActivities(packageName, user);
148 } catch (RemoteException re) {
149 throw new RuntimeException("Failed to call LauncherAppsService");
151 if (activities == null) {
152 return Collections.EMPTY_LIST;
154 ArrayList<LauncherActivityInfo> lais = new ArrayList<LauncherActivityInfo>();
155 final int count = activities.size();
156 for (int i = 0; i < count; i++) {
157 ResolveInfo ri = activities.get(i);
158 long firstInstallTime = 0;
160 firstInstallTime = mPm.getPackageInfo(ri.activityInfo.packageName,
161 PackageManager.GET_UNINSTALLED_PACKAGES).firstInstallTime;
162 } catch (NameNotFoundException nnfe) {
163 // Sorry, can't find package
165 LauncherActivityInfo lai = new LauncherActivityInfo(mContext, ri, user,
168 Log.v(TAG, "Returning activity for profile " + user + " : "
169 + lai.getComponentName());
176 static ComponentName getComponentName(ResolveInfo ri) {
177 return new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name);
181 * Returns the activity info for a given intent and user handle, if it resolves. Otherwise it
184 * @param intent The intent to find a match for.
185 * @param user The profile to look in for a match.
186 * @return An activity info object if there is a match.
188 public LauncherActivityInfo resolveActivity(Intent intent, UserHandle user) {
190 ResolveInfo ri = mService.resolveActivity(intent, user);
192 long firstInstallTime = 0;
194 firstInstallTime = mPm.getPackageInfo(ri.activityInfo.packageName,
195 PackageManager.GET_UNINSTALLED_PACKAGES).firstInstallTime;
196 } catch (NameNotFoundException nnfe) {
197 // Sorry, can't find package
199 LauncherActivityInfo info = new LauncherActivityInfo(mContext, ri, user,
203 } catch (RemoteException re) {
204 throw new RuntimeException("Failed to call LauncherAppsService");
210 * Starts a Main activity in the specified profile.
212 * @param component The ComponentName of the activity to launch
213 * @param user The UserHandle of the profile
214 * @param sourceBounds The Rect containing the source bounds of the clicked icon
215 * @param opts Options to pass to startActivity
217 public void startMainActivity(ComponentName component, UserHandle user, Rect sourceBounds,
220 Log.i(TAG, "StartMainActivity " + component + " " + user.getIdentifier());
223 mService.startActivityAsUser(component, sourceBounds, opts, user);
224 } catch (RemoteException re) {
230 * Starts the settings activity to show the application details for a
231 * package in the specified profile.
233 * @param component The ComponentName of the package to launch settings for.
234 * @param user The UserHandle of the profile
235 * @param sourceBounds The Rect containing the source bounds of the clicked icon
236 * @param opts Options to pass to startActivity
238 public void startAppDetailsActivity(ComponentName component, UserHandle user,
239 Rect sourceBounds, Bundle opts) {
241 mService.showAppDetailsAsUser(component, sourceBounds, opts, user);
242 } catch (RemoteException re) {
248 * Checks if the package is installed and enabled for a profile.
250 * @param packageName The package to check.
251 * @param user The UserHandle of the profile.
253 * @return true if the package exists and is enabled.
255 public boolean isPackageEnabled(String packageName, UserHandle user) {
257 return mService.isPackageEnabled(packageName, user);
258 } catch (RemoteException re) {
259 throw new RuntimeException("Failed to call LauncherAppsService");
264 * Checks if the activity exists and it enabled for a profile.
266 * @param component The activity to check.
267 * @param user The UserHandle of the profile.
269 * @return true if the activity exists and is enabled.
271 public boolean isActivityEnabled(ComponentName component, UserHandle user) {
273 return mService.isActivityEnabled(component, user);
274 } catch (RemoteException re) {
275 throw new RuntimeException("Failed to call LauncherAppsService");
281 * Registers a callback for changes to packages in current and managed profiles.
283 * @param callback The callback to register.
285 public void registerCallback(Callback callback) {
286 registerCallback(callback, null);
290 * Registers a callback for changes to packages in current and managed profiles.
292 * @param callback The callback to register.
293 * @param handler that should be used to post callbacks on, may be null.
295 public void registerCallback(Callback callback, Handler handler) {
296 synchronized (this) {
297 if (callback != null && findCallbackLocked(callback) < 0) {
298 boolean addedFirstCallback = mCallbacks.size() == 0;
299 addCallbackLocked(callback, handler);
300 if (addedFirstCallback) {
302 mService.addOnAppsChangedListener(mAppsChangedListener);
303 } catch (RemoteException re) {
311 * Unregisters a callback that was previously registered.
313 * @param callback The callback to unregister.
314 * @see #registerCallback(Callback)
316 public void unregisterCallback(Callback callback) {
317 synchronized (this) {
318 removeCallbackLocked(callback);
319 if (mCallbacks.size() == 0) {
321 mService.removeOnAppsChangedListener(mAppsChangedListener);
322 } catch (RemoteException re) {
328 /** @return position in mCallbacks for callback or -1 if not present. */
329 private int findCallbackLocked(Callback callback) {
330 if (callback == null) {
331 throw new IllegalArgumentException("Callback cannot be null");
333 final int size = mCallbacks.size();
334 for (int i = 0; i < size; ++i) {
335 if (mCallbacks.get(i).mCallback == callback) {
342 private void removeCallbackLocked(Callback callback) {
343 int pos = findCallbackLocked(callback);
345 mCallbacks.remove(pos);
349 private void addCallbackLocked(Callback callback, Handler handler) {
350 // Remove if already present.
351 removeCallbackLocked(callback);
352 if (handler == null) {
353 handler = new Handler();
355 CallbackMessageHandler toAdd = new CallbackMessageHandler(handler.getLooper(), callback);
356 mCallbacks.add(toAdd);
359 private IOnAppsChangedListener.Stub mAppsChangedListener = new IOnAppsChangedListener.Stub() {
362 public void onPackageRemoved(UserHandle user, String packageName)
363 throws RemoteException {
365 Log.d(TAG, "onPackageRemoved " + user.getIdentifier() + "," + packageName);
367 synchronized (LauncherApps.this) {
368 for (CallbackMessageHandler callback : mCallbacks) {
369 callback.postOnPackageRemoved(packageName, user);
375 public void onPackageChanged(UserHandle user, String packageName) throws RemoteException {
377 Log.d(TAG, "onPackageChanged " + user.getIdentifier() + "," + packageName);
379 synchronized (LauncherApps.this) {
380 for (CallbackMessageHandler callback : mCallbacks) {
381 callback.postOnPackageChanged(packageName, user);
387 public void onPackageAdded(UserHandle user, String packageName) throws RemoteException {
389 Log.d(TAG, "onPackageAdded " + user.getIdentifier() + "," + packageName);
391 synchronized (LauncherApps.this) {
392 for (CallbackMessageHandler callback : mCallbacks) {
393 callback.postOnPackageAdded(packageName, user);
399 public void onPackagesAvailable(UserHandle user, String[] packageNames, boolean replacing)
400 throws RemoteException {
402 Log.d(TAG, "onPackagesAvailable " + user.getIdentifier() + "," + packageNames);
404 synchronized (LauncherApps.this) {
405 for (CallbackMessageHandler callback : mCallbacks) {
406 callback.postOnPackagesAvailable(packageNames, user, replacing);
412 public void onPackagesUnavailable(UserHandle user, String[] packageNames, boolean replacing)
413 throws RemoteException {
415 Log.d(TAG, "onPackagesUnavailable " + user.getIdentifier() + "," + packageNames);
417 synchronized (LauncherApps.this) {
418 for (CallbackMessageHandler callback : mCallbacks) {
419 callback.postOnPackagesUnavailable(packageNames, user, replacing);
425 private static class CallbackMessageHandler extends Handler {
426 private static final int MSG_ADDED = 1;
427 private static final int MSG_REMOVED = 2;
428 private static final int MSG_CHANGED = 3;
429 private static final int MSG_AVAILABLE = 4;
430 private static final int MSG_UNAVAILABLE = 5;
432 private LauncherApps.Callback mCallback;
434 private static class CallbackInfo {
435 String[] packageNames;
441 public CallbackMessageHandler(Looper looper, LauncherApps.Callback callback) {
442 super(looper, null, true);
443 mCallback = callback;
447 public void handleMessage(Message msg) {
448 if (mCallback == null || !(msg.obj instanceof CallbackInfo)) {
451 CallbackInfo info = (CallbackInfo) msg.obj;
454 mCallback.onPackageAdded(info.packageName, info.user);
457 mCallback.onPackageRemoved(info.packageName, info.user);
460 mCallback.onPackageChanged(info.packageName, info.user);
463 mCallback.onPackagesAvailable(info.packageNames, info.user, info.replacing);
465 case MSG_UNAVAILABLE:
466 mCallback.onPackagesUnavailable(info.packageNames, info.user, info.replacing);
471 public void postOnPackageAdded(String packageName, UserHandle user) {
472 CallbackInfo info = new CallbackInfo();
473 info.packageName = packageName;
475 obtainMessage(MSG_ADDED, info).sendToTarget();
478 public void postOnPackageRemoved(String packageName, UserHandle user) {
479 CallbackInfo info = new CallbackInfo();
480 info.packageName = packageName;
482 obtainMessage(MSG_REMOVED, info).sendToTarget();
485 public void postOnPackageChanged(String packageName, UserHandle user) {
486 CallbackInfo info = new CallbackInfo();
487 info.packageName = packageName;
489 obtainMessage(MSG_CHANGED, info).sendToTarget();
492 public void postOnPackagesAvailable(String[] packageNames, UserHandle user,
494 CallbackInfo info = new CallbackInfo();
495 info.packageNames = packageNames;
496 info.replacing = replacing;
498 obtainMessage(MSG_AVAILABLE, info).sendToTarget();
501 public void postOnPackagesUnavailable(String[] packageNames, UserHandle user,
503 CallbackInfo info = new CallbackInfo();
504 info.packageNames = packageNames;
505 info.replacing = replacing;
507 obtainMessage(MSG_UNAVAILABLE, info).sendToTarget();