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 17package android.support.v7.internal.view.menu; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.os.Parcelable; 22import android.support.v7.appcompat.R; 23import android.support.v7.internal.widget.ListPopupWindow; 24import android.view.KeyEvent; 25import android.view.LayoutInflater; 26import android.view.MenuItem; 27import android.view.View; 28import android.view.View.MeasureSpec; 29import android.view.ViewGroup; 30import android.view.ViewTreeObserver; 31import android.widget.AdapterView; 32import android.widget.BaseAdapter; 33import android.widget.FrameLayout; 34import android.widget.ListAdapter; 35import android.widget.PopupWindow; 36 37import java.util.ArrayList; 38 39/** 40 * Presents a menu as a small, simple popup anchored to another view. 41 * 42 * @hide 43 */ 44public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.OnKeyListener, 45 ViewTreeObserver.OnGlobalLayoutListener, PopupWindow.OnDismissListener, 46 MenuPresenter { 47 48 private static final String TAG = "MenuPopupHelper"; 49 50 static final int ITEM_LAYOUT = R.layout.abc_popup_menu_item_layout; 51 52 private Context mContext; 53 private LayoutInflater mInflater; 54 private ListPopupWindow mPopup; 55 private MenuBuilder mMenu; 56 private int mPopupMaxWidth; 57 private View mAnchorView; 58 private boolean mOverflowOnly; 59 private ViewTreeObserver mTreeObserver; 60 61 private MenuAdapter mAdapter; 62 63 private Callback mPresenterCallback; 64 65 boolean mForceShowIcon; 66 67 private ViewGroup mMeasureParent; 68 69 public MenuPopupHelper(Context context, MenuBuilder menu) { 70 this(context, menu, null, false); 71 } 72 73 public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) { 74 this(context, menu, anchorView, false); 75 } 76 77 public MenuPopupHelper(Context context, MenuBuilder menu, 78 View anchorView, boolean overflowOnly) { 79 mContext = context; 80 mInflater = LayoutInflater.from(context); 81 mMenu = menu; 82 mOverflowOnly = overflowOnly; 83 84 final Resources res = context.getResources(); 85 mPopupMaxWidth = Math.max(res.getDisplayMetrics().widthPixels / 2, 86 res.getDimensionPixelSize(R.dimen.abc_config_prefDialogWidth)); 87 88 mAnchorView = anchorView; 89 90 menu.addMenuPresenter(this); 91 } 92 93 public void setAnchorView(View anchor) { 94 mAnchorView = anchor; 95 } 96 97 public void setForceShowIcon(boolean forceShow) { 98 mForceShowIcon = forceShow; 99 } 100 101 public void show() { 102 if (!tryShow()) { 103 throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor"); 104 } 105 } 106 107 public boolean tryShow() { 108 mPopup = new ListPopupWindow(mContext, null, R.attr.popupMenuStyle); 109 mPopup.setOnDismissListener(this); 110 mPopup.setOnItemClickListener(this); 111 112 mAdapter = new MenuAdapter(mMenu); 113 mPopup.setAdapter(mAdapter); 114 mPopup.setModal(true); 115 116 View anchor = mAnchorView; 117 if (anchor != null) { 118 final boolean addGlobalListener = mTreeObserver == null; 119 mTreeObserver = anchor.getViewTreeObserver(); // Refresh to latest 120 if (addGlobalListener) { 121 mTreeObserver.addOnGlobalLayoutListener(this); 122 } 123 mPopup.setAnchorView(anchor); 124 } else { 125 return false; 126 } 127 128 mPopup.setContentWidth(Math.min(measureContentWidth(mAdapter), mPopupMaxWidth)); 129 mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); 130 mPopup.show(); 131 mPopup.getListView().setOnKeyListener(this); 132 return true; 133 } 134 135 public void dismiss() { 136 if (isShowing()) { 137 mPopup.dismiss(); 138 } 139 } 140 141 public void onDismiss() { 142 mPopup = null; 143 mMenu.close(); 144 if (mTreeObserver != null) { 145 if (!mTreeObserver.isAlive()) { 146 mTreeObserver = mAnchorView.getViewTreeObserver(); 147 } 148 mTreeObserver.removeGlobalOnLayoutListener(this); 149 mTreeObserver = null; 150 } 151 } 152 153 public boolean isShowing() { 154 return mPopup != null && mPopup.isShowing(); 155 } 156 157 @Override 158 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 159 MenuAdapter adapter = mAdapter; 160 adapter.mAdapterMenu.performItemAction(adapter.getItem(position), 0); 161 } 162 163 public boolean onKey(View v, int keyCode, KeyEvent event) { 164 if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) { 165 dismiss(); 166 return true; 167 } 168 return false; 169 } 170 171 private int measureContentWidth(ListAdapter adapter) { 172 // Menus don't tend to be long, so this is more sane than it looks. 173 int width = 0; 174 View itemView = null; 175 int itemType = 0; 176 final int widthMeasureSpec = 177 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 178 final int heightMeasureSpec = 179 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 180 final int count = adapter.getCount(); 181 for (int i = 0; i < count; i++) { 182 final int positionType = adapter.getItemViewType(i); 183 if (positionType != itemType) { 184 itemType = positionType; 185 itemView = null; 186 } 187 if (mMeasureParent == null) { 188 mMeasureParent = new FrameLayout(mContext); 189 } 190 itemView = adapter.getView(i, itemView, mMeasureParent); 191 itemView.measure(widthMeasureSpec, heightMeasureSpec); 192 width = Math.max(width, itemView.getMeasuredWidth()); 193 } 194 return width; 195 } 196 197 @Override 198 public void onGlobalLayout() { 199 if (isShowing()) { 200 final View anchor = mAnchorView; 201 if (anchor == null || !anchor.isShown()) { 202 dismiss(); 203 } else if (isShowing()) { 204 // Recompute window size and position 205 mPopup.show(); 206 } 207 } 208 } 209 210 @Override 211 public void initForMenu(Context context, MenuBuilder menu) { 212 // Don't need to do anything; we added as a presenter in the constructor. 213 } 214 215 @Override 216 public MenuView getMenuView(ViewGroup root) { 217 throw new UnsupportedOperationException("MenuPopupHelpers manage their own views"); 218 } 219 220 @Override 221 public void updateMenuView(boolean cleared) { 222 if (mAdapter != null) { 223 mAdapter.notifyDataSetChanged(); 224 } 225 } 226 227 @Override 228 public void setCallback(Callback cb) { 229 mPresenterCallback = cb; 230 } 231 232 @Override 233 public boolean onSubMenuSelected(SubMenuBuilder subMenu) { 234 if (subMenu.hasVisibleItems()) { 235 MenuPopupHelper subPopup = new MenuPopupHelper(mContext, subMenu, mAnchorView, false); 236 subPopup.setCallback(mPresenterCallback); 237 238 boolean preserveIconSpacing = false; 239 final int count = subMenu.size(); 240 for (int i = 0; i < count; i++) { 241 MenuItem childItem = subMenu.getItem(i); 242 if (childItem.isVisible() && childItem.getIcon() != null) { 243 preserveIconSpacing = true; 244 break; 245 } 246 } 247 subPopup.setForceShowIcon(preserveIconSpacing); 248 249 if (subPopup.tryShow()) { 250 if (mPresenterCallback != null) { 251 mPresenterCallback.onOpenSubMenu(subMenu); 252 } 253 return true; 254 } 255 } 256 return false; 257 } 258 259 @Override 260 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { 261 // Only care about the (sub)menu we're presenting. 262 if (menu != mMenu) { 263 return; 264 } 265 266 dismiss(); 267 if (mPresenterCallback != null) { 268 mPresenterCallback.onCloseMenu(menu, allMenusAreClosing); 269 } 270 } 271 272 @Override 273 public boolean flagActionItems() { 274 return false; 275 } 276 277 public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) { 278 return false; 279 } 280 281 public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) { 282 return false; 283 } 284 285 @Override 286 public int getId() { 287 return 0; 288 } 289 290 @Override 291 public Parcelable onSaveInstanceState() { 292 return null; 293 } 294 295 @Override 296 public void onRestoreInstanceState(Parcelable state) { 297 } 298 299 private class MenuAdapter extends BaseAdapter { 300 301 private MenuBuilder mAdapterMenu; 302 private int mExpandedIndex = -1; 303 304 public MenuAdapter(MenuBuilder menu) { 305 mAdapterMenu = menu; 306 findExpandedIndex(); 307 } 308 309 public int getCount() { 310 ArrayList<MenuItemImpl> items = mOverflowOnly ? 311 mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems(); 312 if (mExpandedIndex < 0) { 313 return items.size(); 314 } 315 return items.size() - 1; 316 } 317 318 public MenuItemImpl getItem(int position) { 319 ArrayList<MenuItemImpl> items = mOverflowOnly ? 320 mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems(); 321 if (mExpandedIndex >= 0 && position >= mExpandedIndex) { 322 position++; 323 } 324 return items.get(position); 325 } 326 327 public long getItemId(int position) { 328 // Since a menu item's ID is optional, we'll use the position as an 329 // ID for the item in the AdapterView 330 return position; 331 } 332 333 public View getView(int position, View convertView, ViewGroup parent) { 334 if (convertView == null) { 335 convertView = mInflater.inflate(ITEM_LAYOUT, parent, false); 336 } 337 338 MenuView.ItemView itemView = (MenuView.ItemView) convertView; 339 if (mForceShowIcon) { 340 ((ListMenuItemView) convertView).setForceShowIcon(true); 341 } 342 itemView.initialize(getItem(position), 0); 343 return convertView; 344 } 345 346 void findExpandedIndex() { 347 final MenuItemImpl expandedItem = mMenu.getExpandedItem(); 348 if (expandedItem != null) { 349 final ArrayList<MenuItemImpl> items = mMenu.getNonActionItems(); 350 final int count = items.size(); 351 for (int i = 0; i < count; i++) { 352 final MenuItemImpl item = items.get(i); 353 if (item == expandedItem) { 354 mExpandedIndex = i; 355 return; 356 } 357 } 358 } 359 mExpandedIndex = -1; 360 } 361 362 @Override 363 public void notifyDataSetChanged() { 364 findExpandedIndex(); 365 super.notifyDataSetChanged(); 366 } 367 } 368} 369 370