Refactor find and select dialogs

(This is a work in progress -- assets in particular are not final)

Use common code when showing the select and find dialogs. Both
now use similarly constructed contextual action bars and share
resources where possible. Both share a common base class so
supporting code can communicate with either.

Companion changes in frameworks/base and external/webkit

Change-Id: I62e15afd2730444985f8dbd2472df3cae351f47a
http://b/262451
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index e6982c9..e752711 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -860,9 +860,9 @@
             }
             // Do not need to check for null, since the current tab will have
             // at least a main WebView, or we would have returned above.
-            if (getTopWindow().getFindIsUp()) {
+            if (dialogIsUp()) {
                 // Do not show the fake title bar, which would cover up the
-                // FindDialog.
+                // find or select dialog.
                 return;
             }
 
@@ -1302,6 +1302,22 @@
         getTopWindow().requestFocus();
     }
 
+    private WebView showDialog(WebDialog dialog) {
+        // Need to do something special for Tablet
+        Tab tab = mTabControl.getCurrentTab();
+        if (tab.getSubWebView() == null) {
+            // If the find or select is being performed on the main webview,
+            // remove the embedded title bar.
+            WebView mainView = tab.getWebView();
+            if (mainView != null) {
+                mainView.setEmbeddedTitleBar(null);
+            }
+        }
+        hideFakeTitleBar();
+        mMenuState = EMPTY_MENU;
+        return tab.showDialog(dialog);
+    }
+
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         if (!mCanChord) {
@@ -1389,27 +1405,13 @@
                 break;
 
             case R.id.find_menu_id:
-                if (null == mFindDialog) {
-                    mFindDialog = new FindDialog(this);
-                }
-                // Need to do something special for Tablet
-                Tab tab = mTabControl.getCurrentTab();
-                if (tab.getSubWebView() == null) {
-                    // If the Find is being performed on the main webview,
-                    // remove the embedded title bar.
-                    WebView mainView = tab.getWebView();
-                    if (mainView != null) {
-                        mainView.setEmbeddedTitleBar(null);
-                    }
-                }
-                hideFakeTitleBar();
-                tab.showFind(mFindDialog);
-                mMenuState = EMPTY_MENU;
+                showFindDialog();
                 break;
 
             case R.id.select_text_id:
-                getTopWindow().emulateShiftHeld();
+                showSelectDialog();
                 break;
+
             case R.id.page_info_menu_id:
                 showPageInfo(mTabControl.getCurrentTab(), false);
                 break;
@@ -1495,18 +1497,28 @@
         startActivity(i);
     }
 
