Example usage for android.support.v4.graphics.drawable IconCompat createWithResource

List of usage examples for android.support.v4.graphics.drawable IconCompat createWithResource

Introduction

In this page you can find the example usage for android.support.v4.graphics.drawable IconCompat createWithResource.

Prototype

public static IconCompat createWithResource(Context context, @DrawableRes int resId) 

Source Link

Document

Create an Icon pointing to a drawable resource.

Usage

From source file:org.strongswan.android.ui.VpnProfileSelectActivity.java

@Override
public void onVpnProfileSelected(VpnProfile profile) {
    Intent shortcut = new Intent(VpnProfileControlActivity.START_PROFILE);
    shortcut.putExtra(VpnProfileControlActivity.EXTRA_VPN_PROFILE_ID, profile.getUUID().toString());

    ShortcutInfoCompat.Builder builder = new ShortcutInfoCompat.Builder(this, profile.getUUID().toString());
    builder.setIntent(shortcut);//from   ww  w  . jav a 2  s .c  o  m
    builder.setShortLabel(profile.getName());
    builder.setIcon(IconCompat.createWithResource(this, R.mipmap.ic_shortcut));
    setResult(RESULT_OK, ShortcutManagerCompat.createShortcutResultIntent(this, builder.build()));
    finish();
}

From source file:org.traccar.client.ShortcutActivity.java

private void setShortcutResult(String label, @DrawableRes int iconResId, String action) {
    Intent intent = new Intent(Intent.ACTION_DEFAULT, null, this, ShortcutActivity.class);
    intent.putExtra(EXTRA_ACTION, action);

    ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(this, action).setShortLabel(label)
            .setIcon(IconCompat.createWithResource(this, iconResId)).setIntent(intent).build();

    setResult(RESULT_OK, ShortcutManagerCompat.createShortcutResultIntent(this, shortcut));
}

From source file:net.gsantner.opoc.util.ShareUtil.java

/**
 * Try to create a new desktop shortcut on the launcher. Add permissions:
 * <uses-permission android:name="android.permission.INSTALL_SHORTCUT" />
 * <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
 *
 * @param intent  The intent to be invoked on tap
 * @param iconRes Icon resource for the item
 * @param title   Title of the item//  w w  w.  j a v  a 2 s.  c  o m
 */
public void createLauncherDesktopShortcut(Intent intent, @DrawableRes int iconRes, String title) {
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    if (intent.getAction() == null) {
        intent.setAction(Intent.ACTION_VIEW);
    }

    ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(_context,
            Long.toString(new Random().nextLong())).setIntent(intent)
                    .setIcon(IconCompat.createWithResource(_context, iconRes)).setShortLabel(title)
                    .setLongLabel(title).build();
    ShortcutManagerCompat.requestPinShortcut(_context, shortcut, null);
}

From source file:org.deviceconnect.android.deviceplugin.demo.DemoSettingFragment.java

private void createShortcut() {
    Activity activity = getActivity();//  www  . j  av a2s .  c om
    if (activity == null) {
        return;
    }
    Context context = activity.getApplicationContext();
    Intent shortcut = createDemoPageIntent();

    ShortcutInfoCompat.Builder builder = new ShortcutInfoCompat.Builder(context, CAMERA_DEMO_SHORTCUT_ID)
            .setIcon(IconCompat.createWithResource(context, getShortcutIconResource(mDemoInstaller)))
            .setShortLabel(getShortcutShortLabel(mDemoInstaller))
            .setLongLabel(getShortcutLongLabel(mDemoInstaller)).setIntent(shortcut);
    ComponentName mainActivity = getMainActivity(context);
    if (mainActivity != null) {
        builder.setActivity(mainActivity);
    }
    ShortcutInfoCompat info = builder.build();
    boolean result = ShortcutManagerCompat.requestPinShortcut(context, info, null);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        // OS 8???OS???????????
        if (result) {
            showShurtcutResult(getString(R.string.demo_page_settings_button_create_shortcut_success));
        } else {
            showShurtcutResult(getString(R.string.demo_page_settings_button_create_shortcut_error));
        }
    }
}

From source file:org.thoughtcrime.securesms.conversation.ConversationActivity.java

private void handleAddShortcut() {
    Log.i(TAG, "Creating home screen shortcut for recipient " + recipient.getAddress());

    new AsyncTask<Void, Void, IconCompat>() {

        @Override/*from ww w.j  av  a 2s. c  o m*/
        protected IconCompat doInBackground(Void... voids) {
            Context context = getApplicationContext();
            IconCompat icon = null;

            if (recipient.getContactPhoto() != null) {
                try {
                    Bitmap bitmap = BitmapFactory
                            .decodeStream(recipient.getContactPhoto().openInputStream(context));
                    bitmap = BitmapUtil.createScaledBitmap(bitmap, 300, 300);
                    icon = IconCompat.createWithAdaptiveBitmap(bitmap);
                } catch (IOException e) {
                    Log.w(TAG,
                            "Failed to decode contact photo during shortcut creation. Falling back to generic icon.",
                            e);
                }
            }

            if (icon == null) {
                icon = IconCompat.createWithResource(context,
                        recipient.isGroupRecipient() ? R.mipmap.ic_group_shortcut
                                : R.mipmap.ic_person_shortcut);
            }

            return icon;
        }

        @Override
        protected void onPostExecute(IconCompat icon) {
            Context context = getApplicationContext();
            String name = Optional.fromNullable(recipient.getName())
                    .or(Optional.fromNullable(recipient.getProfileName())).or(recipient.toShortString());

            ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context,
                    recipient.getAddress().serialize() + '-' + System.currentTimeMillis()).setShortLabel(name)
                            .setIcon(icon)
                            .setIntent(ShortcutLauncherActivity.createIntent(context, recipient.getAddress()))
                            .build();

            if (ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null)) {
                Toast.makeText(context, getString(R.string.ConversationActivity_added_to_home_screen),
                        Toast.LENGTH_LONG).show();
            }
        }
    }.execute();
}

From source file:com.amaze.filemanager.fragments.MainFragment.java

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;/*from   www.j  a v a 2s.c o  m*/
    }

    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);
}