Example usage for android.widget PopupMenu setOnMenuItemClickListener

List of usage examples for android.widget PopupMenu setOnMenuItemClickListener

Introduction

In this page you can find the example usage for android.widget PopupMenu setOnMenuItemClickListener.

Prototype

public void setOnMenuItemClickListener(OnMenuItemClickListener listener) 

Source Link

Document

Sets a listener that will be notified when the user selects an item from the menu.

Usage

From source file:org.gateshipone.odyssey.views.NowPlayingView.java

/**
 * Called to open the popup menu on the top right corner.
 *
 * @param v View to which the popup menu should be attached
 *///from  w  w w  . j  a v a 2s.c om
private void showAdditionalOptionsMenu(View v) {
    PopupMenu menu = new PopupMenu(getContext(), v);
    // Inflate the menu from a menu xml file
    menu.inflate(R.menu.popup_menu_nowplaying_view);
    // Set the main NowPlayingView as a listener (directly implements callback)
    menu.setOnMenuItemClickListener(this);
    // Open the menu itself
    menu.show();
}

From source file:com.javielinux.tweettopics2.TweetTopicsActivity.java

private void showMenuColumnsOptions(View view) {
    final List<String> list = new ArrayList<String>();
    final List<Long> types = new ArrayList<Long>();

    ArrayList<Entity> userEntityList = DataFramework.getInstance().getEntityList("users",
            "service is null or service = \"twitter.com\"");
    for (Entity user : userEntityList) {
        list.add(getString(R.string.addColumnUser, user.getString("name")));
        types.add(user.getId());// w  w  w.j a v  a 2 s .  c  om
    }
    list.add(getString(R.string.addTrendingTopic));
    types.add(1001L);
    list.add(getString(R.string.addSavedTweet));
    types.add(1002L);
    list.add(getString(R.string.sortColumns));
    types.add(1003L);
    list.add(getString(R.string.removeColumns));
    types.add(1004L);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        PopupMenu popupMenu = new PopupMenu(this, view);
        int count = 0;
        for (String item : list) {
            popupMenu.getMenu().add(Menu.NONE, count, Menu.NONE, item);
            count++;
        }
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                long id = types.get(item.getItemId());
                String title = list.get(item.getItemId());
                switch ((int) id) {
                case 1001:
                    newTrending();
                    break;
                case 1002:
                    openSavedTweetColumn();
                    break;
                case 1003:
                    goToSortColumns();
                    break;
                case 1004:
                    goToRemoveColumns();
                    break;
                default:
                    showActionBarIndicatorAndMovePager(-1);
                    showDialogAddColumnUser(title, id);
                    break;
                }
                return true;
            }
        });
        popupMenu.show();
    } else {
        AlertDialogFragment frag = new AlertDialogFragment();
        Bundle args = new Bundle();
        args.putInt(AlertDialogFragment.KEY_ALERT_TITLE, R.string.actions);
        args.putBoolean(AlertDialogFragment.KEY_ALERT_HAS_POSITIVE_BUTTON, false);
        args.putBoolean(AlertDialogFragment.KEY_ALERT_CANCELABLE, false);
        args.putStringArrayList(AlertDialogFragment.KEY_ALERT_ARRAY_STRING_ITEMS, (ArrayList<String>) list);
        frag.setArguments(args);
        frag.setAlertButtonListener(new AlertDialogFragment.AlertButtonListener() {
            @Override
            public void OnAlertButtonOk() {
            }

            @Override
            public void OnAlertButtonCancel() {
            }

            @Override
            public void OnAlertButtonNeutral() {
            }

            @Override
            public void OnAlertItems(int which) {
                long id = types.get(which);
                String title = list.get(which);
                switch ((int) id) {
                case 1001:
                    newTrending();
                    break;
                case 1002:
                    openSavedTweetColumn();
                    break;
                case 1003:
                    goToSortColumns();
                    break;
                case 1004:
                    goToRemoveColumns();
                    break;
                default:
                    showActionBarIndicatorAndMovePager(-1);
                    showDialogAddColumnUser(title, id);
                    break;
                }
            }
        });
        frag.show(getSupportFragmentManager(), "dialog");
    }
}

