ActionMenuItemView.java revision c1eea136eaae62091b44d7b06a3d61ac694e4fd7
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;
2035aecd5884a5ccfe380903e39f30f468315e8f92Adam Powellimport android.content.res.Resources;
2124340afbab8ad4c191784fda8023407205bc0a75Adam Powellimport android.content.res.TypedArray;
227b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powellimport android.graphics.Rect;
2396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.graphics.drawable.Drawable;
24c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunneimport android.text.TextUtils;
2596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.util.AttributeSet;
267b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powellimport android.view.Gravity;
277bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powellimport android.view.MotionEvent;
2896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.view.View;
297bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powellimport android.view.accessibility.AccessibilityEvent;
3075d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powellimport android.widget.TextView;
317b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powellimport android.widget.Toast;
3296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
3396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell/**
3496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * @hide
3596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell */
3675d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powellpublic class ActionMenuItemView extends TextView
377b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        implements MenuView.ItemView, View.OnClickListener, View.OnLongClickListener,
387b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        ActionMenuView.ActionMenuChildView {
3996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    private static final String TAG = "ActionMenuItemView";
40a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
4196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    private MenuItemImpl mItemData;
4296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    private CharSequence mTitle;
4375d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell    private Drawable mIcon;
4496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    private MenuBuilder.ItemInvoker mItemInvoker;
45a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
4635aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell    private boolean mAllowTextWithIcon;
4735aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell    private boolean mExpandedFormat;
4824340afbab8ad4c191784fda8023407205bc0a75Adam Powell    private int mMinWidth;
4975d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell    private int mSavedPaddingLeft;
501f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell
51c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell    private static final int MAX_ICON_SIZE = 32; // dp
52c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell    private int mMaxIconSize;
53c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell
5496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public ActionMenuItemView(Context context) {
5596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        this(context, null);
5696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
57a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
5896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public ActionMenuItemView(Context context, AttributeSet attrs) {
5935aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell        this(context, attrs, 0);
6096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
61a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
6296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) {
6396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        super(context, attrs, defStyle);
6435aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell        final Resources res = context.getResources();
6535aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell        mAllowTextWithIcon = res.getBoolean(
6635aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell                com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon);
6724340afbab8ad4c191784fda8023407205bc0a75Adam Powell        TypedArray a = context.obtainStyledAttributes(attrs,
6824340afbab8ad4c191784fda8023407205bc0a75Adam Powell                com.android.internal.R.styleable.ActionMenuItemView, 0, 0);
6924340afbab8ad4c191784fda8023407205bc0a75Adam Powell        mMinWidth = a.getDimensionPixelSize(
7024340afbab8ad4c191784fda8023407205bc0a75Adam Powell                com.android.internal.R.styleable.ActionMenuItemView_minWidth, 0);
7124340afbab8ad4c191784fda8023407205bc0a75Adam Powell        a.recycle();
7296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
73c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell        final float density = res.getDisplayMetrics().density;
74c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell        mMaxIconSize = (int) (MAX_ICON_SIZE * density + 0.5f);
75c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell
766bddd8771d05889024778caa78fb1eaae68a0802Adam Powell        setOnClickListener(this);
777b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        setOnLongClickListener(this);
7875d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell
79367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell        mSavedPaddingLeft = -1;
80367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell    }
81367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell
82367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell    @Override
83367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell    public void setPadding(int l, int t, int r, int b) {
84367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell        mSavedPaddingLeft = l;
85367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell        super.setPadding(l, t, r, b);
861f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell    }
871f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell
8896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public MenuItemImpl getItemData() {
8996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        return mItemData;
9096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
9196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
9296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void initialize(MenuItemImpl itemData, int menuType) {
9396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        mItemData = itemData;
94a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
9596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        setIcon(itemData.getIcon());
96e7d468410b3a783560d5158a5798cef1b4b67702Adam Powell        setTitle(itemData.getTitleForItemView(this)); // Title only takes effect if there is no icon
9796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        setId(itemData.getItemId());
98a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
9996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
10096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        setEnabled(itemData.isEnabled());
10196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
102a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
1031f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell    public void onClick(View v) {
1041f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        if (mItemInvoker != null) {
1051f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell            mItemInvoker.invokeItem(mItemData);
10696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        }
10796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
108a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
10996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void setItemInvoker(MenuBuilder.ItemInvoker invoker) {
11096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        mItemInvoker = invoker;
11196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
11296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
11396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public boolean prefersCondensedTitle() {
114e7d468410b3a783560d5158a5798cef1b4b67702Adam Powell        return true;
11596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
11696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
11796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void setCheckable(boolean checkable) {
11896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        // TODO Support checkable action items
11996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
12096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
12196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void setChecked(boolean checked) {
12296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        // TODO Support checkable action items
12396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
12496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
12535aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell    public void setExpandedFormat(boolean expandedFormat) {
12635aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell        if (mExpandedFormat != expandedFormat) {
12735aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell            mExpandedFormat = expandedFormat;
12835aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell            if (mItemData != null) {
12935aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell                mItemData.actionFormatChanged();
13035aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell            }
13135aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell        }
13235aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell    }
13335aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell
134c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne    private void updateTextButtonVisibility() {
13575d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell        boolean visible = !TextUtils.isEmpty(mTitle);
13675d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell        visible &= mIcon == null ||
13735aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell                (mItemData.showsTextAsAction() && (mAllowTextWithIcon || mExpandedFormat));
13835aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell
13975d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell        setText(visible ? mTitle : null);
140c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne    }
141c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne
14296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void setIcon(Drawable icon) {
14375d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell        mIcon = icon;
144c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell        if (icon != null) {
145c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell            int width = icon.getIntrinsicWidth();
146c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell            int height = icon.getIntrinsicHeight();
147c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell            if (width > mMaxIconSize) {
148c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell                final float scale = (float) mMaxIconSize / width;
149c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell                width = mMaxIconSize;
150c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell                height *= scale;
151c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell            }
152c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell            if (height > mMaxIconSize) {
153c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell                final float scale = (float) mMaxIconSize / height;
154c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell                height = mMaxIconSize;
155c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell                width *= scale;
156c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell            }
157c1eea136eaae62091b44d7b06a3d61ac694e4fd7Adam Powell            icon.setBounds(0, 0, width, height);
158c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell        }
159c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell        setCompoundDrawables(icon, null, null, null);
1609f125d341521efc8535e32e35a641be50d405c36Adam Powell
161c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne        updateTextButtonVisibility();
16296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
163c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne
164be4d68e7b238b8ee879de0481e39c40d3f1683b6Adam Powell    public boolean hasText() {
16575d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell        return !TextUtils.isEmpty(getText());
166be4d68e7b238b8ee879de0481e39c40d3f1683b6Adam Powell    }
16796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
16896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void setShortcut(boolean showShortcut, char shortcutKey) {
16996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        // Action buttons don't show text for shortcut keys.
17096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
17196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
17296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void setTitle(CharSequence title) {
17396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        mTitle = title;
174a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
1757bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell        setContentDescription(mTitle);
176c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne        updateTextButtonVisibility();
17796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
17896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
1797bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell    @Override
1807bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
1817bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell        onPopulateAccessibilityEvent(event);
1827bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell        return true;
1837bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell    }
1847bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell
1857bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell    @Override
1867bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell    public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
1877bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell        super.onPopulateAccessibilityEvent(event);
1887bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell        final CharSequence cdesc = getContentDescription();
1897bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell        if (!TextUtils.isEmpty(cdesc)) {
1907bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell            event.getText().add(cdesc);
1917bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell        }
1927bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell    }
1937bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell
1947bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell    @Override
1957bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell    public boolean dispatchHoverEvent(MotionEvent event) {
1967bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell        // Don't allow children to hover; we want this to be treated as a single component.
1977bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell        return onHoverEvent(event);
1987bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell    }
1997bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell
20096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public boolean showsIcon() {
20196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        return true;
20296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
203696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell
204696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    public boolean needsDividerBefore() {
205696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell        return hasText() && mItemData.getIcon() == null;
206696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    }
207696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell
208696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    public boolean needsDividerAfter() {
209696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell        return hasText();
210696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    }
2117b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell
2127b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell    @Override
2137b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell    public boolean onLongClick(View v) {
2147b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        if (hasText()) {
2157b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell            // Don't show the cheat sheet for items that already show text.
2167b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell            return false;
2177b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        }
2187b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell
2197b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        final int[] screenPos = new int[2];
2207b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        final Rect displayFrame = new Rect();
2217b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        getLocationOnScreen(screenPos);
2227b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        getWindowVisibleDisplayFrame(displayFrame);
2237b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell
2247b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        final Context context = getContext();
2257b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        final int width = getWidth();
2267b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        final int height = getHeight();
2277b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        final int midy = screenPos[1] + height / 2;
2287b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
2297b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell
2307b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT);
2317b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        if (midy < displayFrame.height()) {
2327b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell            // Show along the top; follow action buttons
233aac0d4ed026d1cfbcf3fa81c6e4eb96f4347ca17Fabrice Di Meglio            cheatSheet.setGravity(Gravity.TOP | Gravity.END,
2347b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell                    screenWidth - screenPos[0] - width / 2, height);
2357b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        } else {
2367b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell            // Show along the bottom center
2377b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell            cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
2387b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        }
2397b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        cheatSheet.show();
2407b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell        return true;
2417b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell    }
24224340afbab8ad4c191784fda8023407205bc0a75Adam Powell
24324340afbab8ad4c191784fda8023407205bc0a75Adam Powell    @Override
24424340afbab8ad4c191784fda8023407205bc0a75Adam Powell    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
24575d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell        final boolean textVisible = hasText();
246367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell        if (textVisible && mSavedPaddingLeft >= 0) {
247367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell            super.setPadding(mSavedPaddingLeft, getPaddingTop(),
248367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell                    getPaddingRight(), getPaddingBottom());
24975d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell        }
25075d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell
25124340afbab8ad4c191784fda8023407205bc0a75Adam Powell        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
25224340afbab8ad4c191784fda8023407205bc0a75Adam Powell
25324340afbab8ad4c191784fda8023407205bc0a75Adam Powell        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
25475d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
25524340afbab8ad4c191784fda8023407205bc0a75Adam Powell        final int oldMeasuredWidth = getMeasuredWidth();
25675d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell        final int targetWidth = widthMode == MeasureSpec.AT_MOST ? Math.min(widthSize, mMinWidth)
25724340afbab8ad4c191784fda8023407205bc0a75Adam Powell                : mMinWidth;
25824340afbab8ad4c191784fda8023407205bc0a75Adam Powell
25924340afbab8ad4c191784fda8023407205bc0a75Adam Powell        if (widthMode != MeasureSpec.EXACTLY && mMinWidth > 0 && oldMeasuredWidth < targetWidth) {
26024340afbab8ad4c191784fda8023407205bc0a75Adam Powell            // Remeasure at exactly the minimum width.
26124340afbab8ad4c191784fda8023407205bc0a75Adam Powell            super.onMeasure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY),
26224340afbab8ad4c191784fda8023407205bc0a75Adam Powell                    heightMeasureSpec);
26324340afbab8ad4c191784fda8023407205bc0a75Adam Powell        }
26475d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell
26575d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell        if (!textVisible && mIcon != null) {
26675d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell            // TextView won't center compound drawables in both dimensions without
26775d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell            // a little coercion. Pad in to center the icon after we've measured.
26875d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell            final int w = getMeasuredWidth();
269c0047d4e111b8cfbffdbebb55f846c7ae935e53eAdam Powell            final int dw = mIcon.getBounds().width();
270367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell            super.setPadding((w - dw) / 2, getPaddingTop(), getPaddingRight(), getPaddingBottom());
27175d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell        }
27224340afbab8ad4c191784fda8023407205bc0a75Adam Powell    }
27396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell}
274