Java Code Examples for android.content.pm.LauncherApps#hasShortcutHostPermission()

The following examples show how to use android.content.pm.LauncherApps#hasShortcutHostPermission() . 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: AppUtils.java    From HgLauncher with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Fetch and return shortcuts for a specific app.
 *
 * @param launcherApps  LauncherApps service from an activity.
 * @param componentName The component name to flatten to package name.
 *
 * @return A list of shortcut. Null if nonexistent.
 */
@TargetApi(Build.VERSION_CODES.N_MR1)
public static List<ShortcutInfo> getShortcuts(LauncherApps launcherApps, String componentName) {
    // Return nothing if we don't have permission to retrieve shortcuts.
    if (launcherApps == null || !launcherApps.hasShortcutHostPermission()) {
        return new ArrayList<>(0);
    }

    LauncherApps.ShortcutQuery shortcutQuery = new LauncherApps.ShortcutQuery();
    shortcutQuery.setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC
            | LauncherApps.ShortcutQuery.FLAG_MATCH_MANIFEST
            | LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED);
    shortcutQuery.setPackage(getPackageName(componentName));

    return launcherApps.getShortcuts(shortcutQuery, Process.myUserHandle());
}
 
Example 2
Source File: AppUtils.java    From HgLauncher with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Fetch and return shortcuts for a specific app.
 *
 * @param launcherApps  LauncherApps service from an activity.
 * @param componentName The component name to flatten to package name.
 *
 * @return A list of shortcut. Null if nonexistent.
 */
@TargetApi(Build.VERSION_CODES.N_MR1)
public static List<ShortcutInfo> getShortcuts(LauncherApps launcherApps, String componentName) {
    // Return nothing if we don't have permission to retrieve shortcuts.
    if (launcherApps == null || !launcherApps.hasShortcutHostPermission()) {
        return new ArrayList<>(0);
    }

    LauncherApps.ShortcutQuery shortcutQuery = new LauncherApps.ShortcutQuery();
    shortcutQuery.setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC
            | LauncherApps.ShortcutQuery.FLAG_MATCH_MANIFEST
            | LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED);
    shortcutQuery.setPackage(getPackageName(componentName));

    return launcherApps.getShortcuts(shortcutQuery, Process.myUserHandle());
}
 
Example 3
Source File: ShortcutsLoader.java    From paper-launcher with MIT License 5 votes vote down vote up
@SuppressLint("WrongConstant")
public static List<ShortcutInfo> loadShortcuts(Context context, String packageName) {
    if (!SDKUtil.AT_LEAST_N_MR1) {
        return null;
    }

    LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
    if (launcherApps == null) {
        return null;
    }

    if (!launcherApps.hasShortcutHostPermission()) {
        return null;
    }

    PackageManager packageManager = context.getPackageManager();
    Intent mainIntent = new Intent("android.intent.action.MAIN", null);
    mainIntent.addCategory("android.intent.category.LAUNCHER");

    if (packageManager != null && packageManager.queryIntentActivities(mainIntent, 0) != null) {
        try {
            return launcherApps.getShortcuts((new LauncherApps.ShortcutQuery())
                            .setPackage(packageName).setQueryFlags(QUERY_FLAGS),
                    UserHandle.getUserHandleForUid(context.getPackageManager().getPackageUid(packageName, PackageManager.GET_META_DATA)));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();

            return null;
        }
    }

    return null;
}
 
Example 4
Source File: ContextMenuActivity.java    From Taskbar with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N_MR1)
private int getLauncherShortcuts() {
    LauncherApps launcherApps = (LauncherApps) getSystemService(LAUNCHER_APPS_SERVICE);
    if(launcherApps.hasShortcutHostPermission()) {
        UserManager userManager = (UserManager) getSystemService(USER_SERVICE);

        LauncherApps.ShortcutQuery query = new LauncherApps.ShortcutQuery();
        query.setActivity(ComponentName.unflattenFromString(entry.getComponentName()));
        query.setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC
                | LauncherApps.ShortcutQuery.FLAG_MATCH_MANIFEST
                | LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED);

        shortcuts = launcherApps.getShortcuts(query, userManager.getUserForSerialNumber(entry.getUserId(this)));
        if(shortcuts != null)
            return shortcuts.size();
    }

    return 0;
}
 