From source file:lewa.support.v7.internal.widget.ActionBarContextView.java

private void initTitle() {
    if (!lewaInitTitle()) {
        if (mTitleLayout == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            inflater.inflate(R.layout.action_bar_title_item_with_spinner, this);
            //inflater.inflate(R.layout.abc_action_bar_title_item, this);
            mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1);
            // mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title);
            // mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle);
            // if (mTitleStyleRes != 0) {
            // mTitleView.setTextAppearance(mContext, mTitleStyleRes);
            // }/*from  w w w  . j a  va 2  s  .  c  o m*/
            // if (mSubtitleStyleRes != 0) {
            // mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes);
            // }

            mSpinner = (Button) mTitleLayout.findViewById(R.id.action_bar_spinner);
            mSpinner.setText("One item selected");

            final PopupMenu popMenu = new PopupMenu(mContext, mSpinner);
            final Menu menu = popMenu.getMenu();
            menu.add(Menu.NONE, android.R.id.selectAll, Menu.NONE, android.R.string.selectAll);
            popMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    if (null != mActionMode) {
                        if (mActionMode instanceof ActionModeImpl) {
                            return ((ActionModeImpl) mActionMode).onMenuItemSelected((MenuBuilder) menu, item);
                        }
                        if (mActionMode instanceof StandaloneActionMode) {
                            return ((StandaloneActionMode) mActionMode).onMenuItemSelected((MenuBuilder) menu,
                                    item);
                        }
                        // Woody Guo @ 2012/12/18: State of the selection menu item
                        // is totally controlled by the calling client
                        /*
                         * mActionMode.setSelectionMode(
                         *         ActionMode.SELECT_ALL == mActionMode.getSelectionMode()
                         *         ? ActionMode.SELECT_NONE : ActionMode.SELECT_ALL);
                         * item.setTitle(ActionMode.SELECT_ALL == mActionMode.getSelectionMode()
                         *         ? android.R.string.selectAll : R.string.selectNone);
                         * return true;
                         */
                    }
                    return false;
                }
            });

            mSpinner.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    popMenu.getMenu().getItem(0)
                            .setTitle(ActionMode.SELECT_ALL == mActionMode.getSelectionMode()
                                    ? android.R.string.selectAll
                                    : R.string.selectNone);
                    popMenu.show();
                }
            });
        }

        mSpinner.setText(mTitle);
    }
    if (true) {
        if (mTitleView != null) {
            mTitleView.setText(mTitle);
        }
        //mSubtitleView.setText(mSubtitle);
    }
    final boolean hasTitle = !TextUtils.isEmpty(mTitle);
    //final boolean hasSubtitle = !TextUtils.isEmpty(mSubtitle);
    //mSubtitleView.setVisibility(hasSubtitle ? VISIBLE : GONE);
    //mTitleLayout.setVisibility(hasTitle || hasSubtitle ? VISIBLE : GONE);
    mTitleLayout.setVisibility(hasTitle ? VISIBLE : GONE);
    if (mTitleLayout.getParent() == null) {
        addView(mTitleLayout);
    }
}

From source file:com.msopentech.applicationgateway.EnterpriseBrowserActivity.java

