EditableInputConnection.java revision adb435835fb9a5f2bb74d29930b239dde18504a7
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
17package com.android.internal.widget;
18
19import android.os.Bundle;
20import android.os.IBinder;
21import android.text.Editable;
22import android.text.method.KeyListener;
23import android.text.style.CorrectionSpan;
24import android.util.Log;
25import android.view.inputmethod.BaseInputConnection;
26import android.view.inputmethod.CompletionInfo;
27import android.view.inputmethod.CorrectionInfo;
28import android.view.inputmethod.ExtractedText;
29import android.view.inputmethod.ExtractedTextRequest;
30import android.widget.TextView;
31
32public class EditableInputConnection extends BaseInputConnection {
33    private static final boolean DEBUG = false;
34    private static final String TAG = "EditableInputConnection";
35
36    private final TextView mTextView;
37
38    public EditableInputConnection(TextView textview) {
39        super(textview, true);
40        mTextView = textview;
41    }
42
43    @Override
44    public Editable getEditable() {
45        TextView tv = mTextView;
46        if (tv != null) {
47            return tv.getEditableText();
48        }
49        return null;
50    }
51
52    @Override
53    public boolean beginBatchEdit() {
54        mTextView.beginBatchEdit();
55        return true;
56    }
57
58    @Override
59    public boolean endBatchEdit() {
60        mTextView.endBatchEdit();
61        return true;
62    }
63
64    @Override
65    public boolean clearMetaKeyStates(int states) {
66        final Editable content = getEditable();
67        if (content == null) return false;
68        KeyListener kl = mTextView.getKeyListener();
69        if (kl != null) {
70            try {
71                kl.clearMetaKeyState(mTextView, content, states);
72            } catch (AbstractMethodError e) {
73                // This is an old listener that doesn't implement the
74                // new method.
75            }
76        }
77        return true;
78    }
79
80    @Override
81    public boolean commitCompletion(CompletionInfo text) {
82        if (DEBUG) Log.v(TAG, "commitCompletion " + text);
83        mTextView.beginBatchEdit();
84        mTextView.onCommitCompletion(text);
85        mTextView.endBatchEdit();
86        return true;
87    }
88
89    /**
90     * Calls the {@link TextView#onCommitCorrection} method of the associated TextView.
91     */
92    @Override
93    public boolean commitCorrection(CorrectionInfo correctionInfo) {
94        if (DEBUG) Log.v(TAG, "commitCorrection" + correctionInfo);
95        mTextView.beginBatchEdit();
96        mTextView.onCommitCorrection(correctionInfo);
97        mTextView.endBatchEdit();
98        return true;
99    }
100
101    @Override
102    public boolean performEditorAction(int actionCode) {
103        if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode);
104        mTextView.onEditorAction(actionCode);
105        return true;
106    }
107
108    @Override
109    public boolean performContextMenuAction(int id) {
110        if (DEBUG) Log.v(TAG, "performContextMenuAction " + id);
111        mTextView.beginBatchEdit();
112        mTextView.onTextContextMenuItem(id);
113        mTextView.endBatchEdit();
114        return true;
115    }
116
117    @Override
118    public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
119        if (mTextView != null) {
120            ExtractedText et = new ExtractedText();
121            if (mTextView.extractText(request, et)) {
122                if ((flags&GET_EXTRACTED_TEXT_MONITOR) != 0) {
123                    mTextView.setExtracting(request);
124                }
125                return et;
126            }
127        }
128        return null;
129    }
130
131    @Override
132    public boolean performPrivateCommand(String action, Bundle data) {
133        mTextView.onPrivateIMECommand(action, data);
134        return true;
135    }
136
137    @Override
138    public boolean commitText(CharSequence text, int newCursorPosition) {
139        if (mTextView == null) {
140            return super.commitText(text, newCursorPosition);
141        }
142
143        mTextView.resetErrorChangedFlag();
144        boolean success = super.commitText(text, newCursorPosition);
145        mTextView.hideErrorIfUnchanged();
146
147        return success;
148    }
149
150    @Override
151    public boolean setCorrectionSpan(IBinder token, CorrectionSpan correctionSpan, int start,
152            int end, int flags) {
153        mTextView.beginBatchEdit();
154        boolean retval = mTextView.setCorrectionSpan(token, correctionSpan, start, end, flags);
155        mTextView.endBatchEdit();
156        return retval;
157    }
158}
159