private Drawable getDefaultAppDrawable(ComponentName componentName, UserHandle userHandle) { try { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { LauncherApps launcher = (LauncherApps) ctx.getSystemService(Context.LAUNCHER_APPS_SERVICE); LauncherActivityInfo info = launcher.getActivityList(componentName.getPackageName(), userHandle.getRealHandle()).get(0); return info.getBadgedIcon(0); } else { return pm.getActivityIcon(componentName); } } catch (NameNotFoundException | IndexOutOfBoundsException e) { Log.e(TAG, "Unable to found component " + componentName.toString() + e); return null; } }
final Drawable iconDrawable = launcherApps.getShortcutIconDrawable(shortcutInfo, 0); ShortcutsPojo pojo = new ShortcutsPojo(id, shortcutInfo.getPackage(), shortcutInfo.getId(), drawableToBitmap(iconDrawable));
@TargetApi(Build.VERSION_CODES.O) private void doOreoLaunch(Context context, View v) { final LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE); assert launcherApps != null; // Only the default launcher is allowed to start shortcuts if (!launcherApps.hasShortcutHostPermission()) { Toast.makeText(context, context.getString(R.string.shortcuts_no_host_permission), Toast.LENGTH_LONG).show(); return; } LauncherApps.ShortcutQuery query = new LauncherApps.ShortcutQuery(); query.setPackage(shortcutPojo.packageName); query.setShortcutIds(Collections.singletonList(shortcutPojo.getOreoId())); query.setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED); List<UserHandle> userHandles = launcherApps.getProfiles(); // Find the correct UserHandle, and launch the shortcut. for (UserHandle userHandle : userHandles) { List<ShortcutInfo> shortcuts = launcherApps.getShortcuts(query, userHandle); if (shortcuts != null && shortcuts.size() > 0 && shortcuts.get(0).isEnabled()) { launcherApps.startShortcut(shortcuts.get(0), v.getClipBounds(), null); return; } } // Application removed? Invalid shortcut? Shortcut to an app on an unmounted SD card? Toast.makeText(context, R.string.application_not_found, Toast.LENGTH_LONG).show(); }
public static ArrayList<Shortcuts> getRemoteShortcutsOnAPI25(Activity activity, String targetPackageName, int uid) throws Exception { LauncherApps launcherApps = (LauncherApps) activity.getApplicationContext().getSystemService(Context.LAUNCHER_APPS_SERVICE); if (!launcherApps.hasShortcutHostPermission()) { Log.e(TAG, "Don't have permission, you may need set this app as default launcher!"); throw new Exception("Don't have permission, you may need set this app as default launcher!"); int queryFlags = LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC | LauncherApps.ShortcutQuery.FLAG_MATCH_MANIFEST | LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED; List<ShortcutInfo> shortcutInfoList = launcherApps.getShortcuts( new LauncherApps.ShortcutQuery().setPackage(targetPackageName).setQueryFlags(queryFlags), UserHandle.getUserHandleForUid(uid)); Drawable shortcutsImage = launcherApps.getShortcutIconDrawable(shortcutInfoList.get(j), metrics.densityDpi); Bitmap shortcutsImageBitmap = Utils.convertDrawableToBitmap(shortcutsImage); Drawable shortcutsImageBadged = launcherApps.getShortcutBadgedIconDrawable(shortcutInfoList.get(j), metrics.densityDpi); Bitmap shortcutsImageBadgedBitmap = Utils.convertDrawableToBitmap(shortcutsImageBadged);
if (!launcherApps.hasShortcutHostPermission()) { return null; return launcherApps.getShortcuts((new LauncherApps.ShortcutQuery()) .setPackage(packageName).setQueryFlags(QUERY_FLAGS), UserHandle.getUserHandleForUid(context.getPackageManager().getPackageUid(packageName, PackageManager.GET_META_DATA)));
/** * Open an activity displaying details regarding the current package */ private void launchAppDetails(Context context, AppPojo app) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { LauncherApps launcher = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE); assert launcher != null; launcher.startAppDetailsActivity(className, appPojo.userHandle.getRealHandle(), null, null); } else { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", app.packageName, null)); context.startActivity(intent); } }
@Override public void doLaunch(Context context, View v) { try { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { LauncherApps launcher = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE); assert launcher != null; launcher.startMainActivity(className, appPojo.userHandle.getRealHandle(), v.getClipBounds(), null); } else { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setComponent(className); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { intent.setSourceBounds(v.getClipBounds()); } context.startActivity(intent); } } catch (ActivityNotFoundException | NullPointerException e) { // Application was just removed? // (null pointer exception can be thrown on Lollipop+ when app is missing) Toast.makeText(context, R.string.application_not_found, Toast.LENGTH_LONG).show(); } } }
assert launcher != null; launcher.registerCallback(new LauncherApps.Callback() { @Override public void onPackageAdded(String packageName, android.os.UserHandle user) {
@TargetApi(25) public void startShortcut(String packageName, String id, Rect sourceBounds, Bundle startActivityOptions, UserHandle user) { if (AndroidVersion.isAtLeastNougatMR1) { try { mLauncherApps.startShortcut(packageName, id, sourceBounds, startActivityOptions, user); mWasLastCallSuccess = true; } catch (SecurityException|IllegalStateException e) { e.printStackTrace(); mWasLastCallSuccess = false; } } }
@TargetApi(25) public boolean hasHostPermission() { if (AndroidVersion.isAtLeastNougatMR1) { try { return mLauncherApps.hasShortcutHostPermission(); } catch (SecurityException|IllegalStateException e) { e.printStackTrace(); } } return false; } }
shortcutInfos = mLauncherApps.getShortcuts(q, user); mWasLastCallSuccess = true; } catch (SecurityException|IllegalStateException e) {
@Override public boolean isPackageEnabledForProfile(String packageName, UserHandle user) { return mLauncherApps.isPackageEnabled(packageName, user); }
@Override public boolean isActivityEnabledForProfile(ComponentName component, UserHandle user) { return mLauncherApps.isActivityEnabled(component, user); }
final LauncherApps launcherApps = (LauncherApps) mMain.getSystemService(Context.LAUNCHER_APPS_SERVICE); if (launcherApps!=null && launcherApps.hasShortcutHostPermission()) { try { 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) { shortcutInfos = launcherApps.getShortcuts(q, android.os.Process.myUserHandle());
@Override public void showAppDetailsForProfile(ComponentName component, UserHandle user, Rect sourceBounds, Bundle opts) { mLauncherApps.startAppDetailsActivity(component, user, sourceBounds, opts); }
@Override public void startActivityForProfile(ComponentName component, UserHandle user, Rect sourceBounds, Bundle opts) { mLauncherApps.startMainActivity(component, user, sourceBounds, opts); }
public void addOnAppsChangedCallback(LauncherAppsCompat.OnAppsChangedCallbackCompat callback) { WrappedCallback wrappedCallback = new WrappedCallback(callback); synchronized (mCallbacks) { mCallbacks.put(callback, wrappedCallback); } mLauncherApps.registerCallback(wrappedCallback); }
public static void launchShortcut(View view, ShortcutInfo shortcutInfo) { if (!SDKUtil.AT_LEAST_N_MR1) { return; } ((LauncherApps) view.getContext().getSystemService(Context.LAUNCHER_APPS_SERVICE)) .startShortcut(shortcutInfo, IntentUtil.getViewBounds(view), new Bundle()); } }
@TargetApi(25) public boolean hasHostPermission() { if (Utilities.isNycMR1OrAbove()) { try { return mLauncherApps.hasShortcutHostPermission(); } catch (SecurityException|IllegalStateException e) { Log.e(TAG, "Failed to make shortcut manager call", e); } } return false; } }
shortcutInfos = mLauncherApps.getShortcuts(q, user.getUser()); mWasLastCallSuccess = true; } catch (SecurityException|IllegalStateException e) {