1/* 2 * Copyright (C) 2013 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 android.support.v7.internal.view.menu; 18 19import android.content.Context; 20import android.database.DataSetObserver; 21import android.os.Bundle; 22import android.os.Parcelable; 23import android.support.v7.appcompat.R; 24import android.util.SparseArray; 25import android.view.ContextThemeWrapper; 26import android.view.LayoutInflater; 27import android.view.View; 28import android.view.ViewGroup; 29import android.widget.AdapterView; 30import android.widget.BaseAdapter; 31import android.widget.ListAdapter; 32 33import java.util.ArrayList; 34 35/** 36 * MenuPresenter for list-style menus. 37 * 38 * @hide 39 */ 40public class ListMenuPresenter implements MenuPresenter, AdapterView.OnItemClickListener { 41 private static final String TAG = "ListMenuPresenter"; 42 43 Context mContext; 44 LayoutInflater mInflater; 45 MenuBuilder mMenu; 46 47 ExpandedMenuView mMenuView; 48 49 private int mItemIndexOffset; 50 int mThemeRes; 51 int mItemLayoutRes; 52 53 private Callback mCallback; 54 MenuAdapter mAdapter; 55 56 private int mId; 57 58 public static final String VIEWS_TAG = "android:menu:list"; 59 60 /** 61 * Construct a new ListMenuPresenter. 62 * @param context Context to use for theming. This will supersede the context provided 63 * to initForMenu when this presenter is added. 64 * @param itemLayoutRes Layout resource for individual item views. 65 */ 66 public ListMenuPresenter(Context context, int itemLayoutRes) { 67 this(itemLayoutRes, 0); 68 mContext = context; 69 mInflater = LayoutInflater.from(mContext); 70 } 71 72 /** 73 * Construct a new ListMenuPresenter. 74 * @param itemLayoutRes Layout resource for individual item views. 75 * @param themeRes Resource ID of a theme to use for views. 76 */ 77 public ListMenuPresenter(int itemLayoutRes, int themeRes) { 78 mItemLayoutRes = itemLayoutRes; 79 mThemeRes = themeRes; 80 } 81 82 @Override 83 public void initForMenu(Context context, MenuBuilder menu) { 84 if (mThemeRes != 0) { 85 mContext = new ContextThemeWrapper(context, mThemeRes); 86 mInflater = LayoutInflater.from(mContext); 87 } else if (mContext != null) { 88 mContext = context; 89 if (mInflater == null) { 90 mInflater = LayoutInflater.from(mContext); 91 } 92 } 93 mMenu = menu; 94 if (mAdapter != null) { 95 mAdapter.notifyDataSetChanged(); 96 } 97 } 98 99 @Override 100 public MenuView getMenuView(ViewGroup root) { 101 if (mMenuView == null) { 102 mMenuView = (ExpandedMenuView) mInflater.inflate( 103 R.layout.abc_expanded_menu_layout, root, false); 104 if (mAdapter == null) { 105 mAdapter = new MenuAdapter(); 106 } 107 mMenuView.setAdapter(mAdapter); 108 mMenuView.setOnItemClickListener(this); 109 } 110 return mMenuView; 111 } 112 113 /** 114 * Call this instead of getMenuView if you want to manage your own ListView. 115 * For proper operation, the ListView hosting this adapter should add 116 * this presenter as an OnItemClickListener. 117 * 118 * @return A ListAdapter containing the items in the menu. 119 */ 120 public ListAdapter getAdapter() { 121 if (mAdapter == null) { 122 mAdapter = new MenuAdapter(); 123 } 124 return mAdapter; 125 } 126 127 @Override 128 public void updateMenuView(boolean cleared) { 129 if (mAdapter != null) mAdapter.notifyDataSetChanged(); 130 } 131 132 @Override 133 public void setCallback(Callback cb) { 134 mCallback = cb; 135 } 136 137 @Override 138 public boolean onSubMenuSelected(SubMenuBuilder subMenu) { 139 if (!subMenu.hasVisibleItems()) return false; 140 141 // The window manager will give us a token. 142 new MenuDialogHelper(subMenu).show(null); 143 if (mCallback != null) { 144 mCallback.onOpenSubMenu(subMenu); 145 } 146 return true; 147 } 148 149 @Override 150 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { 151 if (mCallback != null) { 152 mCallback.onCloseMenu(menu, allMenusAreClosing); 153 } 154 } 155 156 int getItemIndexOffset() { 157 return mItemIndexOffset; 158 } 159 160 public void setItemIndexOffset(int offset) { 161 mItemIndexOffset = offset; 162 if (mMenuView != null) { 163 updateMenuView(false); 164 } 165 } 166 167 @Override 168 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 169 mMenu.performItemAction(mAdapter.getItem(position), this, 0); 170 } 171 172 @Override 173 public boolean flagActionItems() { 174 return false; 175 } 176 177 public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) { 178 return false; 179 } 180 181 public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) { 182 return false; 183 } 184 185 public void saveHierarchyState(Bundle outState) { 186 SparseArray<Parcelable> viewStates = new SparseArray<Parcelable>(); 187 if (mMenuView != null) { 188 ((View) mMenuView).saveHierarchyState(viewStates); 189 } 190 outState.putSparseParcelableArray(VIEWS_TAG, viewStates); 191 } 192 193 public void restoreHierarchyState(Bundle inState) { 194 SparseArray<Parcelable> viewStates = inState.getSparseParcelableArray(VIEWS_TAG); 195 if (viewStates != null) { 196 ((View) mMenuView).restoreHierarchyState(viewStates); 197 } 198 } 199 200 public void setId(int id) { 201 mId = id; 202 } 203 204 @Override 205 public int getId() { 206 return mId; 207 } 208 209 @Override 210 public Parcelable onSaveInstanceState() { 211 if (mMenuView == null) { 212 return null; 213 } 214 215 Bundle state = new Bundle(); 216 saveHierarchyState(state); 217 return state; 218 } 219 220 @Override 221 public void onRestoreInstanceState(Parcelable state) { 222 restoreHierarchyState((Bundle) state); 223 } 224 225 private class MenuAdapter extends BaseAdapter { 226 private int mExpandedIndex = -1; 227 228 public MenuAdapter() { 229 findExpandedIndex(); 230 } 231 232 public int getCount() { 233 ArrayList<MenuItemImpl> items = mMenu.getNonActionItems(); 234 int count = items.size() - mItemIndexOffset; 235 if (mExpandedIndex < 0) { 236 return count; 237 } 238 return count - 1; 239 } 240 241 public MenuItemImpl getItem(int position) { 242 ArrayList<MenuItemImpl> items = mMenu.getNonActionItems(); 243 position += mItemIndexOffset; 244 if (mExpandedIndex >= 0 && position >= mExpandedIndex) { 245 position++; 246 } 247 return items.get(position); 248 } 249 250 public long getItemId(int position) { 251 // Since a menu item's ID is optional, we'll use the position as an 252 // ID for the item in the AdapterView 253 return position; 254 } 255 256 public View getView(int position, View convertView, ViewGroup parent) { 257 if (convertView == null) { 258 convertView = mInflater.inflate(mItemLayoutRes, parent, false); 259 } 260 261 MenuView.ItemView itemView = (MenuView.ItemView) convertView; 262 itemView.initialize(getItem(position), 0); 263 return convertView; 264 } 265 266 void findExpandedIndex() { 267 final MenuItemImpl expandedItem = mMenu.getExpandedItem(); 268 if (expandedItem != null) { 269 final ArrayList<MenuItemImpl> items = mMenu.getNonActionItems(); 270 final int count = items.size(); 271 for (int i = 0; i < count; i++) { 272 final MenuItemImpl item = items.get(i); 273 if (item == expandedItem) { 274 mExpandedIndex = i; 275 return; 276 } 277 } 278 } 279 mExpandedIndex = -1; 280 } 281 282 @Override 283 public void notifyDataSetChanged() { 284 findExpandedIndex(); 285 super.notifyDataSetChanged(); 286 } 287 } 288} 289