Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b103709d authored by Keisuke Kuroyanagi's avatar Keisuke Kuroyanagi
Browse files

Paste as plain text by Ctrl-Shift-V

Detect Ctrl-Shft-V in onKeyShortcut() and perform paste
as plain text.

Bug: 19287899
Change-Id: I0b27bf6155222a042eeb61dfbd4544c8312904ce
parent bbcec733
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -1710,6 +1710,7 @@ package android {
    field public static final int message = 16908299; // 0x102000b
    field public static final int navigationBarBackground = 16908336; // 0x1020030
    field public static final int paste = 16908322; // 0x1020022
    field public static final int pasteAsPlainText = 16908339; // 0x1020033
    field public static final int primary = 16908300; // 0x102000c
    field public static final int progress = 16908301; // 0x102000d
    field public static final int redo = 16908338; // 0x1020032
+1 −0
Original line number Diff line number Diff line
@@ -1786,6 +1786,7 @@ package android {
    field public static final int message = 16908299; // 0x102000b
    field public static final int navigationBarBackground = 16908336; // 0x1020030
    field public static final int paste = 16908322; // 0x1020022
    field public static final int pasteAsPlainText = 16908339; // 0x1020033
    field public static final int primary = 16908300; // 0x102000c
    field public static final int progress = 16908301; // 0x102000d
    field public static final int redo = 16908338; // 0x1020032
+20 −4
Original line number Diff line number Diff line
@@ -8412,6 +8412,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                        return onTextContextMenuItem(ID_REDO);
                    }
                    break;
                case KeyEvent.KEYCODE_V:
                    if (canPaste()) {
                        return onTextContextMenuItem(ID_PASTE_AS_PLAIN_TEXT);
                    }
            }
        }
        return super.onKeyShortcut(keyCode, event);
@@ -8794,6 +8798,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
    static final int ID_CUT = android.R.id.cut;
    static final int ID_COPY = android.R.id.copy;
    static final int ID_PASTE = android.R.id.paste;
    static final int ID_PASTE_AS_PLAIN_TEXT = android.R.id.pasteAsPlainText;

    /**
     * Called when a context menu option for the text view is selected.  Currently
@@ -8834,7 +8839,11 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                return true;  // Returns true even if nothing was undone.

            case ID_PASTE:
                paste(min, max);
                paste(min, max, true /* withFormatting */);
                return true;

            case ID_PASTE_AS_PLAIN_TEXT:
                paste(min, max, false /* withFormatting */);
                return true;

            case ID_CUT:
@@ -9018,14 +9027,21 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
    /**
     * Paste clipboard content between min and max positions.
     */
    private void paste(int min, int max) {
    private void paste(int min, int max, boolean withFormatting) {
        ClipboardManager clipboard =
            (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = clipboard.getPrimaryClip();
        if (clip != null) {
            boolean didFirst = false;
            for (int i=0; i<clip.getItemCount(); i++) {
                CharSequence paste = clip.getItemAt(i).coerceToStyledText(getContext());
                final CharSequence paste;
                if (withFormatting) {
                    paste = clip.getItemAt(i).coerceToStyledText(getContext());
                } else {
                    // Get an item as text and remove all spans by toString().
                    final CharSequence text = clip.getItemAt(i).coerceToText(getContext());
                    paste = (text instanceof Spanned) ? text.toString() : text;
                }
                if (paste != null) {
                    if (!didFirst) {
                        Selection.setSelection((Spannable) mText, max);
+1 −0
Original line number Diff line number Diff line
@@ -91,4 +91,5 @@
  <item type="id" name="navigationBarBackground" />
  <item type="id" name="undo" />
  <item type="id" name="redo" />
  <item type="id" name="pasteAsPlainText" />
</resources>
+3 −0
Original line number Diff line number Diff line
@@ -2639,5 +2639,8 @@
  <public type="id" name="undo" />
  <!-- Context menu ID for the "Redo" menu item to redo the last text edit operation. -->
  <public type="id" name="redo" />
  <!-- Context menu ID for the "Paste as plain text" menu item to to copy the current contents
          of the clipboard into the text view without formatting. -->
  <public type="id" name="pasteAsPlainText" />

</resources>