BaseInputConnection.java revision a465a170ce5d7155580fd308d1e50092365117e4
1/*
2 * Copyright (C) 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.content.Context;
20import android.content.res.TypedArray;
21import android.os.Bundle;
22import android.os.Handler;
23import android.os.SystemClock;
24import android.text.Editable;
25import android.text.NoCopySpan;
26import android.text.Selection;
27import android.text.Spannable;
28import android.text.SpannableStringBuilder;
29import android.text.Spanned;
30import android.text.TextUtils;
31import android.text.method.MetaKeyKeyListener;
32import android.util.Log;
33import android.util.LogPrinter;
34import android.view.KeyCharacterMap;
35import android.view.KeyEvent;
36import android.view.View;
37import android.view.ViewRoot;
38
39class ComposingText implements NoCopySpan {
40}
41
42/**
43 * Base class for implementors of the InputConnection interface, taking care
44 * of most of the common behavior for providing a connection to an Editable.
45 * Implementors of this class will want to be sure to implement
46 * {@link #getEditable} to provide access to their own editable object.
47 */
48public class BaseInputConnection implements InputConnection {
49    private static final boolean DEBUG = false;
50    private static final String TAG = "BaseInputConnection";
51    static final Object COMPOSING = new ComposingText();
52
53    final InputMethodManager mIMM;
54    final View mTargetView;
55    final boolean mDummyMode;
56
57    private Object[] mDefaultComposingSpans;
58
59    Editable mEditable;
60    KeyCharacterMap mKeyCharacterMap;
61
62    BaseInputConnection(InputMethodManager mgr, boolean fullEditor) {
63        mIMM = mgr;
64        mTargetView = null;
65        mDummyMode = !fullEditor;
66    }
67
68    public BaseInputConnection(View targetView, boolean fullEditor) {
69        mIMM = (InputMethodManager)targetView.getContext().getSystemService(
70                Context.INPUT_METHOD_SERVICE);
71        mTargetView = targetView;
72        mDummyMode = !fullEditor;
73    }
74
75    public static final void removeComposingSpans(Spannable text) {
76        text.removeSpan(COMPOSING);
77        Object[] sps = text.getSpans(0, text.length(), Object.class);
78        if (sps != null) {
79            for (int i=sps.length-1; i>=0; i--) {
80                Object o = sps[i];
81                if ((text.getSpanFlags(o)&Spanned.SPAN_COMPOSING) != 0) {
82                    text.removeSpan(o);
83                }
84            }
85        }
86    }
87
88    public static void setComposingSpans(Spannable text) {
89        final Object[] sps = text.getSpans(0, text.length(), Object.class);
90        if (sps != null) {
91            for (int i=sps.length-1; i>=0; i--) {
92                final Object o = sps[i];
93                if (o == COMPOSING) {
94                    text.removeSpan(o);
95                    continue;
96                }
97                final int fl = text.getSpanFlags(o);
98                if ((fl&(Spanned.SPAN_COMPOSING|Spanned.SPAN_POINT_MARK_MASK))
99                        != (Spanned.SPAN_COMPOSING|Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)) {
100                    text.setSpan(o, text.getSpanStart(o), text.getSpanEnd(o),
101                            (fl&Spanned.SPAN_POINT_MARK_MASK)
102                                    | Spanned.SPAN_COMPOSING
103                                    | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
104                }
105            }
106        }
107
108        text.setSpan(COMPOSING, 0, text.length(),
109                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
110    }
111
112    public static int getComposingSpanStart(Spannable text) {
113        return text.getSpanStart(COMPOSING);
114    }
115
116    public static int getComposingSpanEnd(Spannable text) {
117        return text.getSpanEnd(COMPOSING);
118    }
119
120    /**
121     * Return the target of edit operations.  The default implementation
122     * returns its own fake editable that is just used for composing text;
123     * subclasses that are real text editors should override this and
124     * supply their own.
125     */
126    public Editable getEditable() {
127        if (mEditable == null) {
128            mEditable = Editable.Factory.getInstance().newEditable("");
129            Selection.setSelection(mEditable, 0);
130        }
131        return mEditable;
132    }
133
134    /**
135     * Default implementation does nothing.
136     */
137    public boolean beginBatchEdit() {
138        return false;
139    }
140
141    /**
142     * Default implementation does nothing.
143     */
144    public boolean endBatchEdit() {
145        return false;
146    }
147
148    /**
149     * Default implementation uses
150     * {@link MetaKeyKeyListener#clearMetaKeyState(long, int)
151     * MetaKeyKeyListener.clearMetaKeyState(long, int)} to clear the state.
152     */
153    public boolean clearMetaKeyStates(int states) {
154        final Editable content = getEditable();
155        if (content == null) return false;
156        MetaKeyKeyListener.clearMetaKeyState(content, states);
157        return true;
158    }
159
160    /**
161     * Default implementation does nothing.
162     */
163    public boolean commitCompletion(CompletionInfo text) {
164        return false;
165    }
166
167    /**
168     * Default implementation replaces any existing composing text with
169     * the given text.  In addition, only if dummy mode, a key event is
170     * sent for the new text and the current editable buffer cleared.
171     */
172    public boolean commitText(CharSequence text, int newCursorPosition) {
173        if (DEBUG) Log.v(TAG, "commitText " + text);
174        replaceText(text, newCursorPosition, false);
175        sendCurrentText();
176        return true;
177    }
178
179    /**
180     * The default implementation performs the deletion around the current
181     * selection position of the editable text.
182     */
183    public boolean deleteSurroundingText(int leftLength, int rightLength) {
184        if (DEBUG) Log.v(TAG, "deleteSurroundingText " + leftLength
185                + " / " + rightLength);
186        final Editable content = getEditable();
187        if (content == null) return false;
188
189        beginBatchEdit();
190
191        int a = Selection.getSelectionStart(content);
192        int b = Selection.getSelectionEnd(content);
193
194        if (a > b) {
195            int tmp = a;
196            a = b;
197            b = tmp;
198        }
199
200        // ignore the composing text.
201        int ca = getComposingSpanStart(content);
202        int cb = getComposingSpanEnd(content);
203        if (cb < ca) {
204            int tmp = ca;
205            ca = cb;
206            cb = tmp;
207        }
208        if (ca != -1 && cb != -1) {
209            if (ca < a) a = ca;
210            if (cb > b) b = cb;
211        }
212
213        int deleted = 0;
214
215        if (leftLength > 0) {
216            int start = a - leftLength;
217            if (start < 0) start = 0;
218            content.delete(start, a);
219            deleted = a - start;
220        }
221
222        if (rightLength > 0) {
223            b = b - deleted;
224
225            int end = b + rightLength;
226            if (end > content.length()) end = content.length();
227
228            content.delete(b, end);
229        }
230
231        endBatchEdit();
232
233        return true;
234    }
235
236    /**
237     * The default implementation removes the composing state from the
238     * current editable text.  In addition, only if dummy mode, a key event is
239     * sent for the new text and the current editable buffer cleared.
240     */
241    public boolean finishComposingText() {
242        if (DEBUG) Log.v(TAG, "finishComposingText");
243        final Editable content = getEditable();
244        if (content != null) {
245            beginBatchEdit();
246            removeComposingSpans(content);
247            endBatchEdit();
248            sendCurrentText();
249        }
250        return true;
251    }
252
253    /**
254     * The default implementation uses TextUtils.getCapsMode to get the
255     * cursor caps mode for the current selection position in the editable
256     * text, unless in dummy mode in which case 0 is always returned.
257     */
258    public int getCursorCapsMode(int reqModes) {
259        if (mDummyMode) return 0;
260
261        final Editable content = getEditable();
262        if (content == null) return 0;
263
264        int a = Selection.getSelectionStart(content);
265        int b = Selection.getSelectionEnd(content);
266
267        if (a > b) {
268            int tmp = a;
269            a = b;
270            b = tmp;
271        }
272
273        return TextUtils.getCapsMode(content, a, reqModes);
274    }
275
276    /**
277     * The default implementation always returns null.
278     */
279    public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
280        return null;
281    }
282
283    /**
284     * The default implementation returns the given amount of text from the
285     * current cursor position in the buffer.
286     */
287    public CharSequence getTextBeforeCursor(int length, int flags) {
288        final Editable content = getEditable();
289        if (content == null) return null;
290
291        int a = Selection.getSelectionStart(content);
292        int b = Selection.getSelectionEnd(content);
293
294        if (a > b) {
295            int tmp = a;
296            a = b;
297            b = tmp;
298        }
299
300        if (a <= 0) {
301            return "";
302        }
303
304        if (length > a) {
305            length = a;
306        }
307
308        if ((flags&GET_TEXT_WITH_STYLES) != 0) {
309            return content.subSequence(a - length, a);
310        }
311        return TextUtils.substring(content, a - length, a);
312    }
313
314    /**
315     * The default implementation returns the given amount of text from the
316     * current cursor position in the buffer.
317     */
318    public CharSequence getTextAfterCursor(int length, int flags) {
319        final Editable content = getEditable();
320        if (content == null) return null;
321
322        int a = Selection.getSelectionStart(content);
323        int b = Selection.getSelectionEnd(content);
324
325        if (a > b) {
326            int tmp = a;
327            a = b;
328            b = tmp;
329        }
330
331        if (b + length > content.length()) {
332            length = content.length() - b;
333        }
334
335
336        if ((flags&GET_TEXT_WITH_STYLES) != 0) {
337            return content.subSequence(b, b + length);
338        }
339        return TextUtils.substring(content, b, b + length);
340    }
341
342    /**
343     * The default implementation does nothing.
344     */
345    public boolean performEditorAction(int actionCode) {
346        return false;
347    }
348
349    /**
350     * The default implementation does nothing.
351     */
352    public boolean performContextMenuAction(int id) {
353        return false;
354    }
355
356    /**
357     * The default implementation does nothing.
358     */
359    public boolean performPrivateCommand(String action, Bundle data) {
360        return false;
361    }
362
363    /**
364     * The default implementation places the given text into the editable,
365     * replacing any existing composing text.  The new text is marked as
366     * in a composing state with the composing style.
367     */
368    public boolean setComposingText(CharSequence text, int newCursorPosition) {
369        if (DEBUG) Log.v(TAG, "setComposingText " + text);
370        replaceText(text, newCursorPosition, true);
371        return true;
372    }
373
374    /**
375     * The default implementation changes the selection position in the
376     * current editable text.
377     */
378    public boolean setSelection(int start, int end) {
379        if (DEBUG) Log.v(TAG, "setSelection " + start + ", " + end);
380        final Editable content = getEditable();
381        if (content == null) return false;
382        int len = content.length();
383        if (start > len || end > len) {
384            // If the given selection is out of bounds, just ignore it.
385            // Most likely the text was changed out from under the IME,
386            // the the IME is going to have to update all of its state
387            // anyway.
388            return true;
389        }
390        if (start == end && MetaKeyKeyListener.getMetaState(content,
391                MetaKeyKeyListener.META_SELECTING) != 0) {
392            // If we are in selection mode, then we want to extend the
393            // selection instead of replacing it.
394            Selection.extendSelection(content, start);
395        } else {
396            Selection.setSelection(content, start, end);
397        }
398        return true;
399    }
400
401    /**
402     * Provides standard implementation for sending a key event to the window
403     * attached to the input connection's view.
404     */
405    public boolean sendKeyEvent(KeyEvent event) {
406        synchronized (mIMM.mH) {
407            Handler h = mTargetView != null ? mTargetView.getHandler() : null;
408            if (h == null) {
409                if (mIMM.mServedView != null) {
410                    h = mIMM.mServedView.getHandler();
411                }
412            }
413            if (h != null) {
414                h.sendMessage(h.obtainMessage(ViewRoot.DISPATCH_KEY_FROM_IME,
415                        event));
416            }
417        }
418        return false;
419    }
420
421    /**
422     * Updates InputMethodManager with the current fullscreen mode.
423     */
424    public boolean reportFullscreenMode(boolean enabled) {
425        mIMM.setFullscreenMode(enabled);
426        return true;
427    }
428
429    private void sendCurrentText() {
430        if (!mDummyMode) {
431            return;
432        }
433
434        Editable content = getEditable();
435        if (content != null) {
436            final int N = content.length();
437            if (N == 0) {
438                return;
439            }
440            if (N == 1) {
441                // If it's 1 character, we have a chance of being
442                // able to generate normal key events...
443                if (mKeyCharacterMap == null) {
444                    mKeyCharacterMap = KeyCharacterMap.load(
445                            KeyCharacterMap.BUILT_IN_KEYBOARD);
446                }
447                char[] chars = new char[1];
448                content.getChars(0, 1, chars, 0);
449                KeyEvent[] events = mKeyCharacterMap.getEvents(chars);
450                if (events != null) {
451                    for (int i=0; i<events.length; i++) {
452                        if (DEBUG) Log.v(TAG, "Sending: " + events[i]);
453                        sendKeyEvent(events[i]);
454                    }
455                    content.clear();
456                    return;
457                }
458            }
459
460            // Otherwise, revert to the special key event containing
461            // the actual characters.
462            KeyEvent event = new KeyEvent(SystemClock.uptimeMillis(),
463                    content.toString(), KeyCharacterMap.BUILT_IN_KEYBOARD, 0);
464            sendKeyEvent(event);
465            content.clear();
466        }
467    }
468
469    private void replaceText(CharSequence text, int newCursorPosition,
470            boolean composing) {
471        final Editable content = getEditable();
472        if (content == null) {
473            return;
474        }
475
476        beginBatchEdit();
477
478        // delete composing text set previously.
479        int a = getComposingSpanStart(content);
480        int b = getComposingSpanEnd(content);
481
482        if (DEBUG) Log.v(TAG, "Composing span: " + a + " to " + b);
483
484        if (b < a) {
485            int tmp = a;
486            a = b;
487            b = tmp;
488        }
489
490        if (a != -1 && b != -1) {
491            removeComposingSpans(content);
492        } else {
493            a = Selection.getSelectionStart(content);
494            b = Selection.getSelectionEnd(content);
495            if (a < 0) a = 0;
496            if (b < 0) b = 0;
497            if (b < a) {
498                int tmp = a;
499                a = b;
500                b = tmp;
501            }
502        }
503
504        if (composing) {
505            Spannable sp = null;
506            if (!(text instanceof Spannable)) {
507                sp = new SpannableStringBuilder(text);
508                text = sp;
509                if (mDefaultComposingSpans == null) {
510                    Context context;
511                    if (mTargetView != null) {
512                        context = mTargetView.getContext();
513                    } else if (mIMM.mServedView != null) {
514                        context = mIMM.mServedView.getContext();
515                    } else {
516                        context = null;
517                    }
518                    if (context != null) {
519                        TypedArray ta = context.getTheme()
520                                .obtainStyledAttributes(new int[] {
521                                        com.android.internal.R.attr.candidatesTextStyleSpans
522                                });
523                        CharSequence style = ta.getText(0);
524                        ta.recycle();
525                        if (style != null && style instanceof Spanned) {
526                            mDefaultComposingSpans = ((Spanned)style).getSpans(
527                                    0, style.length(), Object.class);
528                        }
529                    }
530                }
531                if (mDefaultComposingSpans != null) {
532                    for (int i = 0; i < mDefaultComposingSpans.length; ++i) {
533                        sp.setSpan(mDefaultComposingSpans[i], 0, sp.length(),
534                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
535                    }
536                }
537            } else {
538                sp = (Spannable)text;
539            }
540            setComposingSpans(sp);
541        }
542
543        if (DEBUG) Log.v(TAG, "Replacing from " + a + " to " + b + " with \""
544                + text + "\", composing=" + composing
545                + ", type=" + text.getClass().getCanonicalName());
546
547        if (DEBUG) {
548            LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG);
549            lp.println("Current text:");
550            TextUtils.dumpSpans(content, lp, "  ");
551            lp.println("Composing text:");
552            TextUtils.dumpSpans(text, lp, "  ");
553        }
554
555        // Position the cursor appropriately, so that after replacing the
556        // desired range of text it will be located in the correct spot.
557        // This allows us to deal with filters performing edits on the text
558        // we are providing here.
559        if (newCursorPosition > 0) {
560            newCursorPosition += b - 1;
561        } else {
562            newCursorPosition += a;
563        }
564        if (newCursorPosition < 0) newCursorPosition = 0;
565        if (newCursorPosition > content.length())
566            newCursorPosition = content.length();
567        Selection.setSelection(content, newCursorPosition);
568
569        content.replace(a, b, text);
570
571        if (DEBUG) {
572            LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG);
573            lp.println("Final text:");
574            TextUtils.dumpSpans(content, lp, "  ");
575        }
576
577        endBatchEdit();
578    }
579}
580