blob: 7f7528dd3de07073f5117181e6c82149e3b0119c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007-2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.internal.widget;
18
19import android.os.Bundle;
20import android.text.Editable;
satokf9f01002011-05-19 21:31:50 +090021import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.text.method.KeyListener;
satokf9f01002011-05-19 21:31:50 +090023import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.util.Log;
25import android.view.inputmethod.BaseInputConnection;
26import android.view.inputmethod.CompletionInfo;
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080027import android.view.inputmethod.CorrectionInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.view.inputmethod.ExtractedText;
29import android.view.inputmethod.ExtractedTextRequest;
Yohei Yukawaa277db22014-08-21 18:38:44 -070030import android.view.inputmethod.InputConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.widget.TextView;
32
33public class EditableInputConnection extends BaseInputConnection {
34 private static final boolean DEBUG = false;
35 private static final String TAG = "EditableInputConnection";
36
37 private final TextView mTextView;
38
Gilles Debunnec478c172011-12-19 17:29:24 -080039 // Keeps track of nested begin/end batch edit to ensure this connection always has a
40 // balanced impact on its associated TextView.
41 // A negative value means that this connection has been finished by the InputMethodManager.
42 private int mBatchEditNesting;
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 public EditableInputConnection(TextView textview) {
Dianne Hackborn51bf0772009-03-24 19:11:41 -070045 super(textview, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 mTextView = textview;
47 }
48
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080049 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 public Editable getEditable() {
51 TextView tv = mTextView;
52 if (tv != null) {
53 return tv.getEditableText();
54 }
55 return null;
56 }
Gilles Debunnec478c172011-12-19 17:29:24 -080057
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080058 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 public boolean beginBatchEdit() {
Gilles Debunnec478c172011-12-19 17:29:24 -080060 synchronized(this) {
61 if (mBatchEditNesting >= 0) {
62 mTextView.beginBatchEdit();
63 mBatchEditNesting++;
64 return true;
65 }
66 }
67 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 }
Gilles Debunnec478c172011-12-19 17:29:24 -080069
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080070 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 public boolean endBatchEdit() {
Gilles Debunnec478c172011-12-19 17:29:24 -080072 synchronized(this) {
73 if (mBatchEditNesting > 0) {
Gilles Debunne9d69ecb2012-02-24 16:07:09 -080074 // When the connection is reset by the InputMethodManager and reportFinish
Gilles Debunnec478c172011-12-19 17:29:24 -080075 // is called, some endBatchEdit calls may still be asynchronously received from the
76 // IME. Do not take these into account, thus ensuring that this IC's final
77 // contribution to mTextView's nested batch edit count is zero.
78 mTextView.endBatchEdit();
79 mBatchEditNesting--;
80 return true;
81 }
82 }
83 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 }
Gilles Debunnec478c172011-12-19 17:29:24 -080085
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080086 @Override
Yohei Yukawa9f9afe522016-03-30 12:03:51 -070087 public void closeConnection() {
88 super.closeConnection();
Gilles Debunne9d69ecb2012-02-24 16:07:09 -080089 synchronized(this) {
90 while (mBatchEditNesting > 0) {
91 endBatchEdit();
92 }
93 // Will prevent any further calls to begin or endBatchEdit
94 mBatchEditNesting = -1;
95 }
96 }
97
98 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 public boolean clearMetaKeyStates(int states) {
100 final Editable content = getEditable();
101 if (content == null) return false;
102 KeyListener kl = mTextView.getKeyListener();
103 if (kl != null) {
104 try {
105 kl.clearMetaKeyState(mTextView, content, states);
106 } catch (AbstractMethodError e) {
107 // This is an old listener that doesn't implement the
108 // new method.
109 }
110 }
111 return true;
112 }
Gilles Debunnec478c172011-12-19 17:29:24 -0800113
114 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public boolean commitCompletion(CompletionInfo text) {
116 if (DEBUG) Log.v(TAG, "commitCompletion " + text);
117 mTextView.beginBatchEdit();
118 mTextView.onCommitCompletion(text);
119 mTextView.endBatchEdit();
120 return true;
121 }
122
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -0800123 /**
124 * Calls the {@link TextView#onCommitCorrection} method of the associated TextView.
125 */
126 @Override
127 public boolean commitCorrection(CorrectionInfo correctionInfo) {
128 if (DEBUG) Log.v(TAG, "commitCorrection" + correctionInfo);
129 mTextView.beginBatchEdit();
130 mTextView.onCommitCorrection(correctionInfo);
131 mTextView.endBatchEdit();
132 return true;
133 }
134
135 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 public boolean performEditorAction(int actionCode) {
137 if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode);
138 mTextView.onEditorAction(actionCode);
139 return true;
140 }
141
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -0800142 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 public boolean performContextMenuAction(int id) {
144 if (DEBUG) Log.v(TAG, "performContextMenuAction " + id);
145 mTextView.beginBatchEdit();
146 mTextView.onTextContextMenuItem(id);
147 mTextView.endBatchEdit();
148 return true;
149 }
150
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -0800151 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
153 if (mTextView != null) {
154 ExtractedText et = new ExtractedText();
155 if (mTextView.extractText(request, et)) {
156 if ((flags&GET_EXTRACTED_TEXT_MONITOR) != 0) {
157 mTextView.setExtracting(request);
158 }
159 return et;
160 }
161 }
162 return null;
163 }
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -0800164
165 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 public boolean performPrivateCommand(String action, Bundle data) {
167 mTextView.onPrivateIMECommand(action, data);
168 return true;
169 }
170
171 @Override
172 public boolean commitText(CharSequence text, int newCursorPosition) {
173 if (mTextView == null) {
174 return super.commitText(text, newCursorPosition);
175 }
satokf9f01002011-05-19 21:31:50 +0900176 if (text instanceof Spanned) {
177 Spanned spanned = ((Spanned) text);
178 SuggestionSpan[] spans = spanned.getSpans(0, text.length(), SuggestionSpan.class);
179 mIMM.registerSuggestionSpansForNotification(spans);
180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
Gilles Debunneb7fc63f2011-01-27 15:55:29 -0800182 mTextView.resetErrorChangedFlag();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 boolean success = super.commitText(text, newCursorPosition);
Gilles Debunneb7fc63f2011-01-27 15:55:29 -0800184 mTextView.hideErrorIfUnchanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 return success;
187 }
Yohei Yukawa0023d0e2014-07-11 04:13:03 +0900188
189 @Override
Yohei Yukawad8636ea2014-09-02 22:03:30 -0700190 public boolean requestCursorUpdates(int cursorUpdateMode) {
Yohei Yukawaa277db22014-08-21 18:38:44 -0700191 if (DEBUG) Log.v(TAG, "requestUpdateCursorAnchorInfo " + cursorUpdateMode);
Yohei Yukawaff328ae2014-07-18 04:22:39 +0900192
Yohei Yukawa72d35fa2014-08-28 23:57:11 -0700193 // It is possible that any other bit is used as a valid flag in a future release.
194 // We should reject the entire request in such a case.
Yohei Yukawad8636ea2014-09-02 22:03:30 -0700195 final int KNOWN_FLAGS_MASK = InputConnection.CURSOR_UPDATE_IMMEDIATE |
196 InputConnection.CURSOR_UPDATE_MONITOR;
Yohei Yukawa72d35fa2014-08-28 23:57:11 -0700197 final int unknownFlags = cursorUpdateMode & ~KNOWN_FLAGS_MASK;
198 if (unknownFlags != 0) {
199 if (DEBUG) {
200 Log.d(TAG, "Rejecting requestUpdateCursorAnchorInfo due to unknown flags." +
201 " cursorUpdateMode=" + cursorUpdateMode +
202 " unknownFlags=" + unknownFlags);
203 }
204 return false;
205 }
206
Yohei Yukawaff328ae2014-07-18 04:22:39 +0900207 if (mIMM == null) {
208 // In this case, TYPE_CURSOR_ANCHOR_INFO is not handled.
Yohei Yukawaa277db22014-08-21 18:38:44 -0700209 // TODO: Return some notification code rather than false to indicate method that
Yohei Yukawaff328ae2014-07-18 04:22:39 +0900210 // CursorAnchorInfo is temporarily unavailable.
Yohei Yukawaa277db22014-08-21 18:38:44 -0700211 return false;
Yohei Yukawaff328ae2014-07-18 04:22:39 +0900212 }
Yohei Yukawaa277db22014-08-21 18:38:44 -0700213 mIMM.setUpdateCursorAnchorInfoMode(cursorUpdateMode);
Yohei Yukawad8636ea2014-09-02 22:03:30 -0700214 if ((cursorUpdateMode & InputConnection.CURSOR_UPDATE_IMMEDIATE) != 0) {
Yohei Yukawaff328ae2014-07-18 04:22:39 +0900215 if (mTextView == null) {
216 // In this case, FLAG_CURSOR_ANCHOR_INFO_IMMEDIATE is silently ignored.
217 // TODO: Return some notification code for the input method that indicates
218 // FLAG_CURSOR_ANCHOR_INFO_IMMEDIATE is ignored.
219 } else if (mTextView.isInLayout()) {
220 // In this case, the view hierarchy is currently undergoing a layout pass.
221 // IMM#updateCursorAnchorInfo is supposed to be called soon after the layout
222 // pass is finished.
223 } else {
224 // This will schedule a layout pass of the view tree, and the layout event
225 // eventually triggers IMM#updateCursorAnchorInfo.
226 mTextView.requestLayout();
227 }
228 }
Yohei Yukawaa277db22014-08-21 18:38:44 -0700229 return true;
Yohei Yukawa0023d0e2014-07-11 04:13:03 +0900230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231}