| 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.os.Handler; |
| 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 private int mHighlightMenuId; |
| 45 |
| 46 /** |
| 40 * Constructs an AppMenuHandler object. | 47 * Constructs an AppMenuHandler object. |
| 41 * @param activity Activity that is using the AppMenu. | 48 * @param activity Activity that is using the AppMenu. |
| 42 * @param delegate Delegate used to check the desired AppMenu properties on
show. | 49 * @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. | 50 * @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. | 51 * It is assumed to have back_menu_id, forward_menu_id, bookmark_
this_page_id. |
| 45 */ | 52 */ |
| 46 public AppMenuHandler(Activity activity, AppMenuPropertiesDelegate delegate, | 53 public AppMenuHandler(Activity activity, AppMenuPropertiesDelegate delegate, |
| 47 int menuResourceId) { | 54 int menuResourceId) { |
| 48 mActivity = activity; | 55 mActivity = activity; |
| 49 mDelegate = delegate; | 56 mDelegate = delegate; |
| 50 mObservers = new ArrayList<AppMenuObserver>(); | 57 mObservers = new ArrayList<AppMenuObserver>(); |
| 51 mMenuResourceId = menuResourceId; | 58 mMenuResourceId = menuResourceId; |
| 52 mHardwareButtonMenuAnchor = activity.findViewById(R.id.menu_anchor_stub)
; | 59 mHardwareButtonMenuAnchor = activity.findViewById(R.id.menu_anchor_stub)
; |
| 53 assert mHardwareButtonMenuAnchor != null | 60 assert mHardwareButtonMenuAnchor != null |
| 54 : "Using AppMenu requires to have menu_anchor_stub view"; | 61 : "Using AppMenu requires to have menu_anchor_stub view"; |
| 62 |
| 63 new Handler().postDelayed(new Runnable() { |
| 64 @Override |
| 65 public void run() { |
| 66 setMenuHighlight(R.id.offline_page_id); |
| 67 } |
| 68 }, 2000); |
| 55 } | 69 } |
| 56 | 70 |
| 57 /** | 71 /** |
| 58 * Notifies the menu that the contents of the menu item specified by {@code
menuRowId} have | 72 * 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 | 73 * changed. This should be called if icons, titles, etc. are changing for a
particular menu |
| 60 * item while the menu is open. | 74 * 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 | 75 * @param menuRowId The id of the menu item to change. This must be a row i
d and not a child |
| 62 * id. | 76 * id. |
| 63 */ | 77 */ |
| 64 public void menuItemContentChanged(int menuRowId) { | 78 public void menuItemContentChanged(int menuRowId) { |
| 65 if (mAppMenu != null) mAppMenu.menuItemContentChanged(menuRowId); | 79 if (mAppMenu != null) mAppMenu.menuItemContentChanged(menuRowId); |
| 66 } | 80 } |
| 67 | 81 |
| 82 public void setMenuHighlight(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 |
| 68 /** | 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 f
or the popup, if |
| 71 * null is passed then hardware menu button anchor
will be used. | 92 * null is 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 |
| 73 * showed by tapping on a button, this should be f
alse. If it is | 94 * showed by tapping on a button, this should be fa
lse. If it is |
| 74 * showed by start dragging down on the menu butto
n, this should | 95 * showed by start dragging down on the menu button
, this should |
| 75 * be true. Note that if anchorView is null, this
must | 96 * be true. Note that if anchorView is null, this m
ust |
| 76 * be false since we no longer support hardware me
nu button | 97 * be false since we no longer support hardware men
u button |
| 77 * dragging. | 98 * dragging. |
| 78 * @return True, if the menu is shown, false, if menu is not shown, example
reasons: | 99 * @return True, if the menu is shown, false, if menu is not shown, example
reasons: |
| 79 * the menu is not yet available to be shown, or the menu is already
showing. | 100 * the menu is not yet available to be shown, or the menu is 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(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 | 150 |
| 130 // Use full size of window for abnormal appRect. | 151 // Use full size of window for abnormal appRect. |
| 131 if (appRect.left < 0 && appRect.top < 0) { | 152 if (appRect.left < 0 && appRect.top < 0) { |
| 132 appRect.left = 0; | 153 appRect.left = 0; |
| 133 appRect.top = 0; | 154 appRect.top = 0; |
| 134 appRect.right = mActivity.getWindow().getDecorView().getWidth(); | 155 appRect.right = mActivity.getWindow().getDecorView().getWidth(); |
| 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 mAppMenu.show(wrapper, anchorView, isByPermanentButton, | 160 mAppMenu.show(wrapper, anchorView, isByPermanentButton, rotation, appRec
t, pt.y, |
| 140 rotation, appRect, pt.y, mDelegate.getFooterResourceId()); | 161 mDelegate.getFooterResourceId(), mHighlightMenuId); |
| 141 mAppMenuDragHelper.onShow(startDragging); | 162 mAppMenuDragHelper.onShow(startDragging); |
| 163 setMenuHighlight(0); |
| 142 RecordUserAction.record("MobileMenuShow"); | 164 RecordUserAction.record("MobileMenuShow"); |
| 143 return true; | 165 return true; |
| 144 } | 166 } |
| 145 | 167 |
| 146 void appMenuDismissed() { | 168 void appMenuDismissed() { |
| 147 mAppMenuDragHelper.finishDragging(); | 169 mAppMenuDragHelper.finishDragging(); |
| 148 } | 170 } |
| 149 | 171 |
| 150 /** | 172 /** |
| 151 * @return Whether the App Menu is currently showing. | 173 * @return Whether the App Menu is currently showing. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 /** | 217 /** |
| 196 * Called by AppMenu to report that the App Menu visibility has changed. | 218 * Called by AppMenu to report that the App Menu visibility has changed. |
| 197 * @param isVisible Whether the App Menu is showing. | 219 * @param isVisible Whether the App Menu is showing. |
| 198 */ | 220 */ |
| 199 void onMenuVisibilityChanged(boolean isVisible) { | 221 void onMenuVisibilityChanged(boolean isVisible) { |
| 200 for (int i = 0; i < mObservers.size(); ++i) { | 222 for (int i = 0; i < mObservers.size(); ++i) { |
| 201 mObservers.get(i).onMenuVisibilityChanged(isVisible); | 223 mObservers.get(i).onMenuVisibilityChanged(isVisible); |
| 202 } | 224 } |
| 203 } | 225 } |
| 204 } | 226 } |
| OLD | NEW |