Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | package android.inputmethodservice; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
| 21 | import android.view.inputmethod.ExtractedText; |
| 22 | import android.widget.EditText; |
| 23 | |
| 24 | /*** |
| 25 | * Specialization of {@link EditText} for showing and interacting with the |
| 26 | * extracted text in a full-screen input method. |
| 27 | */ |
| 28 | public class ExtractEditText extends EditText { |
| 29 | private InputMethodService mIME; |
| 30 | private int mSettingExtractedText; |
| 31 | |
| 32 | public ExtractEditText(Context context) { |
| 33 | super(context, null); |
| 34 | } |
| 35 | |
| 36 | public ExtractEditText(Context context, AttributeSet attrs) { |
| 37 | super(context, attrs, com.android.internal.R.attr.editTextStyle); |
| 38 | } |
| 39 | |
| 40 | public ExtractEditText(Context context, AttributeSet attrs, int defStyle) { |
| 41 | super(context, attrs, defStyle); |
| 42 | } |
| 43 | |
| 44 | void setIME(InputMethodService ime) { |
| 45 | mIME = ime; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Start making changes that will not be reported to the client. That |
| 50 | * is, {@link #onSelectionChanged(int, int)} will not result in sending |
| 51 | * the new selection to the client |
| 52 | */ |
| 53 | public void startInternalChanges() { |
| 54 | mSettingExtractedText += 1; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Finish making changes that will not be reported to the client. That |
| 59 | * is, {@link #onSelectionChanged(int, int)} will not result in sending |
| 60 | * the new selection to the client |
| 61 | */ |
| 62 | public void finishInternalChanges() { |
| 63 | mSettingExtractedText -= 1; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Implement just to keep track of when we are setting text from the |
| 68 | * client (vs. seeing changes in ourself from the user). |
| 69 | */ |
| 70 | @Override public void setExtractedText(ExtractedText text) { |
| 71 | try { |
| 72 | mSettingExtractedText++; |
| 73 | super.setExtractedText(text); |
| 74 | } finally { |
| 75 | mSettingExtractedText--; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Report to the underlying text editor about selection changes. |
| 81 | */ |
| 82 | @Override protected void onSelectionChanged(int selStart, int selEnd) { |
| 83 | if (mSettingExtractedText == 0 && mIME != null && selStart >= 0 && selEnd >= 0) { |
| 84 | mIME.onExtractedSelectionChanged(selStart, selEnd); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Redirect clicks to the IME for handling there. First allows any |
| 90 | * on click handler to run, though. |
| 91 | */ |
| 92 | @Override public boolean performClick() { |
| 93 | if (!super.performClick() && mIME != null) { |
| 94 | mIME.onExtractedTextClicked(); |
| 95 | return true; |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | @Override public boolean onTextContextMenuItem(int id) { |
| 101 | if (mIME != null) { |
| 102 | if (mIME.onExtractTextContextMenuItem(id)) { |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | return super.onTextContextMenuItem(id); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * We are always considered to be an input method target. |
| 111 | */ |
Gilles Debunne | e67b58a | 2010-08-31 15:55:31 -0700 | [diff] [blame^] | 112 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | public boolean isInputMethodTarget() { |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Return true if the edit text is currently showing a scroll bar. |
| 119 | */ |
| 120 | public boolean hasVerticalScrollBar() { |
| 121 | return computeVerticalScrollRange() > computeVerticalScrollExtent(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Pretend like the window this view is in always has focus, so its |
| 126 | * highlight and cursor will be displayed. |
| 127 | */ |
| 128 | @Override public boolean hasWindowFocus() { |
Gilles Debunne | e67b58a | 2010-08-31 15:55:31 -0700 | [diff] [blame^] | 129 | return this.isEnabled(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Pretend like this view always has focus, so its |
| 134 | * highlight and cursor will be displayed. |
| 135 | */ |
| 136 | @Override public boolean isFocused() { |
Gilles Debunne | e67b58a | 2010-08-31 15:55:31 -0700 | [diff] [blame^] | 137 | return this.isEnabled(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Pretend like this view always has focus, so its |
| 142 | * highlight and cursor will be displayed. |
| 143 | */ |
| 144 | @Override public boolean hasFocus() { |
Gilles Debunne | e67b58a | 2010-08-31 15:55:31 -0700 | [diff] [blame^] | 145 | return this.isEnabled(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | } |
| 147 | } |