Cleanup menus

 Bug: 5290513

Change-Id: I2ca1de40fe362b74d941056504fc4e3249de03a3
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 4809f13..f6feb4a 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -1248,10 +1248,6 @@
         }
         MenuInflater inflater = mActivity.getMenuInflater();
         inflater.inflate(R.menu.browser, menu);
-        updateInLoadMenuItems(menu);
-        // hold on to the menu reference here; it is used by the page callbacks
-        // to update the menu based on loading state
-        mCachedMenu = menu;
         return true;
     }
 
@@ -1435,6 +1431,10 @@
     }
 
     boolean onPrepareOptionsMenu(Menu menu) {
+        updateInLoadMenuItems(menu);
+        // hold on to the menu reference here; it is used by the page callbacks
+        // to update the menu based on loading state
+        mCachedMenu = menu;
         // Note: setVisible will decide whether an item is visible; while
         // setEnabled() will decide whether an item is enabled, which also means
         // whether the matching shortcut key will function.
@@ -1465,11 +1465,13 @@
         boolean canGoForward = false;
         boolean isHome = false;
         boolean isDesktopUa = false;
+        boolean isLive = false;
         if (tab != null) {
             canGoBack = tab.canGoBack();
             canGoForward = tab.canGoForward();
             isHome = mSettings.getHomePage().equals(tab.getUrl());
             isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
+            isLive = !tab.isSnapshot();
         }
         final MenuItem back = menu.findItem(R.id.back_menu_id);
         back.setEnabled(canGoBack);
@@ -1486,6 +1488,7 @@
             dest.setTitle(source.getTitle());
             dest.setIcon(source.getIcon());
         }
+        menu.setGroupVisible(R.id.NAV_MENU, isLive);
 
         // decide whether to show the share link option
         PackageManager pm = mActivity.getPackageManager();
@@ -1506,6 +1509,8 @@
         counter.setEnabled(showDebugSettings);
         final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
         uaSwitcher.setChecked(isDesktopUa);
+        menu.setGroupVisible(R.id.LIVE_MENU, isLive);
+        menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
 
         mUi.updateMenuState(tab, menu);
     }
@@ -1623,6 +1628,10 @@
                 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
                 break;
 
+            case R.id.snapshot_go_live:
+                goLive();
+                return true;
+
             case R.id.classic_history_menu_id:
                 bookmarksOrHistoryPicker(true);
                 break;
@@ -1690,6 +1699,11 @@
         return true;
     }
 
+    private void goLive() {
+        Tab t = getCurrentTab();
+        t.loadUrl(t.getUrl(), null);
+    }
+
     public boolean onContextItemSelected(MenuItem item) {
         // Let the History and Bookmark fragments handle menus they created.
         if (item.getGroupId() == R.id.CONTEXT_MENU) {
diff --git a/src/com/android/browser/NavigationBarPhone.java b/src/com/android/browser/NavigationBarPhone.java
index 7e8695b..a1e778d 100644
--- a/src/com/android/browser/NavigationBarPhone.java
+++ b/src/com/android/browser/NavigationBarPhone.java
@@ -15,6 +15,7 @@
  */
 package com.android.browser;
 
+import android.app.Activity;
 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
@@ -174,14 +175,21 @@
     }
 
     void showMenu(View anchor) {
-        mOverflowMenuShowing = true;
-        mPopupMenu = new PopupMenu(mContext, anchor);
+        Activity activity = mUiController.getActivity();
+        if (mPopupMenu == null) {
+            mPopupMenu = new PopupMenu(mContext, anchor);
+            mPopupMenu.setOnMenuItemClickListener(this);
+            mPopupMenu.setOnDismissListener(this);
+            if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
+                mPopupMenu = null;
+                return;
+            }
+        }
         Menu menu = mPopupMenu.getMenu();
-        mPopupMenu.getMenuInflater().inflate(R.menu.browser, menu);
-        mUiController.updateMenuState(mBaseUi.getActiveTab(), menu);
-        mPopupMenu.setOnMenuItemClickListener(this);
-        mPopupMenu.setOnDismissListener(this);
-        mPopupMenu.show();
+        if (activity.onPrepareOptionsMenu(menu)) {
+            mOverflowMenuShowing = true;
+            mPopupMenu.show();
+        }
     }
 
     @Override
@@ -193,7 +201,6 @@
 
     private void onMenuHidden() {
         mOverflowMenuShowing = false;
-        mPopupMenu = null;
         mBaseUi.showTitleBarForDuration();
     }
 
