Java Code Examples for android.content.pm.ShortcutManager#addDynamicShortcuts()

The following examples show how to use android.content.pm.ShortcutManager#addDynamicShortcuts() . 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: AppShortcutsHelper.java    From rcloneExplorer with MIT License 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
public static void addRemoteToAppShortcuts(Context context, RemoteItem remoteItem, String id) {
    ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
    if (shortcutManager == null) {
        return;
    }

    Intent intent = new Intent(Intent.ACTION_MAIN, Uri.EMPTY, context, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra(APP_SHORTCUT_REMOTE_NAME, remoteItem.getName());

    ShortcutInfo shortcut = new ShortcutInfo.Builder(context, id)
            .setShortLabel(remoteItem.getName())
            .setIcon(Icon.createWithResource(context, AppShortcutsHelper.getRemoteIcon(remoteItem.getType(), remoteItem.isCrypt())))
            .setIntent(intent)
            .build();

    shortcutManager.addDynamicShortcuts(Collections.singletonList(shortcut));
}
 
Example 2
Source File: Gander.java    From Gander with Apache License 2.0 6 votes vote down vote up
/**
 * Register an app shortcut to launch the Gander UI directly from the launcher on Android 7.0 and above.
 *
 * @param context A valid {@link Context}
 * @return The id of the added shortcut (<code>null</code> if this feature is not supported on the device).
 * It can be used if you want to remove this shortcut later on.
 */
@TargetApi(Build.VERSION_CODES.N_MR1)
@SuppressWarnings("WeakerAccess")
@Nullable
public static String addAppShortcut(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        final String id = context.getPackageName() + ".gander_ui";
        final ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
        final ShortcutInfo shortcut = new ShortcutInfo.Builder(context, id).setShortLabel("Gander")
                .setLongLabel("Open Gander")
                .setIcon(Icon.createWithResource(context, R.drawable.gander_ic_shortcut_primary_24dp))
                .setIntent(getLaunchIntent(context).setAction(Intent.ACTION_VIEW))
                .build();
        shortcutManager.addDynamicShortcuts(Collections.singletonList(shortcut));
        return id;
    } else {
        return null;
    }

}
 
Example 3
Source File: ShortcutHelper.java    From zapp with MIT License 6 votes vote down vote up
/**
 * Adds the given channel as shortcut to the launcher icon.
 * Only call on api level >= 25.
 *
 * @param context to access system services
 * @param channel channel to create a shortcut for
 * @return true if the channel could be added
 */
@TargetApi(25)
public static boolean addShortcutForChannel(Context context, ChannelModel channel) {
	ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);

	if (shortcutManager == null || shortcutManager.getDynamicShortcuts().size() >= 4) {
		return false;
	}

	ShortcutInfo shortcut = new ShortcutInfo.Builder(context, channel.getId())
		.setShortLabel(channel.getName())
		.setLongLabel(channel.getName())
		.setIcon(Icon.createWithResource(context, channel.getDrawableId()))
		.setIntent(ChannelDetailActivity.getStartIntent(context, channel.getId()))
		.build();

	try {
		return shortcutManager.addDynamicShortcuts(Collections.singletonList(shortcut));
	} catch (IllegalArgumentException e) {
		// too many shortcuts
		return false;
	}
}
 
Example 4
Source File: ShortcutHelper.java    From android-proguards with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N_MR1)
public static void enablePostShortcut(@NonNull Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) return;
    ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);

    Intent intent = new Intent(context, PostNewDesignerNewsStory.class);
    intent.setAction(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    ShortcutInfo postShortcut
            = new ShortcutInfo.Builder(context, POST_SHORTCUT_ID)
            .setShortLabel(context.getString(R.string.shortcut_post_short_label))
            .setLongLabel(context.getString(R.string.shortcut_post_long_label))
            .setDisabledMessage(context.getString(R.string.shortcut_post_disabled))
            .setIcon(Icon.createWithResource(context, R.drawable.ic_shortcut_post))
            .setIntent(intent)
            .build();
    shortcutManager.addDynamicShortcuts(Collections.singletonList(postShortcut));
}
 