public void showSettingsPopup(View v) {
    try {/*from w  w w . j  av  a  2  s.  c  o m*/
        PopupMenu popup = new PopupMenu(this, v);
        MenuInflater inflater = popup.getMenuInflater();
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                case R.id.menu_connection: {
                    showAgentsList();
                    return true;
                }
                case R.id.menu_sign_in_or_out: {
                    if (mIsSigninRequired) {
                        // Menu item in sign in mode.
                        //TODO: 
                        showSignIn(null, true);
                        return true;
                    }

                    // Clean up the session.
                    mTraits.sessionID = null;
                    mTraits.token = null;
                    mTraits.agent = null;

                    // Indicate the condition.
                    mIsSigninRequired = true;

                    //Remove all tabs except one.
                    mCustomTabHost.resetTabs();

                    mUrlEditTextView.setText("");

                    mProgressBarView.setProgress(0);

                    // Disable the reload button and set the right image for it.
                    mReloadButtonView.setEnabled(false);
                    mReloadButtonView.setImageResource(R.drawable.reload_button);

                    mStatusButtonView.setImageResource(R.drawable.connection_red);

                    // Since it's the user's intention to sign out, his
                    // history must be dropped.
                    PersistenceManager.dropContent(PersistenceManager.ContentType.HISTORY);

                    return true;
                }
                // Removed from RELEASE version. Should be active for development ONLY.                       
                //                        case R.id.menu_advanced_router_settings: {
                //                            showAdvancedRouterSettings(null);
                //                        }
                default: {
                    return false;
                }
                }
            }
        });
        inflater.inflate(R.menu.settings_menu, popup.getMenu());

        if (mIsSigninRequired) {
            MenuItem signItem = popup.getMenu().findItem(R.id.menu_sign_in_or_out);
            signItem.setTitle(getResources().getString(R.string.browser_menu_item_sign_in));
        }

        // Assumes that agent always has a display name. If display name is not present agent is considered to be not connected.
        MenuItem agentItem = popup.getMenu().findItem(R.id.menu_connection);
        String status = getResources().getString(R.string.browser_menu_item_choose_connection_not_connected);
        if (mTraits != null && mTraits.agent != null && !TextUtils.isEmpty(mTraits.agent.getDisplayName())) {
            status = mTraits.agent.getDisplayName();
        }
        Spannable span = new SpannableString(
                getResources().getString(R.string.browser_menu_item_choose_connection, status));
        span.setSpan(new RelativeSizeSpan(0.8f), 18, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new ForegroundColorSpan(Color.GRAY), 18, span.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        agentItem.setTitle(span);

        popup.show();
    } catch (final Exception e) {
        Utility.showAlertDialog(EnterpriseBrowserActivity.class.getSimpleName()
                + ".showSettingsPopup(): Failed. " + e.toString(), EnterpriseBrowserActivity.this);
    }
}

From source file:nz.ac.auckland.lablet.ExperimentRunViewManager.java

private void showViewMenu() {
    View menuView = findViewById(R.id.action_view);
    PopupMenu popup = new PopupMenu(menuView.getContext(), menuView);

    final List<IExperimentSensor> experimentRuns = getActiveSensors();
    for (int i = 0; i < experimentRuns.size(); i++) {
        IExperimentSensor experiment = experimentRuns.get(i);

        MenuItem item = popup.getMenu().add(1, i, i, experiment.getClass().getSimpleName());
        item.setCheckable(true);/*  w w w .ja v  a 2  s.co m*/
    }
    popup.getMenu().setGroupCheckable(1, true, true);
    IExperimentSensor currentExperimentSensor = experiment.getCurrentExperimentSensor();
    if (currentExperimentSensor != null)
        popup.getMenu().getItem(experimentRuns.indexOf(currentExperimentSensor)).setChecked(true);
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            int itemPosition = menuItem.getItemId();
            IExperimentSensor experimentRun = experimentRuns.get(itemPosition);
            setCurrentSensor(experimentRun);
            pager.requestLayout();
            pager.setCurrentItem(itemPosition, true);
            return true;
        }
    });

    popup.show();
}

