OLD | NEW |
(Empty) | |
| 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.app.Activity; |
| 8 import android.view.ContextThemeWrapper; |
| 9 import android.view.Menu; |
| 10 import android.view.View; |
| 11 import android.widget.PopupMenu; |
| 12 |
| 13 import org.chromium.chrome.R; |
| 14 import org.chromium.chrome.browser.UmaBridge; |
| 15 |
| 16 import java.util.ArrayList; |
| 17 |
| 18 /** |
| 19 * Object responsible for handling the creation, showing, hiding of the AppMenu
and notifying the |
| 20 * AppMenuObservers about these actions. |
| 21 */ |
| 22 public class AppMenuHandler { |
| 23 private AppMenu mAppMenu; |
| 24 private Menu mMenu; |
| 25 private final ArrayList<AppMenuObserver> mObservers; |
| 26 |
| 27 private final AppMenuPropertiesDelegate mDelegate; |
| 28 private final Activity mActivity; |
| 29 |
| 30 /** |
| 31 * Constructs an AppMenuHandler object. |
| 32 * @param activity Activity that is using the AppMenu. |
| 33 * @param delegate Delegate used to check the desired AppMenu properties on
show. |
| 34 */ |
| 35 public AppMenuHandler(Activity activity, AppMenuPropertiesDelegate delegate)
{ |
| 36 mActivity = activity; |
| 37 mDelegate = delegate; |
| 38 mObservers = new ArrayList<AppMenuObserver>(); |
| 39 } |
| 40 |
| 41 /** |
| 42 * Show the app menu. |
| 43 * @param anchorView Anchor view (usually a menu button) to be used
for the popup. |
| 44 * @param isByHardwareButton True if hardware button triggered it. (oppose t
o software |
| 45 * button) |
| 46 * @param startDragging Whether dragging is started. For example, if th
e app menu is |
| 47 * showed by tapping on a button, this should be f
alse. If it is |
| 48 * showed by start dragging down on the menu butto
n, this should |
| 49 * be true. Note that if isByHardwareButton is tru
e, this is |
| 50 * ignored. |
| 51 * @return True, if the menu is shown, false, if menu is not shown, example
reasons: |
| 52 * the menu is not yet available to be shown, or the menu is already
showing. |
| 53 */ |
| 54 public boolean showAppMenu(View anchorView, boolean isByHardwareButton, bool
ean startDragging) { |
| 55 if (!mDelegate.shouldShowAppMenu()) return false; |
| 56 |
| 57 if (mMenu == null) { |
| 58 // Use a PopupMenu to create the Menu object. Note this is not the s
ame as the |
| 59 // AppMenu (mAppMenu) created below. |
| 60 PopupMenu tempMenu = new PopupMenu(mActivity, anchorView); |
| 61 tempMenu.inflate(R.menu.main_menu); |
| 62 mMenu = tempMenu.getMenu(); |
| 63 } |
| 64 mDelegate.prepareMenu(mMenu); |
| 65 |
| 66 if (mAppMenu == null) { |
| 67 mAppMenu = new AppMenu(mActivity, mMenu, mDelegate.getItemRowHeight(
), this); |
| 68 } |
| 69 |
| 70 ContextThemeWrapper wrapper = new ContextThemeWrapper(mActivity, |
| 71 mDelegate.getMenuThemeResourceId()); |
| 72 boolean showIcons = mDelegate.shouldShowIconRow(); |
| 73 mAppMenu.show(wrapper, anchorView, showIcons, isByHardwareButton, startD
ragging); |
| 74 UmaBridge.menuShow(); |
| 75 return true; |
| 76 } |
| 77 |
| 78 /** |
| 79 * @return Whether the App Menu is currently showing. |
| 80 */ |
| 81 public boolean isAppMenuShowing() { |
| 82 return mAppMenu != null && mAppMenu.isShowing(); |
| 83 } |
| 84 |
| 85 /** |
| 86 * @return The App Menu that the menu handler is interacting with. |
| 87 */ |
| 88 AppMenu getAppMenu() { |
| 89 return mAppMenu; |
| 90 } |
| 91 |
| 92 /** |
| 93 * Requests to hide the App Menu. |
| 94 */ |
| 95 public void hideAppMenu() { |
| 96 if (mAppMenu != null && mAppMenu.isShowing()) mAppMenu.dismiss(); |
| 97 } |
| 98 |
| 99 /** |
| 100 * @return The number of items in the AppMenu. |
| 101 */ |
| 102 public int getItemCount() { |
| 103 if (mAppMenu == null) return -1; |
| 104 return mAppMenu.getCount(); |
| 105 } |
| 106 |
| 107 /** |
| 108 * Adds the observer to App Menu. |
| 109 * @param observer Observer that should be notified about App Menu changes. |
| 110 */ |
| 111 public void addObserver(AppMenuObserver observer) { |
| 112 mObservers.add(observer); |
| 113 } |
| 114 |
| 115 /** |
| 116 * Removes the observer from the App Menu. |
| 117 * @param observer Observer that should no longer be notified about App Menu
changes. |
| 118 */ |
| 119 public void removeObserver(AppMenuObserver observer) { |
| 120 mObservers.remove(observer); |
| 121 } |
| 122 |
| 123 /** |
| 124 * Called by AppMenu to report that the App Menu visibility has changed. |
| 125 * @param newState Whether the App Menu is showing. |
| 126 * @param focusedPosition The current focused position. |
| 127 */ |
| 128 void onMenuVisibilityChanged(boolean newState, int focusedPosition) { |
| 129 for (int i = 0; i < mObservers.size(); ++i) { |
| 130 mObservers.get(i).onMenuVisibilityChanged(newState, focusedPosition)
; |
| 131 } |
| 132 } |
| 133 |
| 134 /** |
| 135 * Called by AppMenu to report that the keyboard focus has changed. |
| 136 * @param focusedPosition The new focused position. |
| 137 */ |
| 138 void onKeyboardFocusChanged(int focusedPosition) { |
| 139 for (int i = 0; i < mObservers.size(); ++i) { |
| 140 mObservers.get(i).onKeyboardFocusChanged(focusedPosition); |
| 141 } |
| 142 } |
| 143 |
| 144 /** |
| 145 * Called by AppMenu to report that the keyboard has activated an item. |
| 146 * @param focusedPosition The activated item. |
| 147 */ |
| 148 void onKeyboardActivatedItem(int focusedPosition) { |
| 149 for (int i = 0; i < mObservers.size(); ++i) { |
| 150 mObservers.get(i).onKeyboardActivatedItem(focusedPosition); |
| 151 } |
| 152 } |
| 153 |
| 154 /** |
| 155 * TODO(kkimlabs) remove this call. |
| 156 */ |
| 157 public void hardwareMenuButtonUp() { |
| 158 if (mAppMenu != null) mAppMenu.hardwareMenuButtonUp(); |
| 159 } |
| 160 } |
OLD | NEW |