ExtractEditText.java revision 3001a035439d8134a7d70d796376d1dfbff3cdcd
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     * Pretend like the window this view is in always has focus, so its
102     * highlight and cursor will be displayed.
103     */
104    @Override public boolean hasWindowFocus() {
105        return this.isEnabled() ? true : false;
106    }
107
108    /**
109     * Pretend like this view always has focus, so its
110     * highlight and cursor will be displayed.
111     */
112    @Override public boolean isFocused() {
113        return this.isEnabled() ? true : false;
114    }
115
116    /**
117     * Pretend like this view always has focus, so its
118     * highlight and cursor will be displayed.
119     */
120    @Override public boolean hasFocus() {
121        return this.isEnabled() ? true : false;
122    }
123}
124