Example usage for android.widget PopupMenu getMenu

List of usage examples for android.widget PopupMenu getMenu

Introduction

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

Prototype

public Menu getMenu() 

Source Link

Document

Returns the Menu associated with this popup.

Usage

From source file:com.studyjams.mdvideo.PlayerModule.PlayerActivity.java

private void configurePopupWithTracks(PopupMenu popup, final OnMenuItemClickListener customActionClickListener,
        final int trackType) {
    if (player == null) {
        return;//from   ww w.j  a  va 2 s .c o  m
    }
    int trackCount = player.getTrackCount(trackType);
    if (trackCount == 0) {
        return;
    }
    popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            return (customActionClickListener != null && customActionClickListener.onMenuItemClick(item))
                    || onTrackItemClick(item, trackType);
        }
    });
    Menu menu = popup.getMenu();
    // ID_OFFSET ensures we avoid clashing with Menu.NONE (which equals 0).
    menu.add(MENU_GROUP_TRACKS, DemoPlayer.TRACK_DISABLED + ID_OFFSET, Menu.NONE, R.string.off);
    for (int i = 0; i < trackCount; i++) {
        menu.add(MENU_GROUP_TRACKS, i + ID_OFFSET, Menu.NONE,
                buildTrackName(player.getTrackFormat(trackType, i)));
    }
    menu.setGroupCheckable(MENU_GROUP_TRACKS, true, true);
    menu.findItem(player.getSelectedTrack(trackType) + ID_OFFSET).setChecked(true);
}

