Add a context menu for the title bar.

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

Change-Id: I28f23d4dc1060208c3d12cf8e2bcbd0be24a9c6c
diff --git a/res/menu/title_context.xml b/res/menu/title_context.xml
new file mode 100644
index 0000000..a60f102
--- /dev/null
+++ b/res/menu/title_context.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2009 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+    <!-- These are for the context menu for the title bar.  Note that we need a
+            different item from share_page_menu_id, which looks at the top
+            window (possibly a subwindow), because this should be the main
+            window.-->
+    <item android:id="@+id/title_bar_share_page_url"
+        android:title="@string/share_page"/>
+    <item android:id="@+id/title_bar_copy_page_url"
+        android:title="@string/copy_page_url"/>
+</menu>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 7f8623f..236cf59 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -232,6 +232,9 @@
     <string name="history">History</string>
     <!-- Menu item for viewing downloads from the browser -->
     <string name="menu_view_download">Downloads</string>
+    <!-- Context menu item for copying the url of the current page from the
+            title bar -->
+    <string name="copy_page_url">Copy page url</string>
     <!-- Menu item -->
     <string name="share_page">Share page</string>
     <!-- Context Menu item open the currently selected link in the current
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);
                 }