Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 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 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 17 | package android.widget; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 18 | |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ObjectAnimator; |
| 22 | import android.animation.PropertyValuesHolder; |
Alan Viverette | 28a8468 | 2016-01-04 13:43:23 -0500 | [diff] [blame] | 23 | import android.annotation.NonNull; |
| 24 | import android.annotation.Nullable; |
Artur Satayev | ad9254c | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 25 | import android.compat.annotation.UnsupportedAppUsage; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Adam Powell | bfcdfaf | 2011-08-19 11:46:59 -0700 | [diff] [blame] | 27 | import android.content.res.Configuration; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 28 | import android.content.res.Resources; |
Alan Viverette | f023c25 | 2014-08-28 13:55:18 -0700 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 30 | import android.os.Parcel; |
| 31 | import android.os.Parcelable; |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 32 | import android.util.SparseArray; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 33 | import android.util.SparseBooleanArray; |
Adam Powell | 823f074 | 2011-09-21 17:17:01 -0700 | [diff] [blame] | 34 | import android.view.ActionProvider; |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 35 | import android.view.Gravity; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 36 | import android.view.MenuItem; |
| 37 | import android.view.SoundEffectConstants; |
| 38 | import android.view.View; |
| 39 | import android.view.View.MeasureSpec; |
| 40 | import android.view.ViewGroup; |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 41 | import android.view.ViewTreeObserver; |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 42 | import android.view.accessibility.AccessibilityNodeInfo; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 43 | |
Alan Viverette | 80e7270 | 2013-07-29 11:12:59 -0700 | [diff] [blame] | 44 | import com.android.internal.view.ActionBarPolicy; |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 45 | import com.android.internal.view.menu.ActionMenuItemView; |
| 46 | import com.android.internal.view.menu.BaseMenuPresenter; |
| 47 | import com.android.internal.view.menu.MenuBuilder; |
| 48 | import com.android.internal.view.menu.MenuItemImpl; |
| 49 | import com.android.internal.view.menu.MenuPopupHelper; |
| 50 | import com.android.internal.view.menu.MenuView; |
Oren Blasberg | f44d90b | 2015-08-31 14:15:26 -0700 | [diff] [blame] | 51 | import com.android.internal.view.menu.ShowableListMenu; |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 52 | import com.android.internal.view.menu.SubMenuBuilder; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 53 | |
| 54 | import java.util.ArrayList; |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 55 | import java.util.List; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * MenuPresenter for building action menus as seen in the action bar and action modes. |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 59 | * |
| 60 | * @hide |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 61 | */ |
Adam Powell | 823f074 | 2011-09-21 17:17:01 -0700 | [diff] [blame] | 62 | public class ActionMenuPresenter extends BaseMenuPresenter |
| 63 | implements ActionProvider.SubUiVisibilityListener { |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 64 | private static final int ITEM_ANIMATION_DURATION = 150; |
Chet Haase | d96c87e | 2015-05-07 07:43:22 -0700 | [diff] [blame] | 65 | private static final boolean ACTIONBAR_ANIMATIONS_ENABLED = false; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 66 | |
Chris Banes | 9cc36ca | 2015-02-23 12:30:13 +0000 | [diff] [blame] | 67 | private OverflowMenuButton mOverflowButton; |
Chris Banes | a41b789 | 2015-06-09 13:36:44 +0000 | [diff] [blame] | 68 | private Drawable mPendingOverflowIcon; |
| 69 | private boolean mPendingOverflowIconSet; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 70 | private boolean mReserveOverflow; |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 71 | private boolean mReserveOverflowSet; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 72 | private int mWidthLimit; |
| 73 | private int mActionItemWidthLimit; |
| 74 | private int mMaxItems; |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 75 | private boolean mMaxItemsSet; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 76 | private boolean mStrictWidthLimit; |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 77 | private boolean mWidthLimitSet; |
Adam Powell | b187cd9 | 2011-07-20 14:17:56 -0700 | [diff] [blame] | 78 | private boolean mExpandedActionViewsExclusive; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 79 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 80 | private int mMinCellSize; |
| 81 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 82 | // Group IDs that have been added as actions - used temporarily, allocated here for reuse. |
| 83 | private final SparseBooleanArray mActionButtonGroups = new SparseBooleanArray(); |
| 84 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 85 | private OverflowPopup mOverflowPopup; |
| 86 | private ActionButtonSubmenu mActionButtonPopup; |
| 87 | |
| 88 | private OpenOverflowRunnable mPostedOpenRunnable; |
Alan Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 89 | private ActionMenuPopupCallback mPopupCallback; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 90 | |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 91 | final PopupPresenterCallback mPopupPresenterCallback = new PopupPresenterCallback(); |
| 92 | int mOpenSubMenuId; |
| 93 | |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 94 | // 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 Viverette | 02cd0f9 | 2016-01-13 13:33:17 -0500 | [diff] [blame] | 96 | private SparseArray<MenuItemLayoutInfo> mPreLayoutItems = new SparseArray<>(); |
| 97 | private SparseArray<MenuItemLayoutInfo> mPostLayoutItems = new SparseArray<>(); |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 98 | |
| 99 | // The list of currently running animations on menu items. |
Alan Viverette | 02cd0f9 | 2016-01-13 13:33:17 -0500 | [diff] [blame] | 100 | private List<ItemAnimationInfo> mRunningItemAnimations = new ArrayList<>(); |
Chet Haase | 575217f | 2015-04-09 14:31:25 -0700 | [diff] [blame] | 101 | 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 Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 116 | |
Chet Haase | 575217f | 2015-04-09 14:31:25 -0700 | [diff] [blame] | 117 | @Override |
| 118 | public void onViewDetachedFromWindow(View v) { |
| 119 | ((View) mMenuView).getViewTreeObserver().removeOnPreDrawListener( |
| 120 | mItemAnimationPreDrawListener); |
| 121 | mPreLayoutItems.clear(); |
| 122 | mPostLayoutItems.clear(); |
| 123 | } |
| 124 | }; |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 125 | |
| 126 | |
Adam Powell | 538e565 | 2011-10-11 13:47:08 -0700 | [diff] [blame] | 127 | public ActionMenuPresenter(Context context) { |
| 128 | super(context, com.android.internal.R.layout.action_menu_layout, |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 129 | com.android.internal.R.layout.action_menu_item_layout); |
| 130 | } |
| 131 | |
| 132 | @Override |
Alan Viverette | 28a8468 | 2016-01-04 13:43:23 -0500 | [diff] [blame] | 133 | public void initForMenu(@NonNull Context context, @Nullable MenuBuilder menu) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 134 | super.initForMenu(context, menu); |
| 135 | |
| 136 | final Resources res = context.getResources(); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 137 | |
Adam Powell | b8139af | 2012-04-19 13:52:46 -0700 | [diff] [blame] | 138 | final ActionBarPolicy abp = ActionBarPolicy.get(context); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 139 | if (!mReserveOverflowSet) { |
Adam Powell | b8139af | 2012-04-19 13:52:46 -0700 | [diff] [blame] | 140 | mReserveOverflow = abp.showsOverflowMenuButton(); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if (!mWidthLimitSet) { |
Adam Powell | b8139af | 2012-04-19 13:52:46 -0700 | [diff] [blame] | 144 | mWidthLimit = abp.getEmbeddedMenuWidthLimit(); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 145 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 146 | |
| 147 | // Measure for initial configuration |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 148 | if (!mMaxItemsSet) { |
Adam Powell | b8139af | 2012-04-19 13:52:46 -0700 | [diff] [blame] | 149 | mMaxItems = abp.getMaxActionButtons(); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 150 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 151 | |
| 152 | int width = mWidthLimit; |
| 153 | if (mReserveOverflow) { |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 154 | if (mOverflowButton == null) { |
Adam Powell | 538e565 | 2011-10-11 13:47:08 -0700 | [diff] [blame] | 155 | mOverflowButton = new OverflowMenuButton(mSystemContext); |
Chris Banes | a41b789 | 2015-06-09 13:36:44 +0000 | [diff] [blame] | 156 | if (mPendingOverflowIconSet) { |
| 157 | mOverflowButton.setImageDrawable(mPendingOverflowIcon); |
| 158 | mPendingOverflowIcon = null; |
| 159 | mPendingOverflowIconSet = false; |
| 160 | } |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 161 | final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 162 | mOverflowButton.measure(spec, spec); |
| 163 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 164 | width -= mOverflowButton.getMeasuredWidth(); |
| 165 | } else { |
| 166 | mOverflowButton = null; |
| 167 | } |
| 168 | |
| 169 | mActionItemWidthLimit = width; |
| 170 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 171 | mMinCellSize = (int) (ActionMenuView.MIN_CELL_SIZE * res.getDisplayMetrics().density); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Adam Powell | bfcdfaf | 2011-08-19 11:46:59 -0700 | [diff] [blame] | 174 | public void onConfigurationChanged(Configuration newConfig) { |
| 175 | if (!mMaxItemsSet) { |
Filip Gruszczynski | 6c54e72 | 2015-11-17 12:24:49 -0800 | [diff] [blame] | 176 | mMaxItems = ActionBarPolicy.get(mContext).getMaxActionButtons(); |
Adam Powell | f203e0a | 2012-03-06 17:40:12 -0800 | [diff] [blame] | 177 | } |
| 178 | if (mMenu != null) { |
| 179 | mMenu.onItemsChanged(true); |
Adam Powell | bfcdfaf | 2011-08-19 11:46:59 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 183 | public void setWidthLimit(int width, boolean strict) { |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 184 | mWidthLimit = width; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 185 | mStrictWidthLimit = strict; |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 186 | mWidthLimitSet = true; |
| 187 | } |
| 188 | |
| 189 | public void setReserveOverflow(boolean reserveOverflow) { |
| 190 | mReserveOverflow = reserveOverflow; |
| 191 | mReserveOverflowSet = true; |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | public void setItemLimit(int itemCount) { |
| 195 | mMaxItems = itemCount; |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 196 | mMaxItemsSet = true; |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Adam Powell | b187cd9 | 2011-07-20 14:17:56 -0700 | [diff] [blame] | 199 | public void setExpandedActionViewsExclusive(boolean isExclusive) { |
| 200 | mExpandedActionViewsExclusive = isExclusive; |
| 201 | } |
| 202 | |
Chris Banes | a41b789 | 2015-06-09 13:36:44 +0000 | [diff] [blame] | 203 | 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 Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 221 | @Override |
| 222 | public MenuView getMenuView(ViewGroup root) { |
Chet Haase | 575217f | 2015-04-09 14:31:25 -0700 | [diff] [blame] | 223 | MenuView oldMenuView = mMenuView; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 224 | MenuView result = super.getMenuView(root); |
Chet Haase | 575217f | 2015-04-09 14:31:25 -0700 | [diff] [blame] | 225 | 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 Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 232 | return result; |
| 233 | } |
| 234 | |
| 235 | @Override |
Alan Viverette | fbe4a58 | 2013-09-06 13:45:54 -0700 | [diff] [blame] | 236 | public View getItemView(final MenuItemImpl item, View convertView, ViewGroup parent) { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 237 | View actionView = item.getActionView(); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 238 | if (actionView == null || item.hasCollapsibleActionView()) { |
Adam Powell | c3ca3ea | 2013-10-10 18:07:48 -0700 | [diff] [blame] | 239 | actionView = super.getItemView(item, convertView, parent); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 240 | } |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 241 | actionView.setVisibility(item.isActionViewExpanded() ? View.GONE : View.VISIBLE); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 242 | |
Adam Powell | fe9c354 | 2014-01-10 18:32:08 +0000 | [diff] [blame] | 243 | 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 Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 252 | public void bindItemView(MenuItemImpl item, MenuView.ItemView itemView) { |
Adam Powell | fe9c354 | 2014-01-10 18:32:08 +0000 | [diff] [blame] | 253 | itemView.initialize(item, 0); |
| 254 | |
| 255 | final ActionMenuView menuView = (ActionMenuView) mMenuView; |
| 256 | final ActionMenuItemView actionItemView = (ActionMenuItemView) itemView; |
| 257 | actionItemView.setItemInvoker(menuView); |
| 258 | |
Alan Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 259 | if (mPopupCallback == null) { |
| 260 | mPopupCallback = new ActionMenuPopupCallback(); |
Alan Viverette | fbe4a58 | 2013-09-06 13:45:54 -0700 | [diff] [blame] | 261 | } |
Alan Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 262 | actionItemView.setPopupCallback(mPopupCallback); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public boolean shouldIncludeItem(int childIndex, MenuItemImpl item) { |
| 267 | return item.isActionButton(); |
| 268 | } |
| 269 | |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 270 | /** |
| 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 Haase | 575217f | 2015-04-09 14:31:25 -0700 | [diff] [blame] | 279 | final ViewGroup menuView = (ViewGroup) mMenuView; |
| 280 | final int count = menuView.getChildCount(); |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 281 | SparseArray items = preLayout ? mPreLayoutItems : mPostLayoutItems; |
| 282 | for (int i = 0; i < count; ++i) { |
Chet Haase | 575217f | 2015-04-09 14:31:25 -0700 | [diff] [blame] | 283 | View child = menuView.getChildAt(i); |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 284 | 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 Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 430 | computeMenuItemAnimationInfo(true); |
Chet Haase | 575217f | 2015-04-09 14:31:25 -0700 | [diff] [blame] | 431 | ((View) mMenuView).getViewTreeObserver(). |
| 432 | addOnPreDrawListener(mItemAnimationPreDrawListener); |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 435 | @Override |
| 436 | public void updateMenuView(boolean cleared) { |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 437 | final ViewGroup menuViewParent = (ViewGroup) ((View) mMenuView).getParent(); |
Chet Haase | d96c87e | 2015-05-07 07:43:22 -0700 | [diff] [blame] | 438 | if (menuViewParent != null && ACTIONBAR_ANIMATIONS_ENABLED) { |
Chet Haase | 575217f | 2015-04-09 14:31:25 -0700 | [diff] [blame] | 439 | setupItemAnimations(); |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 440 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 441 | super.updateMenuView(cleared); |
| 442 | |
Adam Powell | da97108 | 2013-10-03 18:21:58 -0700 | [diff] [blame] | 443 | ((View) mMenuView).requestLayout(); |
| 444 | |
Adam Powell | 823f074 | 2011-09-21 17:17:01 -0700 | [diff] [blame] | 445 | 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 Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 456 | 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 Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 469 | if (hasOverflow) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 470 | if (mOverflowButton == null) { |
Adam Powell | 538e565 | 2011-10-11 13:47:08 -0700 | [diff] [blame] | 471 | mOverflowButton = new OverflowMenuButton(mSystemContext); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 472 | } |
| 473 | ViewGroup parent = (ViewGroup) mOverflowButton.getParent(); |
| 474 | if (parent != mMenuView) { |
| 475 | if (parent != null) { |
| 476 | parent.removeView(mOverflowButton); |
| 477 | } |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 478 | ActionMenuView menuView = (ActionMenuView) mMenuView; |
| 479 | menuView.addView(mOverflowButton, menuView.generateOverflowButtonLayoutParams()); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 480 | } |
| 481 | } else if (mOverflowButton != null && mOverflowButton.getParent() == mMenuView) { |
| 482 | ((ViewGroup) mMenuView).removeView(mOverflowButton); |
| 483 | } |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 484 | |
| 485 | ((ActionMenuView) mMenuView).setOverflowReserved(mReserveOverflow); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 486 | } |
| 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 Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 502 | if (anchor == null) { |
Oren Blasberg | 8e12f8d | 2015-09-02 14:25:56 -0700 | [diff] [blame] | 503 | // 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 Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 508 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 509 | |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 510 | mOpenSubMenuId = subMenu.getItem().getItemId(); |
Oren Blasberg | 8e12f8d | 2015-09-02 14:25:56 -0700 | [diff] [blame] | 511 | |
| 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 Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 524 | mActionButtonPopup.show(); |
Oren Blasberg | 8e12f8d | 2015-09-02 14:25:56 -0700 | [diff] [blame] | 525 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 526 | 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 Powell | 70e9f4b | 2011-08-22 16:31:24 -0700 | [diff] [blame] | 550 | if (mReserveOverflow && !isOverflowMenuShowing() && mMenu != null && mMenuView != null && |
Jake Wharton | a647640 | 2012-03-29 01:42:37 -0700 | [diff] [blame] | 551 | mPostedOpenRunnable == null && !mMenu.getNonActionItems().isEmpty()) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 552 | 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 Powell | 678ed0c | 2011-10-27 17:46:07 -0700 | [diff] [blame] | 574 | mPostedOpenRunnable = null; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 575 | 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 Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 590 | @UnsupportedAppUsage |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 591 | 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 Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 613 | @UnsupportedAppUsage |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 614 | public boolean isOverflowMenuShowing() { |
| 615 | return mOverflowPopup != null && mOverflowPopup.isShowing(); |
| 616 | } |
| 617 | |
Adam Powell | 5fcf5b9 | 2013-09-11 08:45:36 -0700 | [diff] [blame] | 618 | public boolean isOverflowMenuShowPending() { |
| 619 | return mPostedOpenRunnable != null || isOverflowMenuShowing(); |
| 620 | } |
| 621 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 622 | /** |
| 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 Viverette | 28a8468 | 2016-01-04 13:43:23 -0500 | [diff] [blame] | 630 | 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 Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 640 | 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 Powell | b187cd9 | 2011-07-20 14:17:56 -0700 | [diff] [blame] | 658 | 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 Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 663 | } |
| 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 Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 675 | 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 Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 683 | // 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 Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 688 | View v = getItemView(item, null, parent); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 689 | if (mStrictWidthLimit) { |
| 690 | cellsRemaining -= ActionMenuView.measureChildForCells(v, |
| 691 | cellSize, cellsRemaining, querySpec, 0); |
| 692 | } else { |
| 693 | v.measure(querySpec, querySpec); |
| 694 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 695 | 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 Powell | ea1ca95 | 2011-06-21 14:07:59 -0700 | [diff] [blame] | 704 | item.setIsActionButton(true); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 705 | } 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 Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 710 | boolean isAction = (maxActions > 0 || inGroup) && widthLimit > 0 && |
| 711 | (!mStrictWidthLimit || cellsRemaining > 0); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 712 | |
| 713 | if (isAction) { |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 714 | View v = getItemView(item, null, parent); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 715 | 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 Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 725 | final int measuredWidth = v.getMeasuredWidth(); |
| 726 | widthLimit -= measuredWidth; |
| 727 | if (firstActionWidth == 0) { |
| 728 | firstActionWidth = measuredWidth; |
| 729 | } |
| 730 | |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 731 | if (mStrictWidthLimit) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 732 | isAction &= widthLimit >= 0; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 733 | } else { |
| 734 | // Did this push the entire first item past the limit? |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 735 | isAction &= widthLimit + firstActionWidth > 0; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 736 | } |
| 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 Powell | 23f4cc0 | 2011-08-18 10:30:46 -0700 | [diff] [blame] | 747 | // Give back the action slot |
| 748 | if (areYouMyGroupie.isActionButton()) maxActions++; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 749 | areYouMyGroupie.setIsActionButton(false); |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
Adam Powell | 23f4cc0 | 2011-08-18 10:30:46 -0700 | [diff] [blame] | 754 | if (isAction) maxActions--; |
| 755 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 756 | item.setIsActionButton(isAction); |
Svetoslav Ganov | 14c4669 | 2012-10-09 18:11:36 -0700 | [diff] [blame] | 757 | } else { |
| 758 | // Neither requires nor requests an action button. |
| 759 | item.setIsActionButton(false); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 760 | } |
| 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 Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 771 | @Override |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 772 | @UnsupportedAppUsage |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 773 | public Parcelable onSaveInstanceState() { |
| 774 | SavedState state = new SavedState(); |
| 775 | state.openSubMenuId = mOpenSubMenuId; |
| 776 | return state; |
| 777 | } |
| 778 | |
| 779 | @Override |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 780 | @UnsupportedAppUsage |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 781 | 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 Powell | 823f074 | 2011-09-21 17:17:01 -0700 | [diff] [blame] | 792 | @Override |
| 793 | public void onSubUiVisibilityChanged(boolean isVisible) { |
| 794 | if (isVisible) { |
| 795 | // Not a submenu, but treat it like one. |
| 796 | super.onSubMenuSelected(null); |
Alan Viverette | 28a8468 | 2016-01-04 13:43:23 -0500 | [diff] [blame] | 797 | } else if (mMenu != null) { |
Alan Viverette | 00aa510 | 2015-11-03 13:03:15 -0500 | [diff] [blame] | 798 | mMenu.close(false /* closeAllMenus */); |
Adam Powell | 823f074 | 2011-09-21 17:17:01 -0700 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 802 | public void setMenuView(ActionMenuView menuView) { |
Chet Haase | 575217f | 2015-04-09 14:31:25 -0700 | [diff] [blame] | 803 | 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 Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 813 | 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 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 833 | public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 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 Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 845 | private class OverflowMenuButton extends ImageButton implements ActionMenuView.ActionMenuChildView { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 846 | 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 Viverette | ca6a3611 | 2013-08-16 14:41:06 -0700 | [diff] [blame] | 853 | |
Alan Viverette | 6996014 | 2013-08-22 17:26:57 -0700 | [diff] [blame] | 854 | setOnTouchListener(new ForwardingListener(this) { |
Alan Viverette | ca6a3611 | 2013-08-16 14:41:06 -0700 | [diff] [blame] | 855 | @Override |
Oren Blasberg | f44d90b | 2015-08-31 14:15:26 -0700 | [diff] [blame] | 856 | public ShowableListMenu getPopup() { |
Alan Viverette | ca6a3611 | 2013-08-16 14:41:06 -0700 | [diff] [blame] | 857 | 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 Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 883 | } |
| 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 Viverette | 80e7270 | 2013-07-29 11:12:59 -0700 | [diff] [blame] | 896 | @Override |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 897 | public boolean needsDividerBefore() { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 898 | return false; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Alan Viverette | 80e7270 | 2013-07-29 11:12:59 -0700 | [diff] [blame] | 901 | @Override |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 902 | public boolean needsDividerAfter() { |
| 903 | return false; |
| 904 | } |
Adam Powell | d5c81db | 2012-08-02 14:35:26 -0700 | [diff] [blame] | 905 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 906 | /** @hide */ |
Adam Powell | d5c81db | 2012-08-02 14:35:26 -0700 | [diff] [blame] | 907 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 908 | public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 909 | super.onInitializeAccessibilityNodeInfoInternal(info); |
Svetoslav Ganov | cb8ed39 | 2013-08-23 20:37:28 -0700 | [diff] [blame] | 910 | info.setCanOpenPopup(true); |
Alan Viverette | 058ac7c | 2013-08-19 16:44:30 -0700 | [diff] [blame] | 911 | } |
Alan Viverette | f023c25 | 2014-08-28 13:55:18 -0700 | [diff] [blame] | 912 | |
| 913 | @Override |
Alan Viverette | 9d9581f | 2014-09-12 12:54:02 -0700 | [diff] [blame] | 914 | protected boolean setFrame(int l, int t, int r, int b) { |
| 915 | final boolean changed = super.setFrame(l, t, r, b); |
Alan Viverette | f023c25 | 2014-08-28 13:55:18 -0700 | [diff] [blame] | 916 | |
Alan Viverette | 199ea8c | 2014-11-20 17:09:27 -0800 | [diff] [blame] | 917 | // Set up the hotspot bounds to square and centered on the image. |
Alan Viverette | f023c25 | 2014-08-28 13:55:18 -0700 | [diff] [blame] | 918 | final Drawable d = getDrawable(); |
| 919 | final Drawable bg = getBackground(); |
| 920 | if (d != null && bg != null) { |
Alan Viverette | 199ea8c | 2014-11-20 17:09:27 -0800 | [diff] [blame] | 921 | 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 Viverette | f023c25 | 2014-08-28 13:55:18 -0700 | [diff] [blame] | 930 | } |
Alan Viverette | 9d9581f | 2014-09-12 12:54:02 -0700 | [diff] [blame] | 931 | |
| 932 | return changed; |
Alan Viverette | f023c25 | 2014-08-28 13:55:18 -0700 | [diff] [blame] | 933 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | private class OverflowPopup extends MenuPopupHelper { |
| 937 | public OverflowPopup(Context context, MenuBuilder menu, View anchorView, |
| 938 | boolean overflowOnly) { |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 939 | super(context, menu, anchorView, overflowOnly, |
| 940 | com.android.internal.R.attr.actionOverflowMenuStyle); |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 941 | setGravity(Gravity.END); |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 942 | setPresenterCallback(mPopupPresenterCallback); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | @Override |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 946 | protected void onDismiss() { |
Alan Viverette | 28a8468 | 2016-01-04 13:43:23 -0500 | [diff] [blame] | 947 | if (mMenu != null) { |
| 948 | mMenu.close(); |
| 949 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 950 | mOverflowPopup = null; |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 951 | |
| 952 | super.onDismiss(); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | |
| 956 | private class ActionButtonSubmenu extends MenuPopupHelper { |
Oren Blasberg | 8e12f8d | 2015-09-02 14:25:56 -0700 | [diff] [blame] | 957 | public ActionButtonSubmenu(Context context, SubMenuBuilder subMenu, View anchorView) { |
| 958 | super(context, subMenu, anchorView, false, |
Alan Viverette | d361a4f | 2014-06-30 16:47:40 -0700 | [diff] [blame] | 959 | com.android.internal.R.attr.actionOverflowMenuStyle); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 960 | |
| 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 Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 966 | |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 967 | setPresenterCallback(mPopupPresenterCallback); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | @Override |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 971 | protected void onDismiss() { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 972 | mActionButtonPopup = null; |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 973 | mOpenSubMenuId = 0; |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 974 | |
| 975 | super.onDismiss(); |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 979 | private class PopupPresenterCallback implements Callback { |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 980 | |
| 981 | @Override |
| 982 | public boolean onOpenSubMenu(MenuBuilder subMenu) { |
Adam Powell | 823f074 | 2011-09-21 17:17:01 -0700 | [diff] [blame] | 983 | if (subMenu == null) return false; |
| 984 | |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 985 | mOpenSubMenuId = ((SubMenuBuilder) subMenu).getItem().getItemId(); |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 986 | final Callback cb = getCallback(); |
Adam Powell | b411b32 | 2013-11-01 18:22:11 -0700 | [diff] [blame] | 987 | return cb != null ? cb.onOpenSubMenu(subMenu) : false; |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | @Override |
| 991 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
Adam Powell | 823f074 | 2011-09-21 17:17:01 -0700 | [diff] [blame] | 992 | if (menu instanceof SubMenuBuilder) { |
Alan Viverette | 00aa510 | 2015-11-03 13:03:15 -0500 | [diff] [blame] | 993 | menu.getRootMenu().close(false /* closeAllMenus */); |
Adam Powell | 823f074 | 2011-09-21 17:17:01 -0700 | [diff] [blame] | 994 | } |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 995 | final Callback cb = getCallback(); |
Adam Powell | b411b32 | 2013-11-01 18:22:11 -0700 | [diff] [blame] | 996 | if (cb != null) { |
| 997 | cb.onCloseMenu(menu, allMenusAreClosing); |
| 998 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 999 | } |
| 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 Viverette | 28a8468 | 2016-01-04 13:43:23 -0500 | [diff] [blame] | 1010 | if (mMenu != null) { |
| 1011 | mMenu.changeMenuMode(); |
| 1012 | } |
Adam Powell | 678ed0c | 2011-10-27 17:46:07 -0700 | [diff] [blame] | 1013 | final View menuView = (View) mMenuView; |
| 1014 | if (menuView != null && menuView.getWindowToken() != null && mPopup.tryShow()) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 1015 | mOverflowPopup = mPopup; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 1016 | } |
Adam Powell | 678ed0c | 2011-10-27 17:46:07 -0700 | [diff] [blame] | 1017 | mPostedOpenRunnable = null; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 1018 | } |
| 1019 | } |
Alan Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 1020 | |
| 1021 | private class ActionMenuPopupCallback extends ActionMenuItemView.PopupCallback { |
| 1022 | @Override |
Oren Blasberg | f44d90b | 2015-08-31 14:15:26 -0700 | [diff] [blame] | 1023 | public ShowableListMenu getPopup() { |
Alan Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 1024 | return mActionButtonPopup != null ? mActionButtonPopup.getPopup() : null; |
| 1025 | } |
| 1026 | } |
Chris Banes | 9cc36ca | 2015-02-23 12:30:13 +0000 | [diff] [blame] | 1027 | |
Chet Haase | 952cba2 | 2015-03-20 13:17:25 -0700 | [diff] [blame] | 1028 | /** |
| 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 Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 1075 | } |