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