ActionMenuItemView.java revision a8a72c38fec3879f5d346840b0d186d5903931b8
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.graphics.drawable.Drawable;
21import android.util.AttributeSet;
22import android.view.SoundEffectConstants;
23import android.view.View;
24import android.widget.ImageButton;
25
26/**
27 * @hide
28 */
29public class ActionMenuItemView extends ImageButton implements MenuView.ItemView {
30    private static final String TAG = "ActionMenuItemView";
31
32    private MenuItemImpl mItemData;
33    private CharSequence mTitle;
34    private MenuBuilder.ItemInvoker mItemInvoker;
35
36    public ActionMenuItemView(Context context) {
37        this(context, null);
38    }
39
40    public ActionMenuItemView(Context context, AttributeSet attrs) {
41        this(context, attrs, com.android.internal.R.attr.actionButtonStyle);
42    }
43
44    public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) {
45        super(context, attrs, defStyle);
46    }
47
48    public MenuItemImpl getItemData() {
49        return mItemData;
50    }
51
52    public void initialize(MenuItemImpl itemData, int menuType) {
53        mItemData = itemData;
54
55        setClickable(true);
56        setFocusable(true);
57        setTitle(itemData.getTitle());
58        setIcon(itemData.getIcon());
59        setId(itemData.getItemId());
60
61        setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
62        setEnabled(itemData.isEnabled());
63    }
64
65    @Override
66    public boolean performClick() {
67        // Let the view's listener have top priority
68        if (super.performClick()) {
69            return true;
70        }
71
72        if (mItemInvoker != null && mItemInvoker.invokeItem(mItemData)) {
73            playSoundEffect(SoundEffectConstants.CLICK);
74            return true;
75        } else {
76            return false;
77        }
78    }
79
80    public void setItemInvoker(MenuBuilder.ItemInvoker invoker) {
81        mItemInvoker = invoker;
82    }
83
84    public boolean prefersCondensedTitle() {
85        return false;
86    }
87
88    public void setCheckable(boolean checkable) {
89        // TODO Support checkable action items
90    }
91
92    public void setChecked(boolean checked) {
93        // TODO Support checkable action items
94    }
95
96    public void setIcon(Drawable icon) {
97        setImageDrawable(icon);
98    }
99
100    public void setShortcut(boolean showShortcut, char shortcutKey) {
101        // Action buttons don't show text for shortcut keys.
102    }
103
104    public void setTitle(CharSequence title) {
105        mTitle = title;
106
107        // populate accessibility description with title
108        setContentDescription(title);
109    }
110
111    public boolean showsIcon() {
112        return true;
113    }
114
115}
116