1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser.appmenu;
7 import android.view.MotionEvent;
8 import android.view.View;
9 import android.view.View.OnTouchListener;
11 import org.chromium.chrome.browser.UmaBridge;
14 * A helper class for a menu button to decide when to show the app menu and forward touch
17 * Simply construct this class and pass the class instance to a menu button as TouchListener.
18 * Then this class will handle everything regarding showing app menu for you.
20 public class AppMenuButtonHelper implements OnTouchListener {
21 private final View mMenuButton;
22 private final AppMenuHandler mMenuHandler;
23 private Runnable mOnAppMenuShownListener;
26 * @param menuButton Menu button instance that will trigger the app menu.
27 * @param menuHandler MenuHandler implementation that can show and get the app menu.
29 public AppMenuButtonHelper(View menuButton, AppMenuHandler menuHandler) {
30 mMenuButton = menuButton;
31 mMenuHandler = menuHandler;
35 * @param onAppMenuShownListener This is called when the app menu is shown by this class.
37 public void setOnAppMenuShownListener(Runnable onAppMenuShownListener) {
38 mOnAppMenuShownListener = onAppMenuShownListener;
42 * Shows the app menu if it is not already shown.
43 * @param startDragging Whether dragging is started.
44 * @return Whether or not if the app menu is successfully shown.
46 private boolean showAppMenu(boolean startDragging) {
47 if (!mMenuHandler.isAppMenuShowing() &&
48 mMenuHandler.showAppMenu(mMenuButton, false, startDragging)) {
49 // Initial start dragging can be canceled in case if it was just single tap.
50 // So we only record non-dragging here, and will deal with those dragging cases in
51 // AppMenuDragHelper class.
52 if (!startDragging) UmaBridge.usingMenu(false, false);
54 if (mOnAppMenuShownListener != null) {
55 mOnAppMenuShownListener.run();
63 * @return Whether app menu is active. That is, AppMenu is showing or menu button is consuming
64 * touch events to prepare AppMenu showing.
66 public boolean isAppMenuActive() {
67 return mMenuButton.isPressed() || mMenuHandler.isAppMenuShowing();
71 * Handle the key press event on a menu button.
72 * @return Whether the app menu was shown as a result of this action.
74 public boolean onEnterKeyPress() {
75 return showAppMenu(false);
80 public boolean onTouch(View view, MotionEvent event) {
81 boolean isTouchEventConsumed = false;
83 switch (event.getActionMasked()) {
84 case MotionEvent.ACTION_DOWN:
85 isTouchEventConsumed |= true;
86 mMenuButton.setPressed(true);
89 case MotionEvent.ACTION_UP:
90 case MotionEvent.ACTION_CANCEL:
91 isTouchEventConsumed |= true;
92 mMenuButton.setPressed(false);
97 // If user starts to drag on this menu button, ACTION_DOWN and all the subsequent touch
98 // events are received here. We need to forward this event to the app menu to handle
99 // dragging correctly.
100 AppMenuDragHelper dragHelper = mMenuHandler.getAppMenuDragHelper();
101 if (dragHelper != null) {
102 isTouchEventConsumed |= dragHelper.handleDragging(event);
104 return isTouchEventConsumed;