From source file:com.battlelancer.seriesguide.ui.ActivityFragment.java

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long id) {
    PopupMenu popupMenu = new PopupMenu(view.getContext(), view);
    Menu menu = popupMenu.getMenu();

    Cursor episode = (Cursor) mAdapter.getItem(position);
    if (episode == null) {
        return false;
    }//from  w w w.jav  a2 s.c  o  m

    // only display the action appropriate for the items current state
    menu.add(0, CONTEXT_CHECKIN_ID, 0, R.string.checkin);
    if (EpisodeTools.isWatched(episode.getInt(ActivityQuery.WATCHED))) {
        menu.add(0, CONTEXT_FLAG_UNWATCHED_ID, 1, R.string.action_unwatched);
    } else {
        menu.add(0, CONTEXT_FLAG_WATCHED_ID, 1, R.string.action_watched);
    }
    if (EpisodeTools.isCollected(episode.getInt(ActivityQuery.COLLECTED))) {
        menu.add(0, CONTEXT_COLLECTION_REMOVE_ID, 2, R.string.action_collection_remove);
    } else {
        menu.add(0, CONTEXT_COLLECTION_ADD_ID, 2, R.string.action_collection_add);
    }

    final int showTvdbId = episode.getInt(ActivityQuery.SHOW_ID);
    final int episodeTvdbId = episode.getInt(ActivityQuery._ID);
    final int seasonNumber = episode.getInt(ActivityQuery.SEASON);
    final int episodeNumber = episode.getInt(ActivityQuery.NUMBER);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case CONTEXT_CHECKIN_ID: {
                checkInEpisode((int) id);
                return true;
            }
            case CONTEXT_FLAG_WATCHED_ID: {
                updateEpisodeWatchedState(showTvdbId, episodeTvdbId, seasonNumber, episodeNumber, true);
                return true;
            }
            case CONTEXT_FLAG_UNWATCHED_ID: {
                updateEpisodeWatchedState(showTvdbId, episodeTvdbId, seasonNumber, episodeNumber, false);
                return true;
            }
            case CONTEXT_COLLECTION_ADD_ID: {
                updateEpisodeCollectionState(showTvdbId, episodeTvdbId, seasonNumber, episodeNumber, true);
                return true;
            }
            case CONTEXT_COLLECTION_REMOVE_ID: {
                updateEpisodeCollectionState(showTvdbId, episodeTvdbId, seasonNumber, episodeNumber, false);
                return true;
            }
            }
            return false;
        }
    });

    popupMenu.show();

    return true;
}

