Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.internal.view.menu; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.drawable.Drawable; |
| 21 | import android.util.AttributeSet; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 22 | import android.view.View; |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 23 | import android.widget.Button; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 24 | import android.widget.ImageButton; |
Adam Powell | d8404b2 | 2010-10-13 14:26:41 -0700 | [diff] [blame] | 25 | import android.widget.LinearLayout; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * @hide |
| 29 | */ |
Adam Powell | d8404b2 | 2010-10-13 14:26:41 -0700 | [diff] [blame] | 30 | public class ActionMenuItemView extends LinearLayout |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 31 | implements MenuView.ItemView, View.OnClickListener, ActionMenuView.ActionMenuChildView { |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 32 | private static final String TAG = "ActionMenuItemView"; |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 33 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 34 | private MenuItemImpl mItemData; |
| 35 | private CharSequence mTitle; |
| 36 | private MenuBuilder.ItemInvoker mItemInvoker; |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 37 | |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 38 | private ImageButton mImageButton; |
| 39 | private Button mTextButton; |
| 40 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 41 | public ActionMenuItemView(Context context) { |
| 42 | this(context, null); |
| 43 | } |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 44 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 45 | public ActionMenuItemView(Context context, AttributeSet attrs) { |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 46 | super(context, attrs); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 47 | } |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 48 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 49 | public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) { |
| 50 | super(context, attrs, defStyle); |
| 51 | } |
| 52 | |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 53 | @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 Powell | 6bddd87 | 2011-03-24 15:34:54 -0700 | [diff] [blame] | 59 | setOnClickListener(this); |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 62 | public MenuItemImpl getItemData() { |
| 63 | return mItemData; |
| 64 | } |
| 65 | |
| 66 | public void initialize(MenuItemImpl itemData, int menuType) { |
| 67 | mItemData = itemData; |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 68 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 69 | setIcon(itemData.getIcon()); |
Adam Powell | e7d4684 | 2011-01-13 21:36:09 -0800 | [diff] [blame] | 70 | setTitle(itemData.getTitleForItemView(this)); // Title only takes effect if there is no icon |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 71 | setId(itemData.getItemId()); |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 72 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 73 | setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE); |
| 74 | setEnabled(itemData.isEnabled()); |
| 75 | } |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 76 | |
Adam Powell | 00df32d | 2010-09-02 13:58:24 -0700 | [diff] [blame] | 77 | @Override |
| 78 | public void setEnabled(boolean enabled) { |
| 79 | super.setEnabled(enabled); |
| 80 | mImageButton.setEnabled(enabled); |
| 81 | mTextButton.setEnabled(enabled); |
| 82 | } |
| 83 | |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 84 | public void onClick(View v) { |
| 85 | if (mItemInvoker != null) { |
| 86 | mItemInvoker.invokeItem(mItemData); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 89 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 90 | public void setItemInvoker(MenuBuilder.ItemInvoker invoker) { |
| 91 | mItemInvoker = invoker; |
| 92 | } |
| 93 | |
| 94 | public boolean prefersCondensedTitle() { |
Adam Powell | e7d4684 | 2011-01-13 21:36:09 -0800 | [diff] [blame] | 95 | return true; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 96 | } |
| 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 Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 107 | mImageButton.setImageDrawable(icon); |
| 108 | if (icon != null) { |
| 109 | mImageButton.setVisibility(VISIBLE); |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 110 | } else { |
| 111 | mImageButton.setVisibility(GONE); |
| 112 | } |
Adam Powell | 9f125d3 | 2011-01-14 15:36:56 -0800 | [diff] [blame] | 113 | |
| 114 | mTextButton.setVisibility(icon == null || mItemData.showsTextAsAction() ? VISIBLE : GONE); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 115 | } |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 116 | |
| 117 | public boolean hasText() { |
| 118 | return mTextButton.getVisibility() != GONE; |
| 119 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 120 | |
| 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 Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 127 | |
| 128 | // populate accessibility description with title |
| 129 | setContentDescription(title); |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 130 | |
Adam Powell | d8404b2 | 2010-10-13 14:26:41 -0700 | [diff] [blame] | 131 | if (mImageButton.getDrawable() == null || mItemData.showsTextAsAction()) { |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 132 | mTextButton.setText(mTitle); |
| 133 | mTextButton.setVisibility(VISIBLE); |
| 134 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | public boolean showsIcon() { |
| 138 | return true; |
| 139 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 140 | |
| 141 | public boolean needsDividerBefore() { |
| 142 | return hasText() && mItemData.getIcon() == null; |
| 143 | } |
| 144 | |
| 145 | public boolean needsDividerAfter() { |
| 146 | return hasText(); |
| 147 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 148 | } |