Example usage for android.graphics.drawable Icon createWithResource

List of usage examples for android.graphics.drawable Icon createWithResource

Introduction

In this page you can find the example usage for android.graphics.drawable Icon createWithResource.

Prototype

public static Icon createWithResource(String resPackage, @DrawableRes int resId) 

Source Link

Document

Create an Icon pointing to a drawable resource.

Usage

From source file:com.oasisfeng.nevo.decorators.media.MediaPlayerDecorator.java

@TargetApi(Build.VERSION_CODES.M)
private Icon fixIconPkg(final Icon icon, final String pkg) {
    if (Icon_getType != null && Icon_mString1 != null)
        try {/*from w  w w.  j a va 2  s . com*/
            final int type = (Integer) Icon_getType.invoke(icon);
            if (type == 2)
                Icon_mString1.set(icon, pkg);
            return icon;
        } catch (final IllegalAccessException | InvocationTargetException ignored) {
        } // Should not happen
    return Icon.createWithResource("", android.R.drawable.ic_media_play); // Fall-back to default icon
}

From source file:com.wizardsofm.deskclock.data.TimerNotificationBuilderN.java

@Override
public Notification buildHeadsUp(Context context, List<Timer> expired) {
    final Timer timer = expired.get(0);

    // First action intent is to reset all timers.
    final Icon icon1 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_stop_24dp);
    final Intent reset = TimerService.createResetExpiredTimersIntent(context);
    final PendingIntent intent1 = Utils.pendingServiceIntent(context, reset);

    // Generate some descriptive text, a title, and an action name based on the timer count.
    final CharSequence stateText;
    final int count = expired.size();
    final List<Notification.Action> actions = new ArrayList<>(2);
    if (count == 1) {
        final String label = timer.getLabel();
        if (TextUtils.isEmpty(label)) {
            stateText = context.getString(com.wizardsofm.deskclock.R.string.timer_times_up);
        } else {/*from w ww.  ja va  2 s .c o  m*/
            stateText = label;
        }

        // Left button: Reset single timer
        final CharSequence title1 = context.getString(com.wizardsofm.deskclock.R.string.timer_stop);
        actions.add(new Notification.Action.Builder(icon1, title1, intent1).build());

        // Right button: Add minute
        final Intent addTime = TimerService.createAddMinuteTimerIntent(context, timer.getId());
        final PendingIntent intent2 = Utils.pendingServiceIntent(context, addTime);
        final Icon icon2 = Icon.createWithResource(context, com.wizardsofm.deskclock.R.drawable.ic_add_24dp);
        final CharSequence title2 = context.getString(com.wizardsofm.deskclock.R.string.timer_plus_1_min);
        actions.add(new Notification.Action.Builder(icon2, title2, intent2).build());

    } else {
        stateText = context.getString(com.wizardsofm.deskclock.R.string.timer_multi_times_up, count);

        // Left button: Reset all timers
        final CharSequence title1 = context.getString(com.wizardsofm.deskclock.R.string.timer_stop_all);
        actions.add(new Notification.Action.Builder(icon1, title1, intent1).build());
    }

    final long base = getChronometerBase(timer);

    final String pname = context.getPackageName();
    final RemoteViews contentView = new RemoteViews(pname,
            com.wizardsofm.deskclock.R.layout.chronometer_notif_content);
    contentView.setChronometerCountDown(com.wizardsofm.deskclock.R.id.chronometer, true);
    contentView.setChronometer(com.wizardsofm.deskclock.R.id.chronometer, base, null, true);
    contentView.setTextViewText(com.wizardsofm.deskclock.R.id.state, stateText);

    // Content intent shows the timer full screen when clicked.
    final Intent content = new Intent(context, ExpiredTimersActivity.class);
    final PendingIntent contentIntent = Utils.pendingActivityIntent(context, content);

    // Full screen intent has flags so it is different than the content intent.
    final Intent fullScreen = new Intent(context, ExpiredTimersActivity.class)
            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    final PendingIntent pendingFullScreen = Utils.pendingActivityIntent(context, fullScreen);

    return new Notification.Builder(context).setOngoing(true).setLocalOnly(true).setShowWhen(false)
            .setAutoCancel(false).setContentIntent(contentIntent).setCustomContentView(contentView)
            .setPriority(Notification.PRIORITY_MAX).setDefaults(Notification.DEFAULT_LIGHTS)
            .setColor(ContextCompat.getColor(context, com.wizardsofm.deskclock.R.color.default_background))
            .setSmallIcon(com.wizardsofm.deskclock.R.drawable.stat_notify_timer)
            .setFullScreenIntent(pendingFullScreen, true).setStyle(new Notification.DecoratedCustomViewStyle())
            .setActions(actions.toArray(new Notification.Action[actions.size()])).build();
}

