summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/ui/PlainTextEditText.java
blob: 8d6e78487bda0f703bb7aad179da2ee8f66168ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright (C) 2015 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.messaging.ui;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;

/**
 * We want the EditText used in Conversations to convert text to plain text on paste.  This
 * conversion would happen anyway on send, so without this class it could appear to the user
 * that we would send e.g. bold or italic formatting, but in the sent message it would just be
 * plain text.
 */
public class PlainTextEditText extends EditText {
    private static final char OBJECT_UNICODE = '\uFFFC';

    public PlainTextEditText(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    // Intercept and modify the paste event. Let everything else through unchanged.
    @Override
    public boolean onTextContextMenuItem(final int id) {
        if (id == android.R.id.paste) {
            // We can use this to know where the text position was originally before we pasted
            final int selectionStartPrePaste = getSelectionStart();

            // Let the EditText's normal paste routine fire, then modify the content after.
            // This is simpler than re-implementing the paste logic, which we'd have to do
            // if we want to get the text from the clipboard ourselves and then modify it.

            final boolean result = super.onTextContextMenuItem(id);
            CharSequence text = getText();
            int selectionStart = getSelectionStart();
            int selectionEnd = getSelectionEnd();

            // There is an option in the Chrome mobile app to copy image; however, instead of the
            // image in the form of the uri, Chrome gives us the html source for the image, which
            // the platform paste code turns into the unicode object character. The below section
            // of code looks for that edge case and replaces it with the url for the image.
            final int startIndex = selectionStart - 1;
            final int pasteStringLength = selectionStart - selectionStartPrePaste;
            // Only going to handle the case where the pasted object is the image
            if (pasteStringLength == 1 && text.charAt(startIndex) == OBJECT_UNICODE) {
                final ClipboardManager clipboard =
                        (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                final ClipData clip = clipboard.getPrimaryClip();
                if (clip != null) {
                    ClipData.Item item = clip.getItemAt(0);
                    StringBuilder sb = new StringBuilder(text);
                    final String url = item.getText().toString();
                    sb.replace(selectionStartPrePaste, selectionStart, url);
                    text = sb.toString();
                    selectionStart = selectionStartPrePaste + url.length();
                    selectionEnd = selectionStart;
                }
            }

            // This removes the formatting due to the conversion to string.
            setText(text.toString(), BufferType.EDITABLE);

            // Restore the cursor selection state.
            setSelection(selectionStart, selectionEnd);
            return result;
        } else {
            return super.onTextContextMenuItem(id);
        }
    }
}