Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / appmenu / AppMenuButtonHelper.java
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.
4
5 package org.chromium.chrome.browser.appmenu;
6
7 import android.view.MotionEvent;
8 import android.view.View;
9 import android.view.View.OnTouchListener;
10
11 import org.chromium.chrome.browser.UmaBridge;
12
13 /**
14  * A helper class for a menu button to decide when to show the app menu and forward touch
15  * events.
16  *
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.
19  */
20 public class AppMenuButtonHelper implements OnTouchListener {
21     private final View mMenuButton;
22     private final AppMenuHandler mMenuHandler;
23     private Runnable mOnAppMenuShownListener;
24
25     /**
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.
28      */
29     public AppMenuButtonHelper(View menuButton, AppMenuHandler menuHandler) {
30         mMenuButton = menuButton;
31         mMenuHandler = menuHandler;
32     }
33
34     /**
35      * @param onAppMenuShownListener This is called when the app menu is shown by this class.
36      */
37     public void setOnAppMenuShownListener(Runnable onAppMenuShownListener) {
38         mOnAppMenuShownListener = onAppMenuShownListener;
39     }
40
41     /**
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.
45      */
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);
53
54             if (mOnAppMenuShownListener != null) {
55                 mOnAppMenuShownListener.run();
56             }
57             return true;
58         }
59         return false;
60     }
61
62     /**
63      * @return Whether app menu is active. That is, AppMenu is showing or menu button is consuming
64      *         touch events to prepare AppMenu showing.
65      */
66     public boolean isAppMenuActive() {
67         return mMenuButton.isPressed() || mMenuHandler.isAppMenuShowing();
68     }
69
70     /**
71      * Handle the key press event on a menu button.
72      * @return Whether the app menu was shown as a result of this action.
73      */
74     public boolean onEnterKeyPress() {
75         return showAppMenu(false);
76     }
77
78     @Override
79
80     public boolean onTouch(View view, MotionEvent event) {
81         boolean isTouchEventConsumed = false;
82
83         switch (event.getActionMasked()) {
84             case MotionEvent.ACTION_DOWN:
85                 isTouchEventConsumed |= true;
86                 mMenuButton.setPressed(true);
87                 showAppMenu(true);
88                 break;
89             case MotionEvent.ACTION_UP:
90             case MotionEvent.ACTION_CANCEL:
91                 isTouchEventConsumed |= true;
92                 mMenuButton.setPressed(false);
93                 break;
94             default:
95         }
96
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);
103         }
104         return isTouchEventConsumed;
105     }
106 }