Search in sources :

Example 1 with ShortcutInfoCompat

use of android.support.v4.content.pm.ShortcutInfoCompat in project AmazeFileManager by TeamAmaze.

the class MainFragment method addShortcut.

private void addShortcut(LayoutElementParcelable path) {
    // Adding shortcut for MainActivity
    // on Home screen
    final Context ctx = getContext();
    if (!ShortcutManagerCompat.isRequestPinShortcutSupported(ctx)) {
        Toast.makeText(getActivity(), getString(R.string.addshortcut_not_supported_by_launcher), Toast.LENGTH_SHORT).show();
        return;
    }
    Intent shortcutIntent = new Intent(ctx, MainActivity.class);
    shortcutIntent.putExtra("path", path.desc);
    shortcutIntent.setAction(Intent.ACTION_MAIN);
    shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    // Using file path as shortcut id.
    ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(ctx, path.desc).setActivity(getMainActivity().getComponentName()).setIcon(IconCompat.createWithResource(ctx, R.mipmap.ic_launcher)).setIntent(shortcutIntent).setLongLabel(path.desc).setShortLabel(new File(path.desc).getName()).build();
    ShortcutManagerCompat.requestPinShortcut(ctx, info, null);
}
Also used : Context(android.content.Context) Intent(android.content.Intent) File(java.io.File) SmbFile(jcifs.smb.SmbFile) HybridFile(com.amaze.filemanager.filesystem.HybridFile) ShortcutInfoCompat(android.support.v4.content.pm.ShortcutInfoCompat)

Example 2 with ShortcutInfoCompat

use of android.support.v4.content.pm.ShortcutInfoCompat in project android_packages_apps_Dialer by LineageOS.

the class MainImpl method createLauncherShortcutO.

@TargetApi(VERSION_CODES.O)
private static void createLauncherShortcutO(Context context) {
    ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, SHORTCUT_KEY).setIcon(IconCompat.createWithResource(context, R.drawable.nui_launcher_icon)).setIntent(MainActivity.getIntent(context)).setShortLabel(context.getString(R.string.nui_shortcut_name)).build();
    ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
}
Also used : ShortcutInfoCompat(android.support.v4.content.pm.ShortcutInfoCompat) TargetApi(android.annotation.TargetApi)

Example 3 with ShortcutInfoCompat

use of android.support.v4.content.pm.ShortcutInfoCompat in project Rocket by mozilla-tw.

the class ShortcutUtils method requestPinShortcut.

// Request pinned shortcut for both API level
public static void requestPinShortcut(@NonNull final Context context, @NonNull final Intent shortcutIntent, @NonNull final String title, @NonNull final String urlAsShortcutId, final Bitmap bitmap) {
    final Bitmap icon;
    final Resources resources = context.getResources();
    final char representativeCharacter = FavIconUtils.getRepresentativeCharacter(urlAsShortcutId);
    if (bitmap == null) {
        // if favicon is not ready, we use the default initial icon with white color
        icon = FavIconUtils.getInitialBitmap(resources, null, representativeCharacter);
    } else {
        // if favicon is ready, resize it using size that fits shortcut better
        icon = FavIconUtils.getRefinedShortcutIcon(resources, bitmap, representativeCharacter);
    }
    // label must not be empty
    String label = title;
    if (TextUtils.isEmpty(title)) {
        label = urlAsShortcutId;
    }
    final ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(context, urlAsShortcutId).setShortLabel(label).setIcon(IconCompat.createWithBitmap(icon)).setIntent(shortcutIntent).build();
    // Display home screen after add to home screen
    final Intent showHome = new Intent(Intent.ACTION_MAIN);
    showHome.addCategory(Intent.CATEGORY_HOME);
    showHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, showHome, PendingIntent.FLAG_UPDATE_CURRENT);
    final IntentSender intentSender = pendingIntent.getIntentSender();
    // TODO: find a way to update the shortcut icon for API 25-. Currently the only way is remove old shortcut and add again.
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
        final ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
        if (shortcutManager != null) {
            final List<ShortcutInfo> list = new ArrayList<>();
            list.add(shortcut.toShortcutInfo());
            shortcutManager.updateShortcuts(list);
        }
    }
    // If the launcher or system didn't support shortcut, we don't bother to call it.
    if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
        ShortcutManagerCompat.requestPinShortcut(context, shortcut, intentSender);
    }
}
Also used : ShortcutInfo(android.content.pm.ShortcutInfo) ShortcutManager(android.content.pm.ShortcutManager) ArrayList(java.util.ArrayList) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Bitmap(android.graphics.Bitmap) Resources(android.content.res.Resources) PendingIntent(android.app.PendingIntent) IntentSender(android.content.IntentSender) ShortcutInfoCompat(android.support.v4.content.pm.ShortcutInfoCompat)

