Example usage for android.view MenuItem setCheckable

List of usage examples for android.view MenuItem setCheckable

Introduction

In this page you can find the example usage for android.view MenuItem setCheckable.

Prototype

public MenuItem setCheckable(boolean checkable);

Source Link

Document

Control whether this item can display a check mark.

Usage

From source file:org.mozilla.gecko.GeckoApp.java

@Override
public boolean onPrepareOptionsMenu(Menu aMenu) {
    Iterator<ExtraMenuItem> i = sExtraMenuItems.iterator();
    while (i.hasNext()) {
        final ExtraMenuItem item = i.next();
        if (aMenu.findItem(item.id) == null) {
            final MenuItem mi = aMenu.add(Menu.NONE, item.id, Menu.NONE, item.label);
            if (item.icon != null) {
                if (item.icon.startsWith("data")) {
                    byte[] raw = GeckoAppShell.decodeBase64(item.icon.substring(22),
                            GeckoAppShell.BASE64_DEFAULT);
                    Bitmap bitmap = BitmapFactory.decodeByteArray(raw, 0, raw.length);
                    BitmapDrawable drawable = new BitmapDrawable(bitmap);
                    mi.setIcon(drawable);
                } else if (item.icon.startsWith("jar:") || item.icon.startsWith("file://")) {
                    GeckoAppShell.getHandler().post(new Runnable() {
                        public void run() {
                            try {
                                URL url = new URL(item.icon);
                                InputStream is = (InputStream) url.getContent();
                                Drawable drawable = Drawable.createFromStream(is, "src");
                                mi.setIcon(drawable);
                            } catch (Exception e) {
                                Log.w(LOGTAG, "onPrepareOptionsMenu: Unable to set icon", e);
                            }/*from  w  w  w  .  j a  v a  2  s .  c  o  m*/
                        }
                    });
                }
            }
            mi.setOnMenuItemClickListener(item);
        }
    }

    if (!sIsGeckoReady)
        aMenu.findItem(R.id.settings).setEnabled(false);

    Tab tab = Tabs.getInstance().getSelectedTab();
    MenuItem bookmark = aMenu.findItem(R.id.bookmark);
    MenuItem forward = aMenu.findItem(R.id.forward);
    MenuItem share = aMenu.findItem(R.id.share);
    MenuItem saveAsPDF = aMenu.findItem(R.id.save_as_pdf);
    MenuItem charEncoding = aMenu.findItem(R.id.char_encoding);

    if (tab == null) {
        bookmark.setEnabled(false);
        forward.setEnabled(false);
        share.setEnabled(false);
        saveAsPDF.setEnabled(false);
        return true;
    }

    bookmark.setEnabled(true);
    bookmark.setCheckable(true);

    if (tab.isBookmark()) {
        bookmark.setChecked(true);
        bookmark.setIcon(R.drawable.ic_menu_bookmark_remove);
    } else {
        bookmark.setChecked(false);
        bookmark.setIcon(R.drawable.ic_menu_bookmark_add);
    }

    forward.setEnabled(tab.canDoForward());

    // Disable share menuitem for about:, chrome: and file: URIs
    String scheme = Uri.parse(tab.getURL()).getScheme();
    boolean enabled = scheme != null
            && !(scheme.equals("about") || scheme.equals("chrome") || scheme.equals("file"));
    share.setEnabled(enabled);

    // Disable save as PDF for about:home and xul pages
    saveAsPDF.setEnabled(!(tab.getURL().equals("about:home")
            || tab.getContentType().equals("application/vnd.mozilla.xul+xml")));

    charEncoding.setVisible(GeckoPreferences.getCharEncodingState());

    return true;
}

From source file:org.mozilla.gecko.BrowserApp.java

