ActionMenuItemView.java revision 367ee326058bbee6fc179b8b1eb2174fe7ba8f45
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
17package com.android.internal.view.menu;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.content.res.TypedArray;
22import android.graphics.Rect;
23import android.graphics.drawable.Drawable;
24import android.text.TextUtils;
25import android.util.AttributeSet;
26import android.view.Gravity;
27import android.view.MotionEvent;
28import android.view.View;
29import android.view.accessibility.AccessibilityEvent;
30import android.widget.TextView;
31import android.widget.Toast;
32
33/**
34 * @hide
35 */
36public class ActionMenuItemView extends TextView
37        implements MenuView.ItemView, View.OnClickListener, View.OnLongClickListener,
38        ActionMenuView.ActionMenuChildView {
39    private static final String TAG = "ActionMenuItemView";
40
41    private MenuItemImpl mItemData;
42    private CharSequence mTitle;
43    private Drawable mIcon;
44    private MenuBuilder.ItemInvoker mItemInvoker;
45
46    private boolean mAllowTextWithIcon;
47    private boolean mExpandedFormat;
48    private int mMinWidth;
49    private int mSavedPaddingLeft;
50
51    public ActionMenuItemView(Context context) {
52        this(context, null);
53    }
54
55    public ActionMenuItemView(Context context, AttributeSet attrs) {
56        this(context, attrs, 0);
57    }
58
59    public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) {
60        super(context, attrs, defStyle);
61        final Resources res = context.getResources();
62        mAllowTextWithIcon = res.getBoolean(
63                com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon);
64        TypedArray a = context.obtainStyledAttributes(attrs,
65                com.android.internal.R.styleable.ActionMenuItemView, 0, 0);
66        mMinWidth = a.getDimensionPixelSize(
67                com.android.internal.R.styleable.ActionMenuItemView_minWidth, 0);
68        a.recycle();
69
70        setOnClickListener(this);
71        setOnLongClickListener(this);
72
73        mSavedPaddingLeft = -1;
74    }
75
76    @Override
77    public void setPadding(int l, int t, int r, int b) {
78        mSavedPaddingLeft = l;
79        super.setPadding(l, t, r, b);
80    }
81
82    public MenuItemImpl getItemData() {
83        return mItemData;
84    }
85
86    public void initialize(MenuItemImpl itemData, int menuType) {
87        mItemData = itemData;
88
89        setIcon(itemData.getIcon());
90        setTitle(itemData.getTitleForItemView(this)); // Title only takes effect if there is no icon
91        setId(itemData.getItemId());
92
93        setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
94        setEnabled(itemData.isEnabled());
95    }
96
97    public void onClick(View v) {
98        if (mItemInvoker != null) {
99            mItemInvoker.invokeItem(mItemData);
100        }
101    }
102
103    public void setItemInvoker(MenuBuilder.ItemInvoker invoker) {
104        mItemInvoker = invoker;
105    }
106
107    public boolean prefersCondensedTitle() {
108        return true;
109    }
110
111    public void setCheckable(boolean checkable) {
112        // TODO Support checkable action items
113    }
114
115    public void setChecked(boolean checked) {
116        // TODO Support checkable action items
117    }
118
119    public void setExpandedFormat(boolean expandedFormat) {
120        if (mExpandedFormat != expandedFormat) {
121            mExpandedFormat = expandedFormat;
122            if (mItemData != null) {
123                mItemData.actionFormatChanged();
124            }
125        }
126    }
127
128    private void updateTextButtonVisibility() {
129        boolean visible = !TextUtils.isEmpty(mTitle);
130        visible &= mIcon == null ||
131                (mItemData.showsTextAsAction() && (mAllowTextWithIcon || mExpandedFormat));
132
133        setText(visible ? mTitle : null);
134    }
135
136    public void setIcon(Drawable icon) {
137        mIcon = icon;
138        setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
139
140        updateTextButtonVisibility();
141    }
142
143    public boolean hasText() {
144        return !TextUtils.isEmpty(getText());
145    }
146
147    public void setShortcut(boolean showShortcut, char shortcutKey) {
148        // Action buttons don't show text for shortcut keys.
149    }
150
151    public void setTitle(CharSequence title) {
152        mTitle = title;
153
154        setContentDescription(mTitle);
155        updateTextButtonVisibility();
156    }
157
158    @Override
159    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
160        onPopulateAccessibilityEvent(event);
161        return true;
162    }
163
164    @Override
165    public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
166        super.onPopulateAccessibilityEvent(event);
167        final CharSequence cdesc = getContentDescription();
168        if (!TextUtils.isEmpty(cdesc)) {
169            event.getText().add(cdesc);
170        }
171    }
172
173    @Override
174    public boolean dispatchHoverEvent(MotionEvent event) {
175        // Don't allow children to hover; we want this to be treated as a single component.
176        return onHoverEvent(event);
177    }
178
179    public boolean showsIcon() {
180        return true;
181    }
182
183    public boolean needsDividerBefore() {
184        return hasText() && mItemData.getIcon() == null;
185    }
186
187    public boolean needsDividerAfter() {
188        return hasText();
189    }
190
191    @Override
192    public boolean onLongClick(View v) {
193        if (hasText()) {
194            // Don't show the cheat sheet for items that already show text.
195            return false;
196        }
197
198        final int[] screenPos = new int[2];
199        final Rect displayFrame = new Rect();
200        getLocationOnScreen(screenPos);
201        getWindowVisibleDisplayFrame(displayFrame);
202
203        final Context context = getContext();
204        final int width = getWidth();
205        final int height = getHeight();
206        final int midy = screenPos[1] + height / 2;
207        final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
208
209        Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT);
210        if (midy < displayFrame.height()) {
211            // Show along the top; follow action buttons
212            cheatSheet.setGravity(Gravity.TOP | Gravity.RIGHT,
213                    screenWidth - screenPos[0] - width / 2, height);
214        } else {
215            // Show along the bottom center
216            cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
217        }
218        cheatSheet.show();
219        return true;
220    }
221
222    @Override
223    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
224        final boolean textVisible = hasText();
225        if (textVisible && mSavedPaddingLeft >= 0) {
226            super.setPadding(mSavedPaddingLeft, getPaddingTop(),
227                    getPaddingRight(), getPaddingBottom());
228        }
229
230        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
231
232        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
233        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
234        final int oldMeasuredWidth = getMeasuredWidth();
235        final int targetWidth = widthMode == MeasureSpec.AT_MOST ? Math.min(widthSize, mMinWidth)
236                : mMinWidth;
237
238        if (widthMode != MeasureSpec.EXACTLY && mMinWidth > 0 && oldMeasuredWidth < targetWidth) {
239            // Remeasure at exactly the minimum width.
240            super.onMeasure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY),
241                    heightMeasureSpec);
242        }
243
244        if (!textVisible && mIcon != null) {
245            // TextView won't center compound drawables in both dimensions without
246            // a little coercion. Pad in to center the icon after we've measured.
247            final int w = getMeasuredWidth();
248            final int dw = mIcon.getIntrinsicWidth();
249            super.setPadding((w - dw) / 2, getPaddingTop(), getPaddingRight(), getPaddingBottom());
250        }
251    }
252}
253