Example 4 with ShortcutInfoCompat

use of android.support.v4.content.pm.ShortcutInfoCompat in project focus-android by mozilla-mobile.

the class HomeScreen method installShortCutViaManager.

/**
 * Create a shortcut via the AppCompat's shortcut manager.
 * <p>
 * On Android versions up to 7 shortcut will be created via system broadcast internally.
 * <p>
 * On Android 8+ the user will have the ability to add the shortcut manually
 * or let the system place it automatically.
 */
private static void installShortCutViaManager(Context context, Bitmap bitmap, String url, String title, boolean blockingEnabled) {
    if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
        final IconCompat icon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) ? IconCompat.createWithAdaptiveBitmap(bitmap) : IconCompat.createWithBitmap(bitmap);
        final ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(context, UUID.randomUUID().toString()).setShortLabel(title).setLongLabel(title).setIcon(icon).setIntent(createShortcutIntent(context, url, blockingEnabled)).build();
        ShortcutManagerCompat.requestPinShortcut(context, shortcut, null);
    }
}
Also used : IconCompat(android.support.v4.graphics.drawable.IconCompat) ShortcutInfoCompat(android.support.v4.content.pm.ShortcutInfoCompat)

Example 5 with ShortcutInfoCompat

use of android.support.v4.content.pm.ShortcutInfoCompat in project PhoneProfilesPlus by henrichg.

the class ShortcutCreatorListFragment method createShortcut.