From source file:eu.faircode.adblocker.AdapterRule.java

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    // Get rule/*www.j  av a2  s . c  o m*/
    final Rule rule = listFiltered.get(position);

    // Handle expanding/collapsing
    holder.llApplication.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            rule.expanded = !rule.expanded;
            notifyItemChanged(position);
        }
    });

    // Show if non default rules
    holder.itemView.setBackgroundColor(rule.changed ? colorChanged : Color.TRANSPARENT);

    // Show expand/collapse indicator
    holder.ivExpander.setImageLevel(rule.expanded ? 1 : 0);

    // Show application icon
    if (rule.info.applicationInfo == null || rule.info.applicationInfo.icon == 0)
        Picasso.with(context).load(android.R.drawable.sym_def_app_icon).into(holder.ivIcon);
    else {
        Uri uri = Uri
                .parse("android.resource://" + rule.info.packageName + "/" + rule.info.applicationInfo.icon);
        Picasso.with(context).load(uri).resize(iconSize, iconSize).into(holder.ivIcon);
    }

    // Show application label
    holder.tvName.setText(rule.name);
    holder.tvDescription.setVisibility(rule.description == null ? View.GONE : View.VISIBLE);
    holder.tvDescription.setText(rule.description);

    // Show application state
    int color = rule.system ? colorOff : colorText;
    if (!rule.internet || !rule.enabled)
        color = Color.argb(128, Color.red(color), Color.green(color), Color.blue(color));
    holder.tvName.setTextColor(color);

    // Show rule count
    new AsyncTask<Object, Object, Long>() {
        @Override
        protected void onPreExecute() {
            holder.tvHosts.setVisibility(View.GONE);
        }

        @Override
        protected Long doInBackground(Object... objects) {
            return DatabaseHelper.getInstance(context).getRuleCount(rule.info.applicationInfo.uid);
        }

        @Override
        protected void onPostExecute(Long rules) {
            if (rules > 0) {
                holder.tvHosts.setVisibility(View.VISIBLE);
                holder.tvHosts.setText(Long.toString(rules));
            }
        }
    }.execute();

    // Wi-Fi settings
    holder.cbWifi.setAlpha(wifiActive ? 1 : 0.5f);
    holder.cbWifi.setOnCheckedChangeListener(null);
    holder.cbWifi.setChecked(rule.wifi_blocked);
    holder.cbWifi.setEnabled(rule.apply);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        Drawable wrap = DrawableCompat.wrap(CompoundButtonCompat.getButtonDrawable(holder.cbWifi));
        DrawableCompat.setTint(wrap, rule.apply ? (rule.wifi_blocked ? colorOff : colorOn) : colorGrayed);
    }
    holder.cbWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            rule.wifi_blocked = isChecked;
            updateRule(rule, true, listAll);
        }
    });

    holder.ivScreenWifi.setEnabled(rule.apply);
    holder.ivScreenWifi.setAlpha(wifiActive ? 1 : 0.5f);
    holder.ivScreenWifi.setVisibility(rule.screen_wifi && rule.wifi_blocked ? View.VISIBLE : View.INVISIBLE);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        Drawable wrap = DrawableCompat.wrap(holder.ivScreenWifi.getDrawable());
        DrawableCompat.setTint(wrap, rule.apply ? colorOn : colorGrayed);
    }

    // Mobile settings
    holder.cbOther.setAlpha(otherActive ? 1 : 0.5f);
    holder.cbOther.setOnCheckedChangeListener(null);
    holder.cbOther.setChecked(rule.other_blocked);
    holder.cbOther.setEnabled(rule.apply);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        Drawable wrap = DrawableCompat.wrap(CompoundButtonCompat.getButtonDrawable(holder.cbOther));
        DrawableCompat.setTint(wrap, rule.apply ? (rule.other_blocked ? colorOff : colorOn) : colorGrayed);
    }
    holder.cbOther.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            rule.other_blocked = isChecked;
            updateRule(rule, true, listAll);
        }
    });

    holder.ivScreenOther.setEnabled(rule.apply);
    holder.ivScreenOther.setAlpha(otherActive ? 1 : 0.5f);
    holder.ivScreenOther.setVisibility(rule.screen_other && rule.other_blocked ? View.VISIBLE : View.INVISIBLE);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        Drawable wrap = DrawableCompat.wrap(holder.ivScreenOther.getDrawable());
        DrawableCompat.setTint(wrap, rule.apply ? colorOn : colorGrayed);
    }

    holder.tvRoaming.setTextColor(rule.apply ? colorOff : colorGrayed);
    holder.tvRoaming.setAlpha(otherActive ? 1 : 0.5f);
    holder.tvRoaming.setVisibility(
            rule.roaming && (!rule.other_blocked || rule.screen_other) ? View.VISIBLE : View.INVISIBLE);

    // Expanded configuration section
    holder.llConfiguration.setVisibility(rule.expanded ? View.VISIBLE : View.GONE);

    // Show application details
    holder.tvUid
            .setText(rule.info.applicationInfo == null ? "?" : Integer.toString(rule.info.applicationInfo.uid));
    holder.tvPackage.setText(rule.info.packageName);
    holder.tvVersion.setText(rule.info.versionName + '/' + rule.info.versionCode);

    // Show application state
    holder.tvDisabled.setVisibility(rule.enabled ? View.GONE : View.VISIBLE);
    holder.tvInternet.setVisibility(rule.internet ? View.GONE : View.VISIBLE);

    // Apply
    holder.cbApply
            .setVisibility(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ? View.GONE : View.VISIBLE);
    holder.cbApply.setEnabled(rule.pkg);
    holder.cbApply.setOnCheckedChangeListener(null);
    holder.cbApply.setChecked(rule.apply);
    holder.cbApply.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            rule.apply = isChecked;
            updateRule(rule, true, listAll);
        }
    });

    // Show related
    holder.btnRelated.setVisibility(rule.relateduids ? View.VISIBLE : View.GONE);
    holder.btnRelated.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent main = new Intent(context, ActivityMain.class);
            main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(rule.info.applicationInfo.uid));
            context.startActivity(main);
        }
    });

    // Launch application settings
    final Intent settings = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    settings.setData(Uri.parse("package:" + rule.info.packageName));
    holder.ibSettings.setVisibility(
            settings.resolveActivity(context.getPackageManager()) == null ? View.GONE : View.VISIBLE);
    holder.ibSettings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            context.startActivity(settings);
        }
    });

    // Launch application
    holder.ibLaunch.setVisibility(rule.intent == null ? View.GONE : View.VISIBLE);
    holder.ibLaunch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            context.startActivity(rule.intent);
        }
    });

    // Show Wi-Fi screen on condition
    holder.cbScreenWifi.setOnCheckedChangeListener(null);
    holder.cbScreenWifi.setChecked(rule.screen_wifi);
    holder.cbScreenWifi.setEnabled(rule.wifi_blocked && rule.apply);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        Drawable wrap = DrawableCompat.wrap(holder.ivWifiLegend.getDrawable());
        DrawableCompat.setTint(wrap, colorOn);
    }

    holder.cbScreenWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            rule.screen_wifi = isChecked;
            updateRule(rule, true, listAll);
        }
    });

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        Drawable wrap = DrawableCompat.wrap(holder.ivOtherLegend.getDrawable());
        DrawableCompat.setTint(wrap, colorOn);
    }

    // Show mobile screen on condition
    holder.cbScreenOther.setOnCheckedChangeListener(null);
    holder.cbScreenOther.setChecked(rule.screen_other);
    holder.cbScreenOther.setEnabled(rule.other_blocked && rule.apply);

    holder.cbScreenOther.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            rule.screen_other = isChecked;
            updateRule(rule, true, listAll);
        }
    });

    // Show roaming condition
    holder.cbRoaming.setOnCheckedChangeListener(null);
    holder.cbRoaming.setChecked(rule.roaming);
    holder.cbRoaming.setEnabled((!rule.other_blocked || rule.screen_other) && rule.apply);

    holder.cbRoaming.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        @TargetApi(Build.VERSION_CODES.M)
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            rule.roaming = isChecked;
            updateRule(rule, true, listAll);

            // Request permissions
            if (isChecked && !Util.hasPhoneStatePermission(context))
                context.requestPermissions(new String[] { Manifest.permission.READ_PHONE_STATE },
                        ActivityMain.REQUEST_ROAMING);
        }
    });

    // Reset rule
    holder.btnClear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Util.areYouSure(view.getContext(), R.string.msg_clear_rules, new Util.DoubtListener() {
                @Override
                public void onSure() {
                    holder.cbApply.setChecked(true);
                    holder.cbWifi.setChecked(rule.wifi_default);
                    holder.cbOther.setChecked(rule.other_default);
                    holder.cbScreenWifi.setChecked(rule.screen_wifi_default);
                    holder.cbScreenOther.setChecked(rule.screen_other_default);
                    holder.cbRoaming.setChecked(rule.roaming_default);
                }
            });
        }
    });

    // Show access rules
    if (rule.expanded) {
        // Access the database when expanded only
        final AdapterAccess badapter = new AdapterAccess(context,
                DatabaseHelper.getInstance(context).getAccess(rule.info.applicationInfo.uid));
        if (filter)
            holder.lvAccess.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, final int bposition, long bid) {
                    Cursor cursor = (Cursor) badapter.getItem(bposition);
                    final long id = cursor.getLong(cursor.getColumnIndex("ID"));
                    int version = cursor.getInt(cursor.getColumnIndex("version"));
                    int protocol = cursor.getInt(cursor.getColumnIndex("protocol"));
                    String daddr = cursor.getString(cursor.getColumnIndex("daddr"));
                    int dport = cursor.getInt(cursor.getColumnIndex("dport"));
                    long time = cursor.getLong(cursor.getColumnIndex("time"));
                    int block = cursor.getInt(cursor.getColumnIndex("block"));

                    PopupMenu popup = new PopupMenu(context, context.findViewById(R.id.vwPopupAnchor));
                    popup.inflate(R.menu.access);
                    popup.getMenu().findItem(R.id.menu_host)
                            .setTitle(Util.getProtocolName(protocol, version, false) + " " + daddr
                                    + (dport > 0 ? "/" + dport : ""));
                    popup.getMenu().findItem(R.id.menu_time)
                            .setTitle(SimpleDateFormat.getDateTimeInstance().format(time));

                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem menuItem) {
                            switch (menuItem.getItemId()) {
                            case R.id.menu_allow:
                                if (IAB.isPurchased(ActivityPro.SKU_FILTER, context)) {
                                    DatabaseHelper.getInstance(context).setAccess(id, 0);
                                    ServiceSinkhole.reload("allow host", context);
                                } else
                                    context.startActivity(new Intent(context, ActivityPro.class));
                                return true;
                            case R.id.menu_block:
                                if (IAB.isPurchased(ActivityPro.SKU_FILTER, context)) {
                                    DatabaseHelper.getInstance(context).setAccess(id, 1);
                                    ServiceSinkhole.reload("block host", context);
                                } else
                                    context.startActivity(new Intent(context, ActivityPro.class));
                                return true;
                            case R.id.menu_reset:
                                DatabaseHelper.getInstance(context).setAccess(id, -1);
                                ServiceSinkhole.reload("reset host", context);
                                return true;
                            }
                            return false;
                        }
                    });

                    if (block == 0)
                        popup.getMenu().removeItem(R.id.menu_allow);
                    else if (block == 1)
                        popup.getMenu().removeItem(R.id.menu_block);

                    popup.show();
                }
            });
        else
            holder.lvAccess.setOnItemClickListener(null);

        holder.lvAccess.setAdapter(badapter);
    } else {
        holder.lvAccess.setAdapter(null);
        holder.lvAccess.setOnItemClickListener(null);
    }

    // Show logging is disabled
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    boolean log_app = prefs.getBoolean("log_app", false);
    holder.tvNolog.setVisibility(log_app ? View.GONE : View.VISIBLE);
    holder.tvNolog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            context.startActivity(new Intent(context, ActivitySettings.class));
        }
    });

    // Show disable access notifications setting
    holder.cbNotify.setOnCheckedChangeListener(null);
    holder.cbNotify.setEnabled(prefs.getBoolean("notify_access", false) && rule.apply);
    holder.cbNotify.setChecked(rule.notify);
    holder.cbNotify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            rule.notify = isChecked;
            updateRule(rule, true, listAll);
        }
    });

    // Clear access log
    holder.btnClearAccess.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Util.areYouSure(view.getContext(), R.string.msg_reset_access, new Util.DoubtListener() {
                @Override
                public void onSure() {
                    DatabaseHelper.getInstance(context).clearAccess(rule.info.applicationInfo.uid);
                    if (rv != null)
                        rv.scrollToPosition(position);
                }
            });
        }
    });

    // Show traffic statistics
    holder.tvStatistics.setText(context.getString(R.string.msg_mbday, rule.upspeed, rule.downspeed));
}

