FileDocCategorySizeDatePackage
LauncherApps.javaAPI DocAndroid 5.1 API19202Thu Mar 12 22:22:10 GMT 2015android.content.pm

LauncherApps

public class LauncherApps extends Object
Class for retrieving a list of launchable activities for the current user and any associated managed profiles. This is mainly for use by launchers. Apps can be queried for each user profile. Since the PackageManager will not deliver package broadcasts for other profiles, you can register for package changes here.

To watch for managed profiles being added or removed, register for the following broadcasts: {@link Intent#ACTION_MANAGED_PROFILE_ADDED} and {@link Intent#ACTION_MANAGED_PROFILE_REMOVED}.

You can retrieve the list of profiles associated with this user with {@link UserManager#getUserProfiles()}.

Fields Summary
static final String
TAG
static final boolean
DEBUG
private android.content.Context
mContext
private android.content.pm.ILauncherApps
mService
private PackageManager
mPm
private List
mCallbacks
private IOnAppsChangedListener.Stub
mAppsChangedListener
Constructors Summary
public LauncherApps(android.content.Context context, android.content.pm.ILauncherApps service)

hide


                   
         
                                                             
              

                                                              
              

                                                                    
              

                                                                                            
              
                 

                                                                                                 
              
                 
    

      
         
        mContext = context;
        mService = service;
        mPm = context.getPackageManager();
    
Methods Summary
private voidaddCallbackLocked(android.content.pm.LauncherApps$Callback callback, android.os.Handler handler)

        // Remove if already present.
        removeCallbackLocked(callback);
        if (handler == null) {
            handler = new Handler();
        }
        CallbackMessageHandler toAdd = new CallbackMessageHandler(handler.getLooper(), callback);
        mCallbacks.add(toAdd);
    
public java.util.ListgetActivityList(java.lang.String packageName, android.os.UserHandle user)
Retrieves a list of launchable activities that match {@link Intent#ACTION_MAIN} and {@link Intent#CATEGORY_LAUNCHER}, for a specified user.

param
packageName The specific package to query. If null, it checks all installed packages in the profile.
param
user The UserHandle of the profile.
return
List of launchable activities. Can be an empty list but will not be null.

        List<ResolveInfo> activities = null;
        try {
            activities = mService.getLauncherActivities(packageName, user);
        } catch (RemoteException re) {
        }
        if (activities == null) {
            return Collections.EMPTY_LIST;
        }
        ArrayList<LauncherActivityInfo> lais = new ArrayList<LauncherActivityInfo>();
        final int count = activities.size();
        for (int i = 0; i < count; i++) {
            ResolveInfo ri = activities.get(i);
            long firstInstallTime = 0;
            try {
                firstInstallTime = mPm.getPackageInfo(ri.activityInfo.packageName,
                    PackageManager.GET_UNINSTALLED_PACKAGES).firstInstallTime;
            } catch (NameNotFoundException nnfe) {
                // Sorry, can't find package
            }
            LauncherActivityInfo lai = new LauncherActivityInfo(mContext, ri, user,
                    firstInstallTime);
            if (DEBUG) {
                Log.v(TAG, "Returning activity for profile " + user + " : "
                        + lai.getComponentName());
            }
            lais.add(lai);
        }
        return lais;
    
static android.content.ComponentNamegetComponentName(ResolveInfo ri)

        return new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name);
    
public booleanisActivityEnabled(android.content.ComponentName component, android.os.UserHandle user)
Checks if the activity exists and it enabled for a profile.

param
component The activity to check.
param
user The UserHandle of the profile.
return
true if the activity exists and is enabled.

        try {
            return mService.isActivityEnabled(component, user);
        } catch (RemoteException re) {
            throw new RuntimeException("Failed to call LauncherAppsService");
        }
    
public booleanisPackageEnabled(java.lang.String packageName, android.os.UserHandle user)
Checks if the package is installed and enabled for a profile.

param
packageName The package to check.
param
user The UserHandle of the profile.
return
true if the package exists and is enabled.

        try {
            return mService.isPackageEnabled(packageName, user);
        } catch (RemoteException re) {
            throw new RuntimeException("Failed to call LauncherAppsService");
        }
    
public voidregisterCallback(android.content.pm.LauncherApps$Callback callback, android.os.Handler handler)
Registers a callback for changes to packages in current and managed profiles.

param
callback The callback to register.
param
handler that should be used to post callbacks on, may be null.

        synchronized (this) {
            if (callback != null && !mCallbacks.contains(callback)) {
                boolean addedFirstCallback = mCallbacks.size() == 0;
                addCallbackLocked(callback, handler);
                if (addedFirstCallback) {
                    try {
                        mService.addOnAppsChangedListener(mAppsChangedListener);
                    } catch (RemoteException re) {
                    }
                }
            }
        }
    
public voidregisterCallback(android.content.pm.LauncherApps$Callback callback)
Registers a callback for changes to packages in current and managed profiles.

param
callback The callback to register.

        registerCallback(callback, null);
    
private voidremoveCallbackLocked(android.content.pm.LauncherApps$Callback callback)

        if (callback == null) {
            throw new IllegalArgumentException("Callback cannot be null");
        }
        final int size = mCallbacks.size();
        for (int i = 0; i < size; ++i) {
            if (mCallbacks.get(i).mCallback == callback) {
                mCallbacks.remove(i);
                return;
            }
        }
    
public LauncherActivityInforesolveActivity(android.content.Intent intent, android.os.UserHandle user)
Returns the activity info for a given intent and user handle, if it resolves. Otherwise it returns null.

param
intent The intent to find a match for.
param
user The profile to look in for a match.
return
An activity info object if there is a match.

        try {
            ResolveInfo ri = mService.resolveActivity(intent, user);
            if (ri != null) {
                long firstInstallTime = 0;
                try {
                    firstInstallTime = mPm.getPackageInfo(ri.activityInfo.packageName,
                            PackageManager.GET_UNINSTALLED_PACKAGES).firstInstallTime;
                } catch (NameNotFoundException nnfe) {
                    // Sorry, can't find package
                }
                LauncherActivityInfo info = new LauncherActivityInfo(mContext, ri, user,
                        firstInstallTime);
                return info;
            }
        } catch (RemoteException re) {
            throw new RuntimeException("Failed to call LauncherAppsService");
        }
        return null;
    
public voidstartAppDetailsActivity(android.content.ComponentName component, android.os.UserHandle user, android.graphics.Rect sourceBounds, android.os.Bundle opts)
Starts the settings activity to show the application details for a package in the specified profile.

param
component The ComponentName of the package to launch settings for.
param
user The UserHandle of the profile
param
sourceBounds The Rect containing the source bounds of the clicked icon
param
opts Options to pass to startActivity

        try {
            mService.showAppDetailsAsUser(component, sourceBounds, opts, user);
        } catch (RemoteException re) {
            // Oops!
        }
    
public voidstartMainActivity(android.content.ComponentName component, android.os.UserHandle user, android.graphics.Rect sourceBounds, android.os.Bundle opts)
Starts a Main activity in the specified profile.

param
component The ComponentName of the activity to launch
param
user The UserHandle of the profile
param
sourceBounds The Rect containing the source bounds of the clicked icon
param
opts Options to pass to startActivity

        if (DEBUG) {
            Log.i(TAG, "StartMainActivity " + component + " " + user.getIdentifier());
        }
        try {
            mService.startActivityAsUser(component, sourceBounds, opts, user);
        } catch (RemoteException re) {
            // Oops!
        }
    
public voidunregisterCallback(android.content.pm.LauncherApps$Callback callback)
Unregisters a callback that was previously registered.

param
callback The callback to unregister.
see
#registerCallback(Callback)

        synchronized (this) {
            removeCallbackLocked(callback);
            if (mCallbacks.size() == 0) {
                try {
                    mService.removeOnAppsChangedListener(mAppsChangedListener);
                } catch (RemoteException re) {
                }
            }
        }