2 * Copyright (C) 2007-2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.internal.widget;
19 import android.os.Bundle;
20 import android.text.Editable;
21 import android.text.method.KeyListener;
22 import android.util.Log;
23 import android.view.inputmethod.BaseInputConnection;
24 import android.view.inputmethod.CompletionInfo;
25 import android.view.inputmethod.ExtractedText;
26 import android.view.inputmethod.ExtractedTextRequest;
27 import android.widget.TextView;
29 public class EditableInputConnection extends BaseInputConnection {
30 private static final boolean DEBUG = false;
31 private static final String TAG = "EditableInputConnection";
33 private final TextView mTextView;
35 public EditableInputConnection(TextView textview) {
36 super(textview, true);
40 public Editable getEditable() {
41 TextView tv = mTextView;
43 return tv.getEditableText();
48 public boolean beginBatchEdit() {
49 mTextView.beginBatchEdit();
53 public boolean endBatchEdit() {
54 mTextView.endBatchEdit();
58 public boolean clearMetaKeyStates(int states) {
59 final Editable content = getEditable();
60 if (content == null) return false;
61 KeyListener kl = mTextView.getKeyListener();
64 kl.clearMetaKeyState(mTextView, content, states);
65 } catch (AbstractMethodError e) {
66 // This is an old listener that doesn't implement the
73 public boolean commitCompletion(CompletionInfo text) {
74 if (DEBUG) Log.v(TAG, "commitCompletion " + text);
75 mTextView.beginBatchEdit();
76 mTextView.onCommitCompletion(text);
77 mTextView.endBatchEdit();
81 public boolean performEditorAction(int actionCode) {
82 if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode);
83 mTextView.onEditorAction(actionCode);
87 public boolean performContextMenuAction(int id) {
88 if (DEBUG) Log.v(TAG, "performContextMenuAction " + id);
89 mTextView.beginBatchEdit();
90 mTextView.onTextContextMenuItem(id);
91 mTextView.endBatchEdit();
95 public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
96 if (mTextView != null) {
97 ExtractedText et = new ExtractedText();
98 if (mTextView.extractText(request, et)) {
99 if ((flags&GET_EXTRACTED_TEXT_MONITOR) != 0) {
100 mTextView.setExtracting(request);
108 public boolean performPrivateCommand(String action, Bundle data) {
109 mTextView.onPrivateIMECommand(action, data);
114 public boolean commitText(CharSequence text, int newCursorPosition) {
115 if (mTextView == null) {
116 return super.commitText(text, newCursorPosition);
119 CharSequence errorBefore = mTextView.getError();
120 boolean success = super.commitText(text, newCursorPosition);
121 CharSequence errorAfter = mTextView.getError();
123 if (errorAfter != null && errorBefore == errorAfter) {
124 mTextView.setError(null, null);