From source file:com.audiokernel.euphonyrmt.fragments.NowPlayingFragment.java

/**
 * Run during fragment initialization, this sets up the cover art popup menu and the coverArt
 * ImageView./*from  w w w  .  j a  v  a  2s.  com*/
 *
 * @param view The view to setup the coverArt ImageView in.
 * @return The resulting ImageView.
 */
private ImageView getCoverArt(final View view) {
    final ImageView coverArt = (ImageView) view.findViewById(R.id.albumCover);
    final PopupMenu coverMenu = new PopupMenu(mActivity, coverArt);
    final Menu menu = coverMenu.getMenu();

    coverArt.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            scrollToNowPlaying();
        }
    });

    menu.add(Menu.NONE, POPUP_COVER_BLACKLIST, Menu.NONE, R.string.otherCover);
    menu.add(Menu.NONE, POPUP_COVER_SELECTIVE_CLEAN, Menu.NONE, R.string.resetCover);
    coverMenu.setOnMenuItemClickListener(this);
    coverArt.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(final View v) {
            final boolean isConsumed;

            if (mCurrentSong != null) {
                menu.setGroupVisible(Menu.NONE, new AlbumInfo(mCurrentSong).isValid());
                coverMenu.show();
                isConsumed = true;
            } else {
                isConsumed = false;
            }

            return isConsumed;
        }
    });

    return coverArt;
}

