blob: 48b604c363cd7b04a33731794d89b24ef2e45ad4 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
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 Project9066cfe2009-03-03 19:31:44 -080017package android.inputmethodservice;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.inputmethod.ExtractedText;
satoka67a3cf2011-09-07 17:14:03 +090022import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import 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 */
29public 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 Viverette617feb92013-09-09 18:09:13 -070041 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 Project9066cfe2009-03-03 19:31:44 -080047 }
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 Debunne459ac632011-07-12 16:36:33 -0700106 if (mIME != null && mIME.onExtractTextContextMenuItem(id)) {
Gilles Debunne14568c32012-01-13 15:26:05 -0800107 // Mode was started on Extracted, needs to be stopped here.
108 // Cut and paste will change the text, which stops selection mode.
109 if (id == android.R.id.copy) stopSelectionActionMode();
Gilles Debunne459ac632011-07-12 16:36:33 -0700110 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 }
112 return super.onTextContextMenuItem(id);
113 }
114
115 /**
116 * We are always considered to be an input method target.
117 */
Gilles Debunnee67b58a2010-08-31 15:55:31 -0700118 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 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 Debunnee67b58a2010-08-31 15:55:31 -0700135 return this.isEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 }
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 Debunnee67b58a2010-08-31 15:55:31 -0700143 return this.isEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 }
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 Debunnee67b58a2010-08-31 15:55:31 -0700151 return this.isEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 }
satoka67a3cf2011-09-07 17:14:03 +0900153
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 Debunne39ba6d92011-11-09 05:26:26 +0100166
167 /**
Gilles Debunnee300be92011-12-06 10:15:56 -0800168 * {@inheritDoc}
Gilles Debunne39ba6d92011-11-09 05:26:26 +0100169 * @hide
170 */
171 @Override
172 protected void deleteText_internal(int start, int end) {
Gilles Debunnee300be92011-12-06 10:15:56 -0800173 // Do not call the super method.
174 // This will change the source TextView instead, which will update the ExtractTextView.
Gilles Debunne39ba6d92011-11-09 05:26:26 +0100175 mIME.onExtractedDeleteText(start, end);
176 }
177
178 /**
Gilles Debunnee300be92011-12-06 10:15:56 -0800179 * {@inheritDoc}
Gilles Debunne39ba6d92011-11-09 05:26:26 +0100180 * @hide
181 */
182 @Override
183 protected void replaceText_internal(int start, int end, CharSequence text) {
Gilles Debunnee300be92011-12-06 10:15:56 -0800184 // Do not call the super method.
185 // This will change the source TextView instead, which will update the ExtractTextView.
Gilles Debunne39ba6d92011-11-09 05:26:26 +0100186 mIME.onExtractedReplaceText(start, end, text);
187 }
188
Gilles Debunnee300be92011-12-06 10:15:56 -0800189 /**
190 * {@inheritDoc}
191 * @hide
192 */
193 @Override
194 protected void setSpan_internal(Object span, int start, int end, int flags) {
195 // Do not call the super method.
196 // This will change the source TextView instead, which will update the ExtractTextView.
197 mIME.onExtractedSetSpan(span, start, end, flags);
198 }
199
200 /**
201 * {@inheritDoc}
202 * @hide
203 */
204 @Override
205 protected void setCursorPosition_internal(int start, int end) {
206 // Do not call the super method.
207 // This will change the source TextView instead, which will update the ExtractTextView.
208 mIME.onExtractedSelectionChanged(start, end);
209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210}