diff --git a/src/com/android/browser/PhoneUi.java b/src/com/android/browser/PhoneUi.java
index f5a76b9..606a47d 100644
--- a/src/com/android/browser/PhoneUi.java
+++ b/src/com/android/browser/PhoneUi.java
@@ -162,7 +162,6 @@
 
     @Override
     public void updateMenuState(Tab tab, Menu menu) {
-        menu.setGroupVisible(R.id.NAV_MENU, (mNavScreen == null));
         MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
         if (bm != null) {
             bm.setVisible(mNavScreen == null);
@@ -173,7 +172,13 @@
         }
         MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
         if (abm != null) {
-            abm.setVisible((tab != null) && !tab.isSnapshot());
+            abm.setVisible((tab != null) && !tab.isSnapshot() && mNavScreen == null);
+        }
+        if (mNavScreen != null) {
+            menu.setGroupVisible(R.id.LIVE_MENU, false);
+            menu.setGroupVisible(R.id.SNAPSHOT_MENU, false);
+            menu.findItem(R.id.page_info_menu_id).setVisible(false);
+            menu.setGroupVisible(R.id.NAV_MENU, false);
         }
     }
 
diff --git a/src/com/android/browser/SnapshotBar.java b/src/com/android/browser/SnapshotBar.java
index 039afcf..2fb90d2 100644
--- a/src/com/android/browser/SnapshotBar.java
+++ b/src/com/android/browser/SnapshotBar.java
@@ -21,7 +21,6 @@
 import android.os.Message;
 import android.text.TextUtils;
 import android.util.AttributeSet;
-import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.View.OnClickListener;
@@ -29,21 +28,18 @@
 import android.view.ViewPropertyAnimator;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
-import android.widget.PopupMenu;
 import android.widget.PopupMenu.OnMenuItemClickListener;
 import android.widget.TextView;
 
 import java.text.DateFormat;
 import java.util.Date;
 
-public class SnapshotBar extends LinearLayout implements OnClickListener,
-        OnMenuItemClickListener {
+public class SnapshotBar extends LinearLayout implements OnClickListener {
 
     private static final int MSG_SHOW_TITLE = 1;
     private static final long DURATION_SHOW_DATE = BaseUi.HIDE_TITLEBAR_DELAY;
 
     private ImageView mFavicon;
-    private View mGoLive;
     private TextView mDate;
     private TextView mTitle;
     private View mBookmarks;
@@ -86,10 +82,7 @@
     @Override
     protected void onFinishInflate() {
         super.onFinishInflate();
-        mGoLive = mFavicon = (ImageView) findViewById(R.id.favicon);
-        if (mGoLive == null) {
-            mGoLive = findViewById(R.id.date_icon);
-        }
+        mFavicon = (ImageView) findViewById(R.id.favicon);
         mDate = (TextView) findViewById(R.id.date);
         mTitle = (TextView) findViewById(R.id.title);
         mBookmarks = findViewById(R.id.all_btn);
@@ -113,7 +106,6 @@
             mToggleContainer.setOnClickListener(this);
             resetAnimation();
         }
-        mGoLive.setOnClickListener(this);
     }
 
     @Override
@@ -173,12 +165,6 @@
     public void onClick(View v) {
         if (mBookmarks == v) {
             mTitleBar.getUiController().bookmarksOrHistoryPicker(false);
-        } else if (mGoLive == v) {
-            PopupMenu popup = new PopupMenu(mContext, mGoLive);
-            Menu menu = popup.getMenu();
-            popup.getMenuInflater().inflate(R.menu.snapshot_go_live, menu);
-            popup.setOnMenuItemClickListener(this);
-            popup.show();
         } else if (mTabSwitcher == v) {
             ((PhoneUi) mTitleBar.getUi()).toggleNavScreen();
         } else if (mOverflowMenu == v) {
@@ -195,21 +181,6 @@
         }
     }
 
-    @Override
-    public boolean onMenuItemClick(MenuItem item) {
-        switch (item.getItemId()) {
-        case R.id.snapshot_go_live:
-            goLive();
-            return true;
-        }
-        return false;
-    }
-
-    private void goLive() {
-        Tab t = mTitleBar.getUi().getActiveTab();
-        t.loadUrl(t.getUrl(), null);
-    }
-
     public void onTabDataChanged(Tab tab) {
         if (!tab.isSnapshot()) return;
         SnapshotTab snapshot = (SnapshotTab) tab;
diff --git a/src/com/android/browser/UiController.java b/src/com/android/browser/UiController.java
index 0da523a..97e99a9 100644
--- a/src/com/android/browser/UiController.java
+++ b/src/com/android/browser/UiController.java
@@ -16,6 +16,7 @@
 
 package com.android.browser;
 
+import android.app.Activity;
 import android.content.Intent;
 import android.view.Menu;
 import android.view.MenuItem;
@@ -100,4 +101,6 @@
 
     void setBlockEvents(boolean block);
 
+    Activity getActivity();
+
 }