-    /*
-     * Remove the FindDialog.
-     */
-    public void closeFind() {
+    private boolean dialogIsUp() {
+        return null != mFindDialog && mFindDialog.isVisible() ||
+            null != mSelectDialog && mSelectDialog.isVisible();
+    }
+
+    private boolean closeDialog(WebDialog dialog) {
+        if (null == dialog || !dialog.isVisible()) return false;
         Tab currentTab = mTabControl.getCurrentTab();
-        if (mFindDialog != null) {
-            currentTab.closeFind(mFindDialog);
-            mFindDialog.dismiss();
-        }
+        currentTab.closeDialog(dialog);
+        dialog.dismiss();
+        return true;
+    }
+
+    /*
+     * Remove the find dialog or select dialog.
+     */
+    public void closeDialogs() {
+        if (!(closeDialog(mFindDialog) || closeDialog(mSelectDialog))) return;
         if (!mXLargeScreenSize) {
             // If the Find was being performed in the main WebView, replace the
             // embedded title bar.
+            Tab currentTab = mTabControl.getCurrentTab();
             if (currentTab.getSubWebView() == null) {
                 WebView mainView = currentTab.getWebView();
                 if (mainView != null) {
@@ -1517,12 +1529,31 @@
         mMenuState = R.id.MAIN_MENU;
         if (mInLoad) {
             // The title bar was hidden, because otherwise it would cover up the
-            // find dialog.  Now that the dialog has been removed, show the fake
-            // title bar once again.
+            // find or select dialog.  Now that the dialog has been removed,
+            // show the fake title bar once again.
             showFakeTitleBar();
         }
     }
 
+    public void showFindDialog() {
+        if (null == mFindDialog) {
+            mFindDialog = new FindDialog(this);
+        }
+        showDialog(mFindDialog).setFindIsUp(true);
+    }
+
+    public void setFindDialogText(String text) {
+        mFindDialog.setText(text);
+    }
+
+    public void showSelectDialog() {
+        if (null == mSelectDialog) {
+            mSelectDialog = new SelectDialog(this);
+        }
+        showDialog(mSelectDialog).setUpSelect();
+        mSelectDialog.hideSoftInput();
+    }
+
     @Override
     public boolean onPrepareOptionsMenu(Menu menu) {
         // This happens when the user begins to hold down the menu key, so
@@ -2514,9 +2545,7 @@
         onProgressChanged(view, INITIAL_PROGRESS);
         mDidStopLoad = false;
         if (!mIsNetworkUp) createAndShowNetworkDialog();
-        if (view.getFindIsUp()) {
-            closeFind();
-        }
+        closeDialogs();
         if (mSettings.isTracing()) {
             String host;
             try {
@@ -3921,6 +3950,7 @@
     private Menu mMenu;
 
     private FindDialog mFindDialog;
+    private SelectDialog mSelectDialog;
     // Used to prevent chording to result in firing two shortcuts immediately
     // one after another.  Fixes bug 1211714.
     boolean mCanChord;
diff --git a/src/com/android/browser/FindDialog.java b/src/com/android/browser/FindDialog.java
index bcd5bb7..9d0ac4b 100644
--- a/src/com/android/browser/FindDialog.java
+++ b/src/com/android/browser/FindDialog.java
@@ -33,10 +33,8 @@
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
-/* package */ class FindDialog extends LinearLayout implements TextWatcher {
-    private WebView         mWebView;
+/* package */ class FindDialog extends WebDialog implements TextWatcher {
     private TextView        mMatches;
-    private BrowserActivity mBrowserActivity;
     
     // Views with which the user can interact.
     private EditText        mEditText;
@@ -49,20 +47,14 @@
     // Once it has been called, enter should move to the next match.
     private boolean         mMatchesFound;
     private int             mNumberOfMatches;
+
     private View.OnClickListener mFindListener = new View.OnClickListener() {
         public void onClick(View v) {
             findNext();
         }
     };
 
-    private View.OnClickListener mFindCancelListener  = 
-            new View.OnClickListener() {
-        public void onClick(View v) {
-            mBrowserActivity.closeFind();
-        }
-    };
-    
-    private View.OnClickListener mFindPreviousListener  = 
+    private View.OnClickListener mFindPreviousListener  =
             new View.OnClickListener() {
         public void onClick(View v) {
             if (mWebView == null) {
@@ -74,15 +66,6 @@
         }
     };
 
-    /*
-     * Remove the soft keyboard from the screen.
-     */
-    private void hideSoftInput() {
-        InputMethodManager imm = (InputMethodManager)
-                mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
-        imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
-    }
-
     private void disableButtons() {
         mPrevButton.setEnabled(false);
         mNextButton.setEnabled(false);
@@ -90,17 +73,13 @@
         mNextButton.setFocusable(false);
     }
 
-    /* package */ void setWebView(WebView webview) {
-        mWebView = webview;
-    }
-
     /* package */ FindDialog(BrowserActivity context) {
         super(context);
-        mBrowserActivity = context;
 
         LayoutInflater factory = LayoutInflater.from(context);
         factory.inflate(R.layout.browser_find, this);
 
+        addCancel();
         mEditText = (EditText) findViewById(R.id.edit);
         
         View button = findViewById(R.id.next);
@@ -111,9 +90,6 @@
         button.setOnClickListener(mFindPreviousListener);
         mPrevButton = button;
         
-        button = findViewById(R.id.done);
-        button.setOnClickListener(mFindCancelListener);
-        
         mMatches = (TextView) findViewById(R.id.matches);
         mMatchesView = findViewById(R.id.matches_view);
         disableButtons();
@@ -121,14 +97,13 @@
     }
 
     /**
-     * Called by BrowserActivity.closeFind.  Start the animation to hide
+     * Called by BrowserActivity.closeDialog.  Start the animation to hide
      * the dialog, inform the WebView that the dialog is being dismissed,
      * and hide the soft keyboard.
      */
     public void dismiss() {
+        super.dismiss();
         mWebView.notifyFindDialogDismissed();
-        startAnimation(AnimationUtils.loadAnimation(mBrowserActivity,
-                R.anim.find_dialog_exit));
         hideSoftInput();
     }
 
@@ -144,7 +119,7 @@
                     return true;
                 } else if (KeyEvent.ACTION_UP == action
                         && !event.isCanceled() && state.isTracking(event)) {
-                    mBrowserActivity.closeFind();
+                    mBrowserActivity.closeDialogs();
                     return true;
                 }
             }
@@ -182,6 +157,7 @@
     }
 
     public void show() {
+        super.show();
         // In case the matches view is showing from a previous search
         mMatchesView.setVisibility(View.INVISIBLE);
         mMatchesFound = false;
@@ -193,8 +169,6 @@
         Selection.setSelection(span, 0, length);
         span.setSpan(this, 0, length, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
         disableButtons();
-        startAnimation(AnimationUtils.loadAnimation(mBrowserActivity,
-                R.anim.find_dialog_enter));
         InputMethodManager imm = (InputMethodManager)
                 mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
         imm.showSoftInput(mEditText, 0);
@@ -251,6 +225,11 @@
         updateMatchesString();
     }
 
+    public void setText(String text) {
+        mEditText.setText(text);
+        findAll();
+    }
+
     private void updateMatchesString() {
         // Note: updateMatchesString is only called by methods that have already
         // checked mWebView for null.
diff --git a/src/com/android/browser/SelectDialog.java b/src/com/android/browser/SelectDialog.java
new file mode 100644
index 0000000..461127a
--- /dev/null
+++ b/src/com/android/browser/SelectDialog.java
@@ -0,0 +1,85 @@
+/*
+ * 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.provider.Browser;
+import android.view.LayoutInflater;
+import android.view.View;
+
+/* package */ class SelectDialog extends WebDialog {
+    private View mCopyButton;
+    private View mSelectAllButton;
+    private View mShareButton;
+    private View mFindButton;
+
+    SelectDialog(BrowserActivity context) {
+        super(context);
+        LayoutInflater factory = LayoutInflater.from(context);
+        factory.inflate(R.layout.browser_select, this);
+        addCancel();
+
+        mCopyButton = findViewById(R.id.copy);
+        mCopyButton.setOnClickListener(mCopyListener);
+        mSelectAllButton = findViewById(R.id.select_all);
+        mSelectAllButton.setOnClickListener(mSelectAllListener);
+        mShareButton = findViewById(R.id.share);
+        mShareButton.setOnClickListener(mShareListener);
+        mFindButton = findViewById(R.id.find);
+        mFindButton.setOnClickListener(mFindListener);
+    }
+
+    private View.OnClickListener mCopyListener = new View.OnClickListener() {
+        public void onClick(View v) {
+            mWebView.copySelection();
+            mBrowserActivity.closeDialogs();
+        }
+    };
+
+    private View.OnClickListener mSelectAllListener = new View.OnClickListener() {
+        public void onClick(View v) {
+            mWebView.selectAll();
+        }
+    };
+
+    private View.OnClickListener mShareListener = new View.OnClickListener() {
+        public void onClick(View v) {
+            String selection = mWebView.getSelection();
+            Browser.sendString(mBrowserActivity, selection);
+            mBrowserActivity.closeDialogs();
+        }
+    };
+
+    private View.OnClickListener mFindListener = new View.OnClickListener() {
+        public void onClick(View v) {
+            String selection = mWebView.getSelection();
+            mBrowserActivity.closeDialogs();
+            mBrowserActivity.showFindDialog();
+            mBrowserActivity.setFindDialogText(selection);
+        }
+    };
+
+    /**
+     * Called by BrowserActivity.closeDialog.  Start the animation to hide
+     * the dialog, and inform the WebView that the dialog is being dismissed.
+     */
+    @Override
+    public void dismiss() {
+        super.dismiss();
+        mWebView.notifySelectDialogDismissed();
+    }
+
+}
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index 79c4574..8353317 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -1029,6 +1029,16 @@
         }
 
         @Override
+        public void onSelectionDone() {
+            if (mInForeground) mActivity.closeDialogs();
+        }
+
+        @Override
+        public void onSelectionStart() {
+            if (mInForeground) mActivity.showSelectDialog();
+        }
+
+        @Override
         public void onShowCustomView(View view,
                 WebChromeClient.CustomViewCallback callback) {
             if (mInForeground) mActivity.onShowCustomView(view, callback);
@@ -1218,8 +1228,8 @@
         public void onPageStarted(WebView view, String url, Bitmap favicon) {
             // Unlike the others, do not call mClient's version, which would
             // change the progress bar.  However, we do want to remove the
-            // find dialog.
-            if (view.getFindIsUp()) mBrowserActivity.closeFind();
+            // find or select dialog.
+            mBrowserActivity.closeDialogs();
         }
         @Override
         public void doUpdateVisitedHistory(WebView view, String url,
@@ -1420,7 +1430,7 @@
      */
     boolean createSubWindow() {
         if (mSubView == null) {
-            if (mMainView.getFindIsUp()) mActivity.closeFind();
+            mActivity.closeDialogs();
             mSubViewContainer = mInflateService.inflate(
                     R.layout.browser_subwindow, null);
             mSubView = (WebView) mSubViewContainer.findViewById(R.id.webview);
@@ -1468,9 +1478,7 @@
      */
     void dismissSubWindow() {
         if (mSubView != null) {
-            if (mSubView.getFindIsUp()) {
-                mActivity.closeFind();
-            }
+            mActivity.closeDialogs();
             BrowserSettings.getInstance().deleteObserver(
                     mSubView.getSettings());
             mSubView.destroy();
@@ -1495,7 +1503,7 @@
     void removeSubWindow(ViewGroup content) {
         if (mSubView != null) {
             content.removeView(mSubViewContainer);
-            if (mSubView.getFindIsUp()) mActivity.closeFind();
+            mActivity.closeDialogs();
         }
     }
 
@@ -1554,7 +1562,7 @@
                 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
         wrapper.removeView(mMainView);
         content.removeView(mContainer);
-        if (mMainView.getFindIsUp()) mActivity.closeFind();
+        mActivity.closeDialogs();
         removeSubWindow(content);
     }
 
@@ -1957,9 +1965,9 @@
     }
 
     /*
-     * Open the find dialog.  Called by BrowserActivity.
+     * Opens the find and select text dialogs.  Called by BrowserActivity.
      */
-    void showFind(FindDialog dialog) {
+    WebView showDialog(WebDialog dialog) {
         LinearLayout container;
         WebView view;
         if (mSubView != null) {
@@ -1975,13 +1983,13 @@
                 ViewGroup.LayoutParams.MATCH_PARENT,
                 ViewGroup.LayoutParams.WRAP_CONTENT));
         dialog.setWebView(view);
-        view.setFindIsUp(true);
+        return view;
     }
 
     /*
-     * Close the find dialog.  Called by BrowserActivity.closeFind.
+     * Close the find or select dialog. Called by BrowserActivity.closeDialog.
      */
-    void closeFind(FindDialog dialog) {
+    void closeDialog(WebDialog dialog) {
         // The dialog may be attached to the subwindow.  Ensure that the
         // correct parent has it removed.
         LinearLayout parent = (LinearLayout) dialog.getParent();
diff --git a/src/com/android/browser/WebDialog.java b/src/com/android/browser/WebDialog.java
new file mode 100644
index 0000000..9995e8f
--- /dev/null
+++ b/src/com/android/browser/WebDialog.java
@@ -0,0 +1,79 @@
+/*
+ * 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.content.Context;
+import android.view.View;
+import android.view.animation.AnimationUtils;
+import android.view.inputmethod.InputMethodManager;
+import android.webkit.WebView;
+import android.widget.LinearLayout;
+
+/* package */ class WebDialog extends LinearLayout {
+    protected WebView         mWebView;
+    protected BrowserActivity mBrowserActivity;
+    private boolean           mIsVisible;
+
+    /* package */ WebDialog(BrowserActivity context) {
+        super(context);
+        mBrowserActivity = context;
+    }
+
+    /* dialogs that have cancel buttons can optionally share code by including a
+     * view with an id of 'done'.
+     */
+    protected void addCancel() {
+        View button = findViewById(R.id.done);
+        if (button != null) button.setOnClickListener(mCancelListener);
+    }
+
+    private View.OnClickListener mCancelListener = new View.OnClickListener() {
+        public void onClick(View v) {
+            mBrowserActivity.closeDialogs();
+        }
+    };
+
+    protected void dismiss() {
+        startAnimation(AnimationUtils.loadAnimation(mBrowserActivity,
+                R.anim.dialog_exit));
+        mIsVisible = false;
+    }
+
+    /*
+     * Remove the soft keyboard from the screen.
+     */
+    protected void hideSoftInput() {
+        InputMethodManager imm = (InputMethodManager)
+                mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
+        imm.hideSoftInputFromWindow(mWebView.getWindowToken(), 0);
+    }
+
+    protected boolean isVisible() {
+        return mIsVisible;
+    }
+
+    /* package */ void setWebView(WebView webview) {
+        mWebView = webview;
+    }
+
+    protected void show() {
+        startAnimation(AnimationUtils.loadAnimation(mBrowserActivity,
+            R.anim.dialog_enter));
+        mIsVisible = true;
+    }
+
+}