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