ActionMenuPresenter.java revision 8c4706252228755ed181d19532b7e0cfec270ba4
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.internal.view.menu; 18 19import com.android.internal.view.menu.ActionMenuView.ActionMenuChildView; 20 21import android.content.Context; 22import android.content.res.Configuration; 23import android.content.res.Resources; 24import android.util.SparseBooleanArray; 25import android.view.MenuItem; 26import android.view.SoundEffectConstants; 27import android.view.View; 28import android.view.View.MeasureSpec; 29import android.view.ViewConfiguration; 30import android.view.ViewGroup; 31import android.widget.ImageButton; 32 33import java.util.ArrayList; 34 35/** 36 * MenuPresenter for building action menus as seen in the action bar and action modes. 37 */ 38public class ActionMenuPresenter extends BaseMenuPresenter { 39 private static final String TAG = "ActionMenuPresenter"; 40 41 private View mOverflowButton; 42 private boolean mReserveOverflow; 43 private boolean mReserveOverflowSet; 44 private int mWidthLimit; 45 private int mActionItemWidthLimit; 46 private int mMaxItems; 47 private boolean mMaxItemsSet; 48 private boolean mStrictWidthLimit; 49 private boolean mWidthLimitSet; 50 51 // Group IDs that have been added as actions - used temporarily, allocated here for reuse. 52 private final SparseBooleanArray mActionButtonGroups = new SparseBooleanArray(); 53 54 private View mScrapActionButtonView; 55 56 private OverflowPopup mOverflowPopup; 57 private ActionButtonSubmenu mActionButtonPopup; 58 59 private OpenOverflowRunnable mPostedOpenRunnable; 60 61 public ActionMenuPresenter() { 62 super(com.android.internal.R.layout.action_menu_layout, 63 com.android.internal.R.layout.action_menu_item_layout); 64 } 65 66 @Override 67 public void initForMenu(Context context, MenuBuilder menu) { 68 super.initForMenu(context, menu); 69 70 final Resources res = context.getResources(); 71 72 if (!mReserveOverflowSet) { 73 mReserveOverflow = !ViewConfiguration.get(context).hasPermanentMenuKey(); 74 } 75 76 if (!mWidthLimitSet) { 77 mWidthLimit = res.getDisplayMetrics().widthPixels / 2; 78 } 79 80 // Measure for initial configuration 81 if (!mMaxItemsSet) { 82 mMaxItems = res.getInteger(com.android.internal.R.integer.max_action_buttons); 83 } 84 85 int width = mWidthLimit; 86 if (mReserveOverflow) { 87 if (mOverflowButton == null) { 88 mOverflowButton = new OverflowMenuButton(mContext); 89 final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 90 mOverflowButton.measure(spec, spec); 91 } 92 width -= mOverflowButton.getMeasuredWidth(); 93 } else { 94 mOverflowButton = null; 95 } 96 97 mActionItemWidthLimit = width; 98 99 // Drop a scrap view as it may no longer reflect the proper context/config. 100 mScrapActionButtonView = null; 101 } 102 103 public void setWidthLimit(int width, boolean strict) { 104 mWidthLimit = width; 105 mStrictWidthLimit = strict; 106 mWidthLimitSet = true; 107 } 108 109 public void setReserveOverflow(boolean reserveOverflow) { 110 mReserveOverflow = reserveOverflow; 111 mReserveOverflowSet = true; 112 } 113 114 public void setItemLimit(int itemCount) { 115 mMaxItems = itemCount; 116 mMaxItemsSet = true; 117 } 118 119 @Override 120 public MenuView getMenuView(ViewGroup root) { 121 MenuView result = super.getMenuView(root); 122 ((ActionMenuView) result).setPresenter(this); 123 return result; 124 } 125 126 @Override 127 public View getItemView(MenuItemImpl item, View convertView, ViewGroup parent) { 128 View actionView = item.getActionView(); 129 actionView = actionView != null && !item.hasCollapsibleActionView() ? 130 actionView : super.getItemView(item, convertView, parent); 131 actionView.setVisibility(item.isActionViewExpanded() ? View.GONE : View.VISIBLE); 132 return actionView; 133 } 134 135 @Override 136 public void bindItemView(MenuItemImpl item, MenuView.ItemView itemView) { 137 itemView.initialize(item, 0); 138 ((ActionMenuItemView) itemView).setItemInvoker((ActionMenuView) mMenuView); 139 } 140 141 @Override 142 public boolean shouldIncludeItem(int childIndex, MenuItemImpl item) { 143 return item.isActionButton(); 144 } 145 146 @Override 147 public void updateMenuView(boolean cleared) { 148 super.updateMenuView(cleared); 149 150 if (mReserveOverflow && mMenu.getNonActionItems().size() > 0) { 151 if (mOverflowButton == null) { 152 mOverflowButton = new OverflowMenuButton(mContext); 153 mOverflowButton.setLayoutParams( 154 ((ActionMenuView) mMenuView).generateOverflowButtonLayoutParams()); 155 } 156 ViewGroup parent = (ViewGroup) mOverflowButton.getParent(); 157 if (parent != mMenuView) { 158 if (parent != null) { 159 parent.removeView(mOverflowButton); 160 } 161 ((ViewGroup) mMenuView).addView(mOverflowButton); 162 } 163 } else if (mOverflowButton != null && mOverflowButton.getParent() == mMenuView) { 164 ((ViewGroup) mMenuView).removeView(mOverflowButton); 165 } 166 } 167 168 @Override 169 public boolean filterLeftoverView(ViewGroup parent, int childIndex) { 170 if (parent.getChildAt(childIndex) == mOverflowButton) return false; 171 return super.filterLeftoverView(parent, childIndex); 172 } 173 174 public boolean onSubMenuSelected(SubMenuBuilder subMenu) { 175 if (!subMenu.hasVisibleItems()) return false; 176 177 SubMenuBuilder topSubMenu = subMenu; 178 while (topSubMenu.getParentMenu() != mMenu) { 179 topSubMenu = (SubMenuBuilder) topSubMenu.getParentMenu(); 180 } 181 View anchor = findViewForItem(topSubMenu.getItem()); 182 if (anchor == null) return false; 183 184 mActionButtonPopup = new ActionButtonSubmenu(mContext, subMenu); 185 mActionButtonPopup.setAnchorView(anchor); 186 mActionButtonPopup.show(); 187 super.onSubMenuSelected(subMenu); 188 return true; 189 } 190 191 private View findViewForItem(MenuItem item) { 192 final ViewGroup parent = (ViewGroup) mMenuView; 193 if (parent == null) return null; 194 195 final int count = parent.getChildCount(); 196 for (int i = 0; i < count; i++) { 197 final View child = parent.getChildAt(i); 198 if (child instanceof MenuView.ItemView && 199 ((MenuView.ItemView) child).getItemData() == item) { 200 return child; 201 } 202 } 203 return null; 204 } 205 206 /** 207 * Display the overflow menu if one is present. 208 * @return true if the overflow menu was shown, false otherwise. 209 */ 210 public boolean showOverflowMenu() { 211 if (mReserveOverflow && !isOverflowMenuShowing() && mMenuView != null && 212 mPostedOpenRunnable == null) { 213 OverflowPopup popup = new OverflowPopup(mContext, mMenu, mOverflowButton, true); 214 mPostedOpenRunnable = new OpenOverflowRunnable(popup); 215 // Post this for later; we might still need a layout for the anchor to be right. 216 ((View) mMenuView).post(mPostedOpenRunnable); 217 218 // ActionMenuPresenter uses null as a callback argument here 219 // to indicate overflow is opening. 220 super.onSubMenuSelected(null); 221 222 return true; 223 } 224 return false; 225 } 226 227 /** 228 * Hide the overflow menu if it is currently showing. 229 * 230 * @return true if the overflow menu was hidden, false otherwise. 231 */ 232 public boolean hideOverflowMenu() { 233 if (mPostedOpenRunnable != null && mMenuView != null) { 234 ((View) mMenuView).removeCallbacks(mPostedOpenRunnable); 235 return true; 236 } 237 238 MenuPopupHelper popup = mOverflowPopup; 239 if (popup != null) { 240 popup.dismiss(); 241 return true; 242 } 243 return false; 244 } 245 246 /** 247 * Dismiss all popup menus - overflow and submenus. 248 * @return true if popups were dismissed, false otherwise. (This can be because none were open.) 249 */ 250 public boolean dismissPopupMenus() { 251 boolean result = hideOverflowMenu(); 252 result |= hideSubMenus(); 253 return result; 254 } 255 256 /** 257 * Dismiss all submenu popups. 258 * 259 * @return true if popups were dismissed, false otherwise. (This can be because none were open.) 260 */ 261 public boolean hideSubMenus() { 262 if (mActionButtonPopup != null) { 263 mActionButtonPopup.dismiss(); 264 return true; 265 } 266 return false; 267 } 268 269 /** 270 * @return true if the overflow menu is currently showing 271 */ 272 public boolean isOverflowMenuShowing() { 273 return mOverflowPopup != null && mOverflowPopup.isShowing(); 274 } 275 276 /** 277 * @return true if space has been reserved in the action menu for an overflow item. 278 */ 279 public boolean isOverflowReserved() { 280 return mReserveOverflow; 281 } 282 283 public boolean flagActionItems() { 284 final ArrayList<MenuItemImpl> visibleItems = mMenu.getVisibleItems(); 285 final int itemsSize = visibleItems.size(); 286 int maxActions = mMaxItems; 287 int widthLimit = mActionItemWidthLimit; 288 final int querySpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 289 final ViewGroup parent = (ViewGroup) mMenuView; 290 291 int requiredItems = 0; 292 int requestedItems = 0; 293 int firstActionWidth = 0; 294 boolean hasOverflow = false; 295 for (int i = 0; i < itemsSize; i++) { 296 MenuItemImpl item = visibleItems.get(i); 297 if (item.requiresActionButton()) { 298 requiredItems++; 299 } else if (item.requestsActionButton()) { 300 requestedItems++; 301 } else { 302 hasOverflow = true; 303 } 304 } 305 306 // Reserve a spot for the overflow item if needed. 307 if (mReserveOverflow && 308 (hasOverflow || requiredItems + requestedItems > maxActions)) { 309 maxActions--; 310 } 311 maxActions -= requiredItems; 312 313 final SparseBooleanArray seenGroups = mActionButtonGroups; 314 seenGroups.clear(); 315 316 // Flag as many more requested items as will fit. 317 for (int i = 0; i < itemsSize; i++) { 318 MenuItemImpl item = visibleItems.get(i); 319 320 if (item.requiresActionButton()) { 321 View v = item.getActionView(); 322 if (v == null || item.hasCollapsibleActionView()) { 323 v = getItemView(item, mScrapActionButtonView, parent); 324 if (mScrapActionButtonView == null) { 325 mScrapActionButtonView = v; 326 } 327 } 328 v.measure(querySpec, querySpec); 329 final int measuredWidth = v.getMeasuredWidth(); 330 widthLimit -= measuredWidth; 331 if (firstActionWidth == 0) { 332 firstActionWidth = measuredWidth; 333 } 334 final int groupId = item.getGroupId(); 335 if (groupId != 0) { 336 seenGroups.put(groupId, true); 337 } 338 item.setIsActionButton(true); 339 } else if (item.requestsActionButton()) { 340 // Items in a group with other items that already have an action slot 341 // can break the max actions rule, but not the width limit. 342 final int groupId = item.getGroupId(); 343 final boolean inGroup = seenGroups.get(groupId); 344 boolean isAction = (maxActions > 0 || inGroup) && widthLimit > 0; 345 maxActions--; 346 347 if (isAction) { 348 View v = item.getActionView(); 349 if (v == null || item.hasCollapsibleActionView()) { 350 v = getItemView(item, mScrapActionButtonView, parent); 351 if (mScrapActionButtonView == null) { 352 mScrapActionButtonView = v; 353 } 354 } 355 v.measure(querySpec, querySpec); 356 final int measuredWidth = v.getMeasuredWidth(); 357 widthLimit -= measuredWidth; 358 if (firstActionWidth == 0) { 359 firstActionWidth = measuredWidth; 360 } 361 362 if (mStrictWidthLimit) { 363 isAction = widthLimit >= 0; 364 } else { 365 // Did this push the entire first item past the limit? 366 isAction = widthLimit + firstActionWidth > 0; 367 } 368 } 369 370 if (isAction && groupId != 0) { 371 seenGroups.put(groupId, true); 372 } else if (inGroup) { 373 // We broke the width limit. Demote the whole group, they all overflow now. 374 seenGroups.put(groupId, false); 375 for (int j = 0; j < i; j++) { 376 MenuItemImpl areYouMyGroupie = visibleItems.get(j); 377 if (areYouMyGroupie.getGroupId() == groupId) { 378 areYouMyGroupie.setIsActionButton(false); 379 } 380 } 381 } 382 383 item.setIsActionButton(isAction); 384 } 385 } 386 return true; 387 } 388 389 @Override 390 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { 391 dismissPopupMenus(); 392 super.onCloseMenu(menu, allMenusAreClosing); 393 } 394 395 private class OverflowMenuButton extends ImageButton implements ActionMenuChildView { 396 public OverflowMenuButton(Context context) { 397 super(context, null, com.android.internal.R.attr.actionOverflowButtonStyle); 398 399 setClickable(true); 400 setFocusable(true); 401 setVisibility(VISIBLE); 402 setEnabled(true); 403 } 404 405 @Override 406 public boolean performClick() { 407 if (super.performClick()) { 408 return true; 409 } 410 411 playSoundEffect(SoundEffectConstants.CLICK); 412 showOverflowMenu(); 413 return true; 414 } 415 416 public boolean needsDividerBefore() { 417 return true; 418 } 419 420 public boolean needsDividerAfter() { 421 return false; 422 } 423 } 424 425 private class OverflowPopup extends MenuPopupHelper { 426 public OverflowPopup(Context context, MenuBuilder menu, View anchorView, 427 boolean overflowOnly) { 428 super(context, menu, anchorView, overflowOnly); 429 } 430 431 @Override 432 public void onDismiss() { 433 super.onDismiss(); 434 mMenu.close(); 435 mOverflowPopup = null; 436 } 437 } 438 439 private class ActionButtonSubmenu extends MenuPopupHelper { 440 private SubMenuBuilder mSubMenu; 441 442 public ActionButtonSubmenu(Context context, SubMenuBuilder subMenu) { 443 super(context, subMenu); 444 mSubMenu = subMenu; 445 446 MenuItemImpl item = (MenuItemImpl) subMenu.getItem(); 447 if (!item.isActionButton()) { 448 // Give a reasonable anchor to nested submenus. 449 setAnchorView(mOverflowButton == null ? (View) mMenuView : mOverflowButton); 450 } 451 } 452 453 @Override 454 public void onDismiss() { 455 super.onDismiss(); 456 mSubMenu.close(); 457 mActionButtonPopup = null; 458 } 459 } 460 461 private class OpenOverflowRunnable implements Runnable { 462 private OverflowPopup mPopup; 463 464 public OpenOverflowRunnable(OverflowPopup popup) { 465 mPopup = popup; 466 } 467 468 public void run() { 469 mMenu.changeMenuMode(); 470 if (mPopup.tryShow()) { 471 mOverflowPopup = mPopup; 472 mPostedOpenRunnable = null; 473 } 474 } 475 } 476} 477