The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 25 | import android.view.inputmethod.CorrectionInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.view.inputmethod.ExtractedText; |
| 27 | import android.view.inputmethod.ExtractedTextRequest; |
| 28 | import android.widget.TextView; |
| 29 | |
| 30 | public 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 Hackborn | 51bf077 | 2009-03-24 19:11:41 -0700 | [diff] [blame] | 37 | super(textview, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | mTextView = textview; |
| 39 | } |
| 40 | |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 41 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | public Editable getEditable() { |
| 43 | TextView tv = mTextView; |
| 44 | if (tv != null) { |
| 45 | return tv.getEditableText(); |
| 46 | } |
| 47 | return null; |
| 48 | } |
| 49 | |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 50 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | public boolean beginBatchEdit() { |
| 52 | mTextView.beginBatchEdit(); |
| 53 | return true; |
| 54 | } |
| 55 | |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 56 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | public boolean endBatchEdit() { |
| 58 | mTextView.endBatchEdit(); |
| 59 | return true; |
| 60 | } |
| 61 | |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 62 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | 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 Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 78 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | 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 Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 87 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | public boolean performEditorAction(int actionCode) { |
| 101 | if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode); |
| 102 | mTextView.onEditorAction(actionCode); |
| 103 | return true; |
| 104 | } |
| 105 | |
Gilles Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 106 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | 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 Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 115 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | 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 Debunne | cf9cf2f | 2010-12-08 17:43:58 -0800 | [diff] [blame] | 128 | |
| 129 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | 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 Debunne | b7fc63f | 2011-01-27 15:55:29 -0800 | [diff] [blame] | 141 | mTextView.resetErrorChangedFlag(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | boolean success = super.commitText(text, newCursorPosition); |
Gilles Debunne | b7fc63f | 2011-01-27 15:55:29 -0800 | [diff] [blame] | 143 | mTextView.hideErrorIfUnchanged(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | return success; |
| 146 | } |
| 147 | } |