Add a context menu for the title bar.

Fix for http://b/issue?id=2131232

Change-Id: I28f23d4dc1060208c3d12cf8e2bcbd0be24a9c6c
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index eb79a3c..160d6f6 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -1032,6 +1032,15 @@
     }
 
     /**
+     * Special method for the fake title bar to call when displaying its context
+     * menu, since it is in its own Window, and its parent does not show a
+     * context menu.
+     */
+    /* package */ void showTitleBarContextMenu() {
+        openContextMenu(mTitleBar);
+    }
+
+    /**
      *  onSaveInstanceState(Bundle map)
      *  onSaveInstanceState is called right before onStop(). The map contains
      *  the saved state.
@@ -1311,15 +1320,20 @@
         // options selector, so set mCanChord to true so we can access them.
         mCanChord = true;
         int id = item.getItemId();
-        final WebView webView = getTopWindow();
-        if (null == webView) {
-            return false;
-        }
-        final HashMap hrefMap = new HashMap();
-        hrefMap.put("webview", webView);
-        final Message msg = mHandler.obtainMessage(
-                FOCUS_NODE_HREF, id, 0, hrefMap);
         switch (id) {
+            // For the context menu from the title bar
+            case R.id.title_bar_share_page_url:
+            case R.id.title_bar_copy_page_url:
+                WebView mainView = mTabControl.getCurrentWebView();
+                if (null == mainView) {
+                    return false;
+                }
+                if (id == R.id.title_bar_share_page_url) {
+                    Browser.sendString(this, mainView.getUrl());
+                } else {
+                    copy(mainView.getUrl());
+                }
+                break;
             // -- Browser context menu
             case R.id.open_context_menu_id:
             case R.id.open_newtab_context_menu_id:
@@ -1327,6 +1341,14 @@
             case R.id.save_link_context_menu_id:
             case R.id.share_link_context_menu_id:
             case R.id.copy_link_context_menu_id:
+                final WebView webView = getTopWindow();
+                if (null == webView) {
+                    return false;
+                }
+                final HashMap hrefMap = new HashMap();
+                hrefMap.put("webview", webView);
+                final Message msg = mHandler.obtainMessage(
+                        FOCUS_NODE_HREF, id, 0, hrefMap);
                 webView.requestFocusNodeHref(msg);
                 break;
 
diff --git a/src/com/android/browser/TitleBar.java b/src/com/android/browser/TitleBar.java
index 3bdc18b..138a68f 100644
--- a/src/com/android/browser/TitleBar.java
+++ b/src/com/android/browser/TitleBar.java
@@ -26,8 +26,12 @@
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.LayerDrawable;
 import android.graphics.drawable.PaintDrawable;
+import android.os.Handler;
+import android.os.Message;
 import android.util.TypedValue;
+import android.view.ContextMenu;
 import android.view.LayoutInflater;
+import android.view.MenuInflater;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewConfiguration;
@@ -55,9 +59,13 @@
     private Drawable        mGenericFavicon;
     private int             mIconDimension;
     private View            mTitleBg;
+    private MyHandler       mHandler;
+
+    private static int LONG_PRESS = 1;
 
     public TitleBar(BrowserActivity context) {
         super(context, null);
+        mHandler = new MyHandler();
         LayoutInflater factory = LayoutInflater.from(context);
         factory.inflate(R.layout.title_bar, this);
         mBrowserActivity = context;
@@ -83,6 +91,26 @@
                 R.drawable.app_web_browser_sm);
     }
 
+    private class MyHandler extends Handler {
+        public void handleMessage(Message msg) {
+            if (msg.what == LONG_PRESS) {
+                // Prevent the normal action from happening by setting the title
+                // bar's state to false.
+                mTitleBg.setPressed(false);
+                // Need to call a special method on BrowserActivity for when the
+                // fake title bar is up, because its ViewGroup does not show a
+                // context menu.
+                mBrowserActivity.showTitleBarContextMenu();
+            }
+        }
+    };
+
+    @Override
+    protected void onCreateContextMenu(ContextMenu menu) {
+        MenuInflater inflater = mBrowserActivity.getMenuInflater();
+        inflater.inflate(R.menu.title_context, menu);
+    }
+
     @Override
     public boolean onTouchEvent(MotionEvent event) {
         switch (event.getAction()) {
@@ -94,6 +122,9 @@
                     mRtButton.setPressed(true);
                 } else {
                     mTitleBg.setPressed(true);
+                    mHandler.sendMessageDelayed(mHandler.obtainMessage(
+                            LONG_PRESS),
+                            ViewConfiguration.getLongPressTimeout());
                 }
                 break;
             case MotionEvent.ACTION_MOVE:
@@ -105,12 +136,14 @@
                     // bar, mark both as not pressed.
                     mTitleBg.setPressed(false);
                     mRtButton.setPressed(false);
+                    mHandler.removeMessages(LONG_PRESS);
                     break;
                 }
                 int x = (int) event.getX();
                 int titleRight = mTitleBg.getRight();
                 if (mTitleBg.isPressed() && x > titleRight + slop) {
                     mTitleBg.setPressed(false);
+                    mHandler.removeMessages(LONG_PRESS);
                 } else if (mRtButton.isPressed() && x < titleRight - slop) {
                     mRtButton.setPressed(false);
                 }
@@ -118,6 +151,7 @@
             case MotionEvent.ACTION_CANCEL:
                 mRtButton.setPressed(false);
                 mTitleBg.setPressed(false);
+                mHandler.removeMessages(LONG_PRESS);
                 break;
             case MotionEvent.ACTION_UP:
                 if (mRtButton.isPressed()) {
@@ -128,6 +162,7 @@
                     }
                     mRtButton.setPressed(false);
                 } else if (mTitleBg.isPressed()) {
+                    mHandler.removeMessages(LONG_PRESS);
                     mBrowserActivity.onSearchRequested();
                     mTitleBg.setPressed(false);
                 }