Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 29632521 authored by Alan Viverette's avatar Alan Viverette
Browse files

Add style support to PopupMenu, clean up constructor javadoc

BUG: 18002523
Change-Id: Ice492686b814460248ccbe9727c64dd002e7ed7a
parent 3d5ecf13
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -38126,6 +38126,7 @@ package android.widget {
  public class PopupMenu {
  public class PopupMenu {
    ctor public PopupMenu(android.content.Context, android.view.View);
    ctor public PopupMenu(android.content.Context, android.view.View);
    ctor public PopupMenu(android.content.Context, android.view.View, int);
    ctor public PopupMenu(android.content.Context, android.view.View, int);
    ctor public PopupMenu(android.content.Context, android.view.View, int, int, int);
    method public void dismiss();
    method public void dismiss();
    method public android.view.View.OnTouchListener getDragToOpenListener();
    method public android.view.View.OnTouchListener getDragToOpenListener();
    method public android.view.Menu getMenu();
    method public android.view.Menu getMenu();
+42 −15
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package android.widget;
package android.widget;


import com.android.internal.R;
import com.android.internal.view.menu.MenuBuilder;
import com.android.internal.view.menu.MenuBuilder;
import com.android.internal.view.menu.MenuPopupHelper;
import com.android.internal.view.menu.MenuPopupHelper;
import com.android.internal.view.menu.MenuPresenter;
import com.android.internal.view.menu.MenuPresenter;
@@ -37,10 +38,11 @@ import android.widget.ListPopupWindow.ForwardingListener;
 * of the popup will dismiss it.
 * of the popup will dismiss it.
 */
 */
public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
    private Context mContext;
    private final Context mContext;
    private MenuBuilder mMenu;
    private final MenuBuilder mMenu;
    private View mAnchor;
    private final View mAnchor;
    private MenuPopupHelper mPopup;
    private final MenuPopupHelper mPopup;

    private OnMenuItemClickListener mMenuItemClickListener;
    private OnMenuItemClickListener mMenuItemClickListener;
    private OnDismissListener mDismissListener;
    private OnDismissListener mDismissListener;
    private OnTouchListener mDragListener;
    private OnTouchListener mDragListener;
@@ -58,31 +60,56 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
    }
    }


    /**
    /**
     * Construct a new PopupMenu.
     * Constructor to create a new popup menu with an anchor view.
     *
     *
     * @param context Context for the PopupMenu.
     * @param context Context the popup menu is running in, through which it
     * @param anchor Anchor view for this popup. The popup will appear below the anchor if there
     *        can access the current theme, resources, etc.
     *               is room, or above it if there is not.
     * @param anchor Anchor view for this popup. The popup will appear below
     *        the anchor if there is room, or above it if there is not.
     */
     */
    public PopupMenu(Context context, View anchor) {
    public PopupMenu(Context context, View anchor) {
        this(context, anchor, Gravity.NO_GRAVITY);
        this(context, anchor, Gravity.NO_GRAVITY);
    }
    }


    /**
    /**
     * Construct a new PopupMenu.
     * Constructor to create a new popup menu with an anchor view and alignment
     * gravity.
     *
     *
     * @param context Context for the PopupMenu.
     * @param context Context the popup menu is running in, through which it
     * @param anchor Anchor view for this popup. The popup will appear below the anchor if there
     *        can access the current theme, resources, etc.
     *               is room, or above it if there is not.
     * @param anchor Anchor view for this popup. The popup will appear below
     * @param gravity The {@link Gravity} value for aligning the popup with its anchor
     *        the anchor if there is room, or above it if there is not.
     * @param gravity The {@link Gravity} value for aligning the popup with its
     *        anchor.
     */
     */
    public PopupMenu(Context context, View anchor, int gravity) {
    public PopupMenu(Context context, View anchor, int gravity) {
        // TODO Theme?
        this(context, anchor, gravity, R.attr.popupMenuStyle, 0);
    }

    /**
     * Constructor a create a new popup menu with a specific style.
     *
     * @param context Context the popup menu is running in, through which it
     *        can access the current theme, resources, etc.
     * @param anchor Anchor view for this popup. The popup will appear below
     *        the anchor if there is room, or above it if there is not.
     * @param gravity The {@link Gravity} value for aligning the popup with its
     *        anchor.
     * @param popupStyleAttr An attribute in the current theme that contains a
     *        reference to a style resource that supplies default values for
     *        the popup window. Can be 0 to not look for defaults.
     * @param popupStyleRes A resource identifier of a style resource that
     *        supplies default values for the popup window, used only if
     *        popupStyleAttr is 0 or can not be found in the theme. Can be 0
     *        to not look for defaults.
     */
    public PopupMenu(Context context, View anchor, int gravity, int popupStyleAttr,
            int popupStyleRes) {
        mContext = context;
        mContext = context;
        mMenu = new MenuBuilder(context);
        mMenu = new MenuBuilder(context);
        mMenu.setCallback(this);
        mMenu.setCallback(this);
        mAnchor = anchor;
        mAnchor = anchor;
        mPopup = new MenuPopupHelper(context, mMenu, anchor);
        mPopup = new MenuPopupHelper(context, mMenu, anchor, false, popupStyleAttr, popupStyleRes);
        mPopup.setGravity(gravity);
        mPopup.setGravity(gravity);
        mPopup.setCallback(this);
        mPopup.setCallback(this);
    }
    }
+10 −3
Original line number Original line Diff line number Diff line
@@ -54,6 +54,7 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On
    private final boolean mOverflowOnly;
    private final boolean mOverflowOnly;
    private final int mPopupMaxWidth;
    private final int mPopupMaxWidth;
    private final int mPopupStyleAttr;
    private final int mPopupStyleAttr;
    private final int mPopupStyleRes;


    private View mAnchorView;
    private View mAnchorView;
    private ListPopupWindow mPopup;
    private ListPopupWindow mPopup;
@@ -73,21 +74,27 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On
    private int mDropDownGravity = Gravity.NO_GRAVITY;
    private int mDropDownGravity = Gravity.NO_GRAVITY;


    public MenuPopupHelper(Context context, MenuBuilder menu) {
    public MenuPopupHelper(Context context, MenuBuilder menu) {
        this(context, menu, null, false, com.android.internal.R.attr.popupMenuStyle);
        this(context, menu, null, false, com.android.internal.R.attr.popupMenuStyle, 0);
    }
    }


    public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) {
    public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) {
        this(context, menu, anchorView, false, com.android.internal.R.attr.popupMenuStyle);
        this(context, menu, anchorView, false, com.android.internal.R.attr.popupMenuStyle, 0);
    }
    }


    public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView,
    public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView,
            boolean overflowOnly, int popupStyleAttr) {
            boolean overflowOnly, int popupStyleAttr) {
        this(context, menu, anchorView, overflowOnly, popupStyleAttr, 0);
    }

    public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView,
            boolean overflowOnly, int popupStyleAttr, int popupStyleRes) {
        mContext = context;
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mInflater = LayoutInflater.from(context);
        mMenu = menu;
        mMenu = menu;
        mAdapter = new MenuAdapter(mMenu);
        mAdapter = new MenuAdapter(mMenu);
        mOverflowOnly = overflowOnly;
        mOverflowOnly = overflowOnly;
        mPopupStyleAttr = popupStyleAttr;
        mPopupStyleAttr = popupStyleAttr;
        mPopupStyleRes = popupStyleRes;


        final Resources res = context.getResources();
        final Resources res = context.getResources();
        mPopupMaxWidth = Math.max(res.getDisplayMetrics().widthPixels / 2,
        mPopupMaxWidth = Math.max(res.getDisplayMetrics().widthPixels / 2,
@@ -122,7 +129,7 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On
    }
    }


    public boolean tryShow() {
    public boolean tryShow() {
        mPopup = new ListPopupWindow(mContext, null, mPopupStyleAttr);
        mPopup = new ListPopupWindow(mContext, null, mPopupStyleAttr, mPopupStyleRes);
        mPopup.setOnDismissListener(this);
        mPopup.setOnDismissListener(this);
        mPopup.setOnItemClickListener(this);
        mPopup.setOnItemClickListener(this);
        mPopup.setAdapter(mAdapter);
        mPopup.setAdapter(mAdapter);