Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | */ |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 16 | package android.widget; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 17 | |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 18 | import android.annotation.NonNull; |
Chris Banes | a41b789 | 2015-06-09 13:36:44 +0000 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Tor Norbye | 417ee5b | 2015-03-10 20:57:37 -0700 | [diff] [blame] | 20 | import android.annotation.StyleRes; |
Artur Satayev | ad9254c | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 21 | import android.compat.annotation.UnsupportedAppUsage; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 23 | import android.content.res.Configuration; |
Chris Banes | a41b789 | 2015-06-09 13:36:44 +0000 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
Alan Viverette | 3d0f21d | 2014-07-10 16:15:01 -0700 | [diff] [blame] | 26 | import android.view.ContextThemeWrapper; |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 27 | import android.view.Gravity; |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 28 | import android.view.Menu; |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 29 | import android.view.MenuItem; |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 30 | import android.view.View; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 31 | import android.view.ViewDebug; |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame] | 32 | import android.view.ViewGroup; |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 33 | import android.view.ViewHierarchyEncoder; |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 34 | import android.view.accessibility.AccessibilityEvent; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 35 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 36 | import com.android.internal.view.menu.ActionMenuItemView; |
| 37 | import com.android.internal.view.menu.MenuBuilder; |
| 38 | import com.android.internal.view.menu.MenuItemImpl; |
Adam Powell | 04c0d46 | 2014-09-04 16:10:24 -0700 | [diff] [blame] | 39 | import com.android.internal.view.menu.MenuPresenter; |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 40 | import com.android.internal.view.menu.MenuView; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 41 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 42 | /** |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 43 | * ActionMenuView is a presentation of a series of menu options as a View. It provides |
| 44 | * several top level options as action buttons while spilling remaining options over as |
| 45 | * items in an overflow menu. This allows applications to present packs of actions inline with |
| 46 | * specific or repeating content. |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 47 | */ |
| 48 | public class ActionMenuView extends LinearLayout implements MenuBuilder.ItemInvoker, MenuView { |
| 49 | private static final String TAG = "ActionMenuView"; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 50 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 51 | static final int MIN_CELL_SIZE = 56; // dips |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 52 | static final int GENERATED_ITEM_PADDING = 4; // dips |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 53 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 54 | private MenuBuilder mMenu; |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame] | 55 | |
Alan Viverette | 3d0f21d | 2014-07-10 16:15:01 -0700 | [diff] [blame] | 56 | /** Context against which to inflate popup menus. */ |
| 57 | private Context mPopupContext; |
| 58 | |
| 59 | /** Theme resource against which to inflate popup menus. */ |
| 60 | private int mPopupTheme; |
| 61 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 62 | private boolean mReserveOverflow; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 63 | private ActionMenuPresenter mPresenter; |
Adam Powell | 04c0d46 | 2014-09-04 16:10:24 -0700 | [diff] [blame] | 64 | private MenuPresenter.Callback mActionMenuPresenterCallback; |
Adam Powell | c461250 | 2014-09-04 19:16:39 -0700 | [diff] [blame] | 65 | private MenuBuilder.Callback mMenuBuilderCallback; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 66 | private boolean mFormatItems; |
Adam Powell | 89b09da | 2011-07-27 11:55:29 -0700 | [diff] [blame] | 67 | private int mFormatItemsWidth; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 68 | private int mMinCellSize; |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 69 | private int mGeneratedItemPadding; |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 70 | |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 71 | private OnMenuItemClickListener mOnMenuItemClickListener; |
| 72 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 73 | public ActionMenuView(Context context) { |
| 74 | this(context, null); |
| 75 | } |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 76 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 77 | public ActionMenuView(Context context, AttributeSet attrs) { |
| 78 | super(context, attrs); |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 79 | setBaselineAligned(false); |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 80 | final float density = context.getResources().getDisplayMetrics().density; |
| 81 | mMinCellSize = (int) (MIN_CELL_SIZE * density); |
| 82 | mGeneratedItemPadding = (int) (GENERATED_ITEM_PADDING * density); |
Alan Viverette | 3d0f21d | 2014-07-10 16:15:01 -0700 | [diff] [blame] | 83 | mPopupContext = context; |
| 84 | mPopupTheme = 0; |
Adam Powell | 773b1b9 | 2010-08-20 15:45:24 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Alan Viverette | 3d0f21d | 2014-07-10 16:15:01 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Specifies the theme to use when inflating popup menus. By default, uses |
| 89 | * the same theme as the action menu view itself. |
| 90 | * |
| 91 | * @param resId theme used to inflate popup menus |
| 92 | * @see #getPopupTheme() |
| 93 | */ |
Tor Norbye | 417ee5b | 2015-03-10 20:57:37 -0700 | [diff] [blame] | 94 | public void setPopupTheme(@StyleRes int resId) { |
Alan Viverette | 3d0f21d | 2014-07-10 16:15:01 -0700 | [diff] [blame] | 95 | if (mPopupTheme != resId) { |
| 96 | mPopupTheme = resId; |
| 97 | if (resId == 0) { |
| 98 | mPopupContext = mContext; |
| 99 | } else { |
| 100 | mPopupContext = new ContextThemeWrapper(mContext, resId); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @return resource identifier of the theme used to inflate popup menus, or |
| 107 | * 0 if menus are inflated against the action menu view theme |
| 108 | * @see #setPopupTheme(int) |
| 109 | */ |
| 110 | public int getPopupTheme() { |
| 111 | return mPopupTheme; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param presenter Menu presenter used to display popup menu |
| 116 | * @hide |
| 117 | */ |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 118 | public void setPresenter(ActionMenuPresenter presenter) { |
| 119 | mPresenter = presenter; |
Adam Powell | e021e6e | 2014-05-23 17:27:24 -0700 | [diff] [blame] | 120 | mPresenter.setMenuView(this); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 123 | @Override |
Adam Powell | 773b1b9 | 2010-08-20 15:45:24 -0700 | [diff] [blame] | 124 | public void onConfigurationChanged(Configuration newConfig) { |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 125 | super.onConfigurationChanged(newConfig); |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 126 | |
Chris Banes | 5cd1326 | 2015-01-19 12:01:46 +0000 | [diff] [blame] | 127 | if (mPresenter != null) { |
| 128 | mPresenter.updateMenuView(false); |
| 129 | |
| 130 | if (mPresenter.isOverflowMenuShowing()) { |
| 131 | mPresenter.hideOverflowMenu(); |
| 132 | mPresenter.showOverflowMenu(); |
| 133 | } |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 134 | } |
Adam Powell | 773b1b9 | 2010-08-20 15:45:24 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 137 | public void setOnMenuItemClickListener(OnMenuItemClickListener listener) { |
| 138 | mOnMenuItemClickListener = listener; |
| 139 | } |
| 140 | |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 141 | @Override |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 142 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 143 | // If we've been given an exact size to match, apply special formatting during layout. |
Adam Powell | 89b09da | 2011-07-27 11:55:29 -0700 | [diff] [blame] | 144 | final boolean wasFormatted = mFormatItems; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 145 | mFormatItems = MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY; |
Adam Powell | 89b09da | 2011-07-27 11:55:29 -0700 | [diff] [blame] | 146 | |
| 147 | if (wasFormatted != mFormatItems) { |
| 148 | mFormatItemsWidth = 0; // Reset this when switching modes |
| 149 | } |
| 150 | |
| 151 | // Special formatting can change whether items can fit as action buttons. |
| 152 | // Kick the menu and update presenters when this changes. |
Adam Powell | da97108 | 2013-10-03 18:21:58 -0700 | [diff] [blame] | 153 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
Adam Powell | 89b09da | 2011-07-27 11:55:29 -0700 | [diff] [blame] | 154 | if (mFormatItems && mMenu != null && widthSize != mFormatItemsWidth) { |
| 155 | mFormatItemsWidth = widthSize; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 156 | mMenu.onItemsChanged(true); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 157 | } |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 158 | |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 159 | final int childCount = getChildCount(); |
| 160 | if (mFormatItems && childCount > 0) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 161 | onMeasureExactFormat(widthMeasureSpec, heightMeasureSpec); |
| 162 | } else { |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 163 | // Previous measurement at exact format may have set margins - reset them. |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 164 | for (int i = 0; i < childCount; i++) { |
| 165 | final View child = getChildAt(i); |
| 166 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 167 | lp.leftMargin = lp.rightMargin = 0; |
| 168 | } |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 169 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | private void onMeasureExactFormat(int widthMeasureSpec, int heightMeasureSpec) { |
| 174 | // We already know the width mode is EXACTLY if we're here. |
| 175 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 176 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 177 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
| 178 | |
| 179 | final int widthPadding = getPaddingLeft() + getPaddingRight(); |
| 180 | final int heightPadding = getPaddingTop() + getPaddingBottom(); |
| 181 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 182 | final int itemHeightSpec = getChildMeasureSpec(heightMeasureSpec, heightPadding, |
| 183 | ViewGroup.LayoutParams.WRAP_CONTENT); |
Adam Powell | 367ee32 | 2012-05-06 18:32:33 -0700 | [diff] [blame] | 184 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 185 | widthSize -= widthPadding; |
| 186 | |
| 187 | // Divide the view into cells. |
| 188 | final int cellCount = widthSize / mMinCellSize; |
| 189 | final int cellSizeRemaining = widthSize % mMinCellSize; |
Adam Powell | 3bb421d | 2011-08-16 15:04:53 -0700 | [diff] [blame] | 190 | |
| 191 | if (cellCount == 0) { |
| 192 | // Give up, nothing fits. |
| 193 | setMeasuredDimension(widthSize, 0); |
| 194 | return; |
| 195 | } |
| 196 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 197 | final int cellSize = mMinCellSize + cellSizeRemaining / cellCount; |
| 198 | |
| 199 | int cellsRemaining = cellCount; |
| 200 | int maxChildHeight = 0; |
| 201 | int maxCellsUsed = 0; |
Adam Powell | 160bb7f | 2011-07-07 10:22:27 -0700 | [diff] [blame] | 202 | int expandableItemCount = 0; |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 203 | int visibleItemCount = 0; |
| 204 | boolean hasOverflow = false; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 205 | |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 206 | // This is used as a bitfield to locate the smallest items present. Assumes childCount < 64. |
| 207 | long smallestItemsAt = 0; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 208 | |
| 209 | final int childCount = getChildCount(); |
| 210 | for (int i = 0; i < childCount; i++) { |
| 211 | final View child = getChildAt(i); |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 212 | if (child.getVisibility() == GONE) continue; |
| 213 | |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 214 | final boolean isGeneratedItem = child instanceof ActionMenuItemView; |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 215 | visibleItemCount++; |
| 216 | |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 217 | if (isGeneratedItem) { |
| 218 | // Reset padding for generated menu item views; it may change below |
| 219 | // and views are recycled. |
| 220 | child.setPadding(mGeneratedItemPadding, 0, mGeneratedItemPadding, 0); |
| 221 | } |
| 222 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 223 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 224 | lp.expanded = false; |
| 225 | lp.extraPixels = 0; |
| 226 | lp.cellsUsed = 0; |
Adam Powell | 160bb7f | 2011-07-07 10:22:27 -0700 | [diff] [blame] | 227 | lp.expandable = false; |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 228 | lp.leftMargin = 0; |
| 229 | lp.rightMargin = 0; |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 230 | lp.preventEdgeOffset = isGeneratedItem && ((ActionMenuItemView) child).hasText(); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 231 | |
| 232 | // Overflow always gets 1 cell. No more, no less. |
| 233 | final int cellsAvailable = lp.isOverflowButton ? 1 : cellsRemaining; |
| 234 | |
| 235 | final int cellsUsed = measureChildForCells(child, cellSize, cellsAvailable, |
Adam Powell | 367ee32 | 2012-05-06 18:32:33 -0700 | [diff] [blame] | 236 | itemHeightSpec, heightPadding); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 237 | |
| 238 | maxCellsUsed = Math.max(maxCellsUsed, cellsUsed); |
Adam Powell | 160bb7f | 2011-07-07 10:22:27 -0700 | [diff] [blame] | 239 | if (lp.expandable) expandableItemCount++; |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 240 | if (lp.isOverflowButton) hasOverflow = true; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 241 | |
| 242 | cellsRemaining -= cellsUsed; |
| 243 | maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 244 | if (cellsUsed == 1) smallestItemsAt |= (1 << i); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 247 | // When we have overflow and a single expanded (text) item, we want to try centering it |
| 248 | // visually in the available space even though overflow consumes some of it. |
| 249 | final boolean centerSingleExpandedItem = hasOverflow && visibleItemCount == 2; |
| 250 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 251 | // Divide space for remaining cells if we have items that can expand. |
| 252 | // Try distributing whole leftover cells to smaller items first. |
| 253 | |
| 254 | boolean needsExpansion = false; |
Adam Powell | 160bb7f | 2011-07-07 10:22:27 -0700 | [diff] [blame] | 255 | while (expandableItemCount > 0 && cellsRemaining > 0) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 256 | int minCells = Integer.MAX_VALUE; |
| 257 | long minCellsAt = 0; // Bit locations are indices of relevant child views |
| 258 | int minCellsItemCount = 0; |
| 259 | for (int i = 0; i < childCount; i++) { |
| 260 | final View child = getChildAt(i); |
| 261 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 262 | |
| 263 | // Don't try to expand items that shouldn't. |
Adam Powell | 160bb7f | 2011-07-07 10:22:27 -0700 | [diff] [blame] | 264 | if (!lp.expandable) continue; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 265 | |
| 266 | // Mark indices of children that can receive an extra cell. |
| 267 | if (lp.cellsUsed < minCells) { |
| 268 | minCells = lp.cellsUsed; |
| 269 | minCellsAt = 1 << i; |
| 270 | minCellsItemCount = 1; |
| 271 | } else if (lp.cellsUsed == minCells) { |
| 272 | minCellsAt |= 1 << i; |
| 273 | minCellsItemCount++; |
| 274 | } |
| 275 | } |
| 276 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 277 | // Items that get expanded will always be in the set of smallest items when we're done. |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 278 | smallestItemsAt |= minCellsAt; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 279 | |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 280 | if (minCellsItemCount > cellsRemaining) break; // Couldn't expand anything evenly. Stop. |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 281 | |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 282 | // We have enough cells, all minimum size items will be incremented. |
| 283 | minCells++; |
| 284 | |
| 285 | for (int i = 0; i < childCount; i++) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 286 | final View child = getChildAt(i); |
| 287 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 288 | if ((minCellsAt & (1 << i)) == 0) { |
| 289 | // If this item is already at our small item count, mark it for later. |
| 290 | if (lp.cellsUsed == minCells) smallestItemsAt |= 1 << i; |
| 291 | continue; |
| 292 | } |
| 293 | |
| 294 | if (centerSingleExpandedItem && lp.preventEdgeOffset && cellsRemaining == 1) { |
| 295 | // Add padding to this item such that it centers. |
| 296 | child.setPadding(mGeneratedItemPadding + cellSize, 0, mGeneratedItemPadding, 0); |
| 297 | } |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 298 | lp.cellsUsed++; |
| 299 | lp.expanded = true; |
| 300 | cellsRemaining--; |
| 301 | } |
| 302 | |
| 303 | needsExpansion = true; |
| 304 | } |
| 305 | |
| 306 | // Divide any space left that wouldn't divide along cell boundaries |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 307 | // evenly among the smallest items |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 308 | |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 309 | final boolean singleItem = !hasOverflow && visibleItemCount == 1; |
| 310 | if (cellsRemaining > 0 && smallestItemsAt != 0 && |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 311 | (cellsRemaining < visibleItemCount - 1 || singleItem || maxCellsUsed > 1)) { |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 312 | float expandCount = Long.bitCount(smallestItemsAt); |
| 313 | |
| 314 | if (!singleItem) { |
| 315 | // The items at the far edges may only expand by half in order to pin to either side. |
| 316 | if ((smallestItemsAt & 1) != 0) { |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 317 | LayoutParams lp = (LayoutParams) getChildAt(0).getLayoutParams(); |
| 318 | if (!lp.preventEdgeOffset) expandCount -= 0.5f; |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 319 | } |
| 320 | if ((smallestItemsAt & (1 << (childCount - 1))) != 0) { |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 321 | LayoutParams lp = ((LayoutParams) getChildAt(childCount - 1).getLayoutParams()); |
| 322 | if (!lp.preventEdgeOffset) expandCount -= 0.5f; |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Adam Powell | 3bb421d | 2011-08-16 15:04:53 -0700 | [diff] [blame] | 326 | final int extraPixels = expandCount > 0 ? |
| 327 | (int) (cellsRemaining * cellSize / expandCount) : 0; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 328 | |
| 329 | for (int i = 0; i < childCount; i++) { |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 330 | if ((smallestItemsAt & (1 << i)) == 0) continue; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 331 | |
| 332 | final View child = getChildAt(i); |
| 333 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 334 | if (child instanceof ActionMenuItemView) { |
| 335 | // If this is one of our views, expand and measure at the larger size. |
| 336 | lp.extraPixels = extraPixels; |
| 337 | lp.expanded = true; |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 338 | if (i == 0 && !lp.preventEdgeOffset) { |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 339 | // First item gets part of its new padding pushed out of sight. |
| 340 | // The last item will get this implicitly from layout. |
| 341 | lp.leftMargin = -extraPixels / 2; |
| 342 | } |
| 343 | needsExpansion = true; |
| 344 | } else if (lp.isOverflowButton) { |
| 345 | lp.extraPixels = extraPixels; |
| 346 | lp.expanded = true; |
| 347 | lp.rightMargin = -extraPixels / 2; |
| 348 | needsExpansion = true; |
| 349 | } else { |
| 350 | // If we don't know what it is, give it some margins instead |
| 351 | // and let it center within its space. We still want to pin |
| 352 | // against the edges. |
| 353 | if (i != 0) { |
| 354 | lp.leftMargin = extraPixels / 2; |
| 355 | } |
| 356 | if (i != childCount - 1) { |
| 357 | lp.rightMargin = extraPixels / 2; |
| 358 | } |
| 359 | } |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 362 | cellsRemaining = 0; |
| 363 | } |
| 364 | |
| 365 | // Remeasure any items that have had extra space allocated to them. |
| 366 | if (needsExpansion) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 367 | for (int i = 0; i < childCount; i++) { |
| 368 | final View child = getChildAt(i); |
| 369 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 370 | |
| 371 | if (!lp.expanded) continue; |
| 372 | |
| 373 | final int width = lp.cellsUsed * cellSize + lp.extraPixels; |
Adam Powell | 367ee32 | 2012-05-06 18:32:33 -0700 | [diff] [blame] | 374 | child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), |
| 375 | itemHeightSpec); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | if (heightMode != MeasureSpec.EXACTLY) { |
| 380 | heightSize = maxChildHeight; |
| 381 | } |
| 382 | |
| 383 | setMeasuredDimension(widthSize, heightSize); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Measure a child view to fit within cell-based formatting. The child's width |
| 388 | * will be measured to a whole multiple of cellSize. |
| 389 | * |
Adam Powell | 160bb7f | 2011-07-07 10:22:27 -0700 | [diff] [blame] | 390 | * <p>Sets the expandable and cellsUsed fields of LayoutParams. |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 391 | * |
| 392 | * @param child Child to measure |
| 393 | * @param cellSize Size of one cell |
| 394 | * @param cellsRemaining Number of cells remaining that this view can expand to fill |
| 395 | * @param parentHeightMeasureSpec MeasureSpec used by the parent view |
| 396 | * @param parentHeightPadding Padding present in the parent view |
| 397 | * @return Number of cells this child was measured to occupy |
| 398 | */ |
| 399 | static int measureChildForCells(View child, int cellSize, int cellsRemaining, |
| 400 | int parentHeightMeasureSpec, int parentHeightPadding) { |
| 401 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 402 | |
| 403 | final int childHeightSize = MeasureSpec.getSize(parentHeightMeasureSpec) - |
| 404 | parentHeightPadding; |
| 405 | final int childHeightMode = MeasureSpec.getMode(parentHeightMeasureSpec); |
| 406 | final int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeightSize, childHeightMode); |
| 407 | |
Adam Powell | a7dec6d | 2012-04-09 15:54:01 -0700 | [diff] [blame] | 408 | final ActionMenuItemView itemView = child instanceof ActionMenuItemView ? |
| 409 | (ActionMenuItemView) child : null; |
| 410 | final boolean hasText = itemView != null && itemView.hasText(); |
| 411 | |
Adam Powell | 160bb7f | 2011-07-07 10:22:27 -0700 | [diff] [blame] | 412 | int cellsUsed = 0; |
Adam Powell | a7dec6d | 2012-04-09 15:54:01 -0700 | [diff] [blame] | 413 | if (cellsRemaining > 0 && (!hasText || cellsRemaining >= 2)) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 414 | final int childWidthSpec = MeasureSpec.makeMeasureSpec( |
| 415 | cellSize * cellsRemaining, MeasureSpec.AT_MOST); |
| 416 | child.measure(childWidthSpec, childHeightSpec); |
| 417 | |
| 418 | final int measuredWidth = child.getMeasuredWidth(); |
| 419 | cellsUsed = measuredWidth / cellSize; |
| 420 | if (measuredWidth % cellSize != 0) cellsUsed++; |
Adam Powell | a7dec6d | 2012-04-09 15:54:01 -0700 | [diff] [blame] | 421 | if (hasText && cellsUsed < 2) cellsUsed = 2; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 422 | } |
Adam Powell | 160bb7f | 2011-07-07 10:22:27 -0700 | [diff] [blame] | 423 | |
Adam Powell | a7dec6d | 2012-04-09 15:54:01 -0700 | [diff] [blame] | 424 | final boolean expandable = !lp.isOverflowButton && hasText; |
Adam Powell | 160bb7f | 2011-07-07 10:22:27 -0700 | [diff] [blame] | 425 | lp.expandable = expandable; |
| 426 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 427 | lp.cellsUsed = cellsUsed; |
| 428 | final int targetWidth = cellsUsed * cellSize; |
| 429 | child.measure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY), |
| 430 | childHeightSpec); |
| 431 | return cellsUsed; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | @Override |
| 435 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 436 | if (!mFormatItems) { |
| 437 | super.onLayout(changed, left, top, right, bottom); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | final int childCount = getChildCount(); |
Chris Banes | 1970cfd4 | 2014-10-29 11:04:49 +0000 | [diff] [blame] | 442 | final int midVertical = (bottom - top) / 2; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 443 | final int dividerWidth = getDividerWidth(); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 444 | int overflowWidth = 0; |
| 445 | int nonOverflowWidth = 0; |
| 446 | int nonOverflowCount = 0; |
| 447 | int widthRemaining = right - left - getPaddingRight() - getPaddingLeft(); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 448 | boolean hasOverflow = false; |
Fabrice Di Meglio | 0762cec | 2012-09-05 19:17:20 -0700 | [diff] [blame] | 449 | final boolean isLayoutRtl = isLayoutRtl(); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 450 | for (int i = 0; i < childCount; i++) { |
| 451 | final View v = getChildAt(i); |
| 452 | if (v.getVisibility() == GONE) { |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | LayoutParams p = (LayoutParams) v.getLayoutParams(); |
| 457 | if (p.isOverflowButton) { |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 458 | overflowWidth = v.getMeasuredWidth(); |
| 459 | if (hasDividerBeforeChildAt(i)) { |
| 460 | overflowWidth += dividerWidth; |
| 461 | } |
| 462 | |
| 463 | int height = v.getMeasuredHeight(); |
Fabrice Di Meglio | 0762cec | 2012-09-05 19:17:20 -0700 | [diff] [blame] | 464 | int r; |
| 465 | int l; |
| 466 | if (isLayoutRtl) { |
| 467 | l = getPaddingLeft() + p.leftMargin; |
| 468 | r = l + overflowWidth; |
| 469 | } else { |
| 470 | r = getWidth() - getPaddingRight() - p.rightMargin; |
| 471 | l = r - overflowWidth; |
| 472 | } |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 473 | int t = midVertical - (height / 2); |
| 474 | int b = t + height; |
| 475 | v.layout(l, t, r, b); |
| 476 | |
| 477 | widthRemaining -= overflowWidth; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 478 | hasOverflow = true; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 479 | } else { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 480 | final int size = v.getMeasuredWidth() + p.leftMargin + p.rightMargin; |
| 481 | nonOverflowWidth += size; |
| 482 | widthRemaining -= size; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 483 | if (hasDividerBeforeChildAt(i)) { |
| 484 | nonOverflowWidth += dividerWidth; |
| 485 | } |
| 486 | nonOverflowCount++; |
| 487 | } |
| 488 | } |
| 489 | |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 490 | if (childCount == 1 && !hasOverflow) { |
| 491 | // Center a single child |
| 492 | final View v = getChildAt(0); |
| 493 | final int width = v.getMeasuredWidth(); |
| 494 | final int height = v.getMeasuredHeight(); |
| 495 | final int midHorizontal = (right - left) / 2; |
| 496 | final int l = midHorizontal - width / 2; |
| 497 | final int t = midVertical - height / 2; |
| 498 | v.layout(l, t, l + width, t + height); |
| 499 | return; |
| 500 | } |
| 501 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 502 | final int spacerCount = nonOverflowCount - (hasOverflow ? 0 : 1); |
Adam Powell | 14b7e2c | 2011-08-12 11:11:50 -0700 | [diff] [blame] | 503 | final int spacerSize = Math.max(0, spacerCount > 0 ? widthRemaining / spacerCount : 0); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 504 | |
Fabrice Di Meglio | 0762cec | 2012-09-05 19:17:20 -0700 | [diff] [blame] | 505 | if (isLayoutRtl) { |
| 506 | int startRight = getWidth() - getPaddingRight(); |
| 507 | for (int i = 0; i < childCount; i++) { |
| 508 | final View v = getChildAt(i); |
| 509 | final LayoutParams lp = (LayoutParams) v.getLayoutParams(); |
| 510 | if (v.getVisibility() == GONE || lp.isOverflowButton) { |
| 511 | continue; |
| 512 | } |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 513 | |
Fabrice Di Meglio | 0762cec | 2012-09-05 19:17:20 -0700 | [diff] [blame] | 514 | startRight -= lp.rightMargin; |
| 515 | int width = v.getMeasuredWidth(); |
| 516 | int height = v.getMeasuredHeight(); |
| 517 | int t = midVertical - height / 2; |
| 518 | v.layout(startRight - width, t, startRight, t + height); |
| 519 | startRight -= width + lp.leftMargin + spacerSize; |
| 520 | } |
| 521 | } else { |
| 522 | int startLeft = getPaddingLeft(); |
| 523 | for (int i = 0; i < childCount; i++) { |
| 524 | final View v = getChildAt(i); |
| 525 | final LayoutParams lp = (LayoutParams) v.getLayoutParams(); |
| 526 | if (v.getVisibility() == GONE || lp.isOverflowButton) { |
| 527 | continue; |
| 528 | } |
| 529 | |
| 530 | startLeft += lp.leftMargin; |
| 531 | int width = v.getMeasuredWidth(); |
| 532 | int height = v.getMeasuredHeight(); |
| 533 | int t = midVertical - height / 2; |
| 534 | v.layout(startLeft, t, startLeft + width, t + height); |
| 535 | startLeft += width + lp.rightMargin + spacerSize; |
| 536 | } |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | @Override |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 541 | public void onDetachedFromWindow() { |
| 542 | super.onDetachedFromWindow(); |
Adam Powell | e021e6e | 2014-05-23 17:27:24 -0700 | [diff] [blame] | 543 | dismissPopupMenus(); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Chris Banes | a41b789 | 2015-06-09 13:36:44 +0000 | [diff] [blame] | 546 | /** |
| 547 | * Set the icon to use for the overflow button. |
| 548 | * |
| 549 | * @param icon Drawable to set, may be null to clear the icon |
| 550 | */ |
| 551 | public void setOverflowIcon(@Nullable Drawable icon) { |
| 552 | getMenu(); |
| 553 | mPresenter.setOverflowIcon(icon); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Return the current drawable used as the overflow icon. |
| 558 | * |
| 559 | * @return The overflow icon drawable |
| 560 | */ |
| 561 | @Nullable |
| 562 | public Drawable getOverflowIcon() { |
| 563 | getMenu(); |
| 564 | return mPresenter.getOverflowIcon(); |
| 565 | } |
| 566 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 567 | /** @hide */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 568 | @UnsupportedAppUsage |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 569 | public boolean isOverflowReserved() { |
| 570 | return mReserveOverflow; |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame] | 571 | } |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 572 | |
| 573 | /** @hide */ |
Adam Powell | b366bba | 2010-07-20 14:26:38 -0700 | [diff] [blame] | 574 | public void setOverflowReserved(boolean reserveOverflow) { |
| 575 | mReserveOverflow = reserveOverflow; |
| 576 | } |
Adam Powell | f0ad6e6 | 2011-01-10 17:14:06 -0800 | [diff] [blame] | 577 | |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame] | 578 | @Override |
| 579 | protected LayoutParams generateDefaultLayoutParams() { |
| 580 | LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 581 | LayoutParams.WRAP_CONTENT); |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 582 | params.gravity = Gravity.CENTER_VERTICAL; |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame] | 583 | return params; |
| 584 | } |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 585 | |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame] | 586 | @Override |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 587 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 588 | return new LayoutParams(getContext(), attrs); |
| 589 | } |
| 590 | |
| 591 | @Override |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame] | 592 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
Adam Powell | 8c1b02e | 2012-07-16 17:58:18 -0700 | [diff] [blame] | 593 | if (p != null) { |
| 594 | final LayoutParams result = p instanceof LayoutParams |
| 595 | ? new LayoutParams((LayoutParams) p) |
| 596 | : new LayoutParams(p); |
Adam Powell | 3f476b3 | 2011-01-03 19:25:36 -0800 | [diff] [blame] | 597 | if (result.gravity <= Gravity.NO_GRAVITY) { |
| 598 | result.gravity = Gravity.CENTER_VERTICAL; |
| 599 | } |
| 600 | return result; |
| 601 | } |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame] | 602 | return generateDefaultLayoutParams(); |
| 603 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 604 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 605 | @Override |
| 606 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 607 | return p != null && p instanceof LayoutParams; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 610 | /** @hide */ |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 611 | public LayoutParams generateOverflowButtonLayoutParams() { |
| 612 | LayoutParams result = generateDefaultLayoutParams(); |
| 613 | result.isOverflowButton = true; |
| 614 | return result; |
| 615 | } |
| 616 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 617 | /** @hide */ |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 618 | public boolean invokeItem(MenuItemImpl item) { |
| 619 | return mMenu.performItemAction(item, 0); |
| 620 | } |
| 621 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 622 | /** @hide */ |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 623 | public int getWindowAnimations() { |
| 624 | return 0; |
| 625 | } |
| 626 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 627 | /** @hide */ |
Alan Viverette | 28a8468 | 2016-01-04 13:43:23 -0500 | [diff] [blame] | 628 | public void initialize(@Nullable MenuBuilder menu) { |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 629 | mMenu = menu; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 632 | /** |
| 633 | * Returns the Menu object that this ActionMenuView is currently presenting. |
| 634 | * |
| 635 | * <p>Applications should use this method to obtain the ActionMenuView's Menu object |
| 636 | * and inflate or add content to it as necessary.</p> |
| 637 | * |
| 638 | * @return the Menu presented by this view |
| 639 | */ |
| 640 | public Menu getMenu() { |
| 641 | if (mMenu == null) { |
| 642 | final Context context = getContext(); |
| 643 | mMenu = new MenuBuilder(context); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 644 | mMenu.setCallback(new MenuBuilderCallback()); |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 645 | mPresenter = new ActionMenuPresenter(context); |
Adam Powell | e8d1536 | 2014-09-09 13:51:16 -0700 | [diff] [blame] | 646 | mPresenter.setReserveOverflow(true); |
Adam Powell | 04c0d46 | 2014-09-04 16:10:24 -0700 | [diff] [blame] | 647 | mPresenter.setCallback(mActionMenuPresenterCallback != null |
| 648 | ? mActionMenuPresenterCallback : new ActionMenuPresenterCallback()); |
Alan Viverette | 3d0f21d | 2014-07-10 16:15:01 -0700 | [diff] [blame] | 649 | mMenu.addMenuPresenter(mPresenter, mPopupContext); |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 650 | mPresenter.setMenuView(this); |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | return mMenu; |
| 654 | } |
| 655 | |
| 656 | /** |
Adam Powell | 04c0d46 | 2014-09-04 16:10:24 -0700 | [diff] [blame] | 657 | * Must be called before the first call to getMenu() |
| 658 | * @hide |
| 659 | */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 660 | @UnsupportedAppUsage |
Adam Powell | c461250 | 2014-09-04 19:16:39 -0700 | [diff] [blame] | 661 | public void setMenuCallbacks(MenuPresenter.Callback pcb, MenuBuilder.Callback mcb) { |
| 662 | mActionMenuPresenterCallback = pcb; |
| 663 | mMenuBuilderCallback = mcb; |
Adam Powell | 04c0d46 | 2014-09-04 16:10:24 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /** |
Adam Powell | e021e6e | 2014-05-23 17:27:24 -0700 | [diff] [blame] | 667 | * Returns the current menu or null if one has not yet been configured. |
| 668 | * @hide Internal use only for action bar integration |
| 669 | */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 670 | @UnsupportedAppUsage |
Adam Powell | e021e6e | 2014-05-23 17:27:24 -0700 | [diff] [blame] | 671 | public MenuBuilder peekMenu() { |
| 672 | return mMenu; |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Show the overflow items from the associated menu. |
| 677 | * |
| 678 | * @return true if the menu was able to be shown, false otherwise |
| 679 | */ |
| 680 | public boolean showOverflowMenu() { |
| 681 | return mPresenter != null && mPresenter.showOverflowMenu(); |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Hide the overflow items from the associated menu. |
| 686 | * |
| 687 | * @return true if the menu was able to be hidden, false otherwise |
| 688 | */ |
| 689 | public boolean hideOverflowMenu() { |
| 690 | return mPresenter != null && mPresenter.hideOverflowMenu(); |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Check whether the overflow menu is currently showing. This may not reflect |
| 695 | * a pending show operation in progress. |
| 696 | * |
| 697 | * @return true if the overflow menu is currently showing |
| 698 | */ |
| 699 | public boolean isOverflowMenuShowing() { |
| 700 | return mPresenter != null && mPresenter.isOverflowMenuShowing(); |
| 701 | } |
| 702 | |
| 703 | /** @hide */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 704 | @UnsupportedAppUsage |
Adam Powell | e021e6e | 2014-05-23 17:27:24 -0700 | [diff] [blame] | 705 | public boolean isOverflowMenuShowPending() { |
| 706 | return mPresenter != null && mPresenter.isOverflowMenuShowPending(); |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * Dismiss any popups associated with this menu view. |
| 711 | */ |
| 712 | public void dismissPopupMenus() { |
| 713 | if (mPresenter != null) { |
| 714 | mPresenter.dismissPopupMenus(); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | /** |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 719 | * @hide Private LinearLayout (superclass) API. Un-hide if LinearLayout API is made public. |
| 720 | */ |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 721 | @Override |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 722 | @UnsupportedAppUsage |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 723 | protected boolean hasDividerBeforeChildAt(int childIndex) { |
Jake Wharton | 825992f | 2012-07-28 21:31:51 -0700 | [diff] [blame] | 724 | if (childIndex == 0) { |
| 725 | return false; |
| 726 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 727 | final View childBefore = getChildAt(childIndex - 1); |
| 728 | final View child = getChildAt(childIndex); |
| 729 | boolean result = false; |
| 730 | if (childIndex < getChildCount() && childBefore instanceof ActionMenuChildView) { |
| 731 | result |= ((ActionMenuChildView) childBefore).needsDividerAfter(); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 732 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 733 | if (childIndex > 0 && child instanceof ActionMenuChildView) { |
| 734 | result |= ((ActionMenuChildView) child).needsDividerBefore(); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 735 | } |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 736 | return result; |
| 737 | } |
| 738 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 739 | /** @hide */ |
| 740 | public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 741 | return false; |
| 742 | } |
| 743 | |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 744 | /** @hide */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 745 | @UnsupportedAppUsage |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 746 | public void setExpandedActionViewsExclusive(boolean exclusive) { |
| 747 | mPresenter.setExpandedActionViewsExclusive(exclusive); |
| 748 | } |
| 749 | |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 750 | /** |
| 751 | * Interface responsible for receiving menu item click events if the items themselves |
| 752 | * do not have individual item click listeners. |
| 753 | */ |
| 754 | public interface OnMenuItemClickListener { |
| 755 | /** |
| 756 | * This method will be invoked when a menu item is clicked if the item itself did |
| 757 | * not already handle the event. |
| 758 | * |
| 759 | * @param item {@link MenuItem} that was clicked |
| 760 | * @return <code>true</code> if the event was handled, <code>false</code> otherwise. |
| 761 | */ |
| 762 | public boolean onMenuItemClick(MenuItem item); |
| 763 | } |
| 764 | |
| 765 | private class MenuBuilderCallback implements MenuBuilder.Callback { |
| 766 | @Override |
| 767 | public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { |
| 768 | return mOnMenuItemClickListener != null && |
| 769 | mOnMenuItemClickListener.onMenuItemClick(item); |
| 770 | } |
| 771 | |
| 772 | @Override |
| 773 | public void onMenuModeChange(MenuBuilder menu) { |
Adam Powell | c461250 | 2014-09-04 19:16:39 -0700 | [diff] [blame] | 774 | if (mMenuBuilderCallback != null) { |
| 775 | mMenuBuilderCallback.onMenuModeChange(menu); |
| 776 | } |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
| 780 | private class ActionMenuPresenterCallback implements ActionMenuPresenter.Callback { |
| 781 | @Override |
| 782 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 783 | } |
| 784 | |
| 785 | @Override |
| 786 | public boolean onOpenSubMenu(MenuBuilder subMenu) { |
| 787 | return false; |
| 788 | } |
| 789 | } |
| 790 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 791 | /** @hide */ |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 792 | public interface ActionMenuChildView { |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 793 | @UnsupportedAppUsage |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 794 | public boolean needsDividerBefore(); |
| 795 | public boolean needsDividerAfter(); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 796 | } |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 797 | |
| 798 | public static class LayoutParams extends LinearLayout.LayoutParams { |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 799 | /** @hide */ |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 800 | @ViewDebug.ExportedProperty(category = "layout") |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 801 | @UnsupportedAppUsage |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 802 | public boolean isOverflowButton; |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 803 | |
| 804 | /** @hide */ |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 805 | @ViewDebug.ExportedProperty(category = "layout") |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 806 | @UnsupportedAppUsage |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 807 | public int cellsUsed; |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 808 | |
| 809 | /** @hide */ |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 810 | @ViewDebug.ExportedProperty(category = "layout") |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 811 | @UnsupportedAppUsage |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 812 | public int extraPixels; |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 813 | |
| 814 | /** @hide */ |
Adam Powell | 160bb7f | 2011-07-07 10:22:27 -0700 | [diff] [blame] | 815 | @ViewDebug.ExportedProperty(category = "layout") |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 816 | @UnsupportedAppUsage |
Adam Powell | 160bb7f | 2011-07-07 10:22:27 -0700 | [diff] [blame] | 817 | public boolean expandable; |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 818 | |
| 819 | /** @hide */ |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 820 | @ViewDebug.ExportedProperty(category = "layout") |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 821 | @UnsupportedAppUsage |
Adam Powell | be3c329 | 2011-08-24 12:52:28 -0700 | [diff] [blame] | 822 | public boolean preventEdgeOffset; |
Adam Powell | 160bb7f | 2011-07-07 10:22:27 -0700 | [diff] [blame] | 823 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 824 | /** @hide */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 825 | @UnsupportedAppUsage |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 826 | public boolean expanded; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 827 | |
| 828 | public LayoutParams(Context c, AttributeSet attrs) { |
| 829 | super(c, attrs); |
| 830 | } |
| 831 | |
Adam Powell | 8c1b02e | 2012-07-16 17:58:18 -0700 | [diff] [blame] | 832 | public LayoutParams(ViewGroup.LayoutParams other) { |
| 833 | super(other); |
| 834 | } |
| 835 | |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 836 | public LayoutParams(LayoutParams other) { |
| 837 | super((LinearLayout.LayoutParams) other); |
| 838 | isOverflowButton = other.isOverflowButton; |
| 839 | } |
| 840 | |
| 841 | public LayoutParams(int width, int height) { |
| 842 | super(width, height); |
| 843 | isOverflowButton = false; |
| 844 | } |
| 845 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 846 | /** @hide */ |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 847 | public LayoutParams(int width, int height, boolean isOverflowButton) { |
| 848 | super(width, height); |
| 849 | this.isOverflowButton = isOverflowButton; |
| 850 | } |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 851 | |
| 852 | /** @hide */ |
| 853 | @Override |
| 854 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 855 | super.encodeProperties(encoder); |
| 856 | |
| 857 | encoder.addProperty("layout:overFlowButton", isOverflowButton); |
| 858 | encoder.addProperty("layout:cellsUsed", cellsUsed); |
| 859 | encoder.addProperty("layout:extraPixels", extraPixels); |
| 860 | encoder.addProperty("layout:expandable", expandable); |
| 861 | encoder.addProperty("layout:preventEdgeOffset", preventEdgeOffset); |
| 862 | } |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 863 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 864 | } |