blob: beacf75b989588f226398f0834733d4d280db6cb [file] [log] [blame]
Adam Powell96675b12010-06-10 18:58:59 -07001/*
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;
Adam Powell96675b12010-06-10 18:58:59 -070022import android.view.View;
Adam Powell1f9c7af2010-08-03 11:26:07 -070023import android.widget.Button;
Adam Powell96675b12010-06-10 18:58:59 -070024import android.widget.ImageButton;
Adam Powelld8404b22010-10-13 14:26:41 -070025import android.widget.LinearLayout;
Adam Powell96675b12010-06-10 18:58:59 -070026
27/**
28 * @hide
29 */
Adam Powelld8404b22010-10-13 14:26:41 -070030public class ActionMenuItemView extends LinearLayout
Adam Powell696cba52011-03-29 10:38:16 -070031 implements MenuView.ItemView, View.OnClickListener, ActionMenuView.ActionMenuChildView {
Adam Powell96675b12010-06-10 18:58:59 -070032 private static final String TAG = "ActionMenuItemView";
Jeff Sharkeya8a72c32010-07-31 18:27:13 -070033
Adam Powell96675b12010-06-10 18:58:59 -070034 private MenuItemImpl mItemData;
35 private CharSequence mTitle;
36 private MenuBuilder.ItemInvoker mItemInvoker;
Jeff Sharkeya8a72c32010-07-31 18:27:13 -070037
Adam Powell1f9c7af2010-08-03 11:26:07 -070038 private ImageButton mImageButton;
39 private Button mTextButton;
40
Adam Powell96675b12010-06-10 18:58:59 -070041 public ActionMenuItemView(Context context) {
42 this(context, null);
43 }
Jeff Sharkeya8a72c32010-07-31 18:27:13 -070044
Adam Powell96675b12010-06-10 18:58:59 -070045 public ActionMenuItemView(Context context, AttributeSet attrs) {
Adam Powellbe4d68e2010-10-08 18:16:34 -070046 super(context, attrs);
Adam Powell96675b12010-06-10 18:58:59 -070047 }
Jeff Sharkeya8a72c32010-07-31 18:27:13 -070048
Adam Powell96675b12010-06-10 18:58:59 -070049 public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) {
50 super(context, attrs, defStyle);
51 }
52
Adam Powell1f9c7af2010-08-03 11:26:07 -070053 @Override
54 public void onFinishInflate() {
55 mImageButton = (ImageButton) findViewById(com.android.internal.R.id.imageButton);
56 mTextButton = (Button) findViewById(com.android.internal.R.id.textButton);
57 mImageButton.setOnClickListener(this);
58 mTextButton.setOnClickListener(this);
Adam Powell6bddd872011-03-24 15:34:54 -070059 setOnClickListener(this);
Adam Powell1f9c7af2010-08-03 11:26:07 -070060 }
61
Adam Powell96675b12010-06-10 18:58:59 -070062 public MenuItemImpl getItemData() {
63 return mItemData;
64 }
65
66 public void initialize(MenuItemImpl itemData, int menuType) {
67 mItemData = itemData;
Jeff Sharkeya8a72c32010-07-31 18:27:13 -070068
Adam Powell96675b12010-06-10 18:58:59 -070069 setIcon(itemData.getIcon());
Adam Powelle7d46842011-01-13 21:36:09 -080070 setTitle(itemData.getTitleForItemView(this)); // Title only takes effect if there is no icon
Adam Powell96675b12010-06-10 18:58:59 -070071 setId(itemData.getItemId());
Jeff Sharkeya8a72c32010-07-31 18:27:13 -070072
Adam Powell96675b12010-06-10 18:58:59 -070073 setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
74 setEnabled(itemData.isEnabled());
75 }
Jeff Sharkeya8a72c32010-07-31 18:27:13 -070076
Adam Powell00df32d2010-09-02 13:58:24 -070077 @Override
78 public void setEnabled(boolean enabled) {
79 super.setEnabled(enabled);
80 mImageButton.setEnabled(enabled);
81 mTextButton.setEnabled(enabled);
82 }
83
Adam Powell1f9c7af2010-08-03 11:26:07 -070084 public void onClick(View v) {
85 if (mItemInvoker != null) {
86 mItemInvoker.invokeItem(mItemData);
Adam Powell96675b12010-06-10 18:58:59 -070087 }
88 }
Jeff Sharkeya8a72c32010-07-31 18:27:13 -070089
Adam Powell96675b12010-06-10 18:58:59 -070090 public void setItemInvoker(MenuBuilder.ItemInvoker invoker) {
91 mItemInvoker = invoker;
92 }
93
94 public boolean prefersCondensedTitle() {
Adam Powelle7d46842011-01-13 21:36:09 -080095 return true;
Adam Powell96675b12010-06-10 18:58:59 -070096 }
97
98 public void setCheckable(boolean checkable) {
99 // TODO Support checkable action items
100 }
101
102 public void setChecked(boolean checked) {
103 // TODO Support checkable action items
104 }
105
106 public void setIcon(Drawable icon) {
Adam Powell1f9c7af2010-08-03 11:26:07 -0700107 mImageButton.setImageDrawable(icon);
108 if (icon != null) {
109 mImageButton.setVisibility(VISIBLE);
Adam Powell1f9c7af2010-08-03 11:26:07 -0700110 } else {
111 mImageButton.setVisibility(GONE);
112 }
Adam Powell9f125d32011-01-14 15:36:56 -0800113
114 mTextButton.setVisibility(icon == null || mItemData.showsTextAsAction() ? VISIBLE : GONE);
Adam Powell96675b12010-06-10 18:58:59 -0700115 }
Adam Powellbe4d68e2010-10-08 18:16:34 -0700116
117 public boolean hasText() {
118 return mTextButton.getVisibility() != GONE;
119 }
Adam Powell96675b12010-06-10 18:58:59 -0700120
121 public void setShortcut(boolean showShortcut, char shortcutKey) {
122 // Action buttons don't show text for shortcut keys.
123 }
124
125 public void setTitle(CharSequence title) {
126 mTitle = title;
Jeff Sharkeya8a72c32010-07-31 18:27:13 -0700127
128 // populate accessibility description with title
129 setContentDescription(title);
Adam Powell1f9c7af2010-08-03 11:26:07 -0700130
Adam Powelld8404b22010-10-13 14:26:41 -0700131 if (mImageButton.getDrawable() == null || mItemData.showsTextAsAction()) {
Adam Powell1f9c7af2010-08-03 11:26:07 -0700132 mTextButton.setText(mTitle);
133 mTextButton.setVisibility(VISIBLE);
134 }
Adam Powell96675b12010-06-10 18:58:59 -0700135 }
136
137 public boolean showsIcon() {
138 return true;
139 }
Adam Powell696cba52011-03-29 10:38:16 -0700140
141 public boolean needsDividerBefore() {
142 return hasText() && mItemData.getIcon() == null;
143 }
144
145 public boolean needsDividerAfter() {
146 return hasText();
147 }
Adam Powell96675b12010-06-10 18:58:59 -0700148}