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