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

Commit 1ca1860f authored by Alan Viverette's avatar Alan Viverette Committed by Android (Google) Code Review
Browse files

Merge "Clean up PopupMenu"

parents 1a6436c9 73238108
Loading
Loading
Loading
Loading
+71 −76
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import com.android.internal.view.menu.MenuBuilder;
import com.android.internal.view.menu.MenuPopupHelper;
import com.android.internal.view.menu.MenuPresenter;
import com.android.internal.view.menu.ShowableListMenu;
import com.android.internal.view.menu.SubMenuBuilder;

import android.annotation.MenuRes;
import android.content.Context;
@@ -33,34 +32,22 @@ import android.view.View;
import android.view.View.OnTouchListener;

/**
 * A PopupMenu displays a {@link Menu} in a modal popup window anchored to a {@link View}.
 * The popup will appear below the anchor view if there is room, or above it if there is not.
 * If the IME is visible the popup will not overlap it until it is touched. Touching outside
 * of the popup will dismiss it.
 * A PopupMenu displays a {@link Menu} in a modal popup window anchored to a
 * {@link View}. The popup will appear below the anchor view if there is room,
 * or above it if there is not. If the IME is visible the popup will not
 * overlap it until it is touched. Touching outside of the popup will dismiss
 * it.
 */
public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
public class PopupMenu {
    private final Context mContext;
    private final MenuBuilder mMenu;
    private final View mAnchor;
    private final MenuPopupHelper mPopup;
    private final boolean mShowCascadingMenus;

    private OnMenuItemClickListener mMenuItemClickListener;
    private OnDismissListener mDismissListener;
    private OnTouchListener mDragListener;

    /**
     * Callback interface used to notify the application that the menu has closed.
     */
    public interface OnDismissListener {
        /**
         * Called when the associated menu has been dismissed.
         *
         * @param menu The PopupMenu that was dismissed.
         */
        public void onDismiss(PopupMenu menu);
    }

