ExtractEditText.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1package android.inputmethodservice;
2
3import android.content.Context;
4import android.util.AttributeSet;
5import android.view.inputmethod.ExtractedText;
6import android.widget.EditText;
7
8/***
9 * Specialization of {@link EditText} for showing and interacting with the
10 * extracted text in a full-screen input method.
11 */
12public class ExtractEditText extends EditText {
13    private InputMethodService mIME;
14    private int mSettingExtractedText;
15
16    public ExtractEditText(Context context) {
17        super(context, null);
18    }
19
20    public ExtractEditText(Context context, AttributeSet attrs) {
21        super(context, attrs, com.android.internal.R.attr.editTextStyle);
22    }
23
24    public ExtractEditText(Context context, AttributeSet attrs, int defStyle) {
25        super(context, attrs, defStyle);
26    }
27
28    void setIME(InputMethodService ime) {
29        mIME = ime;
30    }
31
32    /**
33     * Start making changes that will not be reported to the client.  That
34     * is, {@link #onSelectionChanged(int, int)} will not result in sending
35     * the new selection to the client
36     */
37    public void startInternalChanges() {
38        mSettingExtractedText += 1;
39    }
40
41    /**
42     * Finish making changes that will not be reported to the client.  That
43     * is, {@link #onSelectionChanged(int, int)} will not result in sending
44     * the new selection to the client
45     */
46    public void finishInternalChanges() {
47        mSettingExtractedText -= 1;
48    }
49
50    /**
51     * Implement just to keep track of when we are setting text from the
52     * client (vs. seeing changes in ourself from the user).
53     */
54    @Override public void setExtractedText(ExtractedText text) {
55        try {
56            mSettingExtractedText++;
57            super.setExtractedText(text);
58        } finally {
59            mSettingExtractedText--;
60        }
61    }
62
63    /**
64     * Report to the underlying text editor about selection changes.
65     */
66    @Override protected void onSelectionChanged(int selStart, int selEnd) {
67        if (mSettingExtractedText == 0 && mIME != null && selStart >= 0 && selEnd >= 0) {
68            mIME.onExtractedSelectionChanged(selStart, selEnd);
69        }
70    }
71
72    /**
73     * Redirect clicks to the IME for handling there.  First allows any
74     * on click handler to run, though.
75     */
76    @Override public boolean performClick() {
77        if (!super.performClick() && mIME != null) {
78            mIME.onExtractedTextClicked();
79            return true;
80        }
81        return false;
82    }
83
84    @Override public boolean onTextContextMenuItem(int id) {
85        if (mIME != null) {
86            if (mIME.onExtractTextContextMenuItem(id)) {
87                return true;
88            }
89        }
90        return super.onTextContextMenuItem(id);
91    }
92
93    /**
94     * We are always considered to be an input method target.
95     */
96    public boolean isInputMethodTarget() {
97        return true;
98    }
99
100    /**
101     * Return true if the edit text is currently showing a scroll bar.
102     */
103    public boolean hasVerticalScrollBar() {
104        return computeVerticalScrollRange() > computeVerticalScrollExtent();
105    }
106
107    /**
108     * Pretend like the window this view is in always has focus, so its
109     * highlight and cursor will be displayed.
110     */
111    @Override public boolean hasWindowFocus() {
112        return this.isEnabled() ? true : false;
113    }
114
115    /**
116     * Pretend like this view always has focus, so its
117     * highlight and cursor will be displayed.
118     */
119    @Override public boolean isFocused() {
120        return this.isEnabled() ? true : false;
121    }
122
123    /**
124     * Pretend like this view always has focus, so its
125     * highlight and cursor will be displayed.
126     */
127    @Override public boolean hasFocus() {
128        return this.isEnabled() ? true : false;
129    }
130}
131