The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.widget; |
| 18 | |
| 19 | import android.os.Bundle; |
| 20 | import android.text.Editable; |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 21 | import android.text.Spanned; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.text.method.KeyListener; |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 23 | import android.text.style.SuggestionSpan; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.util.Log; |
| 25 | import android.view.inputmethod.BaseInputConnection; |
| 26 | import android.view.inputmethod.CompletionInfo; |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 27 | import android.view.inputmethod.CorrectionInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.view.inputmethod.ExtractedText; |
| 29 | import android.view.inputmethod.ExtractedTextRequest; |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 30 | import android.view.inputmethod.InputConnection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.widget.TextView; |
| 32 | |
| 33 | public 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 Debunne | c478c17 | 2011-12-19 17:29:24 -0800 | [diff] [blame] | 39 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | public EditableInputConnection(TextView textview) { |
Dianne Hackborn | 51bf077 | 2009-03-24 19:11:41 -0700 | [diff] [blame] | 45 | super(textview, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | mTextView = textview; |
| 47 | } |
| 48 | |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 49 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | public Editable getEditable() { |
| 51 | TextView tv = mTextView; |
| 52 | if (tv != null) { |
| 53 | return tv.getEditableText(); |
| 54 | } |
| 55 | return null; |
| 56 | } |
Gilles Debunne | c478c17 | 2011-12-19 17:29:24 -0800 | [diff] [blame] | 57 | |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 58 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | public boolean beginBatchEdit() { |
Gilles Debunne | c478c17 | 2011-12-19 17:29:24 -0800 | [diff] [blame] | 60 | synchronized(this) { |
| 61 | if (mBatchEditNesting >= 0) { |
| 62 | mTextView.beginBatchEdit(); |
| 63 | mBatchEditNesting++; |
| 64 | return true; |
| 65 | } |
| 66 | } |
| 67 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | } |
Gilles Debunne | c478c17 | 2011-12-19 17:29:24 -0800 | [diff] [blame] | 69 | |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 70 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | public boolean endBatchEdit() { |
Gilles Debunne | c478c17 | 2011-12-19 17:29:24 -0800 | [diff] [blame] | 72 | synchronized(this) { |
| 73 | if (mBatchEditNesting > 0) { |
Gilles Debunne | 9d69ecb | 2012-02-24 16:07:09 -0800 | [diff] [blame] | 74 | // When the connection is reset by the InputMethodManager and reportFinish |
Gilles Debunne | c478c17 | 2011-12-19 17:29:24 -0800 | [diff] [blame] | 75 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | } |
Gilles Debunne | c478c17 | 2011-12-19 17:29:24 -0800 | [diff] [blame] | 85 | |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 86 | /** |
| 87 | * @hide |
| 88 | */ |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 89 | @Override |
Yohei Yukawa | aaa38c9 | 2016-03-27 23:46:04 -0700 | [diff] [blame] | 90 | public void reportFinish() { |
Gilles Debunne | 9d69ecb | 2012-02-24 16:07:09 -0800 | [diff] [blame] | 91 | super.reportFinish(); |
| 92 | |
| 93 | synchronized(this) { |
| 94 | while (mBatchEditNesting > 0) { |
| 95 | endBatchEdit(); |
| 96 | } |
| 97 | // Will prevent any further calls to begin or endBatchEdit |
| 98 | mBatchEditNesting = -1; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | public boolean clearMetaKeyStates(int states) { |
| 104 | final Editable content = getEditable(); |
| 105 | if (content == null) return false; |
| 106 | KeyListener kl = mTextView.getKeyListener(); |
| 107 | if (kl != null) { |
| 108 | try { |
| 109 | kl.clearMetaKeyState(mTextView, content, states); |
| 110 | } catch (AbstractMethodError e) { |
| 111 | // This is an old listener that doesn't implement the |
| 112 | // new method. |
| 113 | } |
| 114 | } |
| 115 | return true; |
| 116 | } |
Gilles Debunne | c478c17 | 2011-12-19 17:29:24 -0800 | [diff] [blame] | 117 | |
| 118 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | public boolean commitCompletion(CompletionInfo text) { |
| 120 | if (DEBUG) Log.v(TAG, "commitCompletion " + text); |
| 121 | mTextView.beginBatchEdit(); |
| 122 | mTextView.onCommitCompletion(text); |
| 123 | mTextView.endBatchEdit(); |
| 124 | return true; |
| 125 | } |
| 126 | |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 127 | /** |
| 128 | * Calls the {@link TextView#onCommitCorrection} method of the associated TextView. |
| 129 | */ |
| 130 | @Override |
| 131 | public boolean commitCorrection(CorrectionInfo correctionInfo) { |
| 132 | if (DEBUG) Log.v(TAG, "commitCorrection" + correctionInfo); |
| 133 | mTextView.beginBatchEdit(); |
| 134 | mTextView.onCommitCorrection(correctionInfo); |
| 135 | mTextView.endBatchEdit(); |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | public boolean performEditorAction(int actionCode) { |
| 141 | if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode); |
| 142 | mTextView.onEditorAction(actionCode); |
| 143 | return true; |
| 144 | } |
| 145 | |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 146 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | public boolean performContextMenuAction(int id) { |
| 148 | if (DEBUG) Log.v(TAG, "performContextMenuAction " + id); |
| 149 | mTextView.beginBatchEdit(); |
| 150 | mTextView.onTextContextMenuItem(id); |
| 151 | mTextView.endBatchEdit(); |
| 152 | return true; |
| 153 | } |
| 154 | |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 155 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) { |
| 157 | if (mTextView != null) { |
| 158 | ExtractedText et = new ExtractedText(); |
| 159 | if (mTextView.extractText(request, et)) { |
| 160 | if ((flags&GET_EXTRACTED_TEXT_MONITOR) != 0) { |
| 161 | mTextView.setExtracting(request); |
| 162 | } |
| 163 | return et; |
| 164 | } |
| 165 | } |
| 166 | return null; |
| 167 | } |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 168 | |
| 169 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | public boolean performPrivateCommand(String action, Bundle data) { |
| 171 | mTextView.onPrivateIMECommand(action, data); |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public boolean commitText(CharSequence text, int newCursorPosition) { |
| 177 | if (mTextView == null) { |
| 178 | return super.commitText(text, newCursorPosition); |
| 179 | } |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 180 | if (text instanceof Spanned) { |
| 181 | Spanned spanned = ((Spanned) text); |
| 182 | SuggestionSpan[] spans = spanned.getSpans(0, text.length(), SuggestionSpan.class); |
| 183 | mIMM.registerSuggestionSpansForNotification(spans); |
| 184 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | |
Gilles Debunne | b7fc63f | 2011-01-27 15:55:29 -0800 | [diff] [blame] | 186 | mTextView.resetErrorChangedFlag(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | boolean success = super.commitText(text, newCursorPosition); |
Gilles Debunne | b7fc63f | 2011-01-27 15:55:29 -0800 | [diff] [blame] | 188 | mTextView.hideErrorIfUnchanged(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | return success; |
| 191 | } |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 192 | |
| 193 | @Override |
Yohei Yukawa | d8636ea | 2014-09-02 22:03:30 -0700 | [diff] [blame] | 194 | public boolean requestCursorUpdates(int cursorUpdateMode) { |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 195 | if (DEBUG) Log.v(TAG, "requestUpdateCursorAnchorInfo " + cursorUpdateMode); |
Yohei Yukawa | ff328ae | 2014-07-18 04:22:39 +0900 | [diff] [blame] | 196 | |
Yohei Yukawa | 72d35fa | 2014-08-28 23:57:11 -0700 | [diff] [blame] | 197 | // It is possible that any other bit is used as a valid flag in a future release. |
| 198 | // We should reject the entire request in such a case. |
Yohei Yukawa | d8636ea | 2014-09-02 22:03:30 -0700 | [diff] [blame] | 199 | final int KNOWN_FLAGS_MASK = InputConnection.CURSOR_UPDATE_IMMEDIATE | |
| 200 | InputConnection.CURSOR_UPDATE_MONITOR; |
Yohei Yukawa | 72d35fa | 2014-08-28 23:57:11 -0700 | [diff] [blame] | 201 | final int unknownFlags = cursorUpdateMode & ~KNOWN_FLAGS_MASK; |
| 202 | if (unknownFlags != 0) { |
| 203 | if (DEBUG) { |
| 204 | Log.d(TAG, "Rejecting requestUpdateCursorAnchorInfo due to unknown flags." + |
| 205 | " cursorUpdateMode=" + cursorUpdateMode + |
| 206 | " unknownFlags=" + unknownFlags); |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
Yohei Yukawa | ff328ae | 2014-07-18 04:22:39 +0900 | [diff] [blame] | 211 | if (mIMM == null) { |
| 212 | // In this case, TYPE_CURSOR_ANCHOR_INFO is not handled. |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 213 | // TODO: Return some notification code rather than false to indicate method that |
Yohei Yukawa | ff328ae | 2014-07-18 04:22:39 +0900 | [diff] [blame] | 214 | // CursorAnchorInfo is temporarily unavailable. |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 215 | return false; |
Yohei Yukawa | ff328ae | 2014-07-18 04:22:39 +0900 | [diff] [blame] | 216 | } |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 217 | mIMM.setUpdateCursorAnchorInfoMode(cursorUpdateMode); |
Yohei Yukawa | d8636ea | 2014-09-02 22:03:30 -0700 | [diff] [blame] | 218 | if ((cursorUpdateMode & InputConnection.CURSOR_UPDATE_IMMEDIATE) != 0) { |
Yohei Yukawa | ff328ae | 2014-07-18 04:22:39 +0900 | [diff] [blame] | 219 | if (mTextView == null) { |
| 220 | // In this case, FLAG_CURSOR_ANCHOR_INFO_IMMEDIATE is silently ignored. |
| 221 | // TODO: Return some notification code for the input method that indicates |
| 222 | // FLAG_CURSOR_ANCHOR_INFO_IMMEDIATE is ignored. |
| 223 | } else if (mTextView.isInLayout()) { |
| 224 | // In this case, the view hierarchy is currently undergoing a layout pass. |
| 225 | // IMM#updateCursorAnchorInfo is supposed to be called soon after the layout |
| 226 | // pass is finished. |
| 227 | } else { |
| 228 | // This will schedule a layout pass of the view tree, and the layout event |
| 229 | // eventually triggers IMM#updateCursorAnchorInfo. |
| 230 | mTextView.requestLayout(); |
| 231 | } |
| 232 | } |
Yohei Yukawa | a277db2 | 2014-08-21 18:38:44 -0700 | [diff] [blame] | 233 | return true; |
Yohei Yukawa | 0023d0e | 2014-07-11 04:13:03 +0900 | [diff] [blame] | 234 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | } |