Add cut and paste to ContentEditable.

 Bug 5806267
 Use visual selection to determine the webkit selection.
 The webkit selection can be used to cut text from an editable
 area. It can also be used to do better complex character text
 copy.

 Webkit change: I194c6d9e2add67151b97092a1a54f5c081296000

Change-Id: I56543d17670a8c98484314c89c7fa6a94cb809e4
diff --git a/core/java/android/webkit/SelectActionModeCallback.java b/core/java/android/webkit/SelectActionModeCallback.java
index 8c174aa..cdf20f6 100644
--- a/core/java/android/webkit/SelectActionModeCallback.java
+++ b/core/java/android/webkit/SelectActionModeCallback.java
@@ -17,6 +17,7 @@
 package android.webkit;
 
 import android.app.SearchManager;
+import android.content.ClipboardManager;
 import android.content.Context;
 import android.content.Intent;
 import android.provider.Browser;
@@ -27,11 +28,16 @@
 class SelectActionModeCallback implements ActionMode.Callback {
     private WebView mWebView;
     private ActionMode mActionMode;
+    private boolean mIsTextSelected = true;
 
     void setWebView(WebView webView) {
         mWebView = webView;
     }
 
+    void setTextSelected(boolean isTextSelected) {
+        mIsTextSelected = isTextSelected;
+    }
+
     void finish() {
         // It is possible that onCreateActionMode was never called, in the case
         // where there is no ActionBar, for example.
@@ -52,17 +58,25 @@
         mode.setTitle(allowText ?
                 context.getString(com.android.internal.R.string.textSelectionCABTitle) : null);
 
-        if (!mode.isUiFocusable()) {
-            // If the action mode UI we're running in isn't capable of taking window focus
-            // the user won't be able to type into the find on page UI. Disable this functionality.
-            // (Note that this should only happen in floating dialog windows.)
-            // This can be removed once we can handle multiple focusable windows at a time
-            // in a better way.
-            final MenuItem findOnPageItem = menu.findItem(com.android.internal.R.id.find);
-            if (findOnPageItem != null) {
-                findOnPageItem.setVisible(false);
-            }
-        }
+        // If the action mode UI we're running in isn't capable of taking window focus
+        // the user won't be able to type into the find on page UI. Disable this functionality.
+        // (Note that this should only happen in floating dialog windows.)
+        // This can be removed once we can handle multiple focusable windows at a time
+        // in a better way.
+        ClipboardManager cm = (ClipboardManager)(context
+                .getSystemService(Context.CLIPBOARD_SERVICE));
+        boolean isFocusable = mode.isUiFocusable();
+        boolean isEditable = mWebView.focusCandidateIsEditableText();
+        boolean canPaste = isEditable && cm.hasPrimaryClip() && isFocusable;
+        boolean canFind = !isEditable && isFocusable;
+        boolean canCut = isEditable && mIsTextSelected && isFocusable;
+        boolean canCopy = mIsTextSelected;
+        boolean canWebSearch = mIsTextSelected;
+        setMenuVisibility(menu, canFind, com.android.internal.R.id.find);
+        setMenuVisibility(menu, canPaste, com.android.internal.R.id.paste);
+        setMenuVisibility(menu, canCut, com.android.internal.R.id.cut);
+        setMenuVisibility(menu, canCopy, com.android.internal.R.id.copy);
+        setMenuVisibility(menu, canWebSearch, com.android.internal.R.id.websearch);
         mActionMode = mode;
         return true;
     }
@@ -75,11 +89,21 @@
     @Override
     public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
         switch(item.getItemId()) {
+            case android.R.id.cut:
+                mWebView.cutSelection();
+                mode.finish();
+                break;
+
             case android.R.id.copy:
                 mWebView.copySelection();
                 mode.finish();
                 break;
 
+            case android.R.id.paste:
+                mWebView.pasteFromClipboard();
+                mode.finish();
+                break;
+
             case com.android.internal.R.id.share:
                 String selection = mWebView.getSelection();
                 Browser.sendString(mWebView.getContext(), selection);
@@ -113,4 +137,11 @@
     public void onDestroyActionMode(ActionMode mode) {
         mWebView.selectionDone();
     }
+
+    private void setMenuVisibility(Menu menu, boolean visible, int resourceId) {
+        final MenuItem item = menu.findItem(resourceId);
+        if (item != null) {
+            item.setVisible(visible);
+        }
+    }
 }