Java Code Examples for android.content.pm.PackageManagerInternal#queryIntentActivities()

The following examples show how to use android.content.pm.PackageManagerInternal#queryIntentActivities() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LauncherAppsService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private ParceledListSlice<ResolveInfo> queryActivitiesForUser(String callingPackage,
        Intent intent, UserHandle user) {
    if (!canAccessProfile(user.getIdentifier(), "Cannot retrieve activities")) {
        return null;
    }

    final int callingUid = injectBinderCallingUid();
    long ident = injectClearCallingIdentity();
    try {
        final PackageManagerInternal pmInt =
                LocalServices.getService(PackageManagerInternal.class);
        List<ResolveInfo> apps = pmInt.queryIntentActivities(intent,
                PackageManager.MATCH_DIRECT_BOOT_AWARE
                        | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
                callingUid, user.getIdentifier());
        return new ParceledListSlice<>(apps);
    } finally {
        injectRestoreCallingIdentity(ident);
    }
}
 
Example 2
Source File: LauncherAppsService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public void startActivityAsUser(IApplicationThread caller, String callingPackage,
        ComponentName component, Rect sourceBounds,
        Bundle opts, UserHandle user) throws RemoteException {
    if (!canAccessProfile(user.getIdentifier(), "Cannot start activity")) {
        return;
    }

    Intent launchIntent = new Intent(Intent.ACTION_MAIN);
    launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    launchIntent.setSourceBounds(sourceBounds);
    launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    launchIntent.setPackage(component.getPackageName());

    boolean canLaunch = false;

    final int callingUid = injectBinderCallingUid();
    long ident = Binder.clearCallingIdentity();
    try {
        final PackageManagerInternal pmInt =
                LocalServices.getService(PackageManagerInternal.class);
        ActivityInfo info = pmInt.getActivityInfo(component,
                PackageManager.MATCH_DIRECT_BOOT_AWARE
                        | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
                callingUid, user.getIdentifier());
        if (!info.exported) {
            throw new SecurityException("Cannot launch non-exported components "
                    + component);
        }

        // Check that the component actually has Intent.CATEGORY_LAUCNCHER
        // as calling startActivityAsUser ignores the category and just
        // resolves based on the component if present.
        List<ResolveInfo> apps = pmInt.queryIntentActivities(launchIntent,
                PackageManager.MATCH_DIRECT_BOOT_AWARE
                        | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
                callingUid, user.getIdentifier());
        final int size = apps.size();
        for (int i = 0; i < size; ++i) {
            ActivityInfo activityInfo = apps.get(i).activityInfo;
            if (activityInfo.packageName.equals(component.getPackageName()) &&
                    activityInfo.name.equals(component.getClassName())) {
                // Found an activity with category launcher that matches
                // this component so ok to launch.
                launchIntent.setPackage(null);
                launchIntent.setComponent(component);
                canLaunch = true;
                break;
            }
        }
        if (!canLaunch) {
            throw new SecurityException("Attempt to launch activity without "
                    + " category Intent.CATEGORY_LAUNCHER " + component);
        }
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
    mActivityManagerInternal.startActivityAsUser(caller, callingPackage,
            launchIntent, opts, user.getIdentifier());
}