From source file:android_network.hetnet.vpn_service.AdapterRule.java

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

    // Get rule//from w  w w  .  j  a v a 2  s  .co  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);

    // 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();

    boolean screen_on = prefs.getBoolean("screen_on", true);

    // Wi-Fi settings
    holder.cbWifi.setEnabled(rule.apply);
    holder.cbWifi.setAlpha(wifiActive ? 1 : 0.5f);
    holder.cbWifi.setOnCheckedChangeListener(null);
    holder.cbWifi.setChecked(rule.wifi_blocked);
    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.setEnabled(rule.apply);
    holder.cbOther.setAlpha(otherActive ? 1 : 0.5f);
    holder.cbOther.setOnCheckedChangeListener(null);
    holder.cbOther.setChecked(rule.other_blocked);
    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);
    holder.tvDescription.setVisibility(rule.description == null ? View.GONE : View.VISIBLE);
    holder.tvDescription.setText(rule.description);

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

    // Show traffic statistics
    holder.tvStatistics
            .setVisibility(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? View.GONE : View.VISIBLE);
    holder.tvStatistics.setText(context.getString(R.string.msg_mbday, rule.upspeed, rule.downspeed));

    // 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, MainActivity.class);
            main.putExtra(MainActivity.EXTRA_SEARCH, Integer.toString(rule.info.applicationInfo.uid));
            context.startActivity(main);
        }
    });

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

    // Data saver
    holder.ibDatasaver.setVisibility(rule.datasaver == null ? View.GONE : View.VISIBLE);
    holder.ibDatasaver.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            context.startActivity(rule.datasaver);
        }
    });

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

    // Apply
    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 Wi-Fi screen on condition
    holder.llScreenWifi.setVisibility(screen_on ? View.VISIBLE : View.GONE);
    holder.cbScreenWifi.setEnabled(rule.wifi_blocked && rule.apply);
    holder.cbScreenWifi.setOnCheckedChangeListener(null);
    holder.cbScreenWifi.setChecked(rule.screen_wifi);

    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.llScreenOther.setVisibility(screen_on ? View.VISIBLE : View.GONE);
    holder.cbScreenOther.setEnabled(rule.other_blocked && rule.apply);
    holder.cbScreenOther.setOnCheckedChangeListener(null);
    holder.cbScreenOther.setChecked(rule.screen_other);
    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.setEnabled((!rule.other_blocked || rule.screen_other) && rule.apply);
    holder.cbRoaming.setOnCheckedChangeListener(null);
    holder.cbRoaming.setChecked(rule.roaming);
    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 },
                        MainActivity.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);
                }
            });
        }
    });

    // Live
    holder.ivLive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            live = !live;
            TypedValue tv = new TypedValue();
            view.getContext().getTheme().resolveAttribute(live ? R.attr.iconPause : R.attr.iconPlay, tv, true);
            holder.ivLive.setImageResource(tv.resourceId);
            if (live)
                AdapterRule.this.notifyDataSetChanged();
        }
    });

    // Show logging is disabled
    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 filtering is disabled
    boolean filter = prefs.getBoolean("filter", false);
    holder.tvNoFilter.setVisibility(filter ? View.GONE : View.VISIBLE);
    holder.tvNoFilter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            context.startActivity(new Intent(context, ActivitySettings.class));
        }
    });

    // 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));
        holder.lvAccess.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int bposition, long bid) {
                PackageManager pm = context.getPackageManager();
                Cursor cursor = (Cursor) badapter.getItem(bposition);
                final long id = cursor.getLong(cursor.getColumnIndex("ID"));
                final int version = cursor.getInt(cursor.getColumnIndex("version"));
                final int protocol = cursor.getInt(cursor.getColumnIndex("protocol"));
                final String daddr = cursor.getString(cursor.getColumnIndex("daddr"));
                final 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 : ""));

                //                    markPro(popup.getMenu().findItem(R.id.menu_allow), ActivityPro.SKU_FILTER);
                //                    markPro(popup.getMenu().findItem(R.id.menu_block), ActivityPro.SKU_FILTER);

                // Whois
                final Intent lookupIP = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://www.tcpiputils.com/whois-lookup/" + daddr));
                if (pm.resolveActivity(lookupIP, 0) == null)
                    popup.getMenu().removeItem(R.id.menu_whois);
                else
                    popup.getMenu().findItem(R.id.menu_whois)
                            .setTitle(context.getString(R.string.title_log_whois, daddr));

                // Lookup port
                final Intent lookupPort = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://www.speedguide.net/port.php?port=" + dport));
                if (dport <= 0 || pm.resolveActivity(lookupPort, 0) == null)
                    popup.getMenu().removeItem(R.id.menu_port);
                else
                    popup.getMenu().findItem(R.id.menu_port)
                            .setTitle(context.getString(R.string.title_log_port, 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_whois:
                            context.startActivity(lookupIP);
                            return true;

                        case R.id.menu_port:
                            context.startActivity(lookupPort);
                            return true;

                        case R.id.menu_allow:
                            if (true) {
                                DatabaseHelper.getInstance(context).setAccess(id, 0);
                                ServiceSinkhole.reload("allow host", context);
                            }
                            //                                        context.startActivity(new Intent(context, ActivityPro.class));
                            return true;

                        case R.id.menu_block:
                            if (true) {
                                DatabaseHelper.getInstance(context).setAccess(id, 1);
                                ServiceSinkhole.reload("block host", context);
                            }
                            //                                        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();
            }
        });

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

    // 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, true);
                    if (!live)
                        notifyDataSetChanged();
                    if (rv != null)
                        rv.scrollToPosition(position);
                }
            });
        }
    });

    // Notify on access
    holder.cbNotify.setEnabled(prefs.getBoolean("notify_access", false) && rule.apply);
    holder.cbNotify.setOnCheckedChangeListener(null);
    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);
        }
    });
}

From source file:im.vector.adapters.VectorMessagesAdapter.java

/**
 * The user taps on the action icon./*from   w  ww. j  a va  2 s .c om*/
 * @param event the selected event.
 * @param textMsg the event text
 * @param anchorView the popup anchor.
 */