private void createShortcut(int position) {
    Profile profile = activityDataWrapper.profileList.get(position);
    boolean isIconResourceID;
    String iconIdentifier;
    Bitmap profileBitmap;
    Bitmap shortcutOverlayBitmap;
    Bitmap profileShortcutBitmap;
    String profileName;
    boolean useCustomColor;
    if (profile != null) {
        isIconResourceID = profile.getIsIconResourceID();
        iconIdentifier = profile.getIconIdentifier();
        profileName = profile._name;
        useCustomColor = profile.getUseCustomColorForIcon();
    } else {
        isIconResourceID = true;
        iconIdentifier = Profile.PROFILE_ICON_DEFAULT;
        profileName = getResources().getString(R.string.profile_name_default);
        useCustomColor = false;
    }
    Intent shortcutIntent;
    if (position == 0) {
        // restart events
        shortcutIntent = new Intent(getActivity().getApplicationContext(), ActionForExternalApplicationActivity.class);
        shortcutIntent.setAction(ActionForExternalApplicationActivity.ACTION_RESTART_EVENTS);
    } else {
        shortcutIntent = new Intent(getActivity().getApplicationContext(), BackgroundActivateProfileActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);
        shortcutIntent.putExtra(PPApplication.EXTRA_STARTUP_SOURCE, PPApplication.STARTUP_SOURCE_SHORTCUT);
        // noinspection ConstantConditions
        shortcutIntent.putExtra(PPApplication.EXTRA_PROFILE_ID, profile._id);
    }
    /*
        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, profileName);
        */
    ShortcutInfoCompat.Builder shortcutBuilder = new ShortcutInfoCompat.Builder(getActivity(), "profile_shortcut");
    shortcutBuilder.setIntent(shortcutIntent);
    shortcutBuilder.setShortLabel(profileName);
    shortcutBuilder.setLongLabel(profileName);
    if (isIconResourceID) {
        // noinspection ConstantConditions
        if (profile._iconBitmap != null)
            profileBitmap = profile._iconBitmap;
        else {
            int iconResource = getResources().getIdentifier(iconIdentifier, "drawable", getActivity().getPackageName());
            profileBitmap = BitmapFactory.decodeResource(getResources(), iconResource);
        }
        shortcutOverlayBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_shortcut_overlay);
    } else {
        Resources resources = getResources();
        int height = (int) resources.getDimension(android.R.dimen.app_icon_size);
        int width = (int) resources.getDimension(android.R.dimen.app_icon_size);
        // Log.d("---- ShortcutCreatorListFragment.generateIconBitmap","resampleBitmapUri");
        profileBitmap = BitmapManipulator.resampleBitmapUri(iconIdentifier, width, height, getActivity().getApplicationContext());
        if (profileBitmap == null) {
            int iconResource = R.drawable.ic_profile_default;
            profileBitmap = BitmapFactory.decodeResource(getResources(), iconResource);
        }
        shortcutOverlayBitmap = BitmapManipulator.resampleResource(resources, R.drawable.ic_shortcut_overlay, width, height);
    }
    if (ApplicationPreferences.applicationWidgetIconColor(activityDataWrapper.context).equals("1")) {
        int monochromeValue = 0xFF;
        String applicationWidgetIconLightness = ApplicationPreferences.applicationWidgetIconLightness(activityDataWrapper.context);
        if (applicationWidgetIconLightness.equals("0"))
            monochromeValue = 0x00;
        if (applicationWidgetIconLightness.equals("25"))
            monochromeValue = 0x40;
        if (applicationWidgetIconLightness.equals("50"))
            monochromeValue = 0x80;
        if (applicationWidgetIconLightness.equals("75"))
            monochromeValue = 0xC0;
        if (applicationWidgetIconLightness.equals("100"))
            monochromeValue = 0xFF;
        if (isIconResourceID || useCustomColor) {
            // icon is from resource or colored by custom color
            profileBitmap = BitmapManipulator.monochromeBitmap(profileBitmap, monochromeValue);
        } else
            profileBitmap = BitmapManipulator.grayScaleBitmap(profileBitmap);
    }
    profileShortcutBitmap = combineImages(profileBitmap, shortcutOverlayBitmap);
    // intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, profileShortcutBitmap);
    shortcutBuilder.setIcon(IconCompat.createWithBitmap(profileShortcutBitmap));
    // intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    // getActivity().getApplicationContext().sendBroadcast(intent);
    ShortcutInfoCompat shortcutInfo = shortcutBuilder.build();
    Intent intent = ShortcutManagerCompat.createShortcutResultIntent(getActivity(), shortcutInfo);
    getActivity().setResult(Activity.RESULT_OK, intent);
    getActivity().finish();
}
Also used : Bitmap(android.graphics.Bitmap) Intent(android.content.Intent) Resources(android.content.res.Resources) ShortcutInfoCompat(android.support.v4.content.pm.ShortcutInfoCompat)

Aggregations

ShortcutInfoCompat (android.support.v4.content.pm.ShortcutInfoCompat)7 Intent (android.content.Intent)5 Resources (android.content.res.Resources)3 Bitmap (android.graphics.Bitmap)3 IconCompat (android.support.v4.graphics.drawable.IconCompat)2 TargetApi (android.annotation.TargetApi)1 PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1 IntentSender (android.content.IntentSender)1 ShortcutInfo (android.content.pm.ShortcutInfo)1 ShortcutManager (android.content.pm.ShortcutManager)1 HybridFile (com.amaze.filemanager.filesystem.HybridFile)1 AppIntent (com.orgzly.android.AppIntent)1 Book (com.orgzly.android.Book)1 Shelf (com.orgzly.android.Shelf)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 SmbFile (jcifs.smb.SmbFile)1