From source file:de.grobox.liberario.activities.MainActivity.java

@TargetApi(Build.VERSION_CODES.N_MR1)
private void registerNougatShortcuts() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "quickhome")
                .setShortLabel(getString(R.string.widget_name_quickhome))
                .setIcon(Icon.createWithResource(getContext(), R.drawable.ic_quickhome_widget))
                .setIntent(TransportrUtils.getShortcutIntent(getContext())).build();
        shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
    }/*from w  w  w  . j  a  v a  2s .c  o m*/
}

From source file:com.hacktx.android.activities.MainActivity.java

private void setupAppShortcuts() {
    // Setup app shortcuts
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1
            && mConfigManager.getValue(ConfigParam.CHECK_IN)) {
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        Intent checkInIntent = new Intent(Intent.ACTION_VIEW);
        checkInIntent.setPackage("com.hacktx.android");
        checkInIntent.setClass(this, CheckInActivity.class);
        checkInIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NO_HISTORY
                | Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
        checkInIntent.putExtra("fromShortcut", true);

        ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "check-in")
                .setShortLabel(getString(R.string.app_shortcut_check_in))
                .setLongLabel(getString(R.string.app_shortcut_check_in))
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(checkInIntent).build();

        shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut));
    }/*from  w  w  w . j av  a  2 s. co  m*/
}

From source file:io.github.runassudo.ptoffline.activities.MainActivity.java

@TargetApi(Build.VERSION_CODES.N_MR1)
private void registerNougatShortcuts() {
    int currentapiVersion = Build.VERSION.SDK_INT;
    if (currentapiVersion >= Build.VERSION_CODES.N_MR1) {
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "quickhome")
                .setShortLabel(getString(R.string.widget_name_quickhome))
                .setIcon(Icon.createWithResource(getContext(), R.drawable.ic_quickhome_widget))
                .setIntent(TransportrUtils.getShortcutIntent(getContext())).build();

        shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
    }/*from  w  w w . ja v  a2  s . c om*/
}

From source file:com.farmerbb.taskbar.MainActivity.java