@SuppressLint("NewApi")
private void onMessageClick(final Event event, final String textMsg, final View anchorView) {
    final PopupMenu popup = (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            ? new PopupMenu(mContext, anchorView, Gravity.END)
            : new PopupMenu(mContext, anchorView);

    popup.getMenuInflater().inflate(R.menu.vector_room_message_settings, popup.getMenu());

    // force to display the icons
    try {
        Field[] fields = popup.getClass().getDeclaredFields();
        for (Field field : fields) {
            if ("mPopup".equals(field.getName())) {
                field.setAccessible(true);
                Object menuPopupHelper = field.get(popup);
                Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
                Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
                setForceIcons.invoke(menuPopupHelper, true);
                break;
            }
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "onMessageClick : force to display the icons failed " + e.getLocalizedMessage());
    }

    Menu menu = popup.getMenu();

    // hide entries
    for (int i = 0; i < menu.size(); i++) {
        menu.getItem(i).setVisible(false);
    }

    menu.findItem(R.id.ic_action_view_source).setVisible(true);
    menu.findItem(R.id.ic_action_vector_permalink).setVisible(true);

    if (!TextUtils.isEmpty(textMsg)) {
        menu.findItem(R.id.ic_action_vector_copy).setVisible(true);
        menu.findItem(R.id.ic_action_vector_quote).setVisible(true);
    }

    if (event.isUploadingMedias(mMediasCache)) {
        menu.findItem(R.id.ic_action_vector_cancel_upload).setVisible(true);
    }

    if (event.isDownloadingMedias(mMediasCache)) {
        menu.findItem(R.id.ic_action_vector_cancel_download).setVisible(true);
    }

    if (event.canBeResent()) {
        menu.findItem(R.id.ic_action_vector_resend_message).setVisible(true);

        if (event.isUndeliverable()) {
            menu.findItem(R.id.ic_action_vector_redact_message).setVisible(true);
        }
    } else if (event.mSentState == Event.SentState.SENT) {

        // test if the event can be redacted
        boolean canBeRedacted = !mIsPreviewMode;

        if (canBeRedacted) {
            // oneself message -> can redact it
            if (TextUtils.equals(event.sender, mSession.getMyUserId())) {
                canBeRedacted = true;
            } else {
                // need the mininum power level to redact an event
                Room room = mSession.getDataHandler().getRoom(event.roomId);

                if ((null != room) && (null != room.getLiveState().getPowerLevels())) {
                    PowerLevels powerLevels = room.getLiveState().getPowerLevels();
                    canBeRedacted = powerLevels.getUserPowerLevel(mSession.getMyUserId()) >= powerLevels.redact;
                }
            }
        }

        menu.findItem(R.id.ic_action_vector_redact_message).setVisible(canBeRedacted);

        if (Event.EVENT_TYPE_MESSAGE.equals(event.type)) {
            Message message = JsonUtils.toMessage(event.getContentAsJsonObject());

            // share / forward the message
            menu.findItem(R.id.ic_action_vector_share).setVisible(true);
            menu.findItem(R.id.ic_action_vector_forward).setVisible(true);

            // save the media in the downloads directory
            if (Message.MSGTYPE_IMAGE.equals(message.msgtype) || Message.MSGTYPE_VIDEO.equals(message.msgtype)
                    || Message.MSGTYPE_FILE.equals(message.msgtype)) {
                menu.findItem(R.id.ic_action_vector_save).setVisible(true);
            }

            // offer to report a message content
            menu.findItem(R.id.ic_action_vector_report)
                    .setVisible(!mIsPreviewMode && !TextUtils.equals(event.sender, mSession.getMyUserId()));
        }

    }

    // display the menu
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(final MenuItem item) {
            // warn the listener
            if (null != mVectorMessagesAdapterEventsListener) {
                mVectorMessagesAdapterEventsListener.onEventAction(event, textMsg, item.getItemId());
            }

            // disable the selection
            mHighlightedEventId = null;
            notifyDataSetChanged();

            return true;
        }
    });

    popup.show();
}

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);
            // }/*w ww . j  a v  a 2s . 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.example.javier.MaterialDesignApp.PlayerActivity.java

private void configurePopupWithTracks(PopupMenu popup, final OnMenuItemClickListener customActionClickListener,
        final int trackType) {
    if (player == null) {
        return;/*  w  w w . java  2s  .  com*/
    }
    String[] tracks = player.getTracks(trackType);
    if (tracks == null) {
        return;
    }
    popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            return (customActionClickListener != null && customActionClickListener.onMenuItemClick(item))
                    || onTrackItemClick(item, trackType);
        }
    });
    Menu menu = popup.getMenu();
    // ID_OFFSET ensures we avoid clashing with Menu.NONE (which equals 0)
    menu.add(MENU_GROUP_TRACKS, DemoPlayer.DISABLED_TRACK + ID_OFFSET, Menu.NONE, R.string.off);
    if (tracks.length == 1 && TextUtils.isEmpty(tracks[0])) {
        menu.add(MENU_GROUP_TRACKS, DemoPlayer.PRIMARY_TRACK + ID_OFFSET, Menu.NONE, R.string.on);
    } else {
        for (int i = 0; i < tracks.length; i++) {
            menu.add(MENU_GROUP_TRACKS, i + ID_OFFSET, Menu.NONE, tracks[i]);
        }
    }
    menu.setGroupCheckable(MENU_GROUP_TRACKS, true, true);
    menu.findItem(player.getSelectedTrackIndex(trackType) + ID_OFFSET).setChecked(true);
}

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());//from   www  .  ja  va 2  s.co m
    }
    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:com.google.android.apps.santatracker.presentquest.MapsActivity.java

