Url bar selection handling updated

    Bug: 3222647
    framework has been updated to change the way tap/double tap
    and long press are handled
    url bar uses a custom action bar to add the share option

Change-Id: Ie75fc07fe71c9e16fc7f83deda8ecddea39b0397
diff --git a/res/layout/url_bar.xml b/res/layout/url_bar.xml
index 2a67c72..ba93d1b 100644
--- a/res/layout/url_bar.xml
+++ b/res/layout/url_bar.xml
@@ -63,7 +63,7 @@
                 android:layout_height="match_parent"
                 style="@style/HoloIcon"
                 android:visibility="gone" />
-            <EditText
+            <TextView
                 android:id="@+id/url_unfocused"
                 android:layout_width="0dip"
                 android:layout_weight="1.0"
@@ -76,9 +76,7 @@
                 android:singleLine="true"
                 android:ellipsize="end"
                 android:lines="1"
-                android:scrollHorizontally="true"
-                android:inputType="textUri"
-                android:imeOptions="actionGo" />
+                android:scrollHorizontally="true" />
             <ImageButton
                 android:id="@+id/star"
                 android:src="@drawable/ic_favorite_off_normal"
diff --git a/res/menu/url_selection.xml b/res/menu/url_selection.xml
new file mode 100644
index 0000000..1bb6d0e
--- /dev/null
+++ b/res/menu/url_selection.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 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">
+    <item android:id="@+id/share"
+        android:icon="@drawable/ic_menu_share_normal"
+        android:title="@string/menu_share_url"
+        android:showAsAction="always|withText"
+        />
+</menu>
+
diff --git a/res/values/strings.xml b/res/values/strings.xml
index e98d9d5..ece6ffa 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -871,4 +871,10 @@
 
     <!-- Button allowing users to import all of their existing bookmarks into an account when setting up syncing with their bookmarks stored in Google Chrome [CHAR-LIMIT=64] -->
     <string name="import_bookmarks_dialog_import">Add your Android bookmarks to bookmarks for <xliff:g id="Google account" example="account@example.com">%s</xliff:g></string>
+
+    <!-- Url Selection Action Mode -->
+
+    <!-- Menu item to share URL selection [CHAR LIMIT=30] -->
+    <string name="menu_share_url">Share</string>
+
 </resources>
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index b4699e3..75dd913 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -493,6 +493,22 @@
 
     }
 
+    @Override
+    public void shareCurrentPage() {
+        shareCurrentPage(mTabControl.getCurrentTab());
+    }
+
+    private void shareCurrentPage(Tab tab) {
+        if (tab != null) {
+            tab.populatePickerData();
+            sharePage(mActivity, tab.getTitle(),
+                    tab.getUrl(), tab.getFavicon(),
+                    createScreenshot(tab.getWebView(),
+                            getDesiredThumbnailWidth(mActivity),
+                            getDesiredThumbnailHeight(mActivity)));
+        }
+    }
+
     /**
      * Share a page, providing the title, url, favicon, and a screenshot.  Uses
      * an {@link Intent} to launch the Activity chooser.
@@ -599,7 +615,7 @@
     }
 
     /**
-     * resume all WebView timers using the WebView instance of the given tab 
+     * resume all WebView timers using the WebView instance of the given tab
      * @param tab guaranteed non-null
      */
     private void resumeWebViewTimers(Tab tab) {
@@ -1581,12 +1597,7 @@
                     mCanChord = false;
                     return false;
                 }
-                currentTab.populatePickerData();
-                sharePage(mActivity, currentTab.getTitle(),
-                        currentTab.getUrl(), currentTab.getFavicon(),
-                        createScreenshot(currentTab.getWebView(),
-                                getDesiredThumbnailWidth(mActivity),
-                                getDesiredThumbnailHeight(mActivity)));
+                shareCurrentPage(currentTab);
                 break;
 
             case R.id.dump_nav_menu_id:
diff --git a/src/com/android/browser/TitleBarXLarge.java b/src/com/android/browser/TitleBarXLarge.java
index 7e54710..0aa09db 100644
--- a/src/com/android/browser/TitleBarXLarge.java
+++ b/src/com/android/browser/TitleBarXLarge.java
@@ -26,12 +26,9 @@
 import android.graphics.Bitmap;
 import android.graphics.drawable.Drawable;
 import android.text.TextUtils;
-import android.view.ContextMenu;
 import android.view.LayoutInflater;
-import android.view.MenuInflater;
 import android.view.View;
 import android.view.View.OnClickListener;
-import android.view.View.OnFocusChangeListener;
 import android.widget.ImageView;
 import android.widget.TextView;
 
@@ -39,11 +36,10 @@
  * tabbed title bar for xlarge screen browser
  */
 public class TitleBarXLarge extends TitleBarBase
