InputConnectionWrapper.java revision 1d1c21c9cb7911d74fe1859ba9f03fcd2edab262
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 android.view.inputmethod;
18
19import android.os.Bundle;
20import android.os.Handler;
21import android.view.KeyEvent;
22
23/**
24 * <p>Wrapper class for proxying calls to another InputConnection.  Subclass and have fun!
25 */
26public class InputConnectionWrapper implements InputConnection {
27    private InputConnection mTarget;
28    final boolean mMutable;
29
30    /**
31     * Initializes a wrapper.
32     *
33     * <p><b>Caveat:</b> Although the system can accept {@code (InputConnection) null} in some
34     * places, you cannot emulate such a behavior by non-null {@link InputConnectionWrapper} that
35     * has {@code null} in {@code target}.</p>
36     * @param target the {@link InputConnection} to be proxied.
37     * @param mutable set {@code true} to protect this object from being reconfigured to target
38     * another {@link InputConnection}.  Note that this is ignored while the target is {@code null}.
39     */
40    public InputConnectionWrapper(InputConnection target, boolean mutable) {
41        mMutable = mutable;
42        mTarget = target;
43    }
44
45    /**
46     * Change the target of the input connection.
47     *
48     * <p><b>Caveat:</b> Although the system can accept {@code (InputConnection) null} in some
49     * places, you cannot emulate such a behavior by non-null {@link InputConnectionWrapper} that
50     * has {@code null} in {@code target}.</p>
51     * @param target the {@link InputConnection} to be proxied.
52     * @throws SecurityException when this wrapper has non-null target and is immutable.
53     */
54    public void setTarget(InputConnection target) {
55        if (mTarget != null && !mMutable) {
56            throw new SecurityException("not mutable");
57        }
58        mTarget = target;
59    }
60
61    /**
62     * {@inheritDoc}
63     * @throws NullPointerException if the target is {@code null}.
64     */
65    public CharSequence getTextBeforeCursor(int n, int flags) {
66        return mTarget.getTextBeforeCursor(n, flags);
67    }
68
69    /**
70     * {@inheritDoc}
71     * @throws NullPointerException if the target is {@code null}.
72     */
73    public CharSequence getTextAfterCursor(int n, int flags) {
74        return mTarget.getTextAfterCursor(n, flags);
75    }
76
77    /**
78     * {@inheritDoc}
79     * @throws NullPointerException if the target is {@code null}.
80     */
81    public CharSequence getSelectedText(int flags) {
82        return mTarget.getSelectedText(flags);
83    }
84
85    /**
86     * {@inheritDoc}
87     * @throws NullPointerException if the target is {@code null}.
88     */
89    public int getCursorCapsMode(int reqModes) {
90        return mTarget.getCursorCapsMode(reqModes);
91    }
92
93    /**
94     * {@inheritDoc}
95     * @throws NullPointerException if the target is {@code null}.
96     */
97    public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
98        return mTarget.getExtractedText(request, flags);
99    }
100
101    /**
102     * {@inheritDoc}
103     * @throws NullPointerException if the target is {@code null}.
104     */
105    public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) {
106        return mTarget.deleteSurroundingTextInCodePoints(beforeLength, afterLength);
107    }
108
109    /**
110     * {@inheritDoc}
111     * @throws NullPointerException if the target is {@code null}.
112     */
113    public boolean deleteSurroundingText(int beforeLength, int afterLength) {
114        return mTarget.deleteSurroundingText(beforeLength, afterLength);
115    }
116
117    /**
118     * {@inheritDoc}
119     * @throws NullPointerException if the target is {@code null}.
120     */
121    public boolean setComposingText(CharSequence text, int newCursorPosition) {
122        return mTarget.setComposingText(text, newCursorPosition);
123    }
124
125    /**
126     * {@inheritDoc}
127     * @throws NullPointerException if the target is {@code null}.
128     */
129    public boolean setComposingRegion(int start, int end) {
130        return mTarget.setComposingRegion(start, end);
131    }
132
133    /**
134     * {@inheritDoc}
135     * @throws NullPointerException if the target is {@code null}.
136     */
137    public boolean finishComposingText() {
138        return mTarget.finishComposingText();
139    }
140
141    /**
142     * {@inheritDoc}
143     * @throws NullPointerException if the target is {@code null}.
144     */
145    public boolean commitText(CharSequence text, int newCursorPosition) {
146        return mTarget.commitText(text, newCursorPosition);
147    }
148
149    /**
150     * {@inheritDoc}
151     * @throws NullPointerException if the target is {@code null}.
152     */
153    public boolean commitCompletion(CompletionInfo text) {
154        return mTarget.commitCompletion(text);
155    }
156
157    /**
158     * {@inheritDoc}
159     * @throws NullPointerException if the target is {@code null}.
160     */
161    public boolean commitCorrection(CorrectionInfo correctionInfo) {
162        return mTarget.commitCorrection(correctionInfo);
163    }
164
165    /**
166     * {@inheritDoc}
167     * @throws NullPointerException if the target is {@code null}.
168     */
169    public boolean setSelection(int start, int end) {
170        return mTarget.setSelection(start, end);
171    }
172
173    /**
174     * {@inheritDoc}
175     * @throws NullPointerException if the target is {@code null}.
176     */
177    public boolean performEditorAction(int editorAction) {
178        return mTarget.performEditorAction(editorAction);
179    }
180
181    /**
182     * {@inheritDoc}
183     * @throws NullPointerException if the target is {@code null}.
184     */
185    public boolean performContextMenuAction(int id) {
186        return mTarget.performContextMenuAction(id);
187    }
188
189    /**
190     * {@inheritDoc}
191     * @throws NullPointerException if the target is {@code null}.
192     */
193    public boolean beginBatchEdit() {
194        return mTarget.beginBatchEdit();
195    }
196
197    /**
198     * {@inheritDoc}
199     * @throws NullPointerException if the target is {@code null}.
200     */
201    public boolean endBatchEdit() {
202        return mTarget.endBatchEdit();
203    }
204
205    /**
206     * {@inheritDoc}
207     * @throws NullPointerException if the target is {@code null}.
208     */
209    public boolean sendKeyEvent(KeyEvent event) {
210        return mTarget.sendKeyEvent(event);
211    }
212
213    /**
214     * {@inheritDoc}
215     * @throws NullPointerException if the target is {@code null}.
216     */
217    public boolean clearMetaKeyStates(int states) {
218        return mTarget.clearMetaKeyStates(states);
219    }
220
221    /**
222     * {@inheritDoc}
223     * @throws NullPointerException if the target is {@code null}.
224     */
225    public boolean reportFullscreenMode(boolean enabled) {
226        return mTarget.reportFullscreenMode(enabled);
227    }
228
229    /**
230     * {@inheritDoc}
231     * @throws NullPointerException if the target is {@code null}.
232     */
233    public boolean performPrivateCommand(String action, Bundle data) {
234        return mTarget.performPrivateCommand(action, data);
235    }
236
237    /**
238     * {@inheritDoc}
239     * @throws NullPointerException if the target is {@code null}.
240     */
241    public boolean requestCursorUpdates(int cursorUpdateMode) {
242        return mTarget.requestCursorUpdates(cursorUpdateMode);
243    }
244
245    /**
246     * {@inheritDoc}
247     * @throws NullPointerException if the target is {@code null}.
248     */
249    public Handler getHandler() {
250        return mTarget.getHandler();
251    }
252}
253