From source file:info.papdt.blacklight.support.adapter.WeiboAdapter.java

void buildPopup(final ViewHolder h) {
    PopupMenu p = new PopupMenu(mContext, h.popup);
    p.inflate(R.menu.popup);/*from w w  w .  ja v  a  2 s .c  om*/
    final Menu m = p.getMenu();

    // Show needed items
    m.findItem(R.id.popup_copy).setVisible(true);
    if (h.msg instanceof CommentModel) {
        m.findItem(R.id.popup_reply).setVisible(true);

        CommentModel cmt = (CommentModel) h.msg;
        if (cmt.user.id.equals(mUid)
                || (cmt.status != null && cmt.status.user != null && cmt.status.user.id.equals(mUid))) {
            m.findItem(R.id.popup_delete).setVisible(true);
        }
    } else {
        m.findItem(R.id.popup_repost).setVisible(true);
        m.findItem(R.id.popup_comment).setVisible(true);

        if (h.msg.liked) {
            m.findItem(R.id.popup_unlike).setVisible(true);
        } else {
            m.findItem(R.id.popup_like).setVisible(true);
        }

        if (h.msg.user != null && h.msg.user.id.equals(mUid)) {
            m.findItem(R.id.popup_delete).setVisible(true);
        }
    }

    p.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.popup_delete) {
                h.delete();
            } else if (id == R.id.popup_copy) {
                h.copy();
            } else if (id == R.id.popup_comment || id == R.id.popup_reply) {
                h.reply();
            } else if (id == R.id.popup_repost) {
                h.repost();
            } else if (id == R.id.popup_like || id == R.id.popup_unlike) {
                new LikeTask().execute(h.msg, h, m);
            }

            return true;
        }
    });

    // Pop up!
    p.show();
}