private void initializeDebugUI() {
    if (!isDebug()) {
        return;/* w ww  . j a  v a2s.c  o  m*/
    }

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_debug);
    fab.setVisibility(View.VISIBLE);

    final PopupMenu popup = new PopupMenu(this, fab);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_debug_popup, popup.getMenu());

    // Show the menu when clicked
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popup.show();
        }
    });

    // Handle debug menu clicks
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            final int id = item.getItemId();
            if (id == R.id.item_drop_present) {
                // This should be about 20m away, so within the action radius
                LatLng fuzzyCurrent = FuzzyLocationUtil.fuzz(mCurrentLatLng);
                addPresent(fuzzyCurrent, true);
            }

            if (id == R.id.item_delete_present) {
                if (!mPresents.isEmpty()) {
                    deletePresent(mPresents.get(0));
                    drawMarkers();
                    drawMarkerLabels();
                }
            }

            if (id == R.id.item_play_jetpack) {
                showPlayJetpackDialog(null);
            }

            if (id == R.id.item_play_present_toss) {
                showPlayWorkshopDialog();
            }

            if (id == R.id.item_collect_20) {
                onPresentsCollected(20);
            }

            if (id == R.id.item_return) {
                onPresentsReturned(3);
            }

            if (id == R.id.item_downlevel) {
                mUser.downlevel();
                setUserProgress();
            }

            if (id == R.id.reset_prefs) {
                // Reset prefs
                mPreferences.resetAll();

                // Reset user
                mUser.presentsCollected = 0;
                mUser.presentsReturned = 0;
                mUser.save();

                // Delete all workshops
                Workshop.deleteAll(Workshop.class);

                // Delete all places and presents
                Present.deleteAll(Present.class);
                Place.deleteAll(Place.class);

                // Restart the activity
                recreate();
            }

            return true;
        }
    });
}

From source file:com.simadanesh.isatis.ScreenSlideActivity.java

private void showfilterMenu() {
    View p = findViewById(R.id.anchorer);
    progressbar.setVisibility(View.GONE);

    final PopupMenu popup = new PopupMenu(this, p);
    //Inflating the Popup using xml file  
    popup.getMenuInflater().inflate(R.menu.filter, popup.getMenu());
    for (int i = 0; i < popup.getMenu().size(); i++) {
        MenuItem item = popup.getMenu().getItem(i);
        String countstr = "0";
        switch (item.getItemId()) {
        case R.id.filter_no_filter:
            countstr = countFilterNofilter();
            break;
        case R.id.filter_no_data:
            countstr = countFilterNodata();
            break;
        case R.id.filter_valid_reading:
            countstr = countFilterValid();
            break;
        case R.id.filter_hilow:
            countstr = countFilterHilow();
            break;
        case R.id.filter_maneh:
            countstr = countFilterManeh();

            break;
        case R.id.filter_inspection:
            countstr = countFilterInspection();
            break;
        case R.id.filter_register_field:
            countstr = countFilterRegisterField();
            break;
        case R.id.filter_not_register_field:
            countstr = countFilterRegisterNotField();
            break;

        }/*from  w  w  w . j  a  va  2s. c  o  m*/
        item.setTitle(item.getTitle() + "\t" + countstr);
    }

    //registering popup with OnMenuItemClickListener  
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.filter_no_filter:
                filterNofilter();
                break;
            case R.id.filter_no_data:
                filterNodata();
                break;
            case R.id.filter_valid_reading:
                filterValid();
                break;
            case R.id.filter_hilow:
                filterHilow();
                break;
            case R.id.filter_maneh:
                //filterManeh();
                showManehMenu();
                break;
            case R.id.filter_inspection:
                filterInspection();
                break;
            case R.id.filter_register_field:
                filterRegisterField();
                break;
            case R.id.filter_not_register_field:
                filterRegisterNotField();
                break;

            }
            return true;
        }
    });

    final Handler handler1 = new Handler();
    progressbar.setVisibility(View.GONE);
    //android.R.attr.progressBarStyleLarge
    handler1.postDelayed(new Runnable() {
        @Override
        public void run() {
            popup.show();
        }
    }, 100);
}

From source file:im.neon.adapters.VectorMessagesAdapter.java

/**
 * The user taps on the action icon.//from  w ww .j a  v a  2s .c  o m
 * @param event the selected event.
 * @param textMsg the event text
 * @param anchorView the popup anchor.
 */