private void updateAddonMenuItem(int id, JSONObject options) {
    // Set attribute for the menu item in cache, if available
    if (mAddonMenuItemsCache != null && !mAddonMenuItemsCache.isEmpty()) {
        for (MenuItemInfo item : mAddonMenuItemsCache) {
            if (item.id == id) {
                item.label = options.optString("name", item.label);
                item.checkable = options.optBoolean("checkable", item.checkable);
                item.checked = options.optBoolean("checked", item.checked);
                item.enabled = options.optBoolean("enabled", item.enabled);
                item.visible = options.optBoolean("visible", item.visible);
                item.added = (mMenu != null);
                break;
            }//from  w w w  .j  a v a  2 s .co  m
        }
    }

    if (mMenu == null) {
        return;
    }

    final MenuItem menuItem = mMenu.findItem(id);
    if (menuItem != null) {
        menuItem.setTitle(options.optString("name", menuItem.getTitle().toString()));
        menuItem.setCheckable(options.optBoolean("checkable", menuItem.isCheckable()));
        menuItem.setChecked(options.optBoolean("checked", menuItem.isChecked()));
        menuItem.setEnabled(options.optBoolean("enabled", menuItem.isEnabled()));
        menuItem.setVisible(options.optBoolean("visible", menuItem.isVisible()));
    }
}

From source file:org.mozilla.gecko.BrowserApp.java

/**
 * Add the provided item to the provided menu, which should be
 * the root (mMenu).//  w  w  w  .j  a  v  a  2  s . c om
 */
private void addAddonMenuItemToMenu(final Menu menu, final MenuItemInfo info) {
    info.added = true;

    final Menu destination;
    if (info.parent == 0) {
        destination = menu;
    } else if (info.parent == GECKO_TOOLS_MENU) {
        // The tools menu only exists in our -v11 resources.
        if (Versions.feature11Plus) {
            final MenuItem tools = menu.findItem(R.id.tools);
            destination = tools != null ? tools.getSubMenu() : menu;
        } else {
            destination = menu;
        }
    } else {
        final MenuItem parent = menu.findItem(info.parent);
        if (parent == null) {
            return;
        }

        Menu parentMenu = findParentMenu(menu, parent);

        if (!parent.hasSubMenu()) {
            parentMenu.removeItem(parent.getItemId());
            destination = parentMenu.addSubMenu(Menu.NONE, parent.getItemId(), Menu.NONE, parent.getTitle());
            if (parent.getIcon() != null) {
                ((SubMenu) destination).getItem().setIcon(parent.getIcon());
            }
        } else {
            destination = parent.getSubMenu();
        }
    }

    final MenuItem item = destination.add(Menu.NONE, info.id, Menu.NONE, info.label);

    item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Menu:Clicked",
                    Integer.toString(info.id - ADDON_MENU_OFFSET)));
            return true;
        }
    });

    if (info.icon == null) {
        item.setIcon(R.drawable.ic_menu_addons_filler);
    } else {
        final int id = info.id;
        BitmapUtils.getDrawable(this, info.icon, new BitmapUtils.BitmapLoader() {
            @Override
            public void onBitmapFound(Drawable d) {
                // TODO: why do we re-find the item?
                final MenuItem item = destination.findItem(id);
                if (item == null) {
                    return;
                }
                if (d == null) {
                    item.setIcon(R.drawable.ic_menu_addons_filler);
                    return;
                }
                item.setIcon(d);
            }
        });
    }

    item.setCheckable(info.checkable);
    item.setChecked(info.checked);
    item.setEnabled(info.enabled);
    item.setVisible(info.visible);
}

From source file:org.mozilla.gecko.BrowserApp.java

