InputConnectionWrapper.java revision e77386e8fbe01b7fbf1a04555d4088796d862f03
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view.inputmethod;
18
19import android.annotation.NonNull;
20import android.os.Bundle;
21import android.os.Handler;
22import android.os.LocaleList;
23import android.view.KeyEvent;
24
25/**
26 * <p>Wrapper class for proxying calls to another InputConnection.  Subclass and have fun!
27 */
28public class InputConnectionWrapper implements InputConnection {
29    private InputConnection mTarget;
30    final boolean mMutable;
31    @InputConnectionInspector.MissingMethodFlags
32    private int mMissingMethodFlags;
33
34    /**
35     * Initializes a wrapper.
36     *
37     * <p><b>Caveat:</b> Although the system can accept {@code (InputConnection) null} in some
38     * places, you cannot emulate such a behavior by non-null {@link InputConnectionWrapper} that
39     * has {@code null} in {@code target}.</p>
40     * @param target the {@link InputConnection} to be proxied.
41     * @param mutable set {@code true} to protect this object from being reconfigured to target
42     * another {@link InputConnection}.  Note that this is ignored while the target is {@code null}.
43     */
44    public InputConnectionWrapper(InputConnection target, boolean mutable) {
45        mMutable = mutable;
46        mTarget = target;
47        mMissingMethodFlags = InputConnectionInspector.getMissingMethodFlags(target);
48    }
49
50    /**
51     * Change the target of the input connection.
52     *
53     * <p><b>Caveat:</b> Although the system can accept {@code (InputConnection) null} in some
54     * places, you cannot emulate such a behavior by non-null {@link InputConnectionWrapper} that
55     * has {@code null} in {@code target}.</p>
56     * @param target the {@link InputConnection} to be proxied.
57     * @throws SecurityException when this wrapper has non-null target and is immutable.
58     */
59    public void setTarget(InputConnection target) {
60        if (mTarget != null && !mMutable) {
61            throw new SecurityException("not mutable");
62        }
63        mTarget = target;
64        mMissingMethodFlags = InputConnectionInspector.getMissingMethodFlags(target);
65    }
66
67    /**
68     * @hide
69     */
70    @InputConnectionInspector.MissingMethodFlags
71    public int getMissingMethodFlags() {
72        return mMissingMethodFlags;
73    }
74
75    /**
76     * {@inheritDoc}
77     * @throws NullPointerException if the target is {@code null}.
78     */
79    @Override
80    public CharSequence getTextBeforeCursor(int n, int flags) {
81        return mTarget.getTextBeforeCursor(n, flags);
82    }
83
84    /**
85     * {@inheritDoc}
86     * @throws NullPointerException if the target is {@code null}.
87     */
88    @Override
89    public CharSequence getTextAfterCursor(int n, int flags) {
90        return mTarget.getTextAfterCursor(n, flags);
91    }
92
93    /**
94     * {@inheritDoc}
95     * @throws NullPointerException if the target is {@code null}.
96     */
97    @Override
98    public CharSequence getSelectedText(int flags) {
99        return mTarget.getSelectedText(flags);
100    }
101
102    /**
103     * {@inheritDoc}
104     * @throws NullPointerException if the target is {@code null}.
105     */
106    @Override
107    public int getCursorCapsMode(int reqModes) {
108        return mTarget.getCursorCapsMode(reqModes);
109    }
110
111    /**
112     * {@inheritDoc}
113     * @throws NullPointerException if the target is {@code null}.
114     */
115    @Override
116    public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
117        return mTarget.getExtractedText(request, flags);
118    }
119
120    /**
121     * {@inheritDoc}
122     * @throws NullPointerException if the target is {@code null}.
123     */
124    @Override
125    public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) {
126        return mTarget.deleteSurroundingTextInCodePoints(beforeLength, afterLength);
127    }
128
129    /**
130     * {@inheritDoc}
131     * @throws NullPointerException if the target is {@code null}.
132     */
133    @Override
134    public boolean deleteSurroundingText(int beforeLength, int afterLength) {
135        return mTarget.deleteSurroundingText(beforeLength, afterLength);
136    }
137
138    /**
139     * {@inheritDoc}
140     * @throws NullPointerException if the target is {@code null}.
141     */
142    @Override
143    public boolean setComposingText(CharSequence text, int newCursorPosition) {
144        return mTarget.setComposingText(text, newCursorPosition);
145    }
146
147    /**
148     * {@inheritDoc}
149     * @throws NullPointerException if the target is {@code null}.
150     */
151    @Override
152    public boolean setComposingRegion(int start, int end) {
153        return mTarget.setComposingRegion(start, end);
154    }
155
156    /**
157     * {@inheritDoc}
158     * @throws NullPointerException if the target is {@code null}.
159     */
160    @Override
161    public boolean finishComposingText() {
162        return mTarget.finishComposingText();
163    }
164
165    /**
166     * {@inheritDoc}
167     * @throws NullPointerException if the target is {@code null}.
168     */
169    @Override
170    public boolean commitText(CharSequence text, int newCursorPosition) {
171        return mTarget.commitText(text, newCursorPosition);
172    }
173
174    /**
175     * {@inheritDoc}
176     * @throws NullPointerException if the target is {@code null}.
177     */
178    @Override
179    public boolean commitCompletion(CompletionInfo text) {
180        return mTarget.commitCompletion(text);
181    }
182
183    /**
184     * {@inheritDoc}
185     * @throws NullPointerException if the target is {@code null}.
186     */
187    @Override
188    public boolean commitCorrection(CorrectionInfo correctionInfo) {
189        return mTarget.commitCorrection(correctionInfo);
190    }
191
192    /**
193     * {@inheritDoc}
194     * @throws NullPointerException if the target is {@code null}.
195     */
196    @Override
197    public boolean setSelection(int start, int end) {
198        return mTarget.setSelection(start, end);
199    }
200
201    /**
202     * {@inheritDoc}
203     * @throws NullPointerException if the target is {@code null}.
204     */
205    @Override
206    public boolean performEditorAction(int editorAction) {
207        return mTarget.performEditorAction(editorAction);
208    }
209
210    /**
211     * {@inheritDoc}
212     * @throws NullPointerException if the target is {@code null}.
213     */
214    @Override
215    public boolean performContextMenuAction(int id) {
216        return mTarget.performContextMenuAction(id);
217    }
218
219    /**
220     * {@inheritDoc}
221     * @throws NullPointerException if the target is {@code null}.
222     */
223    @Override
224    public boolean beginBatchEdit() {
225        return mTarget.beginBatchEdit();
226    }
227
228    /**
229     * {@inheritDoc}
230     * @throws NullPointerException if the target is {@code null}.
231     */
232    @Override
233    public boolean endBatchEdit() {
234        return mTarget.endBatchEdit();
235    }
236
237    /**
238     * {@inheritDoc}
239     * @throws NullPointerException if the target is {@code null}.
240     */
241    @Override
242    public boolean sendKeyEvent(KeyEvent event) {
243        return mTarget.sendKeyEvent(event);
244    }
245
246    /**
247     * {@inheritDoc}
248     * @throws NullPointerException if the target is {@code null}.
249     */
250    @Override
251    public boolean clearMetaKeyStates(int states) {
252        return mTarget.clearMetaKeyStates(states);
253    }
254
255    /**
256     * {@inheritDoc}
257     * @throws NullPointerException if the target is {@code null}.
258     */
259    @Override
260    public boolean reportFullscreenMode(boolean enabled) {
261        return mTarget.reportFullscreenMode(enabled);
262    }
263
264    /**
265     * {@inheritDoc}
266     * @throws NullPointerException if the target is {@code null}.
267     */
268    @Override
269    public boolean performPrivateCommand(String action, Bundle data) {
270        return mTarget.performPrivateCommand(action, data);
271    }
272
273    /**
274     * {@inheritDoc}
275     * @throws NullPointerException if the target is {@code null}.
276     */
277    @Override
278    public boolean requestCursorUpdates(int cursorUpdateMode) {
279        return mTarget.requestCursorUpdates(cursorUpdateMode);
280    }
281
282    /**
283     * {@inheritDoc}
284     * @throws NullPointerException if the target is {@code null}.
285     */
286    @Override
287    public Handler getHandler() {
288        return mTarget.getHandler();
289    }
290
291    /**
292     * {@inheritDoc}
293     * @throws NullPointerException if the target is {@code null}.
294     */
295    @Override
296    public void closeConnection() {
297        mTarget.closeConnection();
298    }
299
300    /**
301     * {@inheritDoc}
302     * @throws NullPointerException if the target is {@code null}.
303     */
304    @Override
305    public boolean commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts) {
306        return mTarget.commitContent(inputContentInfo, flags, opts);
307    }
308
309    /**
310     * {@inheritDoc}
311     * @throws NullPointerException if the target is {@code null}.
312     */
313    @Override
314    public void reportLanguageHint(@NonNull LocaleList languageHint) {
315        mTarget.reportLanguageHint(languageHint);
316    }
317}
318