blob: eb20a76754ea88d32f1c04bb6768268f295059fa [file] [log] [blame]
Adam Powell696cba52011-03-29 10:38:16 -07001/*
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
Adam Powellfa18d182014-01-07 15:56:59 -080017package android.widget;
Adam Powell696cba52011-03-29 10:38:16 -070018
Chet Haase952cba22015-03-20 13:17:25 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
Alan Viverette28a84682016-01-04 13:43:23 -050023import android.annotation.NonNull;
24import android.annotation.Nullable;
Mathew Inwood978c6e22018-08-21 15:58:55 +010025import android.annotation.UnsupportedAppUsage;
Adam Powell696cba52011-03-29 10:38:16 -070026import android.content.Context;
Adam Powellbfcdfaf2011-08-19 11:46:59 -070027import android.content.res.Configuration;
Adam Powell696cba52011-03-29 10:38:16 -070028import android.content.res.Resources;
Alan Viverettef023c252014-08-28 13:55:18 -070029import android.graphics.drawable.Drawable;
Adam Powell11ed1d62011-07-11 21:19:59 -070030import android.os.Parcel;
31import android.os.Parcelable;
Chet Haase952cba22015-03-20 13:17:25 -070032import android.util.SparseArray;
Adam Powell696cba52011-03-29 10:38:16 -070033import android.util.SparseBooleanArray;
Adam Powell823f0742011-09-21 17:17:01 -070034import android.view.ActionProvider;
Adam Powell54c94de2013-09-26 15:36:34 -070035import android.view.Gravity;
Adam Powell696cba52011-03-29 10:38:16 -070036import android.view.MenuItem;
37import android.view.SoundEffectConstants;
38import android.view.View;
39import android.view.View.MeasureSpec;
40import android.view.ViewGroup;
Chet Haase952cba22015-03-20 13:17:25 -070041import android.view.ViewTreeObserver;
Adam Powellad79b902013-06-19 11:33:28 -070042import android.view.accessibility.AccessibilityNodeInfo;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070043
Alan Viverette80e72702013-07-29 11:12:59 -070044import com.android.internal.view.ActionBarPolicy;
Adam Powellfa18d182014-01-07 15:56:59 -080045import com.android.internal.view.menu.ActionMenuItemView;
46import com.android.internal.view.menu.BaseMenuPresenter;
47import com.android.internal.view.menu.MenuBuilder;
48import com.android.internal.view.menu.MenuItemImpl;
49import com.android.internal.view.menu.MenuPopupHelper;
50import com.android.internal.view.menu.MenuView;
Oren Blasbergf44d90b2015-08-31 14:15:26 -070051import com.android.internal.view.menu.ShowableListMenu;
Adam Powellfa18d182014-01-07 15:56:59 -080052import com.android.internal.view.menu.SubMenuBuilder;
Adam Powell696cba52011-03-29 10:38:16 -070053
54import java.util.ArrayList;
Chet Haase952cba22015-03-20 13:17:25 -070055import java.util.List;
Adam Powell696cba52011-03-29 10:38:16 -070056
57/**
58 * MenuPresenter for building action menus as seen in the action bar and action modes.
Adam Powellfa18d182014-01-07 15:56:59 -080059 *
60 * @hide
Adam Powell696cba52011-03-29 10:38:16 -070061 */
Adam Powell823f0742011-09-21 17:17:01 -070062public class ActionMenuPresenter extends BaseMenuPresenter
63 implements ActionProvider.SubUiVisibilityListener {
Chet Haase952cba22015-03-20 13:17:25 -070064 private static final int ITEM_ANIMATION_DURATION = 150;
Chet Haased96c87e2015-05-07 07:43:22 -070065 private static final boolean ACTIONBAR_ANIMATIONS_ENABLED = false;
Adam Powell640a66e2011-04-29 10:18:53 -070066
Chris Banes9cc36ca2015-02-23 12:30:13 +000067 private OverflowMenuButton mOverflowButton;
Chris Banesa41b7892015-06-09 13:36:44 +000068 private Drawable mPendingOverflowIcon;
69 private boolean mPendingOverflowIconSet;
Adam Powell696cba52011-03-29 10:38:16 -070070 private boolean mReserveOverflow;
Adam Powell1ab418a2011-06-09 20:49:49 -070071 private boolean mReserveOverflowSet;
Adam Powell696cba52011-03-29 10:38:16 -070072 private int mWidthLimit;
73 private int mActionItemWidthLimit;
74 private int mMaxItems;
Adam Powell1ab418a2011-06-09 20:49:49 -070075 private boolean mMaxItemsSet;
Adam Powell640a66e2011-04-29 10:18:53 -070076 private boolean mStrictWidthLimit;
Adam Powell1ab418a2011-06-09 20:49:49 -070077 private boolean mWidthLimitSet;
Adam Powellb187cd92011-07-20 14:17:56 -070078 private boolean mExpandedActionViewsExclusive;
Adam Powell696cba52011-03-29 10:38:16 -070079
Adam Powell35aecd52011-07-01 13:43:49 -070080 private int mMinCellSize;
81
Adam Powell696cba52011-03-29 10:38:16 -070082 // Group IDs that have been added as actions - used temporarily, allocated here for reuse.
83 private final SparseBooleanArray mActionButtonGroups = new SparseBooleanArray();
84
Adam Powell696cba52011-03-29 10:38:16 -070085 private OverflowPopup mOverflowPopup;
86 private ActionButtonSubmenu mActionButtonPopup;
87
88 private OpenOverflowRunnable mPostedOpenRunnable;
Alan Viverette56110722014-01-10 14:03:21 -080089 private ActionMenuPopupCallback mPopupCallback;
Adam Powell696cba52011-03-29 10:38:16 -070090
Adam Powell11ed1d62011-07-11 21:19:59 -070091 final PopupPresenterCallback mPopupPresenterCallback = new PopupPresenterCallback();
92 int mOpenSubMenuId;
93
Chet Haase952cba22015-03-20 13:17:25 -070094 // These collections are used to store pre- and post-layout information for menu items,
95 // which is used to determine appropriate animations to run for changed items.
Alan Viverette02cd0f92016-01-13 13:33:17 -050096 private SparseArray<MenuItemLayoutInfo> mPreLayoutItems = new SparseArray<>();
97 private SparseArray<MenuItemLayoutInfo> mPostLayoutItems = new SparseArray<>();
Chet Haase952cba22015-03-20 13:17:25 -070098
99 // The list of currently running animations on menu items.
Alan Viverette02cd0f92016-01-13 13:33:17 -0500100 private List<ItemAnimationInfo> mRunningItemAnimations = new ArrayList<>();
Chet Haase575217f2015-04-09 14:31:25 -0700101 private ViewTreeObserver.OnPreDrawListener mItemAnimationPreDrawListener =
102 new ViewTreeObserver.OnPreDrawListener() {
103 @Override
104 public boolean onPreDraw() {
105 computeMenuItemAnimationInfo(false);
106 ((View) mMenuView).getViewTreeObserver().removeOnPreDrawListener(this);
107 runItemAnimations();
108 return true;
109 }
110 };
111 private View.OnAttachStateChangeListener mAttachStateChangeListener =
112 new View.OnAttachStateChangeListener() {
113 @Override
114 public void onViewAttachedToWindow(View v) {
115 }
Chet Haase952cba22015-03-20 13:17:25 -0700116
Chet Haase575217f2015-04-09 14:31:25 -0700117 @Override
118 public void onViewDetachedFromWindow(View v) {
119 ((View) mMenuView).getViewTreeObserver().removeOnPreDrawListener(
120 mItemAnimationPreDrawListener);
121 mPreLayoutItems.clear();
122 mPostLayoutItems.clear();
123 }
124 };
Chet Haase952cba22015-03-20 13:17:25 -0700125
126
Adam Powell538e5652011-10-11 13:47:08 -0700127 public ActionMenuPresenter(Context context) {
128 super(context, com.android.internal.R.layout.action_menu_layout,
Adam Powell696cba52011-03-29 10:38:16 -0700129 com.android.internal.R.layout.action_menu_item_layout);
130 }
131
132 @Override
Alan Viverette28a84682016-01-04 13:43:23 -0500133 public void initForMenu(@NonNull Context context, @Nullable MenuBuilder menu) {
Adam Powell696cba52011-03-29 10:38:16 -0700134 super.initForMenu(context, menu);
135
136 final Resources res = context.getResources();
Adam Powell1ab418a2011-06-09 20:49:49 -0700137
Adam Powellb8139af2012-04-19 13:52:46 -0700138 final ActionBarPolicy abp = ActionBarPolicy.get(context);
Adam Powell1ab418a2011-06-09 20:49:49 -0700139 if (!mReserveOverflowSet) {
Adam Powellb8139af2012-04-19 13:52:46 -0700140 mReserveOverflow = abp.showsOverflowMenuButton();
Adam Powell1ab418a2011-06-09 20:49:49 -0700141 }
142
143 if (!mWidthLimitSet) {
Adam Powellb8139af2012-04-19 13:52:46 -0700144 mWidthLimit = abp.getEmbeddedMenuWidthLimit();
Adam Powell1ab418a2011-06-09 20:49:49 -0700145 }
Adam Powell696cba52011-03-29 10:38:16 -0700146
147 // Measure for initial configuration
Adam Powell1ab418a2011-06-09 20:49:49 -0700148 if (!mMaxItemsSet) {
Adam Powellb8139af2012-04-19 13:52:46 -0700149 mMaxItems = abp.getMaxActionButtons();
Adam Powell1ab418a2011-06-09 20:49:49 -0700150 }
Adam Powell696cba52011-03-29 10:38:16 -0700151
152 int width = mWidthLimit;
153 if (mReserveOverflow) {
Adam Powell9b4bee02011-04-27 19:24:47 -0700154 if (mOverflowButton == null) {
Adam Powell538e5652011-10-11 13:47:08 -0700155 mOverflowButton = new OverflowMenuButton(mSystemContext);
Chris Banesa41b7892015-06-09 13:36:44 +0000156 if (mPendingOverflowIconSet) {
157 mOverflowButton.setImageDrawable(mPendingOverflowIcon);
158 mPendingOverflowIcon = null;
159 mPendingOverflowIconSet = false;
160 }
Adam Powell9b4bee02011-04-27 19:24:47 -0700161 final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
162 mOverflowButton.measure(spec, spec);
163 }
Adam Powell696cba52011-03-29 10:38:16 -0700164 width -= mOverflowButton.getMeasuredWidth();
165 } else {
166 mOverflowButton = null;
167 }
168
169 mActionItemWidthLimit = width;
170
Adam Powell35aecd52011-07-01 13:43:49 -0700171 mMinCellSize = (int) (ActionMenuView.MIN_CELL_SIZE * res.getDisplayMetrics().density);
Adam Powell696cba52011-03-29 10:38:16 -0700172 }
173
Adam Powellbfcdfaf2011-08-19 11:46:59 -0700174 public void onConfigurationChanged(Configuration newConfig) {
175 if (!mMaxItemsSet) {
Filip Gruszczynski6c54e722015-11-17 12:24:49 -0800176 mMaxItems = ActionBarPolicy.get(mContext).getMaxActionButtons();
Adam Powellf203e0a2012-03-06 17:40:12 -0800177 }
178 if (mMenu != null) {
179 mMenu.onItemsChanged(true);
Adam Powellbfcdfaf2011-08-19 11:46:59 -0700180 }
181 }
182
Adam Powell640a66e2011-04-29 10:18:53 -0700183 public void setWidthLimit(int width, boolean strict) {
Adam Powell1ab418a2011-06-09 20:49:49 -0700184 mWidthLimit = width;
Adam Powell640a66e2011-04-29 10:18:53 -0700185 mStrictWidthLimit = strict;
Adam Powell1ab418a2011-06-09 20:49:49 -0700186 mWidthLimitSet = true;
187 }
188
189 public void setReserveOverflow(boolean reserveOverflow) {
190 mReserveOverflow = reserveOverflow;
191 mReserveOverflowSet = true;
Adam Powell9b4bee02011-04-27 19:24:47 -0700192 }
193
194 public void setItemLimit(int itemCount) {
195 mMaxItems = itemCount;
Adam Powell1ab418a2011-06-09 20:49:49 -0700196 mMaxItemsSet = true;
Adam Powell9b4bee02011-04-27 19:24:47 -0700197 }
198
Adam Powellb187cd92011-07-20 14:17:56 -0700199 public void setExpandedActionViewsExclusive(boolean isExclusive) {
200 mExpandedActionViewsExclusive = isExclusive;
201 }
202
Chris Banesa41b7892015-06-09 13:36:44 +0000203 public void setOverflowIcon(Drawable icon) {
204 if (mOverflowButton != null) {
205 mOverflowButton.setImageDrawable(icon);
206 } else {
207 mPendingOverflowIconSet = true;
208 mPendingOverflowIcon = icon;
209 }
210 }
211
212 public Drawable getOverflowIcon() {
213 if (mOverflowButton != null) {
214 return mOverflowButton.getDrawable();
215 } else if (mPendingOverflowIconSet) {
216 return mPendingOverflowIcon;
217 }
218 return null;
219 }
220
Adam Powell696cba52011-03-29 10:38:16 -0700221 @Override
222 public MenuView getMenuView(ViewGroup root) {
Chet Haase575217f2015-04-09 14:31:25 -0700223 MenuView oldMenuView = mMenuView;
Adam Powell696cba52011-03-29 10:38:16 -0700224 MenuView result = super.getMenuView(root);
Chet Haase575217f2015-04-09 14:31:25 -0700225 if (oldMenuView != result) {
226 ((ActionMenuView) result).setPresenter(this);
227 if (oldMenuView != null) {
228 ((View) oldMenuView).removeOnAttachStateChangeListener(mAttachStateChangeListener);
229 }
230 ((View) result).addOnAttachStateChangeListener(mAttachStateChangeListener);
231 }
Adam Powell696cba52011-03-29 10:38:16 -0700232 return result;
233 }
234
235 @Override
Alan Viverettefbe4a582013-09-06 13:45:54 -0700236 public View getItemView(final MenuItemImpl item, View convertView, ViewGroup parent) {
Adam Powell8d02dea2011-05-31 21:35:13 -0700237 View actionView = item.getActionView();
Adam Powell35aecd52011-07-01 13:43:49 -0700238 if (actionView == null || item.hasCollapsibleActionView()) {
Adam Powellc3ca3ea2013-10-10 18:07:48 -0700239 actionView = super.getItemView(item, convertView, parent);
Adam Powell35aecd52011-07-01 13:43:49 -0700240 }
Adam Powell8d02dea2011-05-31 21:35:13 -0700241 actionView.setVisibility(item.isActionViewExpanded() ? View.GONE : View.VISIBLE);
Adam Powell35aecd52011-07-01 13:43:49 -0700242
Adam Powellfe9c3542014-01-10 18:32:08 +0000243 final ActionMenuView menuParent = (ActionMenuView) parent;
244 final ViewGroup.LayoutParams lp = actionView.getLayoutParams();
245 if (!menuParent.checkLayoutParams(lp)) {
246 actionView.setLayoutParams(menuParent.generateLayoutParams(lp));
247 }
248 return actionView;
249 }
250
251 @Override
Alan Viverette56110722014-01-10 14:03:21 -0800252 public void bindItemView(MenuItemImpl item, MenuView.ItemView itemView) {
Adam Powellfe9c3542014-01-10 18:32:08 +0000253 itemView.initialize(item, 0);
254
255 final ActionMenuView menuView = (ActionMenuView) mMenuView;
256 final ActionMenuItemView actionItemView = (ActionMenuItemView) itemView;
257 actionItemView.setItemInvoker(menuView);
258
Alan Viverette56110722014-01-10 14:03:21 -0800259 if (mPopupCallback == null) {
260 mPopupCallback = new ActionMenuPopupCallback();
Alan Viverettefbe4a582013-09-06 13:45:54 -0700261 }
Alan Viverette56110722014-01-10 14:03:21 -0800262 actionItemView.setPopupCallback(mPopupCallback);
Adam Powell696cba52011-03-29 10:38:16 -0700263 }
264
265 @Override
266 public boolean shouldIncludeItem(int childIndex, MenuItemImpl item) {
267 return item.isActionButton();
268 }
269
Chet Haase952cba22015-03-20 13:17:25 -0700270 /**
271 * Store layout information about current items in the menu. This is stored for
272 * both pre- and post-layout phases and compared in runItemAnimations() to determine
273 * the animations that need to be run on any item changes.
274 *
275 * @param preLayout Whether this is being called in the pre-layout phase. This is passed
276 * into the MenuItemLayoutInfo structure to store the appropriate position values.
277 */
278 private void computeMenuItemAnimationInfo(boolean preLayout) {
Chet Haase575217f2015-04-09 14:31:25 -0700279 final ViewGroup menuView = (ViewGroup) mMenuView;
280 final int count = menuView.getChildCount();
Chet Haase952cba22015-03-20 13:17:25 -0700281 SparseArray items = preLayout ? mPreLayoutItems : mPostLayoutItems;
282 for (int i = 0; i < count; ++i) {
Chet Haase575217f2015-04-09 14:31:25 -0700283 View child = menuView.getChildAt(i);
Chet Haase952cba22015-03-20 13:17:25 -0700284 final int id = child.getId();
285 if (id > 0 && child.getWidth() != 0 && child.getHeight() != 0) {
286 MenuItemLayoutInfo info = new MenuItemLayoutInfo(child, preLayout);
287 items.put(id, info);
288 }
289 }
290 }
291
292 /**
293 * This method is called once both the pre-layout and post-layout steps have
294 * happened. It figures out which views are new (didn't exist prior to layout),
295 * gone (existed pre-layout, but are now gone), or changed (exist in both,
296 * but in a different location) and runs appropriate animations on those views.
297 * Items are tracked by ids, since the underlying views that represent items
298 * pre- and post-layout may be different.
299 */
300 private void runItemAnimations() {
301 for (int i = 0; i < mPreLayoutItems.size(); ++i) {
302 int id = mPreLayoutItems.keyAt(i);
303 final MenuItemLayoutInfo menuItemLayoutInfoPre = mPreLayoutItems.get(id);
304 final int postLayoutIndex = mPostLayoutItems.indexOfKey(id);
305 if (postLayoutIndex >= 0) {
306 // item exists pre and post: see if it's changed
307 final MenuItemLayoutInfo menuItemLayoutInfoPost =
308 mPostLayoutItems.valueAt(postLayoutIndex);
309 PropertyValuesHolder pvhX = null;
310 PropertyValuesHolder pvhY = null;
311 if (menuItemLayoutInfoPre.left != menuItemLayoutInfoPost.left) {
312 pvhX = PropertyValuesHolder.ofFloat(View.TRANSLATION_X,
313 (menuItemLayoutInfoPre.left - menuItemLayoutInfoPost.left), 0);
314 }
315 if (menuItemLayoutInfoPre.top != menuItemLayoutInfoPost.top) {
316 pvhY = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y,
317 menuItemLayoutInfoPre.top - menuItemLayoutInfoPost.top, 0);
318 }
319 if (pvhX != null || pvhY != null) {
320 for (int j = 0; j < mRunningItemAnimations.size(); ++j) {
321 ItemAnimationInfo oldInfo = mRunningItemAnimations.get(j);
322 if (oldInfo.id == id && oldInfo.animType == ItemAnimationInfo.MOVE) {
323 oldInfo.animator.cancel();
324 }
325 }
326 ObjectAnimator anim;
327 if (pvhX != null) {
328 if (pvhY != null) {
329 anim = ObjectAnimator.ofPropertyValuesHolder(menuItemLayoutInfoPost.view,
330 pvhX, pvhY);
331 } else {
332 anim = ObjectAnimator.ofPropertyValuesHolder(menuItemLayoutInfoPost.view, pvhX);
333 }
334 } else {
335 anim = ObjectAnimator.ofPropertyValuesHolder(menuItemLayoutInfoPost.view, pvhY);
336 }
337 anim.setDuration(ITEM_ANIMATION_DURATION);
338 anim.start();
339 ItemAnimationInfo info = new ItemAnimationInfo(id, menuItemLayoutInfoPost, anim,
340 ItemAnimationInfo.MOVE);
341 mRunningItemAnimations.add(info);
342 anim.addListener(new AnimatorListenerAdapter() {
343 @Override
344 public void onAnimationEnd(Animator animation) {
345 for (int j = 0; j < mRunningItemAnimations.size(); ++j) {
346 if (mRunningItemAnimations.get(j).animator == animation) {
347 mRunningItemAnimations.remove(j);
348 break;
349 }
350 }
351 }
352 });
353 }
354 mPostLayoutItems.remove(id);
355 } else {
356 // item used to be there, is now gone
357 float oldAlpha = 1;
358 for (int j = 0; j < mRunningItemAnimations.size(); ++j) {
359 ItemAnimationInfo oldInfo = mRunningItemAnimations.get(j);
360 if (oldInfo.id == id && oldInfo.animType == ItemAnimationInfo.FADE_IN) {
361 oldAlpha = oldInfo.menuItemLayoutInfo.view.getAlpha();
362 oldInfo.animator.cancel();
363 }
364 }
365 ObjectAnimator anim = ObjectAnimator.ofFloat(menuItemLayoutInfoPre.view, View.ALPHA,
366 oldAlpha, 0);
367 // Re-using the view from pre-layout assumes no view recycling
368 ((ViewGroup) mMenuView).getOverlay().add(menuItemLayoutInfoPre.view);
369 anim.setDuration(ITEM_ANIMATION_DURATION);
370 anim.start();
371 ItemAnimationInfo info = new ItemAnimationInfo(id, menuItemLayoutInfoPre, anim, ItemAnimationInfo.FADE_OUT);
372 mRunningItemAnimations.add(info);
373 anim.addListener(new AnimatorListenerAdapter() {
374 @Override
375 public void onAnimationEnd(Animator animation) {
376 for (int j = 0; j < mRunningItemAnimations.size(); ++j) {
377 if (mRunningItemAnimations.get(j).animator == animation) {
378 mRunningItemAnimations.remove(j);
379 break;
380 }
381 }
382 ((ViewGroup) mMenuView).getOverlay().remove(menuItemLayoutInfoPre.view);
383 }
384 });
385 }
386 }
387 for (int i = 0; i < mPostLayoutItems.size(); ++i) {
388 int id = mPostLayoutItems.keyAt(i);
389 final int postLayoutIndex = mPostLayoutItems.indexOfKey(id);
390 if (postLayoutIndex >= 0) {
391 // item is new
392 final MenuItemLayoutInfo menuItemLayoutInfo =
393 mPostLayoutItems.valueAt(postLayoutIndex);
394 float oldAlpha = 0;
395 for (int j = 0; j < mRunningItemAnimations.size(); ++j) {
396 ItemAnimationInfo oldInfo = mRunningItemAnimations.get(j);
397 if (oldInfo.id == id && oldInfo.animType == ItemAnimationInfo.FADE_OUT) {
398 oldAlpha = oldInfo.menuItemLayoutInfo.view.getAlpha();
399 oldInfo.animator.cancel();
400 }
401 }
402 ObjectAnimator anim = ObjectAnimator.ofFloat(menuItemLayoutInfo.view, View.ALPHA,
403 oldAlpha, 1);
404 anim.start();
405 anim.setDuration(ITEM_ANIMATION_DURATION);
406 ItemAnimationInfo info = new ItemAnimationInfo(id, menuItemLayoutInfo, anim, ItemAnimationInfo.FADE_IN);
407 mRunningItemAnimations.add(info);
408 anim.addListener(new AnimatorListenerAdapter() {
409 @Override
410 public void onAnimationEnd(Animator animation) {
411 for (int j = 0; j < mRunningItemAnimations.size(); ++j) {
412 if (mRunningItemAnimations.get(j).animator == animation) {
413 mRunningItemAnimations.remove(j);
414 break;
415 }
416 }
417 }
418 });
419 }
420 }
421 mPreLayoutItems.clear();
422 mPostLayoutItems.clear();
423 }
424
425 /**
426 * Gets position/existence information on menu items before and after layout,
427 * which is then fed into runItemAnimations()
428 */
429 private void setupItemAnimations() {
Chet Haase952cba22015-03-20 13:17:25 -0700430 computeMenuItemAnimationInfo(true);
Chet Haase575217f2015-04-09 14:31:25 -0700431 ((View) mMenuView).getViewTreeObserver().
432 addOnPreDrawListener(mItemAnimationPreDrawListener);
Chet Haase952cba22015-03-20 13:17:25 -0700433 }
434
Adam Powell696cba52011-03-29 10:38:16 -0700435 @Override
436 public void updateMenuView(boolean cleared) {
Adam Powellad79b902013-06-19 11:33:28 -0700437 final ViewGroup menuViewParent = (ViewGroup) ((View) mMenuView).getParent();
Chet Haased96c87e2015-05-07 07:43:22 -0700438 if (menuViewParent != null && ACTIONBAR_ANIMATIONS_ENABLED) {
Chet Haase575217f2015-04-09 14:31:25 -0700439 setupItemAnimations();
Adam Powellad79b902013-06-19 11:33:28 -0700440 }
Adam Powell696cba52011-03-29 10:38:16 -0700441 super.updateMenuView(cleared);
442
Adam Powellda971082013-10-03 18:21:58 -0700443 ((View) mMenuView).requestLayout();
444
Adam Powell823f0742011-09-21 17:17:01 -0700445 if (mMenu != null) {
446 final ArrayList<MenuItemImpl> actionItems = mMenu.getActionItems();
447 final int count = actionItems.size();
448 for (int i = 0; i < count; i++) {
449 final ActionProvider provider = actionItems.get(i).getActionProvider();
450 if (provider != null) {
451 provider.setSubUiVisibilityListener(this);
452 }
453 }
454 }
455
Adam Powell275702c2011-09-23 17:34:04 -0700456 final ArrayList<MenuItemImpl> nonActionItems = mMenu != null ?
457 mMenu.getNonActionItems() : null;
458
459 boolean hasOverflow = false;
460 if (mReserveOverflow && nonActionItems != null) {
461 final int count = nonActionItems.size();
462 if (count == 1) {
463 hasOverflow = !nonActionItems.get(0).isActionViewExpanded();
464 } else {
465 hasOverflow = count > 0;
466 }
467 }
468
Adam Powell14b7e2c2011-08-12 11:11:50 -0700469 if (hasOverflow) {
Adam Powell696cba52011-03-29 10:38:16 -0700470 if (mOverflowButton == null) {
Adam Powell538e5652011-10-11 13:47:08 -0700471 mOverflowButton = new OverflowMenuButton(mSystemContext);
Adam Powell696cba52011-03-29 10:38:16 -0700472 }
473 ViewGroup parent = (ViewGroup) mOverflowButton.getParent();
474 if (parent != mMenuView) {
475 if (parent != null) {
476 parent.removeView(mOverflowButton);
477 }
Adam Powell35aecd52011-07-01 13:43:49 -0700478 ActionMenuView menuView = (ActionMenuView) mMenuView;
479 menuView.addView(mOverflowButton, menuView.generateOverflowButtonLayoutParams());
Adam Powell696cba52011-03-29 10:38:16 -0700480 }
481 } else if (mOverflowButton != null && mOverflowButton.getParent() == mMenuView) {
482 ((ViewGroup) mMenuView).removeView(mOverflowButton);
483 }
Adam Powell14b7e2c2011-08-12 11:11:50 -0700484
485 ((ActionMenuView) mMenuView).setOverflowReserved(mReserveOverflow);
Adam Powell696cba52011-03-29 10:38:16 -0700486 }
487
488 @Override
489 public boolean filterLeftoverView(ViewGroup parent, int childIndex) {
490 if (parent.getChildAt(childIndex) == mOverflowButton) return false;
491 return super.filterLeftoverView(parent, childIndex);
492 }
493
494 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
495 if (!subMenu.hasVisibleItems()) return false;
496
497 SubMenuBuilder topSubMenu = subMenu;
498 while (topSubMenu.getParentMenu() != mMenu) {
499 topSubMenu = (SubMenuBuilder) topSubMenu.getParentMenu();
500 }
501 View anchor = findViewForItem(topSubMenu.getItem());
Adam Powell11ed1d62011-07-11 21:19:59 -0700502 if (anchor == null) {
Oren Blasberg8e12f8d2015-09-02 14:25:56 -0700503 // This means the submenu was opened from an overflow menu item, indicating the
504 // MenuPopupHelper will handle opening the submenu via its MenuPopup. Return false to
505 // ensure that the MenuPopup acts as presenter for the submenu, and acts on its
506 // responsibility to display the new submenu.
507 return false;
Adam Powell11ed1d62011-07-11 21:19:59 -0700508 }
Adam Powell696cba52011-03-29 10:38:16 -0700509
Adam Powell11ed1d62011-07-11 21:19:59 -0700510 mOpenSubMenuId = subMenu.getItem().getItemId();
Oren Blasberg8e12f8d2015-09-02 14:25:56 -0700511
512 boolean preserveIconSpacing = false;
513 final int count = subMenu.size();
514 for (int i = 0; i < count; i++) {
515 MenuItem childItem = subMenu.getItem(i);
516 if (childItem.isVisible() && childItem.getIcon() != null) {
517 preserveIconSpacing = true;
518 break;
519 }
520 }
521
522 mActionButtonPopup = new ActionButtonSubmenu(mContext, subMenu, anchor);
523 mActionButtonPopup.setForceShowIcon(preserveIconSpacing);
Adam Powell696cba52011-03-29 10:38:16 -0700524 mActionButtonPopup.show();
Oren Blasberg8e12f8d2015-09-02 14:25:56 -0700525
Adam Powell696cba52011-03-29 10:38:16 -0700526 super.onSubMenuSelected(subMenu);
527 return true;
528 }
529
530 private View findViewForItem(MenuItem item) {
531 final ViewGroup parent = (ViewGroup) mMenuView;
532 if (parent == null) return null;
533
534 final int count = parent.getChildCount();
535 for (int i = 0; i < count; i++) {
536 final View child = parent.getChildAt(i);
537 if (child instanceof MenuView.ItemView &&
538 ((MenuView.ItemView) child).getItemData() == item) {
539 return child;
540 }
541 }
542 return null;
543 }
544
545 /**
546 * Display the overflow menu if one is present.
547 * @return true if the overflow menu was shown, false otherwise.
548 */
549 public boolean showOverflowMenu() {
Adam Powell70e9f4b2011-08-22 16:31:24 -0700550 if (mReserveOverflow && !isOverflowMenuShowing() && mMenu != null && mMenuView != null &&
Jake Whartona6476402012-03-29 01:42:37 -0700551 mPostedOpenRunnable == null && !mMenu.getNonActionItems().isEmpty()) {
Adam Powell696cba52011-03-29 10:38:16 -0700552 OverflowPopup popup = new OverflowPopup(mContext, mMenu, mOverflowButton, true);
553 mPostedOpenRunnable = new OpenOverflowRunnable(popup);
554 // Post this for later; we might still need a layout for the anchor to be right.
555 ((View) mMenuView).post(mPostedOpenRunnable);
556
557 // ActionMenuPresenter uses null as a callback argument here
558 // to indicate overflow is opening.
559 super.onSubMenuSelected(null);
560
561 return true;
562 }
563 return false;
564 }
565
566 /**
567 * Hide the overflow menu if it is currently showing.
568 *
569 * @return true if the overflow menu was hidden, false otherwise.
570 */
571 public boolean hideOverflowMenu() {
572 if (mPostedOpenRunnable != null && mMenuView != null) {
573 ((View) mMenuView).removeCallbacks(mPostedOpenRunnable);
Adam Powell678ed0c2011-10-27 17:46:07 -0700574 mPostedOpenRunnable = null;
Adam Powell696cba52011-03-29 10:38:16 -0700575 return true;
576 }
577
578 MenuPopupHelper popup = mOverflowPopup;
579 if (popup != null) {
580 popup.dismiss();
581 return true;
582 }
583 return false;
584 }
585
586 /**
587 * Dismiss all popup menus - overflow and submenus.
588 * @return true if popups were dismissed, false otherwise. (This can be because none were open.)
589 */
Mathew Inwood978c6e22018-08-21 15:58:55 +0100590 @UnsupportedAppUsage
Adam Powell696cba52011-03-29 10:38:16 -0700591 public boolean dismissPopupMenus() {
592 boolean result = hideOverflowMenu();
593 result |= hideSubMenus();
594 return result;
595 }
596
597 /**
598 * Dismiss all submenu popups.
599 *
600 * @return true if popups were dismissed, false otherwise. (This can be because none were open.)
601 */
602 public boolean hideSubMenus() {
603 if (mActionButtonPopup != null) {
604 mActionButtonPopup.dismiss();
605 return true;
606 }
607 return false;
608 }
609
610 /**
611 * @return true if the overflow menu is currently showing
612 */
Mathew Inwood978c6e22018-08-21 15:58:55 +0100613 @UnsupportedAppUsage
Adam Powell696cba52011-03-29 10:38:16 -0700614 public boolean isOverflowMenuShowing() {
615 return mOverflowPopup != null && mOverflowPopup.isShowing();
616 }
617
Adam Powell5fcf5b92013-09-11 08:45:36 -0700618 public boolean isOverflowMenuShowPending() {
619 return mPostedOpenRunnable != null || isOverflowMenuShowing();
620 }
621
Adam Powell696cba52011-03-29 10:38:16 -0700622 /**
623 * @return true if space has been reserved in the action menu for an overflow item.
624 */
625 public boolean isOverflowReserved() {
626 return mReserveOverflow;
627 }
628
629 public boolean flagActionItems() {
Alan Viverette28a84682016-01-04 13:43:23 -0500630 final ArrayList<MenuItemImpl> visibleItems;
631 final int itemsSize;
632 if (mMenu != null) {
633 visibleItems = mMenu.getVisibleItems();
634 itemsSize = visibleItems.size();
635 } else {
636 visibleItems = null;
637 itemsSize = 0;
638 }
639
Adam Powell696cba52011-03-29 10:38:16 -0700640 int maxActions = mMaxItems;
641 int widthLimit = mActionItemWidthLimit;
642 final int querySpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
643 final ViewGroup parent = (ViewGroup) mMenuView;
644
645 int requiredItems = 0;
646 int requestedItems = 0;
647 int firstActionWidth = 0;
648 boolean hasOverflow = false;
649 for (int i = 0; i < itemsSize; i++) {
650 MenuItemImpl item = visibleItems.get(i);
651 if (item.requiresActionButton()) {
652 requiredItems++;
653 } else if (item.requestsActionButton()) {
654 requestedItems++;
655 } else {
656 hasOverflow = true;
657 }
Adam Powellb187cd92011-07-20 14:17:56 -0700658 if (mExpandedActionViewsExclusive && item.isActionViewExpanded()) {
659 // Overflow everything if we have an expanded action view and we're
660 // space constrained.
661 maxActions = 0;
662 }
Adam Powell696cba52011-03-29 10:38:16 -0700663 }
664
665 // Reserve a spot for the overflow item if needed.
666 if (mReserveOverflow &&
667 (hasOverflow || requiredItems + requestedItems > maxActions)) {
668 maxActions--;
669 }
670 maxActions -= requiredItems;
671
672 final SparseBooleanArray seenGroups = mActionButtonGroups;
673 seenGroups.clear();
674
Adam Powell35aecd52011-07-01 13:43:49 -0700675 int cellSize = 0;
676 int cellsRemaining = 0;
677 if (mStrictWidthLimit) {
678 cellsRemaining = widthLimit / mMinCellSize;
679 final int cellSizeRemaining = widthLimit % mMinCellSize;
680 cellSize = mMinCellSize + cellSizeRemaining / cellsRemaining;
681 }
682
Adam Powell696cba52011-03-29 10:38:16 -0700683 // Flag as many more requested items as will fit.
684 for (int i = 0; i < itemsSize; i++) {
685 MenuItemImpl item = visibleItems.get(i);
686
687 if (item.requiresActionButton()) {
Chet Haase952cba22015-03-20 13:17:25 -0700688 View v = getItemView(item, null, parent);
Adam Powell35aecd52011-07-01 13:43:49 -0700689 if (mStrictWidthLimit) {
690 cellsRemaining -= ActionMenuView.measureChildForCells(v,
691 cellSize, cellsRemaining, querySpec, 0);
692 } else {
693 v.measure(querySpec, querySpec);
694 }
Adam Powell696cba52011-03-29 10:38:16 -0700695 final int measuredWidth = v.getMeasuredWidth();
696 widthLimit -= measuredWidth;
697 if (firstActionWidth == 0) {
698 firstActionWidth = measuredWidth;
699 }
700 final int groupId = item.getGroupId();
701 if (groupId != 0) {
702 seenGroups.put(groupId, true);
703 }
Adam Powellea1ca952011-06-21 14:07:59 -0700704 item.setIsActionButton(true);
Adam Powell696cba52011-03-29 10:38:16 -0700705 } else if (item.requestsActionButton()) {
706 // Items in a group with other items that already have an action slot
707 // can break the max actions rule, but not the width limit.
708 final int groupId = item.getGroupId();
709 final boolean inGroup = seenGroups.get(groupId);
Adam Powell35aecd52011-07-01 13:43:49 -0700710 boolean isAction = (maxActions > 0 || inGroup) && widthLimit > 0 &&
711 (!mStrictWidthLimit || cellsRemaining > 0);
Adam Powell696cba52011-03-29 10:38:16 -0700712
713 if (isAction) {
Chet Haase952cba22015-03-20 13:17:25 -0700714 View v = getItemView(item, null, parent);
Adam Powell35aecd52011-07-01 13:43:49 -0700715 if (mStrictWidthLimit) {
716 final int cells = ActionMenuView.measureChildForCells(v,
717 cellSize, cellsRemaining, querySpec, 0);
718 cellsRemaining -= cells;
719 if (cells == 0) {
720 isAction = false;
721 }
722 } else {
723 v.measure(querySpec, querySpec);
724 }
Adam Powell696cba52011-03-29 10:38:16 -0700725 final int measuredWidth = v.getMeasuredWidth();
726 widthLimit -= measuredWidth;
727 if (firstActionWidth == 0) {
728 firstActionWidth = measuredWidth;
729 }
730
Adam Powell640a66e2011-04-29 10:18:53 -0700731 if (mStrictWidthLimit) {
Adam Powell35aecd52011-07-01 13:43:49 -0700732 isAction &= widthLimit >= 0;
Adam Powell640a66e2011-04-29 10:18:53 -0700733 } else {
734 // Did this push the entire first item past the limit?
Adam Powell35aecd52011-07-01 13:43:49 -0700735 isAction &= widthLimit + firstActionWidth > 0;
Adam Powell696cba52011-03-29 10:38:16 -0700736 }
737 }
738
739 if (isAction && groupId != 0) {
740 seenGroups.put(groupId, true);
741 } else if (inGroup) {
742 // We broke the width limit. Demote the whole group, they all overflow now.
743 seenGroups.put(groupId, false);
744 for (int j = 0; j < i; j++) {
745 MenuItemImpl areYouMyGroupie = visibleItems.get(j);
746 if (areYouMyGroupie.getGroupId() == groupId) {
Adam Powell23f4cc02011-08-18 10:30:46 -0700747 // Give back the action slot
748 if (areYouMyGroupie.isActionButton()) maxActions++;
Adam Powell696cba52011-03-29 10:38:16 -0700749 areYouMyGroupie.setIsActionButton(false);
750 }
751 }
752 }
753
Adam Powell23f4cc02011-08-18 10:30:46 -0700754 if (isAction) maxActions--;
755
Adam Powell696cba52011-03-29 10:38:16 -0700756 item.setIsActionButton(isAction);
Svetoslav Ganov14c46692012-10-09 18:11:36 -0700757 } else {
758 // Neither requires nor requests an action button.
759 item.setIsActionButton(false);
Adam Powell696cba52011-03-29 10:38:16 -0700760 }
761 }
762 return true;
763 }
764
765 @Override
766 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
767 dismissPopupMenus();
768 super.onCloseMenu(menu, allMenusAreClosing);
769 }
770
Adam Powell11ed1d62011-07-11 21:19:59 -0700771 @Override
Mathew Inwood978c6e22018-08-21 15:58:55 +0100772 @UnsupportedAppUsage
Adam Powell11ed1d62011-07-11 21:19:59 -0700773 public Parcelable onSaveInstanceState() {
774 SavedState state = new SavedState();
775 state.openSubMenuId = mOpenSubMenuId;
776 return state;
777 }
778
779 @Override
Mathew Inwood978c6e22018-08-21 15:58:55 +0100780 @UnsupportedAppUsage
Adam Powell11ed1d62011-07-11 21:19:59 -0700781 public void onRestoreInstanceState(Parcelable state) {
782 SavedState saved = (SavedState) state;
783 if (saved.openSubMenuId > 0) {
784 MenuItem item = mMenu.findItem(saved.openSubMenuId);
785 if (item != null) {
786 SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
787 onSubMenuSelected(subMenu);
788 }
789 }
790 }
791
Adam Powell823f0742011-09-21 17:17:01 -0700792 @Override
793 public void onSubUiVisibilityChanged(boolean isVisible) {
794 if (isVisible) {
795 // Not a submenu, but treat it like one.
796 super.onSubMenuSelected(null);
Alan Viverette28a84682016-01-04 13:43:23 -0500797 } else if (mMenu != null) {
Alan Viverette00aa5102015-11-03 13:03:15 -0500798 mMenu.close(false /* closeAllMenus */);
Adam Powell823f0742011-09-21 17:17:01 -0700799 }
800 }
801
Adam Powellfa18d182014-01-07 15:56:59 -0800802 public void setMenuView(ActionMenuView menuView) {
Chet Haase575217f2015-04-09 14:31:25 -0700803 if (menuView != mMenuView) {
804 if (mMenuView != null) {
805 ((View) mMenuView).removeOnAttachStateChangeListener(mAttachStateChangeListener);
806 }
807 mMenuView = menuView;
808 menuView.initialize(mMenu);
809 menuView.addOnAttachStateChangeListener(mAttachStateChangeListener);
810 }
Adam Powellfa18d182014-01-07 15:56:59 -0800811 }
812
Adam Powell11ed1d62011-07-11 21:19:59 -0700813 private static class SavedState implements Parcelable {
814 public int openSubMenuId;
815
816 SavedState() {
817 }
818
819 SavedState(Parcel in) {
820 openSubMenuId = in.readInt();
821 }
822
823 @Override
824 public int describeContents() {
825 return 0;
826 }
827
828 @Override
829 public void writeToParcel(Parcel dest, int flags) {
830 dest.writeInt(openSubMenuId);
831 }
832
833 public static final Parcelable.Creator<SavedState> CREATOR
834 = new Parcelable.Creator<SavedState>() {
835 public SavedState createFromParcel(Parcel in) {
836 return new SavedState(in);
837 }
838
839 public SavedState[] newArray(int size) {
840 return new SavedState[size];
841 }
842 };
843 }
844
Adam Powellfa18d182014-01-07 15:56:59 -0800845 private class OverflowMenuButton extends ImageButton implements ActionMenuView.ActionMenuChildView {
Adam Powell696cba52011-03-29 10:38:16 -0700846 public OverflowMenuButton(Context context) {
847 super(context, null, com.android.internal.R.attr.actionOverflowButtonStyle);
848
849 setClickable(true);
850 setFocusable(true);
851 setVisibility(VISIBLE);
852 setEnabled(true);
Alan Viveretteca6a36112013-08-16 14:41:06 -0700853
Alan Viverette69960142013-08-22 17:26:57 -0700854 setOnTouchListener(new ForwardingListener(this) {
Alan Viveretteca6a36112013-08-16 14:41:06 -0700855 @Override
Oren Blasbergf44d90b2015-08-31 14:15:26 -0700856 public ShowableListMenu getPopup() {
Alan Viveretteca6a36112013-08-16 14:41:06 -0700857 if (mOverflowPopup == null) {
858 return null;
859 }
860
861 return mOverflowPopup.getPopup();
862 }
863
864 @Override
865 public boolean onForwardingStarted() {
866 showOverflowMenu();
867 return true;
868 }
869
870 @Override
871 public boolean onForwardingStopped() {
872 // Displaying the popup occurs asynchronously, so wait for
873 // the runnable to finish before deciding whether to stop
874 // forwarding.
875 if (mPostedOpenRunnable != null) {
876 return false;
877 }
878
879 hideOverflowMenu();
880 return true;
881 }
882 });
Adam Powell696cba52011-03-29 10:38:16 -0700883 }
884
885 @Override
886 public boolean performClick() {
887 if (super.performClick()) {
888 return true;
889 }
890
891 playSoundEffect(SoundEffectConstants.CLICK);
892 showOverflowMenu();
893 return true;
894 }
895
Alan Viverette80e72702013-07-29 11:12:59 -0700896 @Override
Adam Powell696cba52011-03-29 10:38:16 -0700897 public boolean needsDividerBefore() {
Adam Powell35aecd52011-07-01 13:43:49 -0700898 return false;
Adam Powell696cba52011-03-29 10:38:16 -0700899 }
900
Alan Viverette80e72702013-07-29 11:12:59 -0700901 @Override
Adam Powell696cba52011-03-29 10:38:16 -0700902 public boolean needsDividerAfter() {
903 return false;
904 }
Adam Powelld5c81db2012-08-02 14:35:26 -0700905
Alan Viverettea54956a2015-01-07 16:05:02 -0800906 /** @hide */
Adam Powelld5c81db2012-08-02 14:35:26 -0700907 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800908 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
909 super.onInitializeAccessibilityNodeInfoInternal(info);
Svetoslav Ganovcb8ed392013-08-23 20:37:28 -0700910 info.setCanOpenPopup(true);
Alan Viverette058ac7c2013-08-19 16:44:30 -0700911 }
Alan Viverettef023c252014-08-28 13:55:18 -0700912
913 @Override
Alan Viverette9d9581f2014-09-12 12:54:02 -0700914 protected boolean setFrame(int l, int t, int r, int b) {
915 final boolean changed = super.setFrame(l, t, r, b);
Alan Viverettef023c252014-08-28 13:55:18 -0700916
Alan Viverette199ea8c2014-11-20 17:09:27 -0800917 // Set up the hotspot bounds to square and centered on the image.
Alan Viverettef023c252014-08-28 13:55:18 -0700918 final Drawable d = getDrawable();
919 final Drawable bg = getBackground();
920 if (d != null && bg != null) {
Alan Viverette199ea8c2014-11-20 17:09:27 -0800921 final int width = getWidth();
922 final int height = getHeight();
923 final int halfEdge = Math.max(width, height) / 2;
924 final int offsetX = getPaddingLeft() - getPaddingRight();
925 final int offsetY = getPaddingTop() - getPaddingBottom();
926 final int centerX = (width + offsetX) / 2;
927 final int centerY = (height + offsetY) / 2;
928 bg.setHotspotBounds(centerX - halfEdge, centerY - halfEdge,
929 centerX + halfEdge, centerY + halfEdge);
Alan Viverettef023c252014-08-28 13:55:18 -0700930 }
Alan Viverette9d9581f2014-09-12 12:54:02 -0700931
932 return changed;
Alan Viverettef023c252014-08-28 13:55:18 -0700933 }
Adam Powell696cba52011-03-29 10:38:16 -0700934 }
935
936 private class OverflowPopup extends MenuPopupHelper {
937 public OverflowPopup(Context context, MenuBuilder menu, View anchorView,
938 boolean overflowOnly) {
Alan Viverette560f1702014-05-05 14:40:07 -0700939 super(context, menu, anchorView, overflowOnly,
940 com.android.internal.R.attr.actionOverflowMenuStyle);
Adam Powell54c94de2013-09-26 15:36:34 -0700941 setGravity(Gravity.END);
Alan Viverette021627e2015-11-25 14:22:00 -0500942 setPresenterCallback(mPopupPresenterCallback);
Adam Powell696cba52011-03-29 10:38:16 -0700943 }
944
945 @Override
Alan Viverette708aa9d2015-11-20 15:21:30 -0500946 protected void onDismiss() {
Alan Viverette28a84682016-01-04 13:43:23 -0500947 if (mMenu != null) {
948 mMenu.close();
949 }
Adam Powell696cba52011-03-29 10:38:16 -0700950 mOverflowPopup = null;
Alan Viverette708aa9d2015-11-20 15:21:30 -0500951
952 super.onDismiss();
Adam Powell696cba52011-03-29 10:38:16 -0700953 }
954 }
955
956 private class ActionButtonSubmenu extends MenuPopupHelper {
Oren Blasberg8e12f8d2015-09-02 14:25:56 -0700957 public ActionButtonSubmenu(Context context, SubMenuBuilder subMenu, View anchorView) {
958 super(context, subMenu, anchorView, false,
Alan Viveretted361a4f2014-06-30 16:47:40 -0700959 com.android.internal.R.attr.actionOverflowMenuStyle);
Adam Powell696cba52011-03-29 10:38:16 -0700960
961 MenuItemImpl item = (MenuItemImpl) subMenu.getItem();
962 if (!item.isActionButton()) {
963 // Give a reasonable anchor to nested submenus.
964 setAnchorView(mOverflowButton == null ? (View) mMenuView : mOverflowButton);
965 }
Adam Powell11ed1d62011-07-11 21:19:59 -0700966
Alan Viverette021627e2015-11-25 14:22:00 -0500967 setPresenterCallback(mPopupPresenterCallback);
Adam Powell696cba52011-03-29 10:38:16 -0700968 }
969
970 @Override
Alan Viverette708aa9d2015-11-20 15:21:30 -0500971 protected void onDismiss() {
Adam Powell696cba52011-03-29 10:38:16 -0700972 mActionButtonPopup = null;
Adam Powell11ed1d62011-07-11 21:19:59 -0700973 mOpenSubMenuId = 0;
Alan Viverette708aa9d2015-11-20 15:21:30 -0500974
975 super.onDismiss();
Adam Powell11ed1d62011-07-11 21:19:59 -0700976 }
977 }
978
Adam Powellfa18d182014-01-07 15:56:59 -0800979 private class PopupPresenterCallback implements Callback {
Adam Powell11ed1d62011-07-11 21:19:59 -0700980
981 @Override
982 public boolean onOpenSubMenu(MenuBuilder subMenu) {
Adam Powell823f0742011-09-21 17:17:01 -0700983 if (subMenu == null) return false;
984
Adam Powell11ed1d62011-07-11 21:19:59 -0700985 mOpenSubMenuId = ((SubMenuBuilder) subMenu).getItem().getItemId();
Adam Powellfa18d182014-01-07 15:56:59 -0800986 final Callback cb = getCallback();
Adam Powellb411b322013-11-01 18:22:11 -0700987 return cb != null ? cb.onOpenSubMenu(subMenu) : false;
Adam Powell11ed1d62011-07-11 21:19:59 -0700988 }
989
990 @Override
991 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
Adam Powell823f0742011-09-21 17:17:01 -0700992 if (menu instanceof SubMenuBuilder) {
Alan Viverette00aa5102015-11-03 13:03:15 -0500993 menu.getRootMenu().close(false /* closeAllMenus */);
Adam Powell823f0742011-09-21 17:17:01 -0700994 }
Adam Powellfa18d182014-01-07 15:56:59 -0800995 final Callback cb = getCallback();
Adam Powellb411b322013-11-01 18:22:11 -0700996 if (cb != null) {
997 cb.onCloseMenu(menu, allMenusAreClosing);
998 }
Adam Powell696cba52011-03-29 10:38:16 -0700999 }
1000 }
1001
1002 private class OpenOverflowRunnable implements Runnable {
1003 private OverflowPopup mPopup;
1004
1005 public OpenOverflowRunnable(OverflowPopup popup) {
1006 mPopup = popup;
1007 }
1008
1009 public void run() {
Alan Viverette28a84682016-01-04 13:43:23 -05001010 if (mMenu != null) {
1011 mMenu.changeMenuMode();
1012 }
Adam Powell678ed0c2011-10-27 17:46:07 -07001013 final View menuView = (View) mMenuView;
1014 if (menuView != null && menuView.getWindowToken() != null && mPopup.tryShow()) {
Adam Powell696cba52011-03-29 10:38:16 -07001015 mOverflowPopup = mPopup;
Adam Powell696cba52011-03-29 10:38:16 -07001016 }
Adam Powell678ed0c2011-10-27 17:46:07 -07001017 mPostedOpenRunnable = null;
Adam Powell696cba52011-03-29 10:38:16 -07001018 }
1019 }
Alan Viverette56110722014-01-10 14:03:21 -08001020
1021 private class ActionMenuPopupCallback extends ActionMenuItemView.PopupCallback {
1022 @Override
Oren Blasbergf44d90b2015-08-31 14:15:26 -07001023 public ShowableListMenu getPopup() {
Alan Viverette56110722014-01-10 14:03:21 -08001024 return mActionButtonPopup != null ? mActionButtonPopup.getPopup() : null;
1025 }
1026 }
Chris Banes9cc36ca2015-02-23 12:30:13 +00001027
Chet Haase952cba22015-03-20 13:17:25 -07001028 /**
1029 * This class holds layout information for a menu item. This is used to determine
1030 * pre- and post-layout information about menu items, which will then be used to
1031 * determine appropriate item animations.
1032 */
1033 private static class MenuItemLayoutInfo {
1034 View view;
1035 int left;
1036 int top;
1037
1038 MenuItemLayoutInfo(View view, boolean preLayout) {
1039 left = view.getLeft();
1040 top = view.getTop();
1041 if (preLayout) {
1042 // We track translation for pre-layout because a view might be mid-animation
1043 // and we need this information to know where to animate from
1044 left += view.getTranslationX();
1045 top += view.getTranslationY();
1046 }
1047 this.view = view;
1048 }
1049 }
1050
1051 /**
1052 * This class is used to store information about currently-running item animations.
1053 * This is used when new animations are scheduled to determine whether any existing
1054 * animations need to be canceled, based on whether the running animations overlap
1055 * with any new animations. For example, if an item is currently animating from
1056 * location A to B and another change dictates that it be animated to C, then the current
1057 * A-B animation will be canceled and a new animation to C will be started.
1058 */
1059 private static class ItemAnimationInfo {
1060 int id;
1061 MenuItemLayoutInfo menuItemLayoutInfo;
1062 Animator animator;
1063 int animType;
1064 static final int MOVE = 0;
1065 static final int FADE_IN = 1;
1066 static final int FADE_OUT = 2;
1067
1068 ItemAnimationInfo(int id, MenuItemLayoutInfo info, Animator anim, int animType) {
1069 this.id = id;
1070 menuItemLayoutInfo = info;
1071 animator = anim;
1072 this.animType = animType;
1073 }
1074 }
Adam Powell696cba52011-03-29 10:38:16 -07001075}