Example 5
Source File: U.java    From Taskbar with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N_MR1)
private static void launchShortcut(Context context, ShortcutInfo shortcut, Bundle bundle, Runnable onError) {
    LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);

    if(launcherApps.hasShortcutHostPermission()) {
        try {
            launcherApps.startShortcut(shortcut, null, bundle);
        } catch (ActivityNotFoundException | NullPointerException
                | IllegalStateException | SecurityException e) {
            if(onError != null) launchApp(context, onError);
        }
    }
}
 
Example 6
Source File: ActionMenu.java    From LaunchTime with GNU General Public License v3.0 4 votes vote down vote up
@Nullable
private List<ShortcutInfo> getOreoShortcutInfos(AppLauncher appitem) {
    List<ShortcutInfo> shortcutInfos = null;
    if (Build.VERSION.SDK_INT>=25) {
        final LauncherApps launcherApps = (LauncherApps) mMain.getSystemService(Context.LAUNCHER_APPS_SERVICE);

        if (launcherApps!=null && launcherApps.hasShortcutHostPermission()) {
            try {

                LauncherApps.ShortcutQuery q = new LauncherApps.ShortcutQuery();
                q.setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC | LauncherApps.ShortcutQuery.FLAG_MATCH_MANIFEST);

                if (appitem.isShortcut()) {
                    Intent launchIntent = Intent.parseUri(appitem.getLinkUri(), 0);
                    q.setPackage(launchIntent.getPackage());
                    q.setActivity(launchIntent.getComponent());
                } else if (appitem.isOreoShortcut()) {
                    LauncherApps.ShortcutQuery query = new LauncherApps.ShortcutQuery();
                    query.setPackage(appitem.getPackageName());
                    query.setShortcutIds(Collections.singletonList(appitem.getLinkUri()));
                    query.setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED);
                    List<ShortcutInfo> shortcuts = launcherApps.getShortcuts(query,android.os.Process.myUserHandle());
                    q.setPackage(appitem.getPackageName());
                    if (shortcuts!=null && shortcuts.size()>0) {
                        q.setActivity(shortcuts.get(0).getActivity());
                    }
                } else {
                    q.setPackage(appitem.getPackageName());
                    q.setActivity(appitem.getBaseComponentName());
                }

                shortcutInfos = launcherApps.getShortcuts(q, android.os.Process.myUserHandle());

                //Log.d(TAG, "Queried shortcuts");

            } catch (Exception e) {
                Log.e(TAG, "Couldn't query shortcuts", e);
            }
        }
    }
    return shortcutInfos;
}
 
Example 7
Source File: LaunchApp.java    From LaunchTime with GNU General Public License v3.0 4 votes vote down vote up
private void launchOreoShortcut(final AppLauncher app) {
    if (Build.VERSION.SDK_INT >= 26) {
        final LauncherApps launcherApps = activity.getSystemService(LauncherApps.class);
        // Only the default launcher is allowed to start shortcuts
        if (launcherApps==null || !launcherApps.hasShortcutHostPermission()) {
            Toast.makeText(activity, "Must be default launcher", Toast.LENGTH_LONG).show();
            return;
        }

        LauncherApps.ShortcutQuery query = new LauncherApps.ShortcutQuery();
        query.setPackage(app.getPackageName());
        query.setShortcutIds(Collections.singletonList(app.getLinkUri()));
        query.setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED);

        List<UserHandle> userHandles = launcherApps.getProfiles();

        Log.d(TAG, "launching " + app.getLinkUri());

        // Find the correct UserHandle, and launch the shortcut.
        boolean disabled = false;
        for (UserHandle userHandle : userHandles) {
            List<ShortcutInfo> shortcuts = launcherApps.getShortcuts(query, userHandle);
            if (shortcuts != null) {
                for (ShortcutInfo s: shortcuts) {
                    if (s!=null && s.isEnabled()) {
                        launcherApps.startShortcut(s, null, null);
                        return;
                    } else {
                        disabled = true;
                    }
                }
            }
        }
        Log.d(TAG, "failed to launch " + app.getLinkUri());
        if (app.getLinkUri().matches("^https?://.*")) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(app.getLinkUri()));
            //intent.setPackage(app.getPackageName());
            activity.startActivity(intent);
        } else {
            Toast.makeText(activity, "Failed to launch shortcut. " + (disabled?"Shortcut seems to be disabled.": "May be expired."), Toast.LENGTH_LONG).show();
        }

    }
}