OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / core / java / com / android / internal / widget / EditableInputConnection.java
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;
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;
28
29 public class EditableInputConnection extends BaseInputConnection {
30     private static final boolean DEBUG = false;
31     private static final String TAG = "EditableInputConnection";
32
33     private final TextView mTextView;
34
35     public EditableInputConnection(TextView textview) {
36         super(textview, true);
37         mTextView = textview;
38     }
39
40     public Editable getEditable() {
41         TextView tv = mTextView;
42         if (tv != null) {
43             return tv.getEditableText();
44         }
45         return null;
46     }
47     
48     public boolean beginBatchEdit() {
49         mTextView.beginBatchEdit();
50         return true;
51     }
52     
53     public boolean endBatchEdit() {
54         mTextView.endBatchEdit();
55         return true;
56     }
57     
58     public boolean clearMetaKeyStates(int states) {
59         final Editable content = getEditable();
60         if (content == null) return false;
61         KeyListener kl = mTextView.getKeyListener();
62         if (kl != null) {
63             try {
64                 kl.clearMetaKeyState(mTextView, content, states);
65             } catch (AbstractMethodError e) {
66                 // This is an old listener that doesn't implement the
67                 // new method.
68             }
69         }
70         return true;
71     }
72     
73     public boolean commitCompletion(CompletionInfo text) {
74         if (DEBUG) Log.v(TAG, "commitCompletion " + text);
75         mTextView.beginBatchEdit();
76         mTextView.onCommitCompletion(text);
77         mTextView.endBatchEdit();
78         return true;
79     }
80
81     public boolean performEditorAction(int actionCode) {
82         if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode);
83         mTextView.onEditorAction(actionCode);
84         return true;
85     }
86     
87     public boolean performContextMenuAction(int id) {
88         if (DEBUG) Log.v(TAG, "performContextMenuAction " + id);
89         mTextView.beginBatchEdit();
90         mTextView.onTextContextMenuItem(id);
91         mTextView.endBatchEdit();
92         return true;
93     }
94     
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);
101                 }
102                 return et;
103             }
104         }
105         return null;
106     }
107     
108     public boolean performPrivateCommand(String action, Bundle data) {
109         mTextView.onPrivateIMECommand(action, data);
110         return true;
111     }
112
113     @Override
114     public boolean commitText(CharSequence text, int newCursorPosition) {
115         if (mTextView == null) {
116             return super.commitText(text, newCursorPosition);
117         }
118
119         CharSequence errorBefore = mTextView.getError();
120         boolean success = super.commitText(text, newCursorPosition);
121         CharSequence errorAfter = mTextView.getError();
122
123         if (errorAfter != null && errorBefore == errorAfter) {
124             mTextView.setError(null, null);
125         }
126
127         return success;
128     }
129 }