Home | History | Annotate | Download | only in menu
      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 android.support.v7.internal.view.menu;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.graphics.drawable.Drawable;
     22 import android.support.v4.content.ContextCompat;
     23 import android.support.v4.view.ActionProvider;
     24 import android.support.v4.internal.view.SupportMenuItem;
     25 import android.support.v4.view.MenuItemCompat;
     26 import android.view.ContextMenu.ContextMenuInfo;
     27 import android.view.MenuItem;
     28 import android.view.SubMenu;
     29 import android.view.View;
     30 
     31 /**
     32  * @hide
     33  */
     34 public class ActionMenuItem implements SupportMenuItem {
     35 
     36     private final int mId;
     37     private final int mGroup;
     38     private final int mCategoryOrder;
     39     private final int mOrdering;
     40 
     41     private CharSequence mTitle;
     42     private CharSequence mTitleCondensed;
     43     private Intent mIntent;
     44     private char mShortcutNumericChar;
     45     private char mShortcutAlphabeticChar;
     46 
     47     private Drawable mIconDrawable;
     48     private int mIconResId = NO_ICON;
     49 
     50     private Context mContext;
     51 
     52     private SupportMenuItem.OnMenuItemClickListener mClickListener;
     53 
     54     private static final int NO_ICON = 0;
     55 
     56     private int mFlags = ENABLED;
     57     private static final int CHECKABLE = 0x00000001;
     58     private static final int CHECKED = 0x00000002;
     59     private static final int EXCLUSIVE = 0x00000004;
     60     private static final int HIDDEN = 0x00000008;
     61     private static final int ENABLED = 0x00000010;
     62 
     63     public ActionMenuItem(Context context, int group, int id, int categoryOrder, int ordering,
     64             CharSequence title) {
     65         mContext = context;
     66         mId = id;
     67         mGroup = group;
     68         mCategoryOrder = categoryOrder;
     69         mOrdering = ordering;
     70         mTitle = title;
     71     }
     72 
     73     public char getAlphabeticShortcut() {
     74         return mShortcutAlphabeticChar;
     75     }
     76 
     77     public int getGroupId() {
     78         return mGroup;
     79     }
     80 
     81     public Drawable getIcon() {
     82         return mIconDrawable;
     83     }
     84 
     85     public Intent getIntent() {
     86         return mIntent;
     87     }
     88 
     89     public int getItemId() {
     90         return mId;
     91     }
     92 
     93     public ContextMenuInfo getMenuInfo() {
     94         return null;
     95     }
     96 
     97     public char getNumericShortcut() {
     98         return mShortcutNumericChar;
     99     }
    100 
    101     public int getOrder() {
    102         return mOrdering;
    103     }
    104 
    105     public SubMenu getSubMenu() {
    106         return null;
    107     }
    108 
    109     public CharSequence getTitle() {
    110         return mTitle;
    111     }
    112 
    113     public CharSequence getTitleCondensed() {
    114         return mTitleCondensed != null ? mTitleCondensed : mTitle;
    115     }
    116 
    117     public boolean hasSubMenu() {
    118         return false;
    119     }
    120 
    121     public boolean isCheckable() {
    122         return (mFlags & CHECKABLE) != 0;
    123     }
    124 
    125     public boolean isChecked() {
    126         return (mFlags & CHECKED) != 0;
    127     }
    128 
    129     public boolean isEnabled() {
    130         return (mFlags & ENABLED) != 0;
    131     }
    132 
    133     public boolean isVisible() {
    134         return (mFlags & HIDDEN) == 0;
    135     }
    136 
    137     public MenuItem setAlphabeticShortcut(char alphaChar) {
    138         mShortcutAlphabeticChar = alphaChar;
    139         return this;
    140     }
    141 
    142     public MenuItem setCheckable(boolean checkable) {
    143         mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0);
    144         return this;
    145     }
    146 
    147     public ActionMenuItem setExclusiveCheckable(boolean exclusive) {
    148         mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0);
    149         return this;
    150     }
    151 
    152     public MenuItem setChecked(boolean checked) {
    153         mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0);
    154         return this;
    155     }
    156 
    157     public MenuItem setEnabled(boolean enabled) {
    158         mFlags = (mFlags & ~ENABLED) | (enabled ? ENABLED : 0);
    159         return this;
    160     }
    161 
    162     public MenuItem setIcon(Drawable icon) {
    163         mIconDrawable = icon;
    164         mIconResId = NO_ICON;
    165         return this;
    166     }
    167 
    168     public MenuItem setIcon(int iconRes) {
    169         mIconResId = iconRes;
    170         mIconDrawable = ContextCompat.getDrawable(mContext, iconRes);
    171         return this;
    172     }
    173 
    174     public MenuItem setIntent(Intent intent) {
    175         mIntent = intent;
    176         return this;
    177     }
    178 
    179     public MenuItem setNumericShortcut(char numericChar) {
    180         mShortcutNumericChar = numericChar;
    181         return this;
    182     }
    183 
    184     public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) {
    185         mClickListener = menuItemClickListener;
    186         return this;
    187     }
    188 
    189     public MenuItem setShortcut(char numericChar, char alphaChar) {
    190         mShortcutNumericChar = numericChar;
    191         mShortcutAlphabeticChar = alphaChar;
    192         return this;
    193     }
    194 
    195     public MenuItem setTitle(CharSequence title) {
    196         mTitle = title;
    197         return this;
    198     }
    199 
    200     public MenuItem setTitle(int title) {
    201         mTitle = mContext.getResources().getString(title);
    202         return this;
    203     }
    204 
    205     public MenuItem setTitleCondensed(CharSequence title) {
    206         mTitleCondensed = title;
    207         return this;
    208     }
    209 
    210     public MenuItem setVisible(boolean visible) {
    211         mFlags = (mFlags & HIDDEN) | (visible ? 0 : HIDDEN);
    212         return this;
    213     }
    214 
    215     public boolean invoke() {
    216         if (mClickListener != null && mClickListener.onMenuItemClick(this)) {
    217             return true;
    218         }
    219 
    220         if (mIntent != null) {
    221             mContext.startActivity(mIntent);
    222             return true;
    223         }
    224 
    225         return false;
    226     }
    227 
    228     public void setShowAsAction(int show) {
    229         // Do nothing. ActionMenuItems always show as action buttons.
    230     }
    231 
    232     public SupportMenuItem setActionView(View actionView) {
    233         throw new UnsupportedOperationException();
    234     }
    235 
    236     public View getActionView() {
    237         return null;
    238     }
    239 
    240     @Override
    241     public MenuItem setActionProvider(android.view.ActionProvider actionProvider) {
    242         throw new UnsupportedOperationException();
    243     }
    244 
    245     @Override
    246     public android.view.ActionProvider getActionProvider() {
    247         throw new UnsupportedOperationException();
    248     }
    249 
    250     @Override
    251     public SupportMenuItem setActionView(int resId) {
    252         throw new UnsupportedOperationException();
    253     }
    254 
    255     @Override
    256     public ActionProvider getSupportActionProvider() {
    257         return null;
    258     }
    259 
    260     @Override
    261     public SupportMenuItem setSupportActionProvider(ActionProvider actionProvider) {
    262         throw new UnsupportedOperationException();
    263     }
    264 
    265     @Override
    266     public SupportMenuItem setShowAsActionFlags(int actionEnum) {
    267         setShowAsAction(actionEnum);
    268         return this;
    269     }
    270 
    271     @Override
    272     public boolean expandActionView() {
    273         return false;
    274     }
    275 
    276     @Override
    277     public boolean collapseActionView() {
    278         return false;
    279     }
    280 
    281     @Override
    282     public boolean isActionViewExpanded() {
    283         return false;
    284     }
    285 
    286     @Override
    287     public MenuItem setOnActionExpandListener(MenuItem.OnActionExpandListener listener) {
    288         throw new UnsupportedOperationException();
    289     }
    290 
    291     @Override
    292     public SupportMenuItem setSupportOnActionExpandListener(MenuItemCompat.OnActionExpandListener listener) {
    293         // No need to save the listener; ActionMenuItem does not support collapsing items.
    294         return this;
    295     }
    296 }
    297