Search in sources :

Example 6 with ShortcutInfoCompat

use of android.support.v4.content.pm.ShortcutInfoCompat in project PhoneProfiles 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 = 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(getString(R.string.shortcut_activate_profile) + profileName);
    if (isIconResourceID) {
        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);
        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));
    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)

Example 7 with ShortcutInfoCompat

use of android.support.v4.content.pm.ShortcutInfoCompat in project orgzly-android by orgzly.

the class BookChooserActivity method onBookClicked.

@Override
public void onBookClicked(long bookId) {
    if (action != null && action.equals(Intent.ACTION_CREATE_SHORTCUT)) {
        // Get Book by its ID
        Shelf shelf = new Shelf(this);
        Book book = shelf.getBook(bookId);
        if (book == null) {
            Toast.makeText(this, R.string.book_does_not_exist_anymore, Toast.LENGTH_SHORT).show();
            setResult(RESULT_CANCELED);
            finish();
            return;
        }
        String id = "notebook-" + bookId;
        String name = book.getName();
        String title = BookUtils.getFragmentTitleForBook(book);
        Intent launchIntent = createLaunchIntent(book);
        IconCompat icon = createIcon();
        ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(this, id).setShortLabel(name).setLongLabel(title).setIcon(icon).setIntent(launchIntent).build();
        setResult(RESULT_OK, ShortcutManagerCompat.createShortcutResultIntent(this, shortcut));
        finish();
    }
}
Also used : Book(com.orgzly.android.Book) IconCompat(android.support.v4.graphics.drawable.IconCompat) Shelf(com.orgzly.android.Shelf) Intent(android.content.Intent) AppIntent(com.orgzly.android.AppIntent) 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