This source file includes following definitions.
- showAppMenu
- appMenuDismissed
- isAppMenuShowing
- getAppMenu
- getAppMenuDragHelper
- hideAppMenu
- addObserver
- removeObserver
- onOptionsItemSelected
- onMenuVisibilityChanged
- hardwareMenuButtonUp
package org.chromium.chrome.browser.appmenu;
import android.app.Activity;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.view.ContextThemeWrapper;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import com.google.common.annotations.VisibleForTesting;
import org.chromium.chrome.browser.UmaBridge;
import java.util.ArrayList;
public class AppMenuHandler {
private AppMenu mAppMenu;
private AppMenuDragHelper mAppMenuDragHelper;
private Menu mMenu;
private final ArrayList<AppMenuObserver> mObservers;
private final int mMenuResourceId;
private final AppMenuPropertiesDelegate mDelegate;
private final Activity mActivity;
public AppMenuHandler(Activity activity, AppMenuPropertiesDelegate delegate,
int menuResourceId) {
mActivity = activity;
mDelegate = delegate;
mObservers = new ArrayList<AppMenuObserver>();
mMenuResourceId = menuResourceId;
}
public boolean showAppMenu(View anchorView, boolean isByHardwareButton, boolean startDragging) {
if (!mDelegate.shouldShowAppMenu() || isAppMenuShowing()) return false;
if (mMenu == null) {
PopupMenu tempMenu = new PopupMenu(mActivity, anchorView);
tempMenu.inflate(mMenuResourceId);
mMenu = tempMenu.getMenu();
}
mDelegate.prepareMenu(mMenu);
if (mAppMenu == null) {
TypedArray a = mActivity.obtainStyledAttributes(
new int[] {android.R.attr.listPreferredItemHeightSmall});
int itemRowHeight = a.getDimensionPixelSize(0, 0);
a.recycle();
mAppMenu = new AppMenu(mMenu, itemRowHeight, this, mActivity.getResources());
mAppMenuDragHelper = new AppMenuDragHelper(mActivity, mAppMenu, itemRowHeight);
}
ContextThemeWrapper wrapper = new ContextThemeWrapper(mActivity,
mDelegate.getMenuThemeResourceId());
Rect appRect = new Rect();
mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(appRect);
int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
mAppMenu.show(wrapper, anchorView, isByHardwareButton, rotation, appRect);
mAppMenuDragHelper.onShow(isByHardwareButton, startDragging);
UmaBridge.menuShow();
return true;
}
void appMenuDismissed() {
mAppMenuDragHelper.onDismiss();
}
public boolean isAppMenuShowing() {
return mAppMenu != null && mAppMenu.isShowing();
}
@VisibleForTesting
AppMenu getAppMenu() {
return mAppMenu;
}
AppMenuDragHelper getAppMenuDragHelper() {
return mAppMenuDragHelper;
}
public void hideAppMenu() {
if (mAppMenu != null && mAppMenu.isShowing()) mAppMenu.dismiss();
}
public void addObserver(AppMenuObserver observer) {
mObservers.add(observer);
}
public void removeObserver(AppMenuObserver observer) {
mObservers.remove(observer);
}
void onOptionsItemSelected(MenuItem item) {
mActivity.onOptionsItemSelected(item);
}
void onMenuVisibilityChanged(boolean isVisible) {
for (int i = 0; i < mObservers.size(); ++i) {
mObservers.get(i).onMenuVisibilityChanged(isVisible);
}
}
public void hardwareMenuButtonUp() {
if (mAppMenuDragHelper != null) mAppMenuDragHelper.hardwareMenuButtonUp();
}
}