Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/SelectionPopupController.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/SelectionPopupController.java b/content/public/android/java/src/org/chromium/content/browser/SelectionPopupController.java |
| index a6f50a38e44ab3251eb55128cfe9d5ae6b551e84..5bc0aeccb4d5788178c67a57a0766393abdbd0ea 100644 |
| --- a/content/public/android/java/src/org/chromium/content/browser/SelectionPopupController.java |
| +++ b/content/public/android/java/src/org/chromium/content/browser/SelectionPopupController.java |
| @@ -7,6 +7,8 @@ package org.chromium.content.browser; |
| import android.annotation.TargetApi; |
| import android.app.Activity; |
| import android.app.SearchManager; |
| +import android.content.ClipData; |
| +import android.content.ClipDescription; |
| import android.content.ClipboardManager; |
| import android.content.Context; |
| import android.content.Intent; |
| @@ -16,7 +18,11 @@ import android.content.res.Resources; |
| import android.graphics.Rect; |
| import android.os.Build; |
| import android.provider.Browser; |
| +import android.text.Spanned; |
| import android.text.TextUtils; |
| +import android.text.style.CharacterStyle; |
| +import android.text.style.ParagraphStyle; |
| +import android.text.style.UpdateAppearance; |
| import android.view.ActionMode; |
| import android.view.Menu; |
| import android.view.MenuInflater; |
| @@ -102,6 +108,7 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| private boolean mEditable; |
| private boolean mIsPasswordType; |
| private boolean mIsInsertion; |
| + private boolean mCanEditRichly; |
|
amaralp
2017/04/13 20:40:18
This is currently only used for the paste menu so
Shimi Zhang
2017/04/15 01:17:15
Done.
|
| private boolean mUnselectAllOnDismiss; |
| private String mLastSelectedText; |
| @@ -251,6 +258,10 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| return actionMode; |
| } |
| + void setCanEditRichly(boolean canEditRichly) { |
|
amaralp
2017/04/13 20:40:17
This also won't be needed.
Shimi Zhang
2017/04/15 01:17:15
Done.
|
| + mCanEditRichly = canEditRichly; |
| + } |
| + |
| void createAndShowPastePopup(int x, int y) { |
| if (mView.getParent() == null || mView.getVisibility() != View.VISIBLE) { |
| return; |
| @@ -266,9 +277,20 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| } |
| @Override |
| + public void pasteAsPlainText() { |
| + mWebContents.pasteAsPlainText(); |
| + mWebContents.dismissTextHandles(); |
| + } |
| + |
| + @Override |
| public boolean canPaste() { |
| return SelectionPopupController.this.canPaste(); |
| } |
| + |
| + @Override |
| + public boolean canPasteAsPlainText() { |
| + return SelectionPopupController.this.canPasteAsPlainText(); |
| + } |
| }; |
| Context windowContext = mWindowAndroid.getContext().get(); |
| if (windowContext == null) return; |
| @@ -427,6 +449,11 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| if (!isSelectionEditable() || !canPaste()) { |
| menu.removeItem(R.id.select_action_menu_paste); |
| + menu.removeItem(R.id.select_action_menu_paste_as_plain_text); |
|
amaralp
2017/04/13 20:40:18
This is only used for the selection popup menu (th
Shimi Zhang
2017/04/15 01:17:15
Done.
|
| + } |
| + |
| + if (!canPasteAsPlainText()) { |
| + menu.removeItem(R.id.select_action_menu_paste_as_plain_text); |
| } |
| if (isInsertion()) { |
| @@ -466,6 +493,30 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| return clipMgr.hasPrimaryClip(); |
| } |
| + // if need to show paste as plain text. |
| + private boolean canPasteAsPlainText() { |
| + if (!mCanEditRichly) return false; |
| + ClipboardManager clipMgr = |
| + (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); |
| + if (!clipMgr.hasPrimaryClip()) return false; |
| + |
| + // In case that plain text is a Spanned. |
| + ClipData clipData = clipMgr.getPrimaryClip(); |
| + ClipDescription description = clipData.getDescription(); |
| + CharSequence text = clipData.getItemAt(0).getText(); |
| + boolean isPlainType = description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN); |
| + if (isPlainType && (text instanceof Spanned)) { |
| + Spanned spanned = (Spanned) text; |
| + Class<?>[] styles = { |
| + CharacterStyle.class, ParagraphStyle.class, UpdateAppearance.class}; |
| + for (Class<?> style : styles) { |
| + Object[] spans = spanned.getSpans(0, spanned.length(), style); |
| + if (spans != null && spans.length > 0) return true; |
| + } |
| + } |
| + return description.hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML); |
| + } |
| + |
| private void updateAssistMenuItem(Menu menu) { |
| // There is no Assist functionality before Android O. |
| if (!BuildInfo.isAtLeastO() || mAssistMenuItemId == 0) return; |
| @@ -538,6 +589,9 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| } else if (id == R.id.select_action_menu_paste) { |
| paste(); |
| mode.finish(); |
| + } else if (id == R.id.select_action_menu_paste_as_plain_text) { |
|
amaralp
2017/04/13 20:40:18
Again this code is for the selection action mode a
Shimi Zhang
2017/04/15 01:17:15
Done.
|
| + pasteAsPlainText(); |
| + mode.finish(); |
| } else if (id == R.id.select_action_menu_share) { |
| share(); |
| mode.finish(); |
| @@ -651,6 +705,14 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| } |
| /** |
| + * Perform a paste as plain text action. |
| + */ |
| + @VisibleForTesting |
| + void pasteAsPlainText() { |
|
amaralp
2017/04/13 20:40:18
Could you add tests for your change?
Shimi Zhang
2017/04/15 01:17:15
Done.
|
| + mWebContents.pasteAsPlainText(); |
| + } |
| + |
| + /** |
| * Perform a share action. |
| */ |
| @VisibleForTesting |