blob: 0d32d4baa1ea8d68f684067eca8c4242d9ba6b71 [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;
satokadb43582011-03-09 10:08:47 +090020import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.text.Editable;
22import android.text.method.KeyListener;
23import android.util.Log;
24import android.view.inputmethod.BaseInputConnection;
25import android.view.inputmethod.CompletionInfo;
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080026import android.view.inputmethod.CorrectionInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.view.inputmethod.ExtractedText;
28import android.view.inputmethod.ExtractedTextRequest;
29import android.widget.TextView;
30
31public class EditableInputConnection extends BaseInputConnection {
32 private static final boolean DEBUG = false;
33 private static final String TAG = "EditableInputConnection";
34
35 private final TextView mTextView;
36
37 public EditableInputConnection(TextView textview) {
Dianne Hackborn51bf0772009-03-24 19:11:41 -070038 super(textview, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 mTextView = textview;
40 }
41
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080042 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 public Editable getEditable() {
44 TextView tv = mTextView;
45 if (tv != null) {
46 return tv.getEditableText();
47 }
48 return null;
49 }
50
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080051 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 public boolean beginBatchEdit() {
53 mTextView.beginBatchEdit();
54 return true;
55 }
56
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080057 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 public boolean endBatchEdit() {
59 mTextView.endBatchEdit();
60 return true;
61 }
62
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080063 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 public boolean clearMetaKeyStates(int states) {
65 final Editable content = getEditable();
66 if (content == null) return false;
67 KeyListener kl = mTextView.getKeyListener();
68 if (kl != null) {
69 try {
70 kl.clearMetaKeyState(mTextView, content, states);
71 } catch (AbstractMethodError e) {
72 // This is an old listener that doesn't implement the
73 // new method.
74 }
75 }
76 return true;
77 }
78
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080079 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 public boolean commitCompletion(CompletionInfo text) {
81 if (DEBUG) Log.v(TAG, "commitCompletion " + text);
82 mTextView.beginBatchEdit();
83 mTextView.onCommitCompletion(text);
84 mTextView.endBatchEdit();
85 return true;
86 }
87
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080088 /**
89 * Calls the {@link TextView#onCommitCorrection} method of the associated TextView.
90 */
91 @Override
92 public boolean commitCorrection(CorrectionInfo correctionInfo) {
93 if (DEBUG) Log.v(TAG, "commitCorrection" + correctionInfo);
94 mTextView.beginBatchEdit();
95 mTextView.onCommitCorrection(correctionInfo);
96 mTextView.endBatchEdit();
97 return true;
98 }
99
100 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 public boolean performEditorAction(int actionCode) {
102 if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode);
103 mTextView.onEditorAction(actionCode);
104 return true;
105 }
106
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -0800107 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 public boolean performContextMenuAction(int id) {
109 if (DEBUG) Log.v(TAG, "performContextMenuAction " + id);
110 mTextView.beginBatchEdit();
111 mTextView.onTextContextMenuItem(id);
112 mTextView.endBatchEdit();
113 return true;
114 }
115
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -0800116 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
118 if (mTextView != null) {
119 ExtractedText et = new ExtractedText();
120 if (mTextView.extractText(request, et)) {
121 if ((flags&GET_EXTRACTED_TEXT_MONITOR) != 0) {
122 mTextView.setExtracting(request);
123 }
124 return et;
125 }
126 }
127 return null;
128 }
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -0800129
130 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 public boolean performPrivateCommand(String action, Bundle data) {
132 mTextView.onPrivateIMECommand(action, data);
133 return true;
134 }
135
136 @Override
137 public boolean commitText(CharSequence text, int newCursorPosition) {
138 if (mTextView == null) {
139 return super.commitText(text, newCursorPosition);
140 }
141
Gilles Debunneb7fc63f2011-01-27 15:55:29 -0800142 mTextView.resetErrorChangedFlag();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 boolean success = super.commitText(text, newCursorPosition);
Gilles Debunneb7fc63f2011-01-27 15:55:29 -0800144 mTextView.hideErrorIfUnchanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 return success;
147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148}