Example usage for android.view MenuItem setIntent

List of usage examples for android.view MenuItem setIntent

Introduction

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

Prototype

public MenuItem setIntent(Intent intent);

Source Link

Document

Change the Intent associated with this item.

Usage

From source file:org.getlantern.firetweet.fragment.CustomTabsFragment.java

@Override
public void onPrepareOptionsMenu(final Menu menu) {
    final Resources res = getResources();
    final boolean hasOfficialKeyAccounts = Utils.hasAccountSignedWithOfficialKeys(getActivity());
    final boolean forcePrivateAPI = mPreferences.getBoolean(KEY_FORCE_USING_PRIVATE_APIS, false);
    final long[] accountIds = getAccountIds(getActivity());
    final MenuItem itemAdd = menu.findItem(R.id.add_submenu);
    if (itemAdd != null && itemAdd.hasSubMenu()) {
        final SubMenu subMenu = itemAdd.getSubMenu();
        subMenu.clear();//ww w.j a v  a  2s.  co m
        final HashMap<String, CustomTabConfiguration> map = getConfiguraionMap();
        final List<Entry<String, CustomTabConfiguration>> tabs = new ArrayList<>(map.entrySet());
        Collections.sort(tabs, CustomTabConfigurationComparator.SINGLETON);
        for (final Entry<String, CustomTabConfiguration> entry : tabs) {
            final String type = entry.getKey();
            final CustomTabConfiguration conf = entry.getValue();

            final boolean isOfficiakKeyAccountRequired = TAB_TYPE_ACTIVITIES_ABOUT_ME.equals(type)
                    || TAB_TYPE_ACTIVITIES_BY_FRIENDS.equals(type);
            final boolean accountIdRequired = conf
                    .getAccountRequirement() == CustomTabConfiguration.ACCOUNT_REQUIRED;

            final Intent intent = new Intent(INTENT_ACTION_ADD_TAB);
            intent.setClass(getActivity(), CustomTabEditorActivity.class);
            intent.putExtra(EXTRA_TYPE, type);
            intent.putExtra(EXTRA_OFFICIAL_KEY_ONLY, isOfficiakKeyAccountRequired);

            final MenuItem subItem = subMenu.add(conf.getDefaultTitle());
            final boolean disabledByNoAccount = accountIdRequired && accountIds.length == 0;
            final boolean disabledByNoOfficialKey = !forcePrivateAPI && isOfficiakKeyAccountRequired
                    && !hasOfficialKeyAccounts;
            final boolean disabledByDuplicateTab = conf.isSingleTab() && isTabAdded(getActivity(), type);
            final boolean shouldDisable = disabledByDuplicateTab || disabledByNoOfficialKey
                    || disabledByNoAccount;
            subItem.setVisible(!shouldDisable);
            subItem.setEnabled(!shouldDisable);
            final Drawable icon = ResourcesCompat.getDrawable(res, conf.getDefaultIcon(), null);
            subItem.setIcon(icon);
            subItem.setIntent(intent);
        }
    }
    ThemeUtils.applyColorFilterToMenuIcon(getActivity(), menu);
}

From source file:com.android.contacts.activities.DialtactsActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.dialtacts_options, menu);

    // set up intents and onClick listeners
    final MenuItem callSettingsMenuItem = menu.findItem(R.id.menu_call_settings);
    final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar);
    final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option);
    final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact);

    callSettingsMenuItem.setIntent(DialtactsActivity.getCallSettingsIntent());
    searchMenuItem.setOnMenuItemClickListener(mSearchMenuItemClickListener);
    filterOptionMenuItem.setOnMenuItemClickListener(mFilterOptionsMenuItemClickListener);
    addContactOptionMenuItem.setIntent(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));

    return true;//ww w  . j  a  v  a2 s  .  com
}

From source file:org.woltage.irssiconnectbot.ConsoleActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    View view = findCurrentView(R.id.console_flip);
    final TerminalView terminalView = (TerminalView) findCurrentView(R.id.console_flip);
    final boolean activeTerminal = (view instanceof TerminalView);
    boolean sessionOpen = false;
    boolean disconnected = false;
    boolean canForwardPorts = false;

    if (activeTerminal) {
        TerminalBridge bridge = ((TerminalView) view).bridge;
        sessionOpen = bridge.isSessionOpen();
        disconnected = bridge.isDisconnected();
        canForwardPorts = bridge.canFowardPorts();
    }//w w w  . j a  v  a 2  s  . com

    menu.setQwertyMode(true);

    disconnect = menu.add(R.string.list_host_disconnect);
    if (hardKeyboard)
        disconnect.setAlphabeticShortcut('w');
    if (!sessionOpen && disconnected)
        disconnect.setTitle(R.string.console_menu_close);
    disconnect.setEnabled(activeTerminal);
    disconnect.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
    disconnect.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            // disconnect or close the currently visible session
            TerminalView terminalView = (TerminalView) findCurrentView(R.id.console_flip);
            TerminalBridge bridge = terminalView.bridge;

            bridge.dispatchDisconnect(true);

            if (bound != null && prefs.getBoolean("unloadKeysOnDisconnect", true)) {
                bound.lockUnusedKeys();
            }
            return true;
        }
    });

    copy = menu.add(R.string.console_menu_copy);
    if (hardKeyboard)
        copy.setAlphabeticShortcut('c');
    copy.setIcon(android.R.drawable.ic_menu_set_as);
    copy.setEnabled(activeTerminal);
    copy.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            // mark as copying and reset any previous bounds
            TerminalView terminalView = (TerminalView) findCurrentView(R.id.console_flip);
            copySource = terminalView.bridge;

            SelectionArea area = copySource.getSelectionArea();
            area.reset();
            area.setBounds(copySource.buffer.getColumns(), copySource.buffer.getRows());
            area.setRow(copySource.buffer.getCursorRow());
            area.setColumn(copySource.buffer.getCursorColumn());

            copySource.setSelectingForCopy(true);

            // Make sure we show the initial selection
            copySource.redraw();

            Toast.makeText(ConsoleActivity.this, getString(R.string.console_copy_start), Toast.LENGTH_LONG)
                    .show();
            return true;
        }
    });

    paste = menu.add(R.string.console_menu_paste);
    if (hardKeyboard)
        paste.setAlphabeticShortcut('v');
    paste.setIcon(android.R.drawable.ic_menu_edit);
    paste.setEnabled(clipboard.hasText() && sessionOpen);
    paste.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            // force insert of clipboard text into current console
            TerminalView terminalView = (TerminalView) findCurrentView(R.id.console_flip);
            TerminalBridge bridge = terminalView.bridge;

            // pull string from clipboard and generate all events to force
            // down
            String clip = clipboard.getText().toString();
            bridge.injectString(clip);

            return true;
        }
    });

    portForward = menu.add(R.string.console_menu_portforwards);
    if (hardKeyboard)
        portForward.setAlphabeticShortcut('f');
    portForward.setIcon(android.R.drawable.ic_menu_manage);
    portForward.setEnabled(sessionOpen && canForwardPorts);
    portForward.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            TerminalView terminalView = (TerminalView) findCurrentView(R.id.console_flip);
            TerminalBridge bridge = terminalView.bridge;

            Intent intent = new Intent(ConsoleActivity.this, PortForwardListActivity.class);
            intent.putExtra(Intent.EXTRA_TITLE, bridge.host.getId());
            ConsoleActivity.this.startActivityForResult(intent, REQUEST_EDIT);
            return true;
        }
    });

    urlscan = menu.add(R.string.console_menu_urlscan);
    if (hardKeyboard)
        urlscan.setAlphabeticShortcut('u');
    urlscan.setIcon(android.R.drawable.ic_menu_search);
    urlscan.setEnabled(activeTerminal);
    urlscan.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            final TerminalView terminal = (TerminalView) findCurrentView(R.id.console_flip);

            TerminalKeyListener handler = terminal.bridge.getKeyHandler();
            handler.urlScan(terminal);

            return true;
        }
    });

    resize = menu.add(R.string.console_menu_resize);
    if (hardKeyboard)
        resize.setAlphabeticShortcut('s');
    resize.setIcon(android.R.drawable.ic_menu_crop);
    resize.setEnabled(sessionOpen);
    resize.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            final TerminalView terminalView = (TerminalView) findCurrentView(R.id.console_flip);

            final View resizeView = inflater.inflate(R.layout.dia_resize, null, false);
            ((EditText) resizeView.findViewById(R.id.width))
                    .setText(prefs.getString("default_fsize_width", "80"));
            ((EditText) resizeView.findViewById(R.id.height))
                    .setText(prefs.getString("default_fsize_height", "25"));

            new AlertDialog.Builder(ConsoleActivity.this).setView(resizeView)
                    .setPositiveButton(R.string.button_resize, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            int width, height;
                            try {
                                width = Integer.parseInt(
                                        ((EditText) resizeView.findViewById(R.id.width)).getText().toString());
                                height = Integer.parseInt(
                                        ((EditText) resizeView.findViewById(R.id.height)).getText().toString());
                            } catch (NumberFormatException nfe) {
                                // TODO change this to a real dialog where we can
                                // make the input boxes turn red to indicate an error.
                                return;
                            }
                            if (width > 0 && height > 0) {
                                terminalView.forceSize(width, height);
                                terminalView.bridge.parentChanged(terminalView);
                            } else {
                                new AlertDialog.Builder(ConsoleActivity.this)
                                        .setMessage("Width and height must be higher than zero.")
                                        .setTitle("Error").show();
                            }
                        }
                    }).setNegativeButton(android.R.string.cancel, null).create().show();

            return true;
        }
    });

    MenuItem ctrlKey = menu.add("Ctrl");
    ctrlKey.setEnabled(activeTerminal);
    ctrlKey.setIcon(R.drawable.button_ctrl);
    MenuItemCompat.setShowAsAction(ctrlKey, MenuItem.SHOW_AS_ACTION_IF_ROOM);
    ctrlKey.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            View flip = findCurrentView(R.id.console_flip);
            if (flip == null)
                return false;

            TerminalView terminal = (TerminalView) flip;

            TerminalKeyListener handler = terminal.bridge.getKeyHandler();
            handler.metaPress(TerminalKeyListener.META_CTRL_ON);
            terminal.bridge.tryKeyVibrate();
            return true;
        }
    });

    MenuItem escKey = menu.add("Esc");
    escKey.setEnabled(activeTerminal);
    escKey.setIcon(R.drawable.button_esc);
    MenuItemCompat.setShowAsAction(escKey, MenuItem.SHOW_AS_ACTION_IF_ROOM);
    escKey.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            View flip = findCurrentView(R.id.console_flip);
            if (flip == null)
                return false;

            TerminalView terminal = (TerminalView) flip;

            TerminalKeyListener handler = terminal.bridge.getKeyHandler();
            handler.sendEscape();
            terminal.bridge.tryKeyVibrate();
            return true;
        }
    });

    MenuItem symKey = menu.add("SYM");
    symKey.setEnabled(activeTerminal);
    symKey.setIcon(R.drawable.button_sym);
    MenuItemCompat.setShowAsAction(symKey, MenuItem.SHOW_AS_ACTION_IF_ROOM);
    symKey.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            View flip = findCurrentView(R.id.console_flip);
            if (flip == null)
                return false;

            TerminalView terminal = (TerminalView) flip;

            TerminalKeyListener handler = terminal.bridge.getKeyHandler();
            handler.showCharPickerDialog(terminal);
            return true;
        }
    });

    MenuItem inputButton = menu.add("Input");
    inputButton.setEnabled(activeTerminal);
    inputButton.setIcon(R.drawable.button_input);
    MenuItemCompat.setShowAsAction(inputButton, MenuItem.SHOW_AS_ACTION_IF_ROOM);
    inputButton.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            View flip = findCurrentView(R.id.console_flip);
            if (flip == null)
                return false;

            final TerminalView terminal = (TerminalView) flip;
            Thread promptThread = new Thread(new Runnable() {
                public void run() {
                    String inj = getCurrentPromptHelper().requestStringPrompt(null, "");
                    terminal.bridge.injectString(inj);
                }
            });
            promptThread.setName("Prompt");
            promptThread.setDaemon(true);
            promptThread.start();
            return true;
        }
    });

    MenuItem keyboard = menu.add("Show Keyboard");
    keyboard.setEnabled(activeTerminal);
    keyboard.setIcon(R.drawable.button_keyboard);
    MenuItemCompat.setShowAsAction(keyboard, MenuItem.SHOW_AS_ACTION_IF_ROOM);
    keyboard.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            View flip = findCurrentView(R.id.console_flip);
            if (flip == null)
                return false;

            inputManager.showSoftInput(flip, InputMethodManager.SHOW_FORCED);
            return true;
        }
    });

    MenuItem keys = menu.add(R.string.console_menu_pubkeys);
    keys.setIcon(android.R.drawable.ic_lock_lock);
    keys.setIntent(new Intent(this, PubkeyListActivity.class));
    keys.setEnabled(activeTerminal);
    keys.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ConsoleActivity.this, PubkeyListActivity.class);
            intent.putExtra(PubkeyListActivity.PICK_MODE, true);
            ConsoleActivity.this.startActivityForResult(intent, REQUEST_SELECT);
            return true;
        }
    });

    return true;
}

From source file:com.samknows.measurement.activity.SamKnowsAggregateStatViewerActivity.java

/**
 * Create the options menu that displays the refresh and about options
 *///from w  w  w.  j  av a  2s.c  o  m
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (Constants.DEBUG) {
        MenuItem item = menu.add(getString(R.string.test_results));
        item.setIntent(new Intent(this, TestResultsTabActivity.class));
    }

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

From source file:de.vanita5.twittnuker.util.Utils.java

public static void addIntentToMenu(final Context context, final Menu menu, final Intent queryIntent,
        final int groupId) {
    if (context == null || menu == null || queryIntent == null)
        return;/*from  ww  w  .  j a va2 s  .  c o  m*/
    final PackageManager pm = context.getPackageManager();
    final Resources res = context.getResources();
    final float density = res.getDisplayMetrics().density;
    final int padding = Math.round(density * 4);
    final List<ResolveInfo> activities = pm.queryIntentActivities(queryIntent, 0);
    for (final ResolveInfo info : activities) {
        final Intent intent = new Intent(queryIntent);
        final Drawable icon = info.loadIcon(pm);
        intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
        final MenuItem item = menu.add(groupId, Menu.NONE, Menu.NONE, info.loadLabel(pm));
        item.setIntent(intent);
        final int iw = icon.getIntrinsicWidth(), ih = icon.getIntrinsicHeight();
        if (iw > 0 && ih > 0) {
            final Drawable iconWithPadding = new PaddingDrawable(icon, padding);
            iconWithPadding.setBounds(0, 0, iw, ih);
            item.setIcon(iconWithPadding);
        } else {
            item.setIcon(icon);
        }
    }
}

From source file:org.getlantern.firetweet.util.Utils.java

public static void addIntentToMenuForExtension(final Context context, final Menu menu, final int groupId,
        final String action, final String parelableKey, final String parelableJSONKey,
        final FiretweetParcelable parcelable) {
    if (context == null || menu == null || action == null || parelableKey == null || parcelable == null)
        return;/*  w  ww  . ja v  a2s. c  om*/
    final PackageManager pm = context.getPackageManager();
    final Resources res = context.getResources();
    final float density = res.getDisplayMetrics().density;
    final int padding = Math.round(density * 4);
    final Intent queryIntent = new Intent(action);
    queryIntent.setExtrasClassLoader(context.getClassLoader());
    final List<ResolveInfo> activities = pm.queryIntentActivities(queryIntent, PackageManager.GET_META_DATA);
    for (final ResolveInfo info : activities) {
        final Intent intent = new Intent(queryIntent);
        if (isExtensionUseJSON(info)) {
            intent.putExtra(parelableJSONKey, JSONSerializer.toJSONObjectString(parcelable));
        } else {
            intent.putExtra(parelableKey, parcelable);
        }
        intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
        final MenuItem item = menu.add(groupId, Menu.NONE, Menu.NONE, info.loadLabel(pm));
        item.setIntent(intent);
        final Drawable metaDataDrawable = getMetadataDrawable(pm, info.activityInfo,
                METADATA_KEY_EXTENSION_ICON);
        final int actionIconColor = ThemeUtils.getThemeForegroundColor(context);
        if (metaDataDrawable != null) {
            metaDataDrawable.mutate();
            metaDataDrawable.setColorFilter(actionIconColor, Mode.SRC_ATOP);
            item.setIcon(metaDataDrawable);
        } else {
            final Drawable icon = info.loadIcon(pm);
            final int iw = icon.getIntrinsicWidth(), ih = icon.getIntrinsicHeight();
            if (iw > 0 && ih > 0) {
                final Drawable iconWithPadding = new PaddingDrawable(icon, padding);
                iconWithPadding.setBounds(0, 0, iw, ih);
                item.setIcon(iconWithPadding);
            } else {
                item.setIcon(icon);
            }
        }

    }
}