Home | History | Annotate | Download | only in actionbarcompat
      1 /*
      2  * Copyright 2011 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.example.android.actionbarcompat;
     18 
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.res.Resources;
     23 import android.view.KeyEvent;
     24 import android.view.Menu;
     25 import android.view.MenuItem;
     26 import android.view.SubMenu;
     27 
     28 import java.util.ArrayList;
     29 
     30 /**
     31  * A <em>really</em> dumb implementation of the {@link android.view.Menu} interface, that's only
     32  * useful for our actionbar-compat purposes. See
     33  * <code>com.android.internal.view.menu.MenuBuilder</code> in AOSP for a more complete
     34  * implementation.
     35  */
     36 public class SimpleMenu implements Menu {
     37 
     38     private Context mContext;
     39     private Resources mResources;
     40 
     41     private ArrayList<SimpleMenuItem> mItems;
     42 
     43     public SimpleMenu(Context context) {
     44         mContext = context;
     45         mResources = context.getResources();
     46         mItems = new ArrayList<SimpleMenuItem>();
     47     }
     48 
     49     public Context getContext() {
     50         return mContext;
     51     }
     52 
     53     public Resources getResources() {
     54         return mResources;
     55     }
     56 
     57     public MenuItem add(CharSequence title) {
     58         return addInternal(0, 0, title);
     59     }
     60 
     61     public MenuItem add(int titleRes) {
     62         return addInternal(0, 0, mResources.getString(titleRes));
     63     }
     64 
     65     public MenuItem add(int groupId, int itemId, int order, CharSequence title) {
     66         return addInternal(itemId, order, title);
     67     }
     68 
     69     public MenuItem add(int groupId, int itemId, int order, int titleRes) {
     70         return addInternal(itemId, order, mResources.getString(titleRes));
     71     }
     72 
     73     /**
     74      * Adds an item to the menu.  The other add methods funnel to this.
     75      */
     76     private MenuItem addInternal(int itemId, int order, CharSequence title) {
     77         final SimpleMenuItem item = new SimpleMenuItem(this, itemId, order, title);
     78         mItems.add(findInsertIndex(mItems, order), item);
     79         return item;
     80     }
     81 
     82     private static int findInsertIndex(ArrayList<? extends MenuItem> items, int order) {
     83         for (int i = items.size() - 1; i >= 0; i--) {
     84             MenuItem item = items.get(i);
     85             if (item.getOrder() <= order) {
     86                 return i + 1;
     87             }
     88         }
     89 
     90         return 0;
     91     }
     92 
     93     public int findItemIndex(int id) {
     94         final int size = size();
     95 
     96         for (int i = 0; i < size; i++) {
     97             SimpleMenuItem item = mItems.get(i);
     98             if (item.getItemId() == id) {
     99                 return i;
    100             }
    101         }
    102 
    103         return -1;
    104     }
    105 
    106     public void removeItem(int itemId) {
    107         removeItemAtInt(findItemIndex(itemId));
    108     }
    109 
    110     private void removeItemAtInt(int index) {
    111         if ((index < 0) || (index >= mItems.size())) {
    112             return;
    113         }
    114         mItems.remove(index);
    115     }
    116 
    117     public void clear() {
    118         mItems.clear();
    119     }
    120 
    121     public MenuItem findItem(int id) {
    122         final int size = size();
    123         for (int i = 0; i < size; i++) {
    124             SimpleMenuItem item = mItems.get(i);
    125             if (item.getItemId() == id) {
    126                 return item;
    127             }
    128         }
    129 
    130         return null;
    131     }
    132 
    133     public int size() {
    134         return mItems.size();
    135     }
    136 
    137     public MenuItem getItem(int index) {
    138         return mItems.get(index);
    139     }
    140 
    141     // Unsupported operations.
    142 
    143     public SubMenu addSubMenu(CharSequence charSequence) {
    144         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    145     }
    146 
    147     public SubMenu addSubMenu(int titleRes) {
    148         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    149     }
    150 
    151     public SubMenu addSubMenu(int groupId, int itemId, int order, CharSequence title) {
    152         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    153     }
    154 
    155     public SubMenu addSubMenu(int groupId, int itemId, int order, int titleRes) {
    156         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    157     }
    158 
    159     public int addIntentOptions(int i, int i1, int i2, ComponentName componentName,
    160             Intent[] intents, Intent intent, int i3, MenuItem[] menuItems) {
    161         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    162     }
    163 
    164     public void removeGroup(int i) {
    165         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    166     }
    167 
    168     public void setGroupCheckable(int i, boolean b, boolean b1) {
    169         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    170     }
    171 
    172     public void setGroupVisible(int i, boolean b) {
    173         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    174     }
    175 
    176     public void setGroupEnabled(int i, boolean b) {
    177         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    178     }
    179 
    180     public boolean hasVisibleItems() {
    181         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    182     }
    183 
    184     public void close() {
    185         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    186     }
    187 
    188     public boolean performShortcut(int i, KeyEvent keyEvent, int i1) {
    189         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    190     }
    191 
    192     public boolean isShortcutKey(int i, KeyEvent keyEvent) {
    193         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    194     }
    195 
    196     public boolean performIdentifierAction(int i, int i1) {
    197         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    198     }
    199 
    200     public void setQwertyMode(boolean b) {
    201         throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
    202     }
    203 }
    204