ActionMenuItemView.java revision 35aecd5884a5ccfe380903e39f30f468315e8f92
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.graphics.drawable.Drawable;
22import android.text.TextUtils;
23import android.util.AttributeSet;
24import android.view.View;
25import android.widget.Button;
26import android.widget.ImageButton;
27import android.widget.LinearLayout;
28
29/**
30 * @hide
31 */
32public class ActionMenuItemView extends LinearLayout
33        implements MenuView.ItemView, View.OnClickListener, ActionMenuView.ActionMenuChildView {
34    private static final String TAG = "ActionMenuItemView";
35
36    private MenuItemImpl mItemData;
37    private CharSequence mTitle;
38    private MenuBuilder.ItemInvoker mItemInvoker;
39
40    private ImageButton mImageButton;
41    private Button mTextButton;
42    private boolean mAllowTextWithIcon;
43    private boolean mShowTextAllCaps;
44    private boolean mExpandedFormat;
45
46    public ActionMenuItemView(Context context) {
47        this(context, null);
48    }
49
50    public ActionMenuItemView(Context context, AttributeSet attrs) {
51        this(context, attrs, 0);
52    }
53
54    public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) {
55        super(context, attrs, defStyle);
56        final Resources res = context.getResources();
57        mAllowTextWithIcon = res.getBoolean(
58                com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon);
59        mShowTextAllCaps = res.getBoolean(com.android.internal.R.bool.config_actionMenuItemAllCaps);
60    }
61
62    @Override
63    public void onFinishInflate() {
64        mImageButton = (ImageButton) findViewById(com.android.internal.R.id.imageButton);
65        mTextButton = (Button) findViewById(com.android.internal.R.id.textButton);
66        mImageButton.setOnClickListener(this);
67        mTextButton.setOnClickListener(this);
68        setOnClickListener(this);
69    }
70
71    public MenuItemImpl getItemData() {
72        return mItemData;
73    }
74
75    public void initialize(MenuItemImpl itemData, int menuType) {
76        mItemData = itemData;
77
78        setIcon(itemData.getIcon());
79        setTitle(itemData.getTitleForItemView(this)); // Title only takes effect if there is no icon
80        setId(itemData.getItemId());
81
82        setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
83        setEnabled(itemData.isEnabled());
84    }
85
86    @Override
87    public void setEnabled(boolean enabled) {
88        super.setEnabled(enabled);
89        mImageButton.setEnabled(enabled);
90        mTextButton.setEnabled(enabled);
91    }
92
93    public void onClick(View v) {
94        if (mItemInvoker != null) {
95            mItemInvoker.invokeItem(mItemData);
96        }
97    }
98
99    public void setItemInvoker(MenuBuilder.ItemInvoker invoker) {
100        mItemInvoker = invoker;
101    }
102
103    public boolean prefersCondensedTitle() {
104        return true;
105    }
106
107    public void setCheckable(boolean checkable) {
108        // TODO Support checkable action items
109    }
110
111    public void setChecked(boolean checked) {
112        // TODO Support checkable action items
113    }
114
115    public void setExpandedFormat(boolean expandedFormat) {
116        if (mExpandedFormat != expandedFormat) {
117            mExpandedFormat = expandedFormat;
118            if (mItemData != null) {
119                mItemData.actionFormatChanged();
120            }
121        }
122    }
123
124    private void updateTextButtonVisibility() {
125        boolean visible = !TextUtils.isEmpty(mTextButton.getText());
126        visible &= mImageButton.getDrawable() == null ||
127                (mItemData.showsTextAsAction() && (mAllowTextWithIcon || mExpandedFormat));
128
129        mTextButton.setVisibility(visible ? VISIBLE : GONE);
130    }
131
132    public void setIcon(Drawable icon) {
133        mImageButton.setImageDrawable(icon);
134        if (icon != null) {
135            mImageButton.setVisibility(VISIBLE);
136        } else {
137            mImageButton.setVisibility(GONE);
138        }
139
140        updateTextButtonVisibility();
141    }
142
143    public boolean hasText() {
144        return mTextButton.getVisibility() != GONE;
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        // populate accessibility description with title
155        setContentDescription(title);
156
157        if (mShowTextAllCaps) {
158            mTextButton.setText(title.toString().toUpperCase(
159                    getContext().getResources().getConfiguration().locale));
160        } else {
161            mTextButton.setText(mTitle);
162        }
163
164        updateTextButtonVisibility();
165    }
166
167    public boolean showsIcon() {
168        return true;
169    }
170
171    public boolean needsDividerBefore() {
172        return hasText() && mItemData.getIcon() == null;
173    }
174
175    public boolean needsDividerAfter() {
176        return hasText();
177    }
178}
179