    /**
     * Constructor to create a new popup menu with an anchor view.
     *
@@ -108,14 +95,40 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
    public PopupMenu(Context context, View anchor, int gravity, int popupStyleAttr,
            int popupStyleRes) {
        mContext = context;
        mShowCascadingMenus = context.getResources().getBoolean(
                com.android.internal.R.bool.config_enableCascadingSubmenus);
        mMenu = new MenuBuilder(context);
        mMenu.setCallback(this);
        mAnchor = anchor;

        mMenu = new MenuBuilder(context);
        mMenu.setCallback(new MenuBuilder.Callback() {
            @Override
            public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
                if (mMenuItemClickListener != null) {
                    return mMenuItemClickListener.onMenuItemClick(item);
                }
                return false;
            }

            @Override
            public void onMenuModeChange(MenuBuilder menu) {
            }
        });

        mPopup = new MenuPopupHelper(context, mMenu, anchor, false, popupStyleAttr, popupStyleRes);
        mPopup.setGravity(gravity);
        mPopup.setCallback(this);
        mPopup.setCallback(new MenuPresenter.Callback() {
            @Override
            public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
                if (mDismissListener != null) {
                    mDismissListener.onDismiss(PopupMenu.this);
                }
            }

            @Override
            public boolean onOpenSubMenu(MenuBuilder subMenu) {
                // The menu presenter will handle opening the submenu itself.
                // Nothing to do here.
                return false;
            }
        });
    }

    /**
@@ -125,7 +138,6 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
     * the next time the popup is shown.
     *
     * @param gravity the gravity used to align the popup window
     *
     * @see #getGravity()
     */
    public void setGravity(int gravity) {
@@ -134,7 +146,6 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {

    /**
     * @return the gravity used to align the popup window to its anchor view
     *
     * @see #setGravity(int)
     */
    public int getGravity() {
@@ -146,8 +157,8 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
     * to implement drag-to-open behavior.
     * <p>
     * When the listener is set on a view, touching that view and dragging
     * outside of its bounds will open the popup window. Lifting will select the
     * currently touched list item.
     * outside of its bounds will open the popup window. Lifting will select
     * the currently touched list item.
     * <p>
     * Example usage:
     * <pre>
@@ -184,9 +195,10 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
    }

    /**
     * @return the {@link Menu} associated with this popup. Populate the returned Menu with
     * items before calling {@link #show()}.
     * Returns the {@link Menu} associated with this popup. Populate the
     * returned Menu with items before calling {@link #show()}.
     *
     * @return the {@link Menu} associated with this popup
     * @see #show()
     * @see #getMenuInflater()
     */
@@ -195,9 +207,8 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
    }

    /**
     * @return a {@link MenuInflater} that can be used to inflate menu items from XML into the
     * menu returned by {@link #getMenu()}.
     *
     * @return a {@link MenuInflater} that can be used to inflate menu items
     *         from XML into the menu returned by {@link #getMenu()}
     * @see #getMenu()
     */
    public MenuInflater getMenuInflater() {
@@ -205,8 +216,9 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
    }

    /**
     * Inflate a menu resource into this PopupMenu. This is equivalent to calling
     * popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu()).
     * Inflate a menu resource into this PopupMenu. This is equivalent to
     * calling {@code popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu())}.
     *
     * @param menuRes Menu resource to inflate
     */
    public void inflate(@MenuRes int menuRes) {
@@ -215,6 +227,7 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {

    /**
     * Show the menu popup anchored to the view specified during construction.
     *
     * @see #dismiss()
     */
    public void show() {
@@ -223,6 +236,7 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {

    /**
     * Dismiss the menu popup.
     *
     * @see #show()
     */
    public void dismiss() {
@@ -230,68 +244,49 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
    }

    /**
     * Set a listener that will be notified when the user selects an item from the menu.
     * Sets a listener that will be notified when the user selects an item from
     * the menu.
     *
     * @param listener Listener to notify
     * @param listener the listener to notify
     */
    public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
        mMenuItemClickListener = listener;
    }

    /**
     * Set a listener that will be notified when this menu is dismissed.
     * Sets a listener that will be notified when this menu is dismissed.
     *
     * @param listener Listener to notify
     * @param listener the listener to notify
     */
    public void setOnDismissListener(OnDismissListener listener) {
        mDismissListener = listener;
    }

    /**
     * @hide
     */
    public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
        if (mMenuItemClickListener != null) {
            return mMenuItemClickListener.onMenuItemClick(item);
        }
        return false;
    }

    /**
     * @hide
     */
    public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
        if (mDismissListener != null) {
            mDismissListener.onDismiss(this);
        }
    }

    /**
     * @hide
     * Interface responsible for receiving menu item click events if the items
     * themselves do not have individual item click listeners.
     */
    public boolean onOpenSubMenu(MenuBuilder subMenu) {
        // The menu presenter will handle opening the submenu itself. Nothing to do here.
        return false;
    }

    public interface OnMenuItemClickListener {
        /**
     * @hide
         * This method will be invoked when a menu item is clicked if the item
         * itself did not already handle the event.
         *
         * @param item the menu item that was clicked
         * @return {@code true} if the event was handled, {@code false}
         *         otherwise
         */
    public void onMenuModeChange(MenuBuilder menu) {
        boolean onMenuItemClick(MenuItem item);
    }

    /**
     * Interface responsible for receiving menu item click events if the items themselves
     * do not have individual item click listeners.
     * Callback interface used to notify the application that the menu has closed.
     */
    public interface OnMenuItemClickListener {
    public interface OnDismissListener {
        /**
         * This method will be invoked when a menu item is clicked if the item itself did
         * not already handle the event.
         * Called when the associated menu has been dismissed.
         *
         * @param item {@link MenuItem} that was clicked
         * @return <code>true</code> if the event was handled, <code>false</code> otherwise.
         * @param menu the popup menu that was dismissed
         */
        public boolean onMenuItemClick(MenuItem item);
        void onDismiss(PopupMenu menu);
    }
}