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 | */ |
| 16 | |
| 17 | package com.android.internal.view.menu; |
| 18 | |
| 19 | import android.content.Context; |
Adam Powell | d5c81db | 2012-08-02 14:35:26 -0700 | [diff] [blame] | 20 | import android.content.res.Configuration; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 22 | import android.content.res.TypedArray; |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 23 | import android.graphics.Rect; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 25 | import android.text.TextUtils; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 26 | import android.util.AttributeSet; |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 27 | import android.view.Gravity; |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 28 | import android.view.MotionEvent; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 29 | import android.view.View; |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 30 | import android.view.accessibility.AccessibilityEvent; |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 31 | import android.widget.ActionMenuView; |
Alan Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 32 | import android.widget.ListPopupWindow; |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 33 | import android.widget.TextView; |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 34 | import android.widget.Toast; |
Alan Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 35 | import android.widget.ListPopupWindow.ForwardingListener; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * @hide |
| 39 | */ |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 40 | public class ActionMenuItemView extends TextView |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 41 | implements MenuView.ItemView, View.OnClickListener, View.OnLongClickListener, |
| 42 | ActionMenuView.ActionMenuChildView { |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 43 | private static final String TAG = "ActionMenuItemView"; |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 44 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 45 | private MenuItemImpl mItemData; |
| 46 | private CharSequence mTitle; |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 47 | private Drawable mIcon; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 48 | private MenuBuilder.ItemInvoker mItemInvoker; |
Alan Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 49 | private ForwardingListener mForwardingListener; |
| 50 | private PopupCallback mPopupCallback; |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 51 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 52 | private boolean mAllowTextWithIcon; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 53 | private boolean mExpandedFormat; |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 54 | private int mMinWidth; |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 55 | private int mSavedPaddingLeft; |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 56 | |
Adam Powell | c0047d4 | 2012-07-30 12:18:15 -0700 | [diff] [blame] | 57 | private static final int MAX_ICON_SIZE = 32; // dp |
| 58 | private int mMaxIconSize; |
| 59 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 60 | public ActionMenuItemView(Context context) { |
| 61 | this(context, null); |
| 62 | } |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 63 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 64 | public ActionMenuItemView(Context context, AttributeSet attrs) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 65 | this(context, attrs, 0); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 66 | } |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 67 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 68 | public ActionMenuItemView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 69 | this(context, attrs, defStyleAttr, 0); |
| 70 | } |
| 71 | |
| 72 | public ActionMenuItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 73 | super(context, attrs, defStyleAttr, defStyleRes); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 74 | final Resources res = context.getResources(); |
| 75 | mAllowTextWithIcon = res.getBoolean( |
| 76 | com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon); |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 77 | final TypedArray a = context.obtainStyledAttributes(attrs, |
| 78 | com.android.internal.R.styleable.ActionMenuItemView, defStyleAttr, defStyleRes); |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 79 | mMinWidth = a.getDimensionPixelSize( |
| 80 | com.android.internal.R.styleable.ActionMenuItemView_minWidth, 0); |
| 81 | a.recycle(); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 82 | |
Adam Powell | c0047d4 | 2012-07-30 12:18:15 -0700 | [diff] [blame] | 83 | final float density = res.getDisplayMetrics().density; |
| 84 | mMaxIconSize = (int) (MAX_ICON_SIZE * density + 0.5f); |
| 85 | |
Adam Powell | 6bddd87 | 2011-03-24 15:34:54 -0700 | [diff] [blame] | 86 | setOnClickListener(this); |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 87 | setOnLongClickListener(this); |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 88 | |
Adam Powell | 367ee32 | 2012-05-06 18:32:33 -0700 | [diff] [blame] | 89 | mSavedPaddingLeft = -1; |
| 90 | } |
| 91 | |
| 92 | @Override |
Adam Powell | d5c81db | 2012-08-02 14:35:26 -0700 | [diff] [blame] | 93 | public void onConfigurationChanged(Configuration newConfig) { |
| 94 | super.onConfigurationChanged(newConfig); |
| 95 | |
| 96 | mAllowTextWithIcon = getContext().getResources().getBoolean( |
| 97 | com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon); |
| 98 | updateTextButtonVisibility(); |
| 99 | } |
| 100 | |
| 101 | @Override |
Adam Powell | 367ee32 | 2012-05-06 18:32:33 -0700 | [diff] [blame] | 102 | public void setPadding(int l, int t, int r, int b) { |
| 103 | mSavedPaddingLeft = l; |
| 104 | super.setPadding(l, t, r, b); |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 107 | public MenuItemImpl getItemData() { |
| 108 | return mItemData; |
| 109 | } |
| 110 | |
Alan Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 111 | @Override |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 112 | public void initialize(MenuItemImpl itemData, int menuType) { |
| 113 | mItemData = itemData; |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 114 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 115 | setIcon(itemData.getIcon()); |
Adam Powell | e7d4684 | 2011-01-13 21:36:09 -0800 | [diff] [blame] | 116 | setTitle(itemData.getTitleForItemView(this)); // Title only takes effect if there is no icon |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 117 | setId(itemData.getItemId()); |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 118 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 119 | setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE); |
| 120 | setEnabled(itemData.isEnabled()); |
Alan Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 121 | |
| 122 | if (itemData.hasSubMenu()) { |
| 123 | if (mForwardingListener == null) { |
| 124 | mForwardingListener = new ActionMenuItemForwardingListener(); |
| 125 | } |
| 126 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 127 | } |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 128 | |
Alan Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 129 | @Override |
| 130 | public boolean onTouchEvent(MotionEvent e) { |
| 131 | if (mItemData.hasSubMenu() && mForwardingListener != null |
| 132 | && mForwardingListener.onTouch(this, e)) { |
| 133 | return true; |
| 134 | } |
| 135 | return super.onTouchEvent(e); |
| 136 | } |
| 137 | |
| 138 | @Override |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 139 | public void onClick(View v) { |
| 140 | if (mItemInvoker != null) { |
| 141 | mItemInvoker.invokeItem(mItemData); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 144 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 145 | public void setItemInvoker(MenuBuilder.ItemInvoker invoker) { |
| 146 | mItemInvoker = invoker; |
| 147 | } |
| 148 | |
Alan Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 149 | public void setPopupCallback(PopupCallback popupCallback) { |
| 150 | mPopupCallback = popupCallback; |
| 151 | } |
| 152 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 153 | public boolean prefersCondensedTitle() { |
Adam Powell | e7d4684 | 2011-01-13 21:36:09 -0800 | [diff] [blame] | 154 | return true; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | public void setCheckable(boolean checkable) { |
| 158 | // TODO Support checkable action items |
| 159 | } |
| 160 | |
| 161 | public void setChecked(boolean checked) { |
| 162 | // TODO Support checkable action items |
| 163 | } |
| 164 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 165 | public void setExpandedFormat(boolean expandedFormat) { |
| 166 | if (mExpandedFormat != expandedFormat) { |
| 167 | mExpandedFormat = expandedFormat; |
| 168 | if (mItemData != null) { |
| 169 | mItemData.actionFormatChanged(); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 174 | private void updateTextButtonVisibility() { |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 175 | boolean visible = !TextUtils.isEmpty(mTitle); |
| 176 | visible &= mIcon == null || |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 177 | (mItemData.showsTextAsAction() && (mAllowTextWithIcon || mExpandedFormat)); |
| 178 | |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 179 | setText(visible ? mTitle : null); |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 182 | public void setIcon(Drawable icon) { |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 183 | mIcon = icon; |
Adam Powell | c1eea13 | 2012-07-31 11:27:19 -0700 | [diff] [blame] | 184 | if (icon != null) { |
| 185 | int width = icon.getIntrinsicWidth(); |
| 186 | int height = icon.getIntrinsicHeight(); |
| 187 | if (width > mMaxIconSize) { |
| 188 | final float scale = (float) mMaxIconSize / width; |
| 189 | width = mMaxIconSize; |
| 190 | height *= scale; |
| 191 | } |
| 192 | if (height > mMaxIconSize) { |
| 193 | final float scale = (float) mMaxIconSize / height; |
| 194 | height = mMaxIconSize; |
| 195 | width *= scale; |
| 196 | } |
| 197 | icon.setBounds(0, 0, width, height); |
Adam Powell | c0047d4 | 2012-07-30 12:18:15 -0700 | [diff] [blame] | 198 | } |
Adam Powell | c0047d4 | 2012-07-30 12:18:15 -0700 | [diff] [blame] | 199 | setCompoundDrawables(icon, null, null, null); |
Adam Powell | 9f125d3 | 2011-01-14 15:36:56 -0800 | [diff] [blame] | 200 | |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 201 | updateTextButtonVisibility(); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 202 | } |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 203 | |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 204 | public boolean hasText() { |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 205 | return !TextUtils.isEmpty(getText()); |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 206 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 207 | |
| 208 | public void setShortcut(boolean showShortcut, char shortcutKey) { |
| 209 | // Action buttons don't show text for shortcut keys. |
| 210 | } |
| 211 | |
| 212 | public void setTitle(CharSequence title) { |
| 213 | mTitle = title; |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 214 | |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 215 | setContentDescription(mTitle); |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 216 | updateTextButtonVisibility(); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 219 | @Override |
| 220 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 221 | onPopulateAccessibilityEvent(event); |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public void onPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 227 | super.onPopulateAccessibilityEvent(event); |
| 228 | final CharSequence cdesc = getContentDescription(); |
| 229 | if (!TextUtils.isEmpty(cdesc)) { |
| 230 | event.getText().add(cdesc); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public boolean dispatchHoverEvent(MotionEvent event) { |
| 236 | // Don't allow children to hover; we want this to be treated as a single component. |
| 237 | return onHoverEvent(event); |
| 238 | } |
| 239 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 240 | public boolean showsIcon() { |
| 241 | return true; |
| 242 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 243 | |
| 244 | public boolean needsDividerBefore() { |
| 245 | return hasText() && mItemData.getIcon() == null; |
| 246 | } |
| 247 | |
| 248 | public boolean needsDividerAfter() { |
| 249 | return hasText(); |
| 250 | } |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 251 | |
| 252 | @Override |
| 253 | public boolean onLongClick(View v) { |
| 254 | if (hasText()) { |
| 255 | // Don't show the cheat sheet for items that already show text. |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | final int[] screenPos = new int[2]; |
| 260 | final Rect displayFrame = new Rect(); |
| 261 | getLocationOnScreen(screenPos); |
| 262 | getWindowVisibleDisplayFrame(displayFrame); |
| 263 | |
| 264 | final Context context = getContext(); |
| 265 | final int width = getWidth(); |
| 266 | final int height = getHeight(); |
| 267 | final int midy = screenPos[1] + height / 2; |
| 268 | final int screenWidth = context.getResources().getDisplayMetrics().widthPixels; |
| 269 | |
| 270 | Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT); |
| 271 | if (midy < displayFrame.height()) { |
| 272 | // Show along the top; follow action buttons |
Fabrice Di Meglio | aac0d4e | 2012-07-19 19:21:26 -0700 | [diff] [blame] | 273 | cheatSheet.setGravity(Gravity.TOP | Gravity.END, |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 274 | screenWidth - screenPos[0] - width / 2, height); |
| 275 | } else { |
| 276 | // Show along the bottom center |
| 277 | cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height); |
| 278 | } |
| 279 | cheatSheet.show(); |
| 280 | return true; |
| 281 | } |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 282 | |
| 283 | @Override |
| 284 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adam Powell | d5c81db | 2012-08-02 14:35:26 -0700 | [diff] [blame] | 285 | if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) { |
| 286 | // Fill all available height. |
| 287 | heightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 288 | MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY); |
| 289 | } |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 290 | final boolean textVisible = hasText(); |
Adam Powell | 367ee32 | 2012-05-06 18:32:33 -0700 | [diff] [blame] | 291 | if (textVisible && mSavedPaddingLeft >= 0) { |
| 292 | super.setPadding(mSavedPaddingLeft, getPaddingTop(), |
| 293 | getPaddingRight(), getPaddingBottom()); |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 296 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 297 | |
| 298 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 299 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 300 | final int oldMeasuredWidth = getMeasuredWidth(); |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 301 | final int targetWidth = widthMode == MeasureSpec.AT_MOST ? Math.min(widthSize, mMinWidth) |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 302 | : mMinWidth; |
| 303 | |
| 304 | if (widthMode != MeasureSpec.EXACTLY && mMinWidth > 0 && oldMeasuredWidth < targetWidth) { |
| 305 | // Remeasure at exactly the minimum width. |
| 306 | super.onMeasure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY), |
| 307 | heightMeasureSpec); |
| 308 | } |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 309 | |
| 310 | if (!textVisible && mIcon != null) { |
| 311 | // TextView won't center compound drawables in both dimensions without |
| 312 | // a little coercion. Pad in to center the icon after we've measured. |
| 313 | final int w = getMeasuredWidth(); |
Adam Powell | c0047d4 | 2012-07-30 12:18:15 -0700 | [diff] [blame] | 314 | final int dw = mIcon.getBounds().width(); |
Adam Powell | 367ee32 | 2012-05-06 18:32:33 -0700 | [diff] [blame] | 315 | super.setPadding((w - dw) / 2, getPaddingTop(), getPaddingRight(), getPaddingBottom()); |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 316 | } |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 317 | } |
Alan Viverette | 5611072 | 2014-01-10 14:03:21 -0800 | [diff] [blame] | 318 | |
| 319 | private class ActionMenuItemForwardingListener extends ForwardingListener { |
| 320 | public ActionMenuItemForwardingListener() { |
| 321 | super(ActionMenuItemView.this); |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public ListPopupWindow getPopup() { |
| 326 | if (mPopupCallback != null) { |
| 327 | return mPopupCallback.getPopup(); |
| 328 | } |
| 329 | return null; |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | protected boolean onForwardingStarted() { |
| 334 | // Call the invoker, then check if the expected popup is showing. |
| 335 | if (mItemInvoker != null && mItemInvoker.invokeItem(mItemData)) { |
| 336 | final ListPopupWindow popup = getPopup(); |
| 337 | return popup != null && popup.isShowing(); |
| 338 | } |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | protected boolean onForwardingStopped() { |
| 344 | final ListPopupWindow popup = getPopup(); |
| 345 | if (popup != null) { |
| 346 | popup.dismiss(); |
| 347 | return true; |
| 348 | } |
| 349 | return false; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | public static abstract class PopupCallback { |
| 354 | public abstract ListPopupWindow getPopup(); |
| 355 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 356 | } |