This source file includes following definitions.
- show
- setPopupOffset
- onItemClick
- onItemClick
- dismiss
- isShowing
- getPopup
- setMenuHeight
package org.chromium.chrome.browser.appmenu;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListPopupWindow;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import org.chromium.chrome.R;
import java.util.ArrayList;
import java.util.List;
public class AppMenu implements OnItemClickListener {
private static final float LAST_ITEM_SHOW_FRACTION = 0.5f;
private static final int DIVIDER_HEIGHT_DP = 1;
private final Menu mMenu;
private final int mItemRowHeight;
private final int mItemDividerHeight;
private final int mVerticalFadeDistance;
private final int mAdditionalVerticalOffset;
private ListPopupWindow mPopup;
private AppMenuAdapter mAdapter;
private AppMenuHandler mHandler;
private int mCurrentScreenRotation = -1;
private boolean mIsByHardwareButton;
AppMenu(Menu menu, int itemRowHeight, AppMenuHandler handler, Resources res) {
mMenu = menu;
mItemRowHeight = itemRowHeight;
assert mItemRowHeight > 0;
mHandler = handler;
final float dpToPx = res.getDisplayMetrics().density;
mItemDividerHeight = Math.round(DIVIDER_HEIGHT_DP * dpToPx);
mAdditionalVerticalOffset = res.getDimensionPixelSize(R.dimen.menu_vertical_offset);
mVerticalFadeDistance = res.getDimensionPixelSize(R.dimen.menu_vertical_fade_distance);
}
void show(Context context, View anchorView, boolean isByHardwareButton, int screenRotation,
Rect visibleDisplayFrame) {
mPopup = new ListPopupWindow(context, null, android.R.attr.popupMenuStyle);
mPopup.setModal(true);
mPopup.setAnchorView(anchorView);
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
mPopup.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
if (mPopup.getAnchorView() instanceof ImageButton) {
((ImageButton) mPopup.getAnchorView()).setSelected(false);
}
mHandler.onMenuVisibilityChanged(false);
}
});
mPopup.setWidth(context.getResources().getDimensionPixelSize(R.dimen.menu_width));
mCurrentScreenRotation = screenRotation;
mIsByHardwareButton = isByHardwareButton;
int numItems = mMenu.size();
List<MenuItem> menuItems = new ArrayList<MenuItem>();
for (int i = 0; i < numItems; ++i) {
MenuItem item = mMenu.getItem(i);
if (item.isVisible()) {
menuItems.add(item);
}
}
mAdapter = new AppMenuAdapter(this, menuItems, LayoutInflater.from(context));
mPopup.setAdapter(mAdapter);
setMenuHeight(menuItems.size(), visibleDisplayFrame);
setPopupOffset(mPopup, mCurrentScreenRotation, visibleDisplayFrame);
mPopup.setOnItemClickListener(this);
mPopup.show();
mPopup.getListView().setDividerHeight(mItemDividerHeight);
mPopup.getListView().setItemsCanFocus(true);
mHandler.onMenuVisibilityChanged(true);
if (mVerticalFadeDistance > 0) {
mPopup.getListView().setVerticalFadingEdgeEnabled(true);
mPopup.getListView().setFadingEdgeLength(mVerticalFadeDistance);
}
}
private void setPopupOffset(ListPopupWindow popup, int screenRotation, Rect appRect) {
Rect paddingRect = new Rect();
popup.getBackground().getPadding(paddingRect);
int[] anchorLocation = new int[2];
popup.getAnchorView().getLocationInWindow(anchorLocation);
if (mIsByHardwareButton) {
int horizontalOffset = -anchorLocation[0];
switch (screenRotation) {
case Surface.ROTATION_0:
case Surface.ROTATION_180:
horizontalOffset += (appRect.width() - mPopup.getWidth()) / 2;
break;
case Surface.ROTATION_90:
horizontalOffset += appRect.width() - mPopup.getWidth();
break;
case Surface.ROTATION_270:
break;
default:
assert false;
break;
}
popup.setHorizontalOffset(horizontalOffset);
popup.setVerticalOffset(mAdditionalVerticalOffset - paddingRect.bottom);
} else {
popup.setVerticalOffset(mAdditionalVerticalOffset - paddingRect.top);
}
}
void onItemClick(MenuItem menuItem) {
if (menuItem.isEnabled()) {
dismiss();
mHandler.onOptionsItemSelected(menuItem);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
onItemClick(mAdapter.getItem(position));
}
void dismiss() {
mHandler.appMenuDismissed();
if (isShowing()) mPopup.dismiss();
}
boolean isShowing() {
if (mPopup == null) {
return false;
}
return mPopup.isShowing();
}
ListPopupWindow getPopup() {
return mPopup;
}
private void setMenuHeight(int numMenuItems, Rect appDimensions) {
assert mPopup.getAnchorView() != null;
View anchorView = mPopup.getAnchorView();
int[] anchorViewLocation = new int[2];
anchorView.getLocationOnScreen(anchorViewLocation);
anchorViewLocation[1] -= appDimensions.top;
int availableScreenSpace = Math.max(anchorViewLocation[1],
appDimensions.height() - anchorViewLocation[1] - anchorView.getHeight());
Rect padding = new Rect();
mPopup.getBackground().getPadding(padding);
availableScreenSpace -= mIsByHardwareButton ? padding.top : padding.bottom;
int numCanFit = availableScreenSpace / (mItemRowHeight + mItemDividerHeight);
if (numCanFit < numMenuItems) {
int spaceForFullItems = numCanFit * (mItemRowHeight + mItemDividerHeight);
int spaceForPartialItem = (int) (LAST_ITEM_SHOW_FRACTION * mItemRowHeight);
if (spaceForFullItems + spaceForPartialItem < availableScreenSpace) {
mPopup.setHeight(spaceForFullItems + spaceForPartialItem +
padding.top + padding.bottom);
} else {
mPopup.setHeight(spaceForFullItems - mItemRowHeight + spaceForPartialItem +
padding.top + padding.bottom);
}
} else {
mPopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
}
}