From source file:org.gateshipone.malp.application.views.NowPlayingView.java

/**
     * Called to open the popup menu on the top right corner.
     */* w  ww. j a v  a 2 s . c o  m*/
     * @param v
     */
    private void showAdditionalOptionsMenu(View v) {
        PopupMenu menu = new PopupMenu(getContext(), v);
        // Inflate the menu from a menu xml file
        menu.inflate(R.menu.popup_menu_nowplaying);
        // Set the main NowPlayingView as a listener (directly implements callback)
        menu.setOnMenuItemClickListener(this);

        // Set the checked menu item state if a MPDCurrentStatus is available
        if (null != mLastStatus) {
            MenuItem singlePlaybackItem = menu.getMenu().findItem(R.id.action_toggle_single_mode);
            singlePlaybackItem.setChecked(mLastStatus.getSinglePlayback() == 1);

            MenuItem consumeItem = menu.getMenu().findItem(R.id.action_toggle_consume_mode);
            consumeItem.setChecked(mLastStatus.getConsume() == 1);
        }

        // Check if the current view is the cover or the playlist. If it is the playlist hide its actions.
        // If the viewswitcher only has one child the dual pane layout is used
        if (mViewSwitcher.getDisplayedChild() == 0 && (mViewSwitcher.getChildCount() > 1)) {
            menu.getMenu().setGroupEnabled(R.id.group_playlist_actions, false);
            menu.getMenu().setGroupVisible(R.id.group_playlist_actions, false);
        }
        // Open the menu itself
        menu.show();
    }