@Override
public boolean onPrepareOptionsMenu(Menu aMenu) {
    if (aMenu == null)
        return false;

    // Hide the tab history panel when hardware menu button is pressed.
    TabHistoryFragment frag = (TabHistoryFragment) getSupportFragmentManager()
            .findFragmentByTag(TAB_HISTORY_FRAGMENT_TAG);
    if (frag != null) {
        frag.dismiss();//from ww  w.  j ava 2s  .co m
    }

    if (!GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) {
        aMenu.findItem(R.id.settings).setEnabled(false);
        aMenu.findItem(R.id.help).setEnabled(false);
    }

    Tab tab = Tabs.getInstance().getSelectedTab();
    final MenuItem bookmark = aMenu.findItem(R.id.bookmark);
    final MenuItem reader = aMenu.findItem(R.id.reading_list);
    final MenuItem back = aMenu.findItem(R.id.back);
    final MenuItem forward = aMenu.findItem(R.id.forward);
    final MenuItem share = aMenu.findItem(R.id.share);
    final MenuItem quickShare = aMenu.findItem(R.id.quickshare);
    final MenuItem saveAsPDF = aMenu.findItem(R.id.save_as_pdf);
    final MenuItem charEncoding = aMenu.findItem(R.id.char_encoding);
    final MenuItem findInPage = aMenu.findItem(R.id.find_in_page);
    final MenuItem desktopMode = aMenu.findItem(R.id.desktop_mode);
    final MenuItem enterGuestMode = aMenu.findItem(R.id.new_guest_session);
    final MenuItem exitGuestMode = aMenu.findItem(R.id.exit_guest_session);

    // Only show the "Quit" menu item on pre-ICS, television devices,
    // or if the user has explicitly enabled the clear on shutdown pref.
    // (We check the pref last to save the pref read.)
    // In ICS+, it's easy to kill an app through the task switcher.
    final boolean visible = Versions.preICS || HardwareUtils.isTelevision() || !PrefUtils
            .getStringSet(GeckoSharedPrefs.forProfile(this), ClearOnShutdownPref.PREF, new HashSet<String>())
            .isEmpty();
    aMenu.findItem(R.id.quit).setVisible(visible);
    aMenu.findItem(R.id.logins).setVisible(AppConstants.NIGHTLY_BUILD);

    if (tab == null || tab.getURL() == null) {
        bookmark.setEnabled(false);
        reader.setEnabled(false);
        back.setEnabled(false);
        forward.setEnabled(false);
        share.setEnabled(false);
        quickShare.setEnabled(false);
        saveAsPDF.setEnabled(false);
        findInPage.setEnabled(false);

        // NOTE: Use MenuUtils.safeSetEnabled because some actions might
        // be on the BrowserToolbar context menu.
        if (Versions.feature11Plus) {
            // There is no page menu prior to v11 resources.
            MenuUtils.safeSetEnabled(aMenu, R.id.page, false);
        }
        MenuUtils.safeSetEnabled(aMenu, R.id.subscribe, false);
        MenuUtils.safeSetEnabled(aMenu, R.id.add_search_engine, false);
        MenuUtils.safeSetEnabled(aMenu, R.id.site_settings, false);
        MenuUtils.safeSetEnabled(aMenu, R.id.add_to_launcher, false);

        return true;
    }

    final boolean inGuestMode = GeckoProfile.get(this).inGuestMode();

    final boolean isAboutReader = AboutPages.isAboutReader(tab.getURL());
    bookmark.setEnabled(!isAboutReader);
    bookmark.setVisible(!inGuestMode);
    bookmark.setCheckable(true);
    bookmark.setChecked(tab.isBookmark());
    bookmark.setIcon(resolveBookmarkIconID(tab.isBookmark()));
    bookmark.setTitle(resolveBookmarkTitleID(tab.isBookmark()));

    reader.setEnabled(isAboutReader || !AboutPages.isAboutPage(tab.getURL()));
    reader.setVisible(!inGuestMode);
    reader.setCheckable(true);
    final boolean isPageInReadingList = tab.isInReadingList();
    reader.setChecked(isPageInReadingList);
    reader.setIcon(resolveReadingListIconID(isPageInReadingList));
    reader.setTitle(resolveReadingListTitleID(isPageInReadingList));

    back.setEnabled(tab.canDoBack());
    forward.setEnabled(tab.canDoForward());
    desktopMode.setChecked(tab.getDesktopMode());
    desktopMode.setIcon(
            tab.getDesktopMode() ? R.drawable.ic_menu_desktop_mode_on : R.drawable.ic_menu_desktop_mode_off);

    View backButtonView = MenuItemCompat.getActionView(back);

    if (backButtonView != null) {
        backButtonView.setOnLongClickListener(new Button.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Tab tab = Tabs.getInstance().getSelectedTab();
                if (tab != null) {
                    closeOptionsMenu();
                    return tabHistoryController.showTabHistory(tab, TabHistoryController.HistoryAction.BACK);
                }
                return false;
            }
        });
    }

    View forwardButtonView = MenuItemCompat.getActionView(forward);

    if (forwardButtonView != null) {
        forwardButtonView.setOnLongClickListener(new Button.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Tab tab = Tabs.getInstance().getSelectedTab();
                if (tab != null) {
                    closeOptionsMenu();
                    return tabHistoryController.showTabHistory(tab, TabHistoryController.HistoryAction.FORWARD);
                }
                return false;
            }
        });
    }

    String url = tab.getURL();
    if (AboutPages.isAboutReader(url)) {
        String urlFromReader = ReaderModeUtils.getUrlFromAboutReader(url);
        if (urlFromReader != null) {
            url = urlFromReader;
        }
    }

    // Disable share menuitem for about:, chrome:, file:, and resource: URIs
    final boolean shareVisible = RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_SHARE);
    share.setVisible(shareVisible);
    final boolean shareEnabled = StringUtils.isShareableUrl(url) && shareVisible;
    share.setEnabled(shareEnabled);
    MenuUtils.safeSetEnabled(aMenu, R.id.downloads,
            RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_DOWNLOADS));

    // NOTE: Use MenuUtils.safeSetEnabled because some actions might
    // be on the BrowserToolbar context menu.
    if (Versions.feature11Plus) {
        MenuUtils.safeSetEnabled(aMenu, R.id.page, !isAboutHome(tab));
    }
    MenuUtils.safeSetEnabled(aMenu, R.id.subscribe, tab.hasFeeds());
    MenuUtils.safeSetEnabled(aMenu, R.id.add_search_engine, tab.hasOpenSearch());
    MenuUtils.safeSetEnabled(aMenu, R.id.site_settings, !isAboutHome(tab));
    MenuUtils.safeSetEnabled(aMenu, R.id.add_to_launcher, !isAboutHome(tab));

    // Action providers are available only ICS+.
    if (Versions.feature14Plus) {
        quickShare.setVisible(shareVisible);
        quickShare.setEnabled(shareEnabled);

        // This provider also applies to the quick share menu item.
        final GeckoActionProvider provider = ((GeckoMenuItem) share).getGeckoActionProvider();
        if (provider != null) {
            Intent shareIntent = provider.getIntent();

            // For efficiency, the provider's intent is only set once
            if (shareIntent == null) {
                shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                provider.setIntent(shareIntent);
            }

            // Replace the existing intent's extras
            shareIntent.putExtra(Intent.EXTRA_TEXT, url);
            shareIntent.putExtra(Intent.EXTRA_SUBJECT, tab.getDisplayTitle());
            shareIntent.putExtra(Intent.EXTRA_TITLE, tab.getDisplayTitle());
            shareIntent.putExtra(ShareDialog.INTENT_EXTRA_DEVICES_ONLY, true);

            // Clear the existing thumbnail extras so we don't share an old thumbnail.
            shareIntent.removeExtra("share_screenshot_uri");

            // Include the thumbnail of the page being shared.
            BitmapDrawable drawable = tab.getThumbnail();
            if (drawable != null) {
                Bitmap thumbnail = drawable.getBitmap();

                // Kobo uses a custom intent extra for sharing thumbnails.
                if (Build.MANUFACTURER.equals("Kobo") && thumbnail != null) {
                    File cacheDir = getExternalCacheDir();

                    if (cacheDir != null) {
                        File outFile = new File(cacheDir, "thumbnail.png");

                        try {
                            java.io.FileOutputStream out = new java.io.FileOutputStream(outFile);
                            thumbnail.compress(Bitmap.CompressFormat.PNG, 90, out);
                        } catch (FileNotFoundException e) {
                            Log.e(LOGTAG, "File not found", e);
                        }

                        shareIntent.putExtra("share_screenshot_uri", Uri.parse(outFile.getPath()));
                    }
                }
            }
        }
    }

    final boolean privateTabVisible = RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_PRIVATE_BROWSING);
    MenuUtils.safeSetVisible(aMenu, R.id.new_private_tab, privateTabVisible);

    // Disable save as PDF for about:home and xul pages.
    saveAsPDF.setEnabled(!(isAboutHome(tab) || tab.getContentType().equals("application/vnd.mozilla.xul+xml")
            || tab.getContentType().startsWith("video/")));

    // Disable find in page for about:home, since it won't work on Java content.
    findInPage.setEnabled(!isAboutHome(tab));

    charEncoding.setVisible(GeckoPreferences.getCharEncodingState());

    if (mProfile.inGuestMode()) {
        exitGuestMode.setVisible(true);
    } else {
        enterGuestMode.setVisible(true);
    }

    if (!RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_GUEST_BROWSING)) {
        MenuUtils.safeSetVisible(aMenu, R.id.new_guest_session, false);
    }

    if (!RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_INSTALL_EXTENSION)) {
        MenuUtils.safeSetVisible(aMenu, R.id.addons, false);
    }

    return true;
}