OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.appmenu; | 5 package org.chromium.chrome.browser.appmenu; |
6 | 6 |
7 import android.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
8 import android.app.Activity; | 8 import android.app.Activity; |
9 import android.content.res.TypedArray; | 9 import android.content.res.TypedArray; |
10 import android.graphics.Point; | 10 import android.graphics.Point; |
11 import android.graphics.Rect; | 11 import android.graphics.Rect; |
12 import android.graphics.drawable.Drawable; | 12 import android.graphics.drawable.Drawable; |
| 13 import android.support.annotation.IdRes; |
13 import android.view.ContextThemeWrapper; | 14 import android.view.ContextThemeWrapper; |
14 import android.view.Menu; | 15 import android.view.Menu; |
15 import android.view.MenuItem; | 16 import android.view.MenuItem; |
16 import android.view.View; | 17 import android.view.View; |
17 import android.widget.PopupMenu; | 18 import android.widget.PopupMenu; |
18 | 19 |
19 import org.chromium.base.metrics.RecordUserAction; | 20 import org.chromium.base.metrics.RecordUserAction; |
20 import org.chromium.chrome.R; | 21 import org.chromium.chrome.R; |
21 | 22 |
22 import java.util.ArrayList; | 23 import java.util.ArrayList; |
23 | 24 |
24 /** | 25 /** |
25 * Object responsible for handling the creation, showing, hiding of the AppMenu
and notifying the | 26 * Object responsible for handling the creation, showing, hiding of the AppMenu
and notifying the |
26 * AppMenuObservers about these actions. | 27 * AppMenuObservers about these actions. |
27 */ | 28 */ |
28 public class AppMenuHandler { | 29 public class AppMenuHandler { |
29 private AppMenu mAppMenu; | 30 private AppMenu mAppMenu; |
30 private AppMenuDragHelper mAppMenuDragHelper; | 31 private AppMenuDragHelper mAppMenuDragHelper; |
31 private Menu mMenu; | 32 private Menu mMenu; |
32 private final ArrayList<AppMenuObserver> mObservers; | 33 private final ArrayList<AppMenuObserver> mObservers; |
33 private final int mMenuResourceId; | 34 private final int mMenuResourceId; |
34 private final View mHardwareButtonMenuAnchor; | 35 private final View mHardwareButtonMenuAnchor; |
35 | 36 |
36 private final AppMenuPropertiesDelegate mDelegate; | 37 private final AppMenuPropertiesDelegate mDelegate; |
37 private final Activity mActivity; | 38 private final Activity mActivity; |
38 | 39 |
39 /** | 40 /** |
| 41 * The resource id of the menu item to highlight when the menu next opens. A
value of 0 means |
| 42 * no item will be highlighted. This value will be cleared after the menu i
s opened. |
| 43 */ |
| 44 @IdRes |
| 45 private int mHighlightMenuId; |
| 46 |
| 47 /** |
40 * Constructs an AppMenuHandler object. | 48 * Constructs an AppMenuHandler object. |
41 * @param activity Activity that is using the AppMenu. | 49 * @param activity Activity that is using the AppMenu. |
42 * @param delegate Delegate used to check the desired AppMenu properties on
show. | 50 * @param delegate Delegate used to check the desired AppMenu properties on
show. |
43 * @param menuResourceId Resource Id that should be used as the source for t
he menu items. | 51 * @param menuResourceId Resource Id that should be used as the source for t
he menu items. |
44 * It is assumed to have back_menu_id, forward_menu_id, bookmark_
this_page_id. | 52 * It is assumed to have back_menu_id, forward_menu_id, bookmark_
this_page_id. |
45 */ | 53 */ |
46 public AppMenuHandler(Activity activity, AppMenuPropertiesDelegate delegate, | 54 public AppMenuHandler(Activity activity, AppMenuPropertiesDelegate delegate, |
47 int menuResourceId) { | 55 int menuResourceId) { |
48 mActivity = activity; | 56 mActivity = activity; |
49 mDelegate = delegate; | 57 mDelegate = delegate; |
50 mObservers = new ArrayList<AppMenuObserver>(); | 58 mObservers = new ArrayList<AppMenuObserver>(); |
51 mMenuResourceId = menuResourceId; | 59 mMenuResourceId = menuResourceId; |
52 mHardwareButtonMenuAnchor = activity.findViewById(R.id.menu_anchor_stub)
; | 60 mHardwareButtonMenuAnchor = activity.findViewById(R.id.menu_anchor_stub)
; |
53 assert mHardwareButtonMenuAnchor != null | 61 assert mHardwareButtonMenuAnchor != null |
54 : "Using AppMenu requires to have menu_anchor_stub view"; | 62 : "Using AppMenu requires to have menu_anchor_stub view"; |
55 } | 63 } |
56 | 64 |
57 /** | 65 /** |
58 * Notifies the menu that the contents of the menu item specified by {@code
menuRowId} have | 66 * Notifies the menu that the contents of the menu item specified by {@code
menuRowId} have |
59 * changed. This should be called if icons, titles, etc. are changing for a
particular menu | 67 * changed. This should be called if icons, titles, etc. are changing for a
particular menu |
60 * item while the menu is open. | 68 * item while the menu is open. |
61 * @param menuRowId The id of the menu item to change. This must be a row i
d and not a child | 69 * @param menuRowId The id of the menu item to change. This must be a row i
d and not a child |
62 * id. | 70 * id. |
63 */ | 71 */ |
64 public void menuItemContentChanged(int menuRowId) { | 72 public void menuItemContentChanged(int menuRowId) { |
65 if (mAppMenu != null) mAppMenu.menuItemContentChanged(menuRowId); | 73 if (mAppMenu != null) mAppMenu.menuItemContentChanged(menuRowId); |
66 } | 74 } |
67 | 75 |
68 /** | 76 /** |
| 77 * Calls attention to this menu and a particular item in it. The menu will
only stay |
| 78 * highlighted for one menu usage. After that the highlight will be cleared
. |
| 79 * @param highlightItemId The id of a menu item to highlight or {@code 0} to
turn off the |
| 80 * highlight. |
| 81 */ |
| 82 public void setMenuHighlight(@IdRes int highlightItemId) { |
| 83 if (mHighlightMenuId == highlightItemId) return; |
| 84 mHighlightMenuId = highlightItemId; |
| 85 boolean highlighting = mHighlightMenuId != 0; |
| 86 for (AppMenuObserver observer : mObservers) observer.onMenuHighlightChan
ged(highlighting); |
| 87 } |
| 88 |
| 89 /** |
69 * Show the app menu. | 90 * Show the app menu. |
70 * @param anchorView Anchor view (usually a menu button) to be used
for the popup, if | 91 * @param anchorView Anchor view (usually a menu button) to be used for t
he popup, if null is |
71 * null is passed then hardware menu button anchor
will be used. | 92 * passed then hardware menu button anchor will be used
. |
72 * @param startDragging Whether dragging is started. For example, if th
e app menu is | 93 * @param startDragging Whether dragging is started. For example, if the app
menu is showed by |
73 * showed by tapping on a button, this should be f
alse. If it is | 94 * tapping on a button, this should be false. If it is
showed by start |
74 * showed by start dragging down on the menu butto
n, this should | 95 * dragging down on the menu button, this should be tru
e. Note that if |
75 * be true. Note that if anchorView is null, this
must | 96 * anchorView is null, this must be false since we no l
onger support |
76 * be false since we no longer support hardware me
nu button | 97 * hardware menu button dragging. |
77 * dragging. | 98 * @return True, if the menu is shown, false, if menu is not sh
own, example |
78 * @return True, if the menu is shown, false, if menu is not shown, example
reasons: | 99 * reasons: the menu is not yet available to be shown,
or the menu is |
79 * the menu is not yet available to be shown, or the menu is already
showing. | 100 * already showing. |
80 */ | 101 */ |
81 // TODO(crbug.com/635567): Fix this properly. | 102 // TODO(crbug.com/635567): Fix this properly. |
82 @SuppressLint("ResourceType") | 103 @SuppressLint("ResourceType") |
83 public boolean showAppMenu(View anchorView, boolean startDragging) { | 104 public boolean showAppMenu(View anchorView, boolean startDragging) { |
84 if (!mDelegate.shouldShowAppMenu() || isAppMenuShowing()) return false; | 105 if (!mDelegate.shouldShowAppMenu() || isAppMenuShowing()) return false; |
85 boolean isByPermanentButton = false; | 106 boolean isByPermanentButton = false; |
86 | 107 |
87 int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotat
ion(); | 108 int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotat
ion(); |
88 if (anchorView == null) { | 109 if (anchorView == null) { |
89 // This fixes the bug where the bottom of the menu starts at the top
of | 110 // This fixes the bug where the bottom of the menu starts at the top
of |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 appRect.bottom = mActivity.getWindow().getDecorView().getHeight(); | 156 appRect.bottom = mActivity.getWindow().getDecorView().getHeight(); |
136 } | 157 } |
137 Point pt = new Point(); | 158 Point pt = new Point(); |
138 mActivity.getWindowManager().getDefaultDisplay().getSize(pt); | 159 mActivity.getWindowManager().getDefaultDisplay().getSize(pt); |
139 | 160 |
140 int footerResourceId = 0; | 161 int footerResourceId = 0; |
141 if (mDelegate.shouldShowFooter(appRect.height())) { | 162 if (mDelegate.shouldShowFooter(appRect.height())) { |
142 footerResourceId = mDelegate.getFooterResourceId(); | 163 footerResourceId = mDelegate.getFooterResourceId(); |
143 } | 164 } |
144 mAppMenu.show(wrapper, anchorView, isByPermanentButton, rotation, appRec
t, pt.y, | 165 mAppMenu.show(wrapper, anchorView, isByPermanentButton, rotation, appRec
t, pt.y, |
145 footerResourceId); | 166 footerResourceId, mHighlightMenuId); |
146 mAppMenuDragHelper.onShow(startDragging); | 167 mAppMenuDragHelper.onShow(startDragging); |
| 168 setMenuHighlight(0); |
147 RecordUserAction.record("MobileMenuShow"); | 169 RecordUserAction.record("MobileMenuShow"); |
148 return true; | 170 return true; |
149 } | 171 } |
150 | 172 |
151 void appMenuDismissed() { | 173 void appMenuDismissed() { |
152 mAppMenuDragHelper.finishDragging(); | 174 mAppMenuDragHelper.finishDragging(); |
153 } | 175 } |
154 | 176 |
155 /** | 177 /** |
156 * @return Whether the App Menu is currently showing. | 178 * @return Whether the App Menu is currently showing. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 /** | 222 /** |
201 * Called by AppMenu to report that the App Menu visibility has changed. | 223 * Called by AppMenu to report that the App Menu visibility has changed. |
202 * @param isVisible Whether the App Menu is showing. | 224 * @param isVisible Whether the App Menu is showing. |
203 */ | 225 */ |
204 void onMenuVisibilityChanged(boolean isVisible) { | 226 void onMenuVisibilityChanged(boolean isVisible) { |
205 for (int i = 0; i < mObservers.size(); ++i) { | 227 for (int i = 0; i < mObservers.size(); ++i) { |
206 mObservers.get(i).onMenuVisibilityChanged(isVisible); | 228 mObservers.get(i).onMenuVisibilityChanged(isVisible); |
207 } | 229 } |
208 } | 230 } |
209 } | 231 } |
OLD | NEW |