Example 5
Source File: LauncherShortcutActivity.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a "New incognito tab" dynamic launcher shortcut.
 * @param context The context used to retrieve the system {@link ShortcutManager}.
 * @return True if addint the shortcut has succeeded. False if the call fails due to rate
 *         limiting. See {@link ShortcutManager#addDynamicShortcuts}.
 */
@TargetApi(Build.VERSION_CODES.N_MR1)
private static boolean addIncognitoLauncherShortcut(Context context) {
    Intent intent = new Intent(LauncherShortcutActivity.ACTION_OPEN_NEW_INCOGNITO_TAB);
    intent.setPackage(context.getPackageName());
    intent.setClass(context, LauncherShortcutActivity.class);

    ShortcutInfo shortcut =
            new ShortcutInfo.Builder(context, DYNAMIC_OPEN_NEW_INCOGNITO_TAB_ID)
                    .setShortLabel(context.getResources().getString(
                            R.string.accessibility_tabstrip_incognito_identifier))
                    .setLongLabel(
                            context.getResources().getString(R.string.menu_new_incognito_tab))
                    .setIcon(Icon.createWithResource(context, R.drawable.shortcut_incognito))
                    .setIntent(intent)
                    .build();

    ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
    return shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut));
}
 
Example 6
Source File: RailActionActivity.java    From shortrain with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

    String id = getIntent().getExtras().getString(RAIL_ID_KEY);
    int railNumber = ShortcutsUtils.getRailNumber(id);

    int rotation = getIntent().getExtras().getInt(RAIL_ROTATION_KEY);

    int nextIcon = 0;
    int nextRotation = 0;

    switch (rotation) {
        case RailInfo.NOT_SET:
            nextIcon = R.drawable.horizontal;
            nextRotation = RailInfo.HORIZONTAL;
            break;
        case RailInfo.HORIZONTAL:
            nextIcon = R.drawable.vertical;
            nextRotation = RailInfo.VERTICAL;
            break;
        case RailInfo.VERTICAL:
            nextIcon = R.drawable.bottom_left_corner;
            nextRotation = RailInfo.BOTTOM_LEFT_CORNER;
            break;
        case RailInfo.BOTTOM_LEFT_CORNER:
            nextIcon = R.drawable.bottom_right_corner;
            nextRotation = RailInfo.BOTTOM_RIGHT_CORNER;
            break;
        case RailInfo.BOTTOM_RIGHT_CORNER:
            nextIcon = R.drawable.top_left_corner;
            nextRotation = RailInfo.TOP_LEFT_CORNER;
            break;
        case RailInfo.TOP_LEFT_CORNER:
            nextIcon = R.drawable.top_right_corner;
            nextRotation = RailInfo.TOP_RIGHT_CORNER;
            break;
        case RailInfo.TOP_RIGHT_CORNER:
            nextIcon = R.drawable.horizontal;
            nextRotation = RailInfo.HORIZONTAL;
            break;
    }

    Rect r = getIntent().getSourceBounds();
    ShortcutInfo thisRailShortcut = ShortcutsUtils.createRailShortcut(this, railNumber, nextIcon, nextRotation, r);

    shortcutManager.updateShortcuts(Collections.singletonList(thisRailShortcut));
    shortcutManager.removeDynamicShortcuts(Collections.singletonList(id));

    int newRailId = ShortcutsUtils.getNextRailNumber(shortcutManager);
    ShortcutInfo railShortcut = ShortcutsUtils.createRailShortcut(this, newRailId);
    shortcutManager.addDynamicShortcuts(Collections.singletonList(railShortcut));
    finish();

    super.onCreate(savedInstanceState);
}