@SuppressLint("NewApi")
private void onMessageClick(final Event event, final String textMsg, final View anchorView) {
    final PopupMenu popup = (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            ? new PopupMenu(mContext, anchorView, Gravity.END)
            : new PopupMenu(mContext, anchorView);

    popup.getMenuInflater().inflate(R.menu.vector_room_message_settings, popup.getMenu());

    // force to display the icons
    try {
        Field[] fields = popup.getClass().getDeclaredFields();
        for (Field field : fields) {
            if ("mPopup".equals(field.getName())) {
                field.setAccessible(true);
                Object menuPopupHelper = field.get(popup);
                Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
                Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
                setForceIcons.invoke(menuPopupHelper, true);
                break;
            }
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "onMessageClick : force to display the icons failed " + e.getLocalizedMessage());
    }

    Menu menu = popup.getMenu();

    // hide entries
    for (int i = 0; i < menu.size(); i++) {
        menu.getItem(i).setVisible(false);
    }

    menu.findItem(R.id.ic_action_view_source).setVisible(true);
    menu.findItem(R.id.ic_action_vector_permalink).setVisible(true);

    if (!TextUtils.isEmpty(textMsg)) {
        menu.findItem(R.id.ic_action_vector_copy).setVisible(true);
        menu.findItem(R.id.ic_action_vector_quote).setVisible(true);
    }

    if (event.isUploadingMedias(mMediasCache)) {
        menu.findItem(R.id.ic_action_vector_cancel_upload).setVisible(true);
    }

    if (event.isDownloadingMedias(mMediasCache)) {
        menu.findItem(R.id.ic_action_vector_cancel_download).setVisible(true);
    }

    if (event.canBeResent()) {
        menu.findItem(R.id.ic_action_vector_resend_message).setVisible(true);

        if (event.isUndeliverable() || event.isUnkownDevice()) {
            menu.findItem(R.id.ic_action_vector_redact_message).setVisible(true);
        }
    } else if (event.mSentState == Event.SentState.SENT) {

        // test if the event can be redacted
        boolean canBeRedacted = !mIsPreviewMode
                && !TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE_ENCRYPTION);

        if (canBeRedacted) {
            // oneself message -> can redact it
            if (TextUtils.equals(event.sender, mSession.getMyUserId())) {
                canBeRedacted = true;
            } else {
                // need the mininum power level to redact an event
                Room room = mSession.getDataHandler().getRoom(event.roomId);

                if ((null != room) && (null != room.getLiveState().getPowerLevels())) {
                    PowerLevels powerLevels = room.getLiveState().getPowerLevels();
                    canBeRedacted = powerLevels.getUserPowerLevel(mSession.getMyUserId()) >= powerLevels.redact;
                }
            }
        }

        menu.findItem(R.id.ic_action_vector_redact_message).setVisible(canBeRedacted);

        if (Event.EVENT_TYPE_MESSAGE.equals(event.getType())) {
            Message message = JsonUtils.toMessage(event.getContentAsJsonObject());

            // share / forward the message
            menu.findItem(R.id.ic_action_vector_share).setVisible(true);
            menu.findItem(R.id.ic_action_vector_forward).setVisible(true);

            // save the media in the downloads directory
            if (Message.MSGTYPE_IMAGE.equals(message.msgtype) || Message.MSGTYPE_VIDEO.equals(message.msgtype)
                    || Message.MSGTYPE_FILE.equals(message.msgtype)) {
                menu.findItem(R.id.ic_action_vector_save).setVisible(true);
            }

            // offer to report a message content
            menu.findItem(R.id.ic_action_vector_report)
                    .setVisible(!mIsPreviewMode && !TextUtils.equals(event.sender, mSession.getMyUserId()));
        }

    }

    // e2e
    menu.findItem(R.id.ic_action_device_verification).setVisible(mE2eIconByEventId.containsKey(event.eventId));

    // display the menu
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(final MenuItem item) {
            // warn the listener
            if (null != mVectorMessagesAdapterEventsListener) {
                mVectorMessagesAdapterEventsListener.onEventAction(event, textMsg, item.getItemId());
            }

            // disable the selection
            mHighlightedEventId = null;
            notifyDataSetChanged();

            return true;
        }
    });

    // fix an issue reported by GA
    try {
        popup.show();
    } catch (Exception e) {
        Log.e(LOG_TAG, " popup.show failed " + e.getMessage());
    }
}