-    implements UrlInputListener, OnClickListener, OnFocusChangeListener {
+    implements UrlInputListener, OnClickListener {
 
     private static final int PROGRESS_MAX = 100;
 
-    private Activity mActivity;
     private UiController mUiController;
 
     private Drawable mStopDrawable;
@@ -67,7 +63,6 @@
 
     public TitleBarXLarge(Activity activity, UiController controller) {
         super(activity);
-        mActivity = activity;
         mUiController = controller;
         Resources resources = activity.getResources();
         mStopDrawable = resources.getDrawable(R.drawable.ic_stop_normal);
@@ -106,25 +101,15 @@
         mGoButton.setOnClickListener(this);
         mClearButton.setOnClickListener(this);
         mUrlFocused.setUrlInputListener(this);
-        mUrlUnfocused.setOnFocusChangeListener(this);
         mUrlFocused.setContainer(mFocusContainer);
+        mUrlFocused.setController(mUiController);
         mUnfocusContainer.setOnClickListener(this);
     }
 
-    public void onFocusChange(View v, boolean hasFocus) {
-        if (hasFocus) {
-            setUrlMode(true);
-            mUrlFocused.selectAll();
-            mUrlFocused.requestFocus();
-            mUrlFocused.setDropDownWidth(mUnfocusContainer.getWidth());
-            mUrlFocused.setDropDownHorizontalOffset(-mUrlFocused.getLeft());
-        }
-    }
-
     @Override
     public void onClick(View v) {
         if (mUnfocusContainer == v) {
-            mUrlUnfocused.requestFocus();
+            setUrlMode(true);
         } else if (mBackButton == v) {
             mUiController.getCurrentTopWebView().goBack();
         } else if (mForwardButton == v) {
@@ -190,6 +175,10 @@
     private void setUrlMode(boolean focused) {
         swapUrlContainer(focused);
         if (focused) {
+            mUrlFocused.selectAll();
+            mUrlFocused.requestFocus();
+            mUrlFocused.setDropDownWidth(mUnfocusContainer.getWidth());
+            mUrlFocused.setDropDownHorizontalOffset(-mUrlFocused.getLeft());
             mSearchButton.setVisibility(View.GONE);
             mGoButton.setVisibility(View.VISIBLE);
         } else {
@@ -203,13 +192,6 @@
         mFocusContainer.setVisibility(focus ? View.VISIBLE : View.GONE);
     }
 
-    @Override
-    public void createContextMenu(ContextMenu menu) {
-        MenuInflater inflater = mActivity.getMenuInflater();
-        inflater.inflate(R.menu.title_context, menu);
-        mActivity.onCreateContextMenu(menu, this, null);
-    }
-
     private void search() {
         setDisplayTitle("");
         mUrlUnfocused.requestFocus();
diff --git a/src/com/android/browser/UiController.java b/src/com/android/browser/UiController.java
index dffebba..c74d74e 100644
--- a/src/com/android/browser/UiController.java
+++ b/src/com/android/browser/UiController.java
@@ -75,4 +75,6 @@
 
     void endActionMode();
 
+    void shareCurrentPage();
+
 }
diff --git a/src/com/android/browser/UrlInputView.java b/src/com/android/browser/UrlInputView.java
index 9c85616..a4c2be3 100644
--- a/src/com/android/browser/UrlInputView.java
+++ b/src/com/android/browser/UrlInputView.java
@@ -22,7 +22,6 @@
 import android.content.res.Configuration;
 import android.text.TextUtils;
 import android.util.AttributeSet;
-import android.view.ActionMode;
 import android.view.KeyEvent;
 import android.view.View;
 import android.view.View.OnFocusChangeListener;
@@ -36,7 +35,8 @@
  * handling suggestions
  */
 public class UrlInputView extends AutoCompleteTextView
-        implements OnFocusChangeListener, OnEditorActionListener, CompletionListener {
+        implements OnFocusChangeListener, OnEditorActionListener,
+        CompletionListener {
 
     private UrlInputListener   mListener;
     private InputMethodManager mInputManager;
@@ -66,11 +66,17 @@
         super.setOnFocusChangeListener(this);
         mAdapter = new SuggestionsAdapter(ctx, this);
         setAdapter(mAdapter);
-        setSelectAllOnFocus(false);
+        setSelectAllOnFocus(true);
         onConfigurationChanged(ctx.getResources().getConfiguration());
         setThreshold(1);
     }
 
+    void setController(UiController controller) {
+        UrlSelectionActionMode urlSelectionMode
+                = new UrlSelectionActionMode(controller);
+        setCustomSelectionActionModeCallback(urlSelectionMode);
+    }
+
     void setContainer(View container) {
         mContainer = container;
     }
@@ -109,12 +115,6 @@
     }
 
     @Override
-    public ActionMode startActionMode(ActionMode.Callback callback) {
-        // suppress selection action mode
-        return null;
-    }
-
-    @Override
     public void setOnFocusChangeListener(OnFocusChangeListener focusListener) {
         mWrappedFocusListener = focusListener;
     }
diff --git a/src/com/android/browser/UrlSelectionActionMode.java b/src/com/android/browser/UrlSelectionActionMode.java
new file mode 100644
index 0000000..5636388
--- /dev/null
+++ b/src/com/android/browser/UrlSelectionActionMode.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2010 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.
+ */
+
+package com.android.browser;
+
+import android.view.ActionMode;
+import android.view.Menu;
+import android.view.MenuItem;
+
+public class UrlSelectionActionMode implements ActionMode.Callback {
+
+    private UiController mUiController;
+
+    public UrlSelectionActionMode(UiController controller) {
+        mUiController = controller;
+    }
+
+    // ActionMode.Callback implementation
+
+    @Override
+    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
+        mode.getMenuInflater().inflate(R.menu.url_selection, menu);
+        return true;
+    }
+
+    @Override
+    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
+        switch (item.getItemId()) {
+            case R.id.share:
+                mUiController.shareCurrentPage();
+                mode.finish();
+                break;
+            default:
+                return false;
+        }
+        return true;
+    }
+
+    @Override
+    public void onDestroyActionMode(ActionMode mode) {
+    }
+
+    @Override
+    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
+        return true;
+    }
+
+}