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; |
satok | a67a3cf | 2011-09-07 17:14:03 +0900 | [diff] [blame] | 22 | import android.view.inputmethod.InputMethodManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.widget.EditText; |
| 24 | |
| 25 | /*** |
| 26 | * Specialization of {@link EditText} for showing and interacting with the |
| 27 | * extracted text in a full-screen input method. |
| 28 | */ |
| 29 | public class ExtractEditText extends EditText { |
| 30 | private InputMethodService mIME; |
| 31 | private int mSettingExtractedText; |
| 32 | |
| 33 | public ExtractEditText(Context context) { |
| 34 | super(context, null); |
| 35 | } |
| 36 | |
| 37 | public ExtractEditText(Context context, AttributeSet attrs) { |
| 38 | super(context, attrs, com.android.internal.R.attr.editTextStyle); |
| 39 | } |
| 40 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 41 | public ExtractEditText(Context context, AttributeSet attrs, int defStyleAttr) { |
| 42 | this(context, attrs, defStyleAttr, 0); |
| 43 | } |
| 44 | |
| 45 | public ExtractEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 46 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void setIME(InputMethodService ime) { |
| 50 | mIME = ime; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Start making changes that will not be reported to the client. That |
| 55 | * is, {@link #onSelectionChanged(int, int)} will not result in sending |
| 56 | * the new selection to the client |
| 57 | */ |
| 58 | public void startInternalChanges() { |
| 59 | mSettingExtractedText += 1; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Finish making changes that will not be reported to the client. That |
| 64 | * is, {@link #onSelectionChanged(int, int)} will not result in sending |
| 65 | * the new selection to the client |
| 66 | */ |
| 67 | public void finishInternalChanges() { |
| 68 | mSettingExtractedText -= 1; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Implement just to keep track of when we are setting text from the |
| 73 | * client (vs. seeing changes in ourself from the user). |
| 74 | */ |
| 75 | @Override public void setExtractedText(ExtractedText text) { |
| 76 | try { |
| 77 | mSettingExtractedText++; |
| 78 | super.setExtractedText(text); |
| 79 | } finally { |
| 80 | mSettingExtractedText--; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Report to the underlying text editor about selection changes. |
| 86 | */ |
| 87 | @Override protected void onSelectionChanged(int selStart, int selEnd) { |
| 88 | if (mSettingExtractedText == 0 && mIME != null && selStart >= 0 && selEnd >= 0) { |
| 89 | mIME.onExtractedSelectionChanged(selStart, selEnd); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Redirect clicks to the IME for handling there. First allows any |
| 95 | * on click handler to run, though. |
| 96 | */ |
| 97 | @Override public boolean performClick() { |
| 98 | if (!super.performClick() && mIME != null) { |
| 99 | mIME.onExtractedTextClicked(); |
| 100 | return true; |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | @Override public boolean onTextContextMenuItem(int id) { |
Gilles Debunne | 459ac63 | 2011-07-12 16:36:33 -0700 | [diff] [blame] | 106 | if (mIME != null && mIME.onExtractTextContextMenuItem(id)) { |
Gilles Debunne | 14568c3 | 2012-01-13 15:26:05 -0800 | [diff] [blame] | 107 | // Mode was started on Extracted, needs to be stopped here. |
Andrei Stingaceanu | cfa13a7 | 2015-07-08 15:07:59 +0100 | [diff] [blame^] | 108 | // Cut will change the text, which stops selection mode. |
| 109 | if (id == android.R.id.copy || id == android.R.id.paste) stopTextActionMode(); |
Gilles Debunne | 459ac63 | 2011-07-12 16:36:33 -0700 | [diff] [blame] | 110 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | } |
| 112 | return super.onTextContextMenuItem(id); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * We are always considered to be an input method target. |
| 117 | */ |
Gilles Debunne | e67b58a | 2010-08-31 15:55:31 -0700 | [diff] [blame] | 118 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | public boolean isInputMethodTarget() { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Return true if the edit text is currently showing a scroll bar. |
| 125 | */ |
| 126 | public boolean hasVerticalScrollBar() { |
| 127 | return computeVerticalScrollRange() > computeVerticalScrollExtent(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Pretend like the window this view is in always has focus, so its |
| 132 | * highlight and cursor will be displayed. |
| 133 | */ |
| 134 | @Override public boolean hasWindowFocus() { |
Gilles Debunne | e67b58a | 2010-08-31 15:55:31 -0700 | [diff] [blame] | 135 | return this.isEnabled(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Pretend like this view always has focus, so its |
| 140 | * highlight and cursor will be displayed. |
| 141 | */ |
| 142 | @Override public boolean isFocused() { |
Gilles Debunne | e67b58a | 2010-08-31 15:55:31 -0700 | [diff] [blame] | 143 | return this.isEnabled(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Pretend like this view always has focus, so its |
| 148 | * highlight and cursor will be displayed. |
| 149 | */ |
| 150 | @Override public boolean hasFocus() { |
Gilles Debunne | e67b58a | 2010-08-31 15:55:31 -0700 | [diff] [blame] | 151 | return this.isEnabled(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | } |
satok | a67a3cf | 2011-09-07 17:14:03 +0900 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * @hide |
| 156 | */ |
| 157 | @Override protected void viewClicked(InputMethodManager imm) { |
| 158 | // As an instance of this class is supposed to be owned by IMS, |
| 159 | // and it has a reference to the IMS (the current IME), |
| 160 | // we just need to call back its onViewClicked() here. |
| 161 | // It should be good to avoid unnecessary IPCs by doing this as well. |
| 162 | if (mIME != null) { |
| 163 | mIME.onViewClicked(false); |
| 164 | } |
| 165 | } |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 166 | |
| 167 | /** |
Andrei Stingaceanu | b1891b3 | 2015-06-19 16:44:37 +0100 | [diff] [blame] | 168 | * @hide |
| 169 | */ |
| 170 | @Override |
| 171 | public boolean isInExtractedMode() { |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | /** |
Gilles Debunne | e300be9 | 2011-12-06 10:15:56 -0800 | [diff] [blame] | 176 | * {@inheritDoc} |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 177 | * @hide |
| 178 | */ |
| 179 | @Override |
| 180 | protected void deleteText_internal(int start, int end) { |
Gilles Debunne | e300be9 | 2011-12-06 10:15:56 -0800 | [diff] [blame] | 181 | // Do not call the super method. |
| 182 | // This will change the source TextView instead, which will update the ExtractTextView. |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 183 | mIME.onExtractedDeleteText(start, end); |
| 184 | } |
| 185 | |
| 186 | /** |
Gilles Debunne | e300be9 | 2011-12-06 10:15:56 -0800 | [diff] [blame] | 187 | * {@inheritDoc} |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 188 | * @hide |
| 189 | */ |
| 190 | @Override |
| 191 | protected void replaceText_internal(int start, int end, CharSequence text) { |
Gilles Debunne | e300be9 | 2011-12-06 10:15:56 -0800 | [diff] [blame] | 192 | // Do not call the super method. |
| 193 | // This will change the source TextView instead, which will update the ExtractTextView. |
Gilles Debunne | 39ba6d9 | 2011-11-09 05:26:26 +0100 | [diff] [blame] | 194 | mIME.onExtractedReplaceText(start, end, text); |
| 195 | } |
| 196 | |
Gilles Debunne | e300be9 | 2011-12-06 10:15:56 -0800 | [diff] [blame] | 197 | /** |
| 198 | * {@inheritDoc} |
| 199 | * @hide |
| 200 | */ |
| 201 | @Override |
| 202 | protected void setSpan_internal(Object span, int start, int end, int flags) { |
| 203 | // Do not call the super method. |
| 204 | // This will change the source TextView instead, which will update the ExtractTextView. |
| 205 | mIME.onExtractedSetSpan(span, start, end, flags); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * {@inheritDoc} |
| 210 | * @hide |
| 211 | */ |
| 212 | @Override |
| 213 | protected void setCursorPosition_internal(int start, int end) { |
| 214 | // Do not call the super method. |
| 215 | // This will change the source TextView instead, which will update the ExtractTextView. |
| 216 | mIME.onExtractedSelectionChanged(start, end); |
| 217 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | } |