The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame^] | 1 | package android.inputmethodservice; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | import android.util.AttributeSet; |
| 5 | import android.view.inputmethod.ExtractedText; |
| 6 | import 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 | */ |
| 12 | public 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 | } |