ActionMenuView.java revision 7ade1be822ed05a143b059319dccd5e9f623b56d
196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell/* 296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * Copyright (C) 2010 The Android Open Source Project 396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * 496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * Licensed under the Apache License, Version 2.0 (the "License"); 596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * you may not use this file except in compliance with the License. 696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * You may obtain a copy of the License at 796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * 896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * http://www.apache.org/licenses/LICENSE-2.0 996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * 1096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * Unless required by applicable law or agreed to in writing, software 1196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * distributed under the License is distributed on an "AS IS" BASIS, 1296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * See the License for the specific language governing permissions and 1496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * limitations under the License. 1596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell */ 1696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellpackage com.android.internal.view.menu; 1796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 1896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.content.Context; 197ade1be822ed05a143b059319dccd5e9f623b56dAdam Powellimport android.content.res.Resources; 207ade1be822ed05a143b059319dccd5e9f623b56dAdam Powellimport android.content.res.TypedArray; 2196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.util.AttributeSet; 227ade1be822ed05a143b059319dccd5e9f623b56dAdam Powellimport android.view.ViewGroup; 2396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.widget.LinearLayout; 2496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 2596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport java.util.ArrayList; 2696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 2796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell/** 2896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * @hide 2996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell */ 3096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellpublic class ActionMenuView extends LinearLayout implements MenuBuilder.ItemInvoker, MenuView { 3196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell private static final String TAG = "ActionMenuView"; 3296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 3396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell private MenuBuilder mMenu; 347ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell 357ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell private int mItemPadding; 367ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell private int mItemMargin; 377ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell private int mMaxItems; 3896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 3996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public ActionMenuView(Context context) { 4096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell this(context, null); 4196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 4296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 4396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public ActionMenuView(Context context, AttributeSet attrs) { 4496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell super(context, attrs); 457ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell 467ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell TypedArray a = context.obtainStyledAttributes(attrs, 477ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell com.android.internal.R.styleable.Theme); 487ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell mItemPadding = a.getDimensionPixelOffset( 497ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell com.android.internal.R.styleable.Theme_actionButtonPadding, 0); 507ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell mItemMargin = mItemPadding / 2; 517ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell a.recycle(); 527ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell 537ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell final Resources res = getResources(); 547ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell final int size = res.getDimensionPixelSize(com.android.internal.R.dimen.action_icon_size); 557ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell final int spaceAvailable = res.getDisplayMetrics().widthPixels / 2; 567ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell final int itemSpace = size + mItemPadding; 577ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell 587ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell mMaxItems = spaceAvailable / (itemSpace > 0 ? itemSpace : 1); 597ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell } 607ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell 617ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell @Override 627ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { 637ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell if (p instanceof LayoutParams) { 647ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell LayoutParams lp = (LayoutParams) p; 657ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell return lp.leftMargin == mItemMargin && lp.rightMargin == mItemMargin && 667ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell lp.width == LayoutParams.WRAP_CONTENT && lp.height == LayoutParams.WRAP_CONTENT; 677ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell } 687ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell return false; 697ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell } 707ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell 717ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell @Override 727ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell protected LayoutParams generateDefaultLayoutParams() { 737ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, 747ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell LayoutParams.WRAP_CONTENT); 757ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell params.leftMargin = mItemMargin; 767ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell params.rightMargin = mItemMargin; 777ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell return params; 787ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell } 797ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell 807ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell @Override 817ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { 827ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell return generateDefaultLayoutParams(); 837ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell } 847ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell 857ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell public int getItemMargin() { 867ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell return mItemMargin; 8796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 8896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 8996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public boolean invokeItem(MenuItemImpl item) { 9096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell return mMenu.performItemAction(item, 0); 9196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 9296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 9396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public int getWindowAnimations() { 9496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell return 0; 9596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 9696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 9796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void initialize(MenuBuilder menu, int menuType) { 987ade1be822ed05a143b059319dccd5e9f623b56dAdam Powell menu.setMaxActionItems(mMaxItems); 9996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell mMenu = menu; 10096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell updateChildren(true); 10196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 10296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 10396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void updateChildren(boolean cleared) { 10496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell removeAllViews(); 10596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 10696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell final ArrayList<MenuItemImpl> itemsToShow = mMenu.getActionItems(); 10796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell final int itemCount = itemsToShow.size(); 10896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 10996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell for (int i = 0; i < itemCount; i++) { 11096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell final MenuItemImpl itemData = itemsToShow.get(i); 11196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell addItemView((ActionMenuItemView) itemData.getItemView(MenuBuilder.TYPE_ACTION_BUTTON, 11296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell this)); 11396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 11496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 11596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 11696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell private void addItemView(ActionMenuItemView view) { 11796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell view.setItemInvoker(this); 11896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell addView(view); 11996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 12096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell} 121