196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell/* 296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * Copyright (C) 2010 The Android Open Source Project 396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * 496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * Licensed under the Apache License, Version 2.0 (the "License"); 596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * you may not use this file except in compliance with the License. 696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * You may obtain a copy of the License at 796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * 896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * http://www.apache.org/licenses/LICENSE-2.0 996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * 1096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * Unless required by applicable law or agreed to in writing, software 1196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * distributed under the License is distributed on an "AS IS" BASIS, 1296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * See the License for the specific language governing permissions and 1496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * limitations under the License. 1596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell */ 1696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 1796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellpackage com.android.internal.view.menu; 1896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 1996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.content.Context; 20d5c81db1e78e98f3e0a1a5cf206865c3056294c4Adam Powellimport android.content.res.Configuration; 2135aecd5884a5ccfe380903e39f30f468315e8f92Adam Powellimport android.content.res.Resources; 2224340afbab8ad4c191784fda8023407205bc0a75Adam Powellimport android.content.res.TypedArray; 237b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powellimport android.graphics.Rect; 2496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.graphics.drawable.Drawable; 25c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunneimport android.text.TextUtils; 2696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.util.AttributeSet; 277b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powellimport android.view.Gravity; 287bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powellimport android.view.MotionEvent; 2996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.view.View; 307bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powellimport android.view.accessibility.AccessibilityEvent; 31fa18d182a3f37505940e73ae6cd76c2e939f7f7cAdam Powellimport android.widget.ActionMenuView; 3256110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viveretteimport android.widget.ListPopupWindow; 3375d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powellimport android.widget.TextView; 347b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powellimport android.widget.Toast; 3556110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viveretteimport android.widget.ListPopupWindow.ForwardingListener; 3696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 3796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell/** 3896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * @hide 3996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell */ 4075d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powellpublic class ActionMenuItemView extends TextView 417b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell implements MenuView.ItemView, View.OnClickListener, View.OnLongClickListener, 427b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell ActionMenuView.ActionMenuChildView { 4396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell private static final String TAG = "ActionMenuItemView"; 44a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 4596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell private MenuItemImpl mItemData; 4696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell private CharSequence mTitle; 4775d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell private Drawable mIcon; 4896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell private MenuBuilder.ItemInvoker mItemInvoker; 4956110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette private ForwardingListener mForwardingListener; 5056110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette private PopupCallback mPopupCallback; 51a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 5235aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell private boolean mAllowTextWithIcon; 5335aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell private boolean mExpandedFormat; 5424340afbab8ad4c191784fda8023407205bc0a75Adam Powell private int mMinWidth; 5575d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell private int mSavedPaddingLeft; 561f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell 57c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell private static final int MAX_ICON_SIZE = 32; // dp 58c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell private int mMaxIconSize; 59c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell 6096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public ActionMenuItemView(Context context) { 6196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell this(context, null); 6296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 63a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 6496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public ActionMenuItemView(Context context, AttributeSet attrs) { 6535aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell this(context, attrs, 0); 6696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 67a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 68617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette public ActionMenuItemView(Context context, AttributeSet attrs, int defStyleAttr) { 69617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette this(context, attrs, defStyleAttr, 0); 70617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette } 71617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette 72617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette public ActionMenuItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 73617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette super(context, attrs, defStyleAttr, defStyleRes); 7435aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell final Resources res = context.getResources(); 7535aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell mAllowTextWithIcon = res.getBoolean( 7635aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon); 77617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette final TypedArray a = context.obtainStyledAttributes(attrs, 78617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette com.android.internal.R.styleable.ActionMenuItemView, defStyleAttr, defStyleRes); 7924340afbab8ad4c191784fda8023407205bc0a75Adam Powell mMinWidth = a.getDimensionPixelSize( 8024340afbab8ad4c191784fda8023407205bc0a75Adam Powell com.android.internal.R.styleable.ActionMenuItemView_minWidth, 0); 8124340afbab8ad4c191784fda8023407205bc0a75Adam Powell a.recycle(); 8296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 83c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell final float density = res.getDisplayMetrics().density; 84c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell mMaxIconSize = (int) (MAX_ICON_SIZE * density + 0.5f); 85c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell 866bddd8771d05889024778caa78fb1eaae68a0802Adam Powell setOnClickListener(this); 877b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell setOnLongClickListener(this); 8875d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell 89367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell mSavedPaddingLeft = -1; 90367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell } 91367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell 92367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell @Override 93d5c81db1e78e98f3e0a1a5cf206865c3056294c4Adam Powell public void onConfigurationChanged(Configuration newConfig) { 94d5c81db1e78e98f3e0a1a5cf206865c3056294c4Adam Powell super.onConfigurationChanged(newConfig); 95d5c81db1e78e98f3e0a1a5cf206865c3056294c4Adam Powell 96d5c81db1e78e98f3e0a1a5cf206865c3056294c4Adam Powell mAllowTextWithIcon = getContext().getResources().getBoolean( 97d5c81db1e78e98f3e0a1a5cf206865c3056294c4Adam Powell com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon); 98d5c81db1e78e98f3e0a1a5cf206865c3056294c4Adam Powell updateTextButtonVisibility(); 99d5c81db1e78e98f3e0a1a5cf206865c3056294c4Adam Powell } 100d5c81db1e78e98f3e0a1a5cf206865c3056294c4Adam Powell 101d5c81db1e78e98f3e0a1a5cf206865c3056294c4Adam Powell @Override 102367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell public void setPadding(int l, int t, int r, int b) { 103367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell mSavedPaddingLeft = l; 104367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell super.setPadding(l, t, r, b); 1051f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell } 1061f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell 10796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public MenuItemImpl getItemData() { 10896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell return mItemData; 10996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 11096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 11156110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette @Override 11296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void initialize(MenuItemImpl itemData, int menuType) { 11396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell mItemData = itemData; 114a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 11596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell setIcon(itemData.getIcon()); 116e7d468410b3a783560d5158a5798cef1b4b67702Adam Powell setTitle(itemData.getTitleForItemView(this)); // Title only takes effect if there is no icon 11796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell setId(itemData.getItemId()); 118a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 11996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE); 12096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell setEnabled(itemData.isEnabled()); 12156110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette 12256110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette if (itemData.hasSubMenu()) { 12356110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette if (mForwardingListener == null) { 12456110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette mForwardingListener = new ActionMenuItemForwardingListener(); 12556110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 12656110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 12756110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 12856110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette 12956110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette @Override 13056110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette public boolean onTouchEvent(MotionEvent e) { 13156110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette if (mItemData.hasSubMenu() && mForwardingListener != null 13256110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette && mForwardingListener.onTouch(this, e)) { 13356110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette return true; 13456110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 13556110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette return super.onTouchEvent(e); 13696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 137a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 13856110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette @Override 1391f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell public void onClick(View v) { 1401f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell if (mItemInvoker != null) { 1411f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell mItemInvoker.invokeItem(mItemData); 14296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 14396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 144a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 14596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void setItemInvoker(MenuBuilder.ItemInvoker invoker) { 14696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell mItemInvoker = invoker; 14796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 14896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 14956110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette public void setPopupCallback(PopupCallback popupCallback) { 15056110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette mPopupCallback = popupCallback; 15156110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 15256110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette 15396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public boolean prefersCondensedTitle() { 154e7d468410b3a783560d5158a5798cef1b4b67702Adam Powell return true; 15596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 15696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 15796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void setCheckable(boolean checkable) { 15896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell // TODO Support checkable action items 15996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 16096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 16196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void setChecked(boolean checked) { 16296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell // TODO Support checkable action items 16396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 16496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 16535aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell public void setExpandedFormat(boolean expandedFormat) { 16635aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell if (mExpandedFormat != expandedFormat) { 16735aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell mExpandedFormat = expandedFormat; 16835aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell if (mItemData != null) { 16935aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell mItemData.actionFormatChanged(); 17035aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell } 17135aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell } 17235aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell } 17335aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell 174c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne private void updateTextButtonVisibility() { 17575d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell boolean visible = !TextUtils.isEmpty(mTitle); 17675d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell visible &= mIcon == null || 17735aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell (mItemData.showsTextAsAction() && (mAllowTextWithIcon || mExpandedFormat)); 17835aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell 17975d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell setText(visible ? mTitle : null); 180c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne } 181c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne 18296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void setIcon(Drawable icon) { 18375d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell mIcon = icon; 184c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell if (icon != null) { 185c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell int width = icon.getIntrinsicWidth(); 186c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell int height = icon.getIntrinsicHeight(); 187c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell if (width > mMaxIconSize) { 188c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell final float scale = (float) mMaxIconSize / width; 189c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell width = mMaxIconSize; 190c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell height *= scale; 191c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell } 192c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell if (height > mMaxIconSize) { 193c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell final float scale = (float) mMaxIconSize / height; 194c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell height = mMaxIconSize; 195c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell width *= scale; 196c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell } 197c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell icon.setBounds(0, 0, width, height); 198c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell } 199c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell setCompoundDrawables(icon, null, null, null); 2009f125d341521efc8535e32e35a641be50d405c36Adam Powell 201c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne updateTextButtonVisibility(); 20296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 203c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne 204be4d68e7b238b8ee879de0481e39c40d3f1683b6Adam Powell public boolean hasText() { 20575d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell return !TextUtils.isEmpty(getText()); 206be4d68e7b238b8ee879de0481e39c40d3f1683b6Adam Powell } 20796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 20896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void setShortcut(boolean showShortcut, char shortcutKey) { 20996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell // Action buttons don't show text for shortcut keys. 21096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 21196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 21296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void setTitle(CharSequence title) { 21396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell mTitle = title; 214a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 2157bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell setContentDescription(mTitle); 216c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne updateTextButtonVisibility(); 21796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 21896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 2197bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell @Override 220a54956a0bc611b1e9b3914edc7a604b59688f6b7Alan Viverette public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) { 2217bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell onPopulateAccessibilityEvent(event); 2227bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell return true; 2237bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell } 2247bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell 2257bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell @Override 226a54956a0bc611b1e9b3914edc7a604b59688f6b7Alan Viverette public void onPopulateAccessibilityEventInternal(AccessibilityEvent event) { 227a54956a0bc611b1e9b3914edc7a604b59688f6b7Alan Viverette super.onPopulateAccessibilityEventInternal(event); 2287bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell final CharSequence cdesc = getContentDescription(); 2297bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell if (!TextUtils.isEmpty(cdesc)) { 2307bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell event.getText().add(cdesc); 2317bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell } 2327bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell } 2337bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell 2347bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell @Override 2357bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell public boolean dispatchHoverEvent(MotionEvent event) { 2367bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell // Don't allow children to hover; we want this to be treated as a single component. 2377bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell return onHoverEvent(event); 2387bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell } 2397bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell 24096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public boolean showsIcon() { 24196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell return true; 24296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 243696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 244696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell public boolean needsDividerBefore() { 245696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell return hasText() && mItemData.getIcon() == null; 246696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell } 247696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 248696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell public boolean needsDividerAfter() { 249696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell return hasText(); 250696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell } 2517b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell 2527b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell @Override 2537b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell public boolean onLongClick(View v) { 2547b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell if (hasText()) { 2557b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell // Don't show the cheat sheet for items that already show text. 2567b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell return false; 2577b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell } 2587b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell 2597b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell final int[] screenPos = new int[2]; 2607b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell final Rect displayFrame = new Rect(); 2617b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell getLocationOnScreen(screenPos); 2627b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell getWindowVisibleDisplayFrame(displayFrame); 2637b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell 2647b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell final Context context = getContext(); 2657b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell final int width = getWidth(); 2667b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell final int height = getHeight(); 2677b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell final int midy = screenPos[1] + height / 2; 268d719539f91d4f5cf6c0f9be0b68b750dcd443433Yigit Boyar int referenceX = screenPos[0] + width / 2; 269d719539f91d4f5cf6c0f9be0b68b750dcd443433Yigit Boyar if (v.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR) { 270d719539f91d4f5cf6c0f9be0b68b750dcd443433Yigit Boyar final int screenWidth = context.getResources().getDisplayMetrics().widthPixels; 271d719539f91d4f5cf6c0f9be0b68b750dcd443433Yigit Boyar referenceX = screenWidth - referenceX; // mirror 272d719539f91d4f5cf6c0f9be0b68b750dcd443433Yigit Boyar } 2737b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT); 2747b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell if (midy < displayFrame.height()) { 2757b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell // Show along the top; follow action buttons 276d74f960eceb98042ede53f74d02e174eb1ad5944Chris Banes cheatSheet.setGravity(Gravity.TOP | Gravity.END, referenceX, 277d74f960eceb98042ede53f74d02e174eb1ad5944Chris Banes screenPos[1] + height - displayFrame.top); 2787b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell } else { 2797b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell // Show along the bottom center 2807b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height); 2817b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell } 2827b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell cheatSheet.show(); 2837b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell return true; 2847b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell } 28524340afbab8ad4c191784fda8023407205bc0a75Adam Powell 28624340afbab8ad4c191784fda8023407205bc0a75Adam Powell @Override 28724340afbab8ad4c191784fda8023407205bc0a75Adam Powell protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 28875d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell final boolean textVisible = hasText(); 289367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell if (textVisible && mSavedPaddingLeft >= 0) { 290367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell super.setPadding(mSavedPaddingLeft, getPaddingTop(), 291367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell getPaddingRight(), getPaddingBottom()); 29275d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell } 29375d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell 29424340afbab8ad4c191784fda8023407205bc0a75Adam Powell super.onMeasure(widthMeasureSpec, heightMeasureSpec); 29524340afbab8ad4c191784fda8023407205bc0a75Adam Powell 29624340afbab8ad4c191784fda8023407205bc0a75Adam Powell final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 29775d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell final int widthSize = MeasureSpec.getSize(widthMeasureSpec); 29824340afbab8ad4c191784fda8023407205bc0a75Adam Powell final int oldMeasuredWidth = getMeasuredWidth(); 29975d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell final int targetWidth = widthMode == MeasureSpec.AT_MOST ? Math.min(widthSize, mMinWidth) 30024340afbab8ad4c191784fda8023407205bc0a75Adam Powell : mMinWidth; 30124340afbab8ad4c191784fda8023407205bc0a75Adam Powell 30224340afbab8ad4c191784fda8023407205bc0a75Adam Powell if (widthMode != MeasureSpec.EXACTLY && mMinWidth > 0 && oldMeasuredWidth < targetWidth) { 30324340afbab8ad4c191784fda8023407205bc0a75Adam Powell // Remeasure at exactly the minimum width. 30424340afbab8ad4c191784fda8023407205bc0a75Adam Powell super.onMeasure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY), 30524340afbab8ad4c191784fda8023407205bc0a75Adam Powell heightMeasureSpec); 30624340afbab8ad4c191784fda8023407205bc0a75Adam Powell } 30775d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell 30875d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell if (!textVisible && mIcon != null) { 30975d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell // TextView won't center compound drawables in both dimensions without 31075d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell // a little coercion. Pad in to center the icon after we've measured. 31175d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell final int w = getMeasuredWidth(); 312c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell final int dw = mIcon.getBounds().width(); 313367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell super.setPadding((w - dw) / 2, getPaddingTop(), getPaddingRight(), getPaddingBottom()); 31475d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell } 31524340afbab8ad4c191784fda8023407205bc0a75Adam Powell } 31656110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette 31756110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette private class ActionMenuItemForwardingListener extends ForwardingListener { 31856110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette public ActionMenuItemForwardingListener() { 31956110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette super(ActionMenuItemView.this); 32056110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 32156110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette 32256110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette @Override 32356110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette public ListPopupWindow getPopup() { 32456110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette if (mPopupCallback != null) { 32556110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette return mPopupCallback.getPopup(); 32656110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 32756110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette return null; 32856110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 32956110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette 33056110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette @Override 33156110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette protected boolean onForwardingStarted() { 33256110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette // Call the invoker, then check if the expected popup is showing. 33356110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette if (mItemInvoker != null && mItemInvoker.invokeItem(mItemData)) { 33456110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette final ListPopupWindow popup = getPopup(); 33556110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette return popup != null && popup.isShowing(); 33656110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 33756110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette return false; 33856110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 33956110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette 34056110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette @Override 34156110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette protected boolean onForwardingStopped() { 34256110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette final ListPopupWindow popup = getPopup(); 34356110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette if (popup != null) { 34456110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette popup.dismiss(); 34556110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette return true; 34656110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 34756110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette return false; 34856110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 34956110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 35056110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette 35156110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette public static abstract class PopupCallback { 35256110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette public abstract ListPopupWindow getPopup(); 35356110725f2a0b3f09703f2cbe9705d97609ee1e5Alan Viverette } 35496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell} 355