private void proceedWithAppLaunch(Bundle savedInstanceState) {
    setContentView(R.layout.main);/*from w ww . j  a v  a2 s.co  m*/

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setCustomView(R.layout.switch_layout);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
    }

    theSwitch = (SwitchCompat) findViewById(R.id.the_switch);
    if (theSwitch != null) {
        final SharedPreferences pref = U.getSharedPreferences(this);
        theSwitch.setChecked(pref.getBoolean("taskbar_active", false));

        theSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                if (U.canDrawOverlays(this)) {
                    boolean firstRun = pref.getBoolean("first_run", true);
                    startTaskbarService();

                    if (firstRun && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !U.isSystemApp(this)) {
                        ApplicationInfo applicationInfo = null;
                        try {
                            applicationInfo = getPackageManager().getApplicationInfo(BuildConfig.APPLICATION_ID,
                                    0);
                        } catch (PackageManager.NameNotFoundException e) {
                            /* Gracefully fail */ }

                        if (applicationInfo != null) {
                            AppOpsManager appOpsManager = (AppOpsManager) getSystemService(
                                    Context.APP_OPS_SERVICE);
                            int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
                                    applicationInfo.uid, applicationInfo.packageName);

                            if (mode != AppOpsManager.MODE_ALLOWED) {
                                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                                builder.setTitle(R.string.pref_header_recent_apps)
                                        .setMessage(R.string.enable_recent_apps)
                                        .setPositiveButton(R.string.action_ok, (dialog, which) -> {
                                            try {
                                                startActivity(
                                                        new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
                                                U.showToastLong(MainActivity.this,
                                                        R.string.usage_stats_message);
                                            } catch (ActivityNotFoundException e) {
                                                U.showErrorDialog(MainActivity.this, "GET_USAGE_STATS");
                                            }
                                        }).setNegativeButton(R.string.action_cancel, null);

                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                        }
                    }
                } else {
                    U.showPermissionDialog(MainActivity.this);
                    compoundButton.setChecked(false);
                }
            } else
                stopTaskbarService();
        });
    }

    if (savedInstanceState == null) {
        if (!getIntent().hasExtra("theme_change"))
            getFragmentManager().beginTransaction()
                    .replace(R.id.fragmentContainer, new AboutFragment(), "AboutFragment").commit();
        else
            getFragmentManager().beginTransaction()
                    .replace(R.id.fragmentContainer, new AppearanceFragment(), "AppearanceFragment").commit();
    }

    if (!BuildConfig.APPLICATION_ID.equals(BuildConfig.BASE_APPLICATION_ID) && freeVersionInstalled()) {
        final SharedPreferences pref = U.getSharedPreferences(this);
        if (!pref.getBoolean("dont_show_uninstall_dialog", false)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.settings_imported_successfully).setMessage(R.string.import_dialog_message)
                    .setPositiveButton(R.string.action_uninstall, (dialog, which) -> {
                        pref.edit().putBoolean("uninstall_dialog_shown", true).apply();

                        try {
                            startActivity(new Intent(Intent.ACTION_DELETE,
                                    Uri.parse("package:" + BuildConfig.BASE_APPLICATION_ID)));
                        } catch (ActivityNotFoundException e) {
                            /* Gracefully fail */ }
                    });

            if (pref.getBoolean("uninstall_dialog_shown", false))
                builder.setNegativeButton(R.string.action_dont_show_again, (dialogInterface, i) -> pref.edit()
                        .putBoolean("dont_show_uninstall_dialog", true).apply());

            AlertDialog dialog = builder.create();
            dialog.show();
            dialog.setCancelable(false);
        }

        if (!pref.getBoolean("uninstall_dialog_shown", false)) {
            if (theSwitch != null)
                theSwitch.setChecked(false);

            SharedPreferences.Editor editor = pref.edit();

            String iconPack = pref.getString("icon_pack", BuildConfig.BASE_APPLICATION_ID);
            if (iconPack.contains(BuildConfig.BASE_APPLICATION_ID)) {
                editor.putString("icon_pack", BuildConfig.APPLICATION_ID);
            } else {
                U.refreshPinnedIcons(this);
            }

            editor.putBoolean("first_run", true);
            editor.apply();
        }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        if (shortcutManager.getDynamicShortcuts().size() == 0) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setClass(this, StartTaskbarActivity.class);
            intent.putExtra("is_launching_shortcut", true);

            ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "start_taskbar")
                    .setShortLabel(getString(R.string.start_taskbar))
                    .setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon_start)).setIntent(intent)
                    .build();

            Intent intent2 = new Intent(Intent.ACTION_MAIN);
            intent2.setClass(this, ShortcutActivity.class);
            intent2.putExtra("is_launching_shortcut", true);

            ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this, "freeform_mode")
                    .setShortLabel(getString(R.string.pref_header_freeform))
                    .setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon_freeform))
                    .setIntent(intent2).build();

            shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut, shortcut2));
        }
    }
}

From source file:com.ht.app.RestaurantsActivity.java

private void setupShortucts() {

    ShortcutManager shortcutManager = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        shortcutManager = getSystemService(ShortcutManager.class);

        // Contact us
        ShortcutInfo webShortcut = null;
        String contact_us = getString(R.string.txt_contact_us);
        webShortcut = new ShortcutInfo.Builder(this, contact_us).setShortLabel(contact_us)
                .setLongLabel(contact_us).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                .setIntent(Utils.getComposeEmailIntent()).setRank(0).build();

        String restaurants = getString(R.string.title_activity_restaurants);
        ShortcutInfo restaurantsShortcut = new ShortcutInfo.Builder(this, restaurants)
                .setShortLabel(restaurants).setLongLabel(restaurants)
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                .setIntent(new Intent(Intent.ACTION_VIEW).setPackage("com.ht")
                        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK).setClass(this, this.getClass()))
                .setRank(2).build();//from w  w w  .j av a2 s .c o m

        String masjid = getString(R.string.title_activity_masjid);
        ShortcutInfo masjidsShortcut = new ShortcutInfo.Builder(this, masjid).setShortLabel(masjid)
                .setLongLabel(masjid).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                .setIntent(new Intent(Intent.ACTION_VIEW).setPackage("com.ht")
                        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK).setClass(this, MasjidActivity.class))
                .setRank(1).build();
        shortcutManager.setDynamicShortcuts(Arrays.asList(webShortcut, restaurantsShortcut, masjidsShortcut));

    }

}

From source file:org.iota.wallet.ui.activity.MainActivity.java

private void updateDynamicShortcuts() {
    ShortcutManager shortcutManager;//from w ww  . j a va  2  s  . com

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {

        Intent intentGenerateQrCode = new Intent(this, MainActivity.class);
        intentGenerateQrCode.setFlags((Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
        intentGenerateQrCode.setAction(Constants.ACTION_GENERATE_QR_CODE);

        ShortcutInfo shortcutGenerateQrCode = new ShortcutInfo.Builder(this, SHORTCUT_ID_GENERATE_QR_CODE)
                .setShortLabel(getString(R.string.shortcut_generate_qr_code))
                .setLongLabel(getString(R.string.shortcut_generate_qr_code))
                .setIcon(Icon.createWithResource(this, R.drawable.ic_shortcut_qr))
                .setIntent(intentGenerateQrCode).build();

        Intent intentTransferIotas = new Intent(this, MainActivity.class);
        intentTransferIotas.setFlags((Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
        intentTransferIotas.setAction(Constants.ACTION_SEND_TRANSFER);

        ShortcutInfo shortcutTransferIotas = new ShortcutInfo.Builder(this, SHORTCUT_ID_SEND_TRANSFER)
                .setShortLabel(getString(R.string.shortcut_send_transfer))
                .setLongLabel(getString(R.string.shortcut_send_transfer))
                .setIcon(Icon.createWithResource(this, R.drawable.ic_shortcut_transaction))
                .setIntent(intentTransferIotas).build();

        shortcutManager = getSystemService(ShortcutManager.class);

        if (shortcutManager != null) {
            if (IOTA.seed != null) {
                shortcutManager
                        .setDynamicShortcuts(Arrays.asList(shortcutGenerateQrCode, shortcutTransferIotas));
                shortcutManager.enableShortcuts(
                        Arrays.asList(SHORTCUT_ID_GENERATE_QR_CODE, SHORTCUT_ID_SEND_TRANSFER));
            } else {
                // remove shortcuts if Iota.seed.isEmpty()
                shortcutManager.disableShortcuts(
                        Arrays.asList(SHORTCUT_ID_GENERATE_QR_CODE, SHORTCUT_ID_SEND_TRANSFER));
                shortcutManager.removeAllDynamicShortcuts();
            }
        }
    }
}

From source file:com.grarak.kerneladiutor.activities.NavigationActivity.java

private void setShortcuts() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1)
        return;//ww w  . ja  va  2  s.  co m
    HashMap<Fragment, Integer> openendFragmentsCount = new HashMap<>();

    for (int id : sActualFragments.keySet()) {
        Fragment fragment = sActualFragments.get(id);
        if (fragment == null || fragment.getClass() == SettingsFragment.class)
            continue;

        int opened = Prefs.getInt(fragment.getClass().getSimpleName() + "_opened", 0, this);
        openendFragmentsCount.put(fragment, opened);
    }

    int max = 0;
    for (Map.Entry<Fragment, Integer> map : openendFragmentsCount.entrySet()) {
        if (max < map.getValue()) {
            max = map.getValue();
        }
    }

    int count = 0;
    List<ShortcutInfo> shortcutInfos = new ArrayList<>();
    ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
    shortcutManager.removeAllDynamicShortcuts();
    for (int i = max; i >= 0; i--) {
        for (Map.Entry<Fragment, Integer> map : openendFragmentsCount.entrySet()) {
            if (i == map.getValue()) {
                NavigationFragment navFragment = getNavigationFragment(map.getKey());
                if (navFragment == null)
                    continue;

                if (count == 4)
                    break;
                count++;

                Intent intent = new Intent(this, MainActivity.class);
                intent.setAction(Intent.ACTION_VIEW);
                intent.putExtra("section", navFragment.mFragment.getClass().getCanonicalName());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

                ShortcutInfo shortcut = new ShortcutInfo.Builder(this,
                        navFragment.mFragment.getClass().getSimpleName())
                                .setShortLabel(getString(navFragment.mId))
                                .setLongLabel(Utils.strFormat(getString(R.string.open),
                                        getString(navFragment.mId)))
                                .setIcon(Icon.createWithResource(this,
                                        navFragment.mDrawable == 0 ? R.drawable.ic_blank
                                                : navFragment.mDrawable))
                                .setIntent(intent).build();
                shortcutInfos.add(shortcut);
            }
        }
    }
    shortcutManager.setDynamicShortcuts(shortcutInfos);
}