blob: 9f9f020b70d49a2d83bcd56b456da4cac051470e [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;
21import android.text.method.KeyListener;
22import android.util.Log;
23import android.view.inputmethod.BaseInputConnection;
24import android.view.inputmethod.CompletionInfo;
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080025import android.view.inputmethod.CorrectionInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.view.inputmethod.ExtractedText;
27import android.view.inputmethod.ExtractedTextRequest;
28import android.widget.TextView;
29
30public class EditableInputConnection extends BaseInputConnection {
31 private static final boolean DEBUG = false;
32 private static final String TAG = "EditableInputConnection";
33
34 private final TextView mTextView;
35
36 public EditableInputConnection(TextView textview) {
Dianne Hackborn51bf0772009-03-24 19:11:41 -070037 super(textview, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 mTextView = textview;
39 }
40
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080041 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 public Editable getEditable() {
43 TextView tv = mTextView;
44 if (tv != null) {
45 return tv.getEditableText();
46 }
47 return null;
48 }
49
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080050 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 public boolean beginBatchEdit() {
52 mTextView.beginBatchEdit();
53 return true;
54 }
55
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080056 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 public boolean endBatchEdit() {
58 mTextView.endBatchEdit();
59 return true;
60 }
61
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080062 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 public boolean clearMetaKeyStates(int states) {
64 final Editable content = getEditable();
65 if (content == null) return false;
66 KeyListener kl = mTextView.getKeyListener();
67 if (kl != null) {
68 try {
69 kl.clearMetaKeyState(mTextView, content, states);
70 } catch (AbstractMethodError e) {
71 // This is an old listener that doesn't implement the
72 // new method.
73 }
74 }
75 return true;
76 }
77
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080078 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 public boolean commitCompletion(CompletionInfo text) {
80 if (DEBUG) Log.v(TAG, "commitCompletion " + text);
81 mTextView.beginBatchEdit();
82 mTextView.onCommitCompletion(text);
83 mTextView.endBatchEdit();
84 return true;
85 }
86
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080087 /**
88 * Calls the {@link TextView#onCommitCorrection} method of the associated TextView.
89 */
90 @Override
91 public boolean commitCorrection(CorrectionInfo correctionInfo) {
92 if (DEBUG) Log.v(TAG, "commitCorrection" + correctionInfo);
93 mTextView.beginBatchEdit();
94 mTextView.onCommitCorrection(correctionInfo);
95 mTextView.endBatchEdit();
96 return true;
97 }
98
99 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 public boolean performEditorAction(int actionCode) {
101 if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode);
102 mTextView.onEditorAction(actionCode);
103 return true;
104 }
105
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -0800106 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 public boolean performContextMenuAction(int id) {
108 if (DEBUG) Log.v(TAG, "performContextMenuAction " + id);
109 mTextView.beginBatchEdit();
110 mTextView.onTextContextMenuItem(id);
111 mTextView.endBatchEdit();
112 return true;
113 }
114
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -0800115 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
117 if (mTextView != null) {
118 ExtractedText et = new ExtractedText();
119 if (mTextView.extractText(request, et)) {
120 if ((flags&GET_EXTRACTED_TEXT_MONITOR) != 0) {
121 mTextView.setExtracting(request);
122 }
123 return et;
124 }
125 }
126 return null;
127 }
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -0800128
129 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 public boolean performPrivateCommand(String action, Bundle data) {
131 mTextView.onPrivateIMECommand(action, data);
132 return true;
133 }
134
135 @Override
136 public boolean commitText(CharSequence text, int newCursorPosition) {
137 if (mTextView == null) {
138 return super.commitText(text, newCursorPosition);
139 }
140
Gilles Debunneb7fc63f2011-01-27 15:55:29 -0800141 mTextView.resetErrorChangedFlag();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 boolean success = super.commitText(text, newCursorPosition);
Gilles Debunneb7fc63f2011-01-27 15:55:29 -0800143 mTextView.hideErrorIfUnchanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 return success;
146 }
147}