ActionMenuItemView.java revision 1f9c7afc5a06576e327a4b1c12688202f53d9462
196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell/*
296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * Copyright (C) 2010 The Android Open Source Project
396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell *
496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * Licensed under the Apache License, Version 2.0 (the "License");
596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * you may not use this file except in compliance with the License.
696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * You may obtain a copy of the License at
796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell *
896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell *      http://www.apache.org/licenses/LICENSE-2.0
996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell *
1096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * Unless required by applicable law or agreed to in writing, software
1196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * distributed under the License is distributed on an "AS IS" BASIS,
1296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * See the License for the specific language governing permissions and
1496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * limitations under the License.
1596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell */
1696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
1796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellpackage com.android.internal.view.menu;
1896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
1996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.content.Context;
2096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.graphics.drawable.Drawable;
2196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.util.AttributeSet;
2296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.view.View;
231f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powellimport android.widget.Button;
241f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powellimport android.widget.FrameLayout;
2596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.widget.ImageButton;
2696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
2796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell/**
2896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * @hide
2996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell */
301f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powellpublic class ActionMenuItemView extends FrameLayout
311f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        implements MenuView.ItemView, View.OnClickListener {
3296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    private static final String TAG = "ActionMenuItemView";
33a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
3496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    private MenuItemImpl mItemData;
3596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    private CharSequence mTitle;
3696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    private MenuBuilder.ItemInvoker mItemInvoker;
37a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
381f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell    private ImageButton mImageButton;
391f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell    private Button mTextButton;
401f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell
4196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public ActionMenuItemView(Context context) {
4296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        this(context, null);
4396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
44a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
4596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public ActionMenuItemView(Context context, AttributeSet attrs) {
4696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        this(context, attrs, com.android.internal.R.attr.actionButtonStyle);
4796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
48a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
4996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) {
5096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        super(context, attrs, defStyle);
5196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
5296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
531f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell    @Override
541f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell    public void onFinishInflate() {
551f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        mImageButton = (ImageButton) findViewById(com.android.internal.R.id.imageButton);
561f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        mTextButton = (Button) findViewById(com.android.internal.R.id.textButton);
571f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        mImageButton.setOnClickListener(this);
581f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        mTextButton.setOnClickListener(this);
591f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell    }
601f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell
6196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public MenuItemImpl getItemData() {
6296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        return mItemData;
6396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
6496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
6596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void initialize(MenuItemImpl itemData, int menuType) {
6696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        mItemData = itemData;
67a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
6896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        setClickable(true);
6996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        setFocusable(true);
7096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        setIcon(itemData.getIcon());
711f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        setTitle(itemData.getTitle()); // Title only takes effect if there is no icon
7296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        setId(itemData.getItemId());
73a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
7496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
7596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        setEnabled(itemData.isEnabled());
7696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
77a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
781f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell    public void onClick(View v) {
791f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        if (mItemInvoker != null) {
801f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell            mItemInvoker.invokeItem(mItemData);
8196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        }
8296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
83a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
8496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void setItemInvoker(MenuBuilder.ItemInvoker invoker) {
8596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        mItemInvoker = invoker;
8696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
8796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
8896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public boolean prefersCondensedTitle() {
8996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        return false;
9096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
9196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
9296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void setCheckable(boolean checkable) {
9396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        // TODO Support checkable action items
9496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
9596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
9696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void setChecked(boolean checked) {
9796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        // TODO Support checkable action items
9896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
9996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
10096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void setIcon(Drawable icon) {
1011f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        mImageButton.setImageDrawable(icon);
1021f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        if (icon != null) {
1031f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell            mImageButton.setVisibility(VISIBLE);
1041f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell            mTextButton.setVisibility(GONE);
1051f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        } else {
1061f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell            mImageButton.setVisibility(GONE);
1071f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        }
10896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
10996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
11096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void setShortcut(boolean showShortcut, char shortcutKey) {
11196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        // Action buttons don't show text for shortcut keys.
11296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
11396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
11496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public void setTitle(CharSequence title) {
11596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        mTitle = title;
116a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey
117a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey        // populate accessibility description with title
118a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey        setContentDescription(title);
1191f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell
1201f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        if (mImageButton.getDrawable() == null) {
1211f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell            mTextButton.setText(mTitle);
1221f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell            mTextButton.setVisibility(VISIBLE);
1231f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell        }
12496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
12596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell
12696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    public boolean showsIcon() {
12796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell        return true;
12896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell    }
12996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell}
130