ActionMenuPresenter.java revision 1ab418a222e1834c4b1312fde355e41a1947af0d
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.ViewGroup; 30import android.widget.ImageButton; 31 32import java.util.ArrayList; 33 34/** 35 * MenuPresenter for building action menus as seen in the action bar and action modes. 36 */ 37public class ActionMenuPresenter extends BaseMenuPresenter { 38 private static final String TAG = "ActionMenuPresenter"; 39 40 private View mOverflowButton; 41 private boolean mReserveOverflow; 42 private boolean mReserveOverflowSet; 43 private int mWidthLimit; 44 private int mActionItemWidthLimit; 45 private int mMaxItems; 46 private boolean mMaxItemsSet; 47 private boolean mStrictWidthLimit; 48 private boolean mWidthLimitSet; 49 50 // Group IDs that have been added as actions - used temporarily, allocated here for reuse. 51 private final SparseBooleanArray mActionButtonGroups = new SparseBooleanArray(); 52 53 private View mScrapActionButtonView; 54 55 private OverflowPopup mOverflowPopup; 56 private ActionButtonSubmenu mActionButtonPopup; 57 58 private OpenOverflowRunnable mPostedOpenRunnable; 59 60 public ActionMenuPresenter() { 61 super(com.android.internal.R.layout.action_menu_layout, 62 com.android.internal.R.layout.action_menu_item_layout); 63 } 64 65 @Override 66 public void initForMenu(Context context, MenuBuilder menu) { 67 super.initForMenu(context, menu); 68 69 final Resources res = context.getResources(); 70 71 if (!mReserveOverflowSet) { 72 // TODO Use the no-buttons specifier instead here 73 mReserveOverflow = res.getConfiguration() 74 .isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE); 75 } 76 77 if (!mWidthLimitSet) { 78 mWidthLimit = res.getDisplayMetrics().widthPixels / 2; 79 } 80 81 // Measure for initial configuration 82 if (!mMaxItemsSet) { 83 mMaxItems = res.getInteger(com.android.internal.R.integer.max_action_buttons); 84 } 85 86 int width = mWidthLimit; 87 if (mReserveOverflow) { 88 if (mOverflowButton == null) { 89 mOverflowButton = new OverflowMenuButton(mContext); 90 final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 91 mOverflowButton.measure(spec, spec); 92 } 93 width -= mOverflowButton.getMeasuredWidth(); 94 } else { 95 mOverflowButton = null; 96 } 97 98 mActionItemWidthLimit = width; 99 100 // Drop a scrap view as it may no longer reflect the proper context/config. 101 mScrapActionButtonView = null; 102 } 103 104 public void setWidthLimit(int width, boolean strict) { 105 mWidthLimit = width; 106 mStrictWidthLimit = strict; 107 mWidthLimitSet = true; 108 } 109 110 public void setReserveOverflow(boolean reserveOverflow) { 111 mReserveOverflow = reserveOverflow; 112 mReserveOverflowSet = true; 113 } 114 115 public void setItemLimit(int itemCount) { 116 mMaxItems = itemCount; 117 mMaxItemsSet = true; 118 } 119 120 @Override 121 public MenuView getMenuView(ViewGroup root) { 122 MenuView result = super.getMenuView(root); 123 ((ActionMenuView) result).setPresenter(this); 124 return result; 125 } 126 127 @Override 128 public View getItemView(MenuItemImpl item, View convertView, ViewGroup parent) { 129 View actionView = item.getActionView(); 130 actionView = actionView != null && !item.hasCollapsibleActionView() ? 131 actionView : super.getItemView(item, convertView, parent); 132 actionView.setVisibility(item.isActionViewExpanded() ? View.GONE : View.VISIBLE); 133 return actionView; 134 } 135 136 @Override 137 public void bindItemView(MenuItemImpl item, MenuView.ItemView itemView) { 138 itemView.initialize(item, 0); 139 ((ActionMenuItemView) itemView).setItemInvoker((ActionMenuView) mMenuView); 140 } 141 142 @Override 143 public boolean shouldIncludeItem(int childIndex, MenuItemImpl item) { 144 return item.isActionButton(); 145 } 146 147 @Override 148 public void updateMenuView(boolean cleared) { 149 super.updateMenuView(cleared); 150 151 if (mReserveOverflow && mMenu.getNonActionItems().size() > 0) { 152 if (mOverflowButton == null) { 153 mOverflowButton = new OverflowMenuButton(mContext); 154 mOverflowButton.setLayoutParams( 155 ((ActionMenuView) mMenuView).generateOverflowButtonLayoutParams()); 156 } 157 ViewGroup parent = (ViewGroup) mOverflowButton.getParent(); 158 if (parent != mMenuView) { 159 if (parent != null) { 160 parent.removeView(mOverflowButton); 161 } 162 ((ViewGroup) mMenuView).addView(mOverflowButton); 163 } 164 } else if (mOverflowButton != null && mOverflowButton.getParent() == mMenuView) { 165 ((ViewGroup) mMenuView).removeView(mOverflowButton); 166 } 167 } 168 169 @Override 170 public boolean filterLeftoverView(ViewGroup parent, int childIndex) { 171 if (parent.getChildAt(childIndex) == mOverflowButton) return false; 172 return super.filterLeftoverView(parent, childIndex); 173 } 174 175 public boolean onSubMenuSelected(SubMenuBuilder subMenu) { 176 if (!subMenu.hasVisibleItems()) return false; 177 178 SubMenuBuilder topSubMenu = subMenu; 179 while (topSubMenu.getParentMenu() != mMenu) { 180 topSubMenu = (SubMenuBuilder) topSubMenu.getParentMenu(); 181 } 182 View anchor = findViewForItem(topSubMenu.getItem()); 183 if (anchor == null) return false; 184 185 mActionButtonPopup = new ActionButtonSubmenu(mContext, subMenu); 186 mActionButtonPopup.setAnchorView(anchor); 187 mActionButtonPopup.show(); 188 super.onSubMenuSelected(subMenu); 189 return true; 190 } 191 192 private View findViewForItem(MenuItem item) { 193 final ViewGroup parent = (ViewGroup) mMenuView; 194 if (parent == null) return null; 195 196 final int count = parent.getChildCount(); 197 for (int i = 0; i < count; i++) { 198 final View child = parent.getChildAt(i); 199 if (child instanceof MenuView.ItemView && 200 ((MenuView.ItemView) child).getItemData() == item) { 201 return child; 202 } 203 } 204 return null; 205 } 206 207 /** 208 * Display the overflow menu if one is present. 209 * @return true if the overflow menu was shown, false otherwise. 210 */ 211 public boolean showOverflowMenu() { 212 if (mReserveOverflow && !isOverflowMenuShowing() && mMenuView != null && 213 mPostedOpenRunnable == null) { 214 OverflowPopup popup = new OverflowPopup(mContext, mMenu, mOverflowButton, true); 215 mPostedOpenRunnable = new OpenOverflowRunnable(popup); 216 // Post this for later; we might still need a layout for the anchor to be right. 217 ((View) mMenuView).post(mPostedOpenRunnable); 218 219 // ActionMenuPresenter uses null as a callback argument here 220 // to indicate overflow is opening. 221 super.onSubMenuSelected(null); 222 223 return true; 224 } 225 return false; 226 } 227 228 /** 229 * Hide the overflow menu if it is currently showing. 230 * 231 * @return true if the overflow menu was hidden, false otherwise. 232 */ 233 public boolean hideOverflowMenu() { 234 if (mPostedOpenRunnable != null && mMenuView != null) { 235 ((View) mMenuView).removeCallbacks(mPostedOpenRunnable); 236 return true; 237 } 238 239 MenuPopupHelper popup = mOverflowPopup; 240 if (popup != null) { 241 popup.dismiss(); 242 return true; 243 } 244 return false; 245 } 246 247 /** 248 * Dismiss all popup menus - overflow and submenus. 249 * @return true if popups were dismissed, false otherwise. (This can be because none were open.) 250 */ 251 public boolean dismissPopupMenus() { 252 boolean result = hideOverflowMenu(); 253 result |= hideSubMenus(); 254 return result; 255 } 256 257 /** 258 * Dismiss all submenu popups. 259 * 260 * @return true if popups were dismissed, false otherwise. (This can be because none were open.) 261 */ 262 public boolean hideSubMenus() { 263 if (mActionButtonPopup != null) { 264 mActionButtonPopup.dismiss(); 265 return true; 266 } 267 return false; 268 } 269 270 /** 271 * @return true if the overflow menu is currently showing 272 */ 273 public boolean isOverflowMenuShowing() { 274 return mOverflowPopup != null && mOverflowPopup.isShowing(); 275 } 276 277 /** 278 * @return true if space has been reserved in the action menu for an overflow item. 279 */ 280 public boolean isOverflowReserved() { 281 return mReserveOverflow; 282 } 283 284 public boolean flagActionItems() { 285 final ArrayList<MenuItemImpl> visibleItems = mMenu.getVisibleItems(); 286 final int itemsSize = visibleItems.size(); 287 int maxActions = mMaxItems; 288 int widthLimit = mActionItemWidthLimit; 289 final int querySpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 290 final ViewGroup parent = (ViewGroup) mMenuView; 291 292 int requiredItems = 0; 293 int requestedItems = 0; 294 int firstActionWidth = 0; 295 boolean hasOverflow = false; 296 for (int i = 0; i < itemsSize; i++) { 297 MenuItemImpl item = visibleItems.get(i); 298 if (item.requiresActionButton()) { 299 requiredItems++; 300 } else if (item.requestsActionButton()) { 301 requestedItems++; 302 } else { 303 hasOverflow = true; 304 } 305 } 306 307 // Reserve a spot for the overflow item if needed. 308 if (mReserveOverflow && 309 (hasOverflow || requiredItems + requestedItems > maxActions)) { 310 maxActions--; 311 } 312 maxActions -= requiredItems; 313 314 final SparseBooleanArray seenGroups = mActionButtonGroups; 315 seenGroups.clear(); 316 317 // Flag as many more requested items as will fit. 318 for (int i = 0; i < itemsSize; i++) { 319 MenuItemImpl item = visibleItems.get(i); 320 321 if (item.requiresActionButton()) { 322 View v = item.getActionView(); 323 if (v == null || item.hasCollapsibleActionView()) { 324 v = getItemView(item, mScrapActionButtonView, parent); 325 if (mScrapActionButtonView == null) { 326 mScrapActionButtonView = v; 327 } 328 } 329 v.measure(querySpec, querySpec); 330 final int measuredWidth = v.getMeasuredWidth(); 331 widthLimit -= measuredWidth; 332 if (firstActionWidth == 0) { 333 firstActionWidth = measuredWidth; 334 } 335 final int groupId = item.getGroupId(); 336 if (groupId != 0) { 337 seenGroups.put(groupId, true); 338 } 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