BaseInputConnection.java revision 1bf5e22da72b477c8b7a45ed85a4dba94be39db5
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 (length > a) {
301            length = a;
302        }
303
304        if ((flags&GET_TEXT_WITH_STYLES) != 0) {
305            return content.subSequence(a - length, a);
306        }
307        return TextUtils.substring(content, a - length, a);
308    }
309
310    /**
311     * The default implementation returns the given amount of text from the
312     * current cursor position in the buffer.
313     */
314    public CharSequence getTextAfterCursor(int length, int flags) {
315        final Editable content = getEditable();
316        if (content == null) return null;
317
318        int a = Selection.getSelectionStart(content);
319        int b = Selection.getSelectionEnd(content);
320
321        if (a > b) {
322            int tmp = a;
323            a = b;
324            b = tmp;
325        }
326
327        if (b + length > content.length()) {
328            length = content.length() - b;
329        }
330
331
332        if ((flags&GET_TEXT_WITH_STYLES) != 0) {
333            return content.subSequence(b, b + length);
334        }
335        return TextUtils.substring(content, b, b + length);
336    }
337
338    /**
339     * The default implementation does nothing.
340     */
341    public boolean performEditorAction(int actionCode) {
342        return false;
343    }
344
345    /**
346     * The default implementation does nothing.
347     */
348    public boolean performContextMenuAction(int id) {
349        return false;
350    }
351
352    /**
353     * The default implementation does nothing.
354     */
355    public boolean performPrivateCommand(String action, Bundle data) {
356        return false;
357    }
358
359    /**
360     * The default implementation places the given text into the editable,
361     * replacing any existing composing text.  The new text is marked as
362     * in a composing state with the composing style.
363     */
364    public boolean setComposingText(CharSequence text, int newCursorPosition) {
365        if (DEBUG) Log.v(TAG, "setComposingText " + text);
366        replaceText(text, newCursorPosition, true);
367        return true;
368    }
369
370    /**
371     * The default implementation changes the selection position in the
372     * current editable text.
373     */
374    public boolean setSelection(int start, int end) {
375        if (DEBUG) Log.v(TAG, "setSelection " + start + ", " + end);
376        final Editable content = getEditable();
377        if (content == null) return false;
378        int len = content.length();
379        if (start > len || end > len) {
380            // If the given selection is out of bounds, just ignore it.
381            // Most likely the text was changed out from under the IME,
382            // the the IME is going to have to update all of its state
383            // anyway.
384            return true;
385        }
386        if (start == end && MetaKeyKeyListener.getMetaState(content,
387                MetaKeyKeyListener.META_SELECTING) != 0) {
388            // If we are in selection mode, then we want to extend the
389            // selection instead of replacing it.
390            Selection.extendSelection(content, start);
391        } else {
392            Selection.setSelection(content, start, end);
393        }
394        return true;
395    }
396
397    /**
398     * Provides standard implementation for sending a key event to the window
399     * attached to the input connection's view.
400     */
401    public boolean sendKeyEvent(KeyEvent event) {
402        synchronized (mIMM.mH) {
403            Handler h = mTargetView != null ? mTargetView.getHandler() : null;
404            if (h == null) {
405                if (mIMM.mServedView != null) {
406                    h = mIMM.mServedView.getHandler();
407                }
408            }
409            if (h != null) {
410                h.sendMessage(h.obtainMessage(ViewRoot.DISPATCH_KEY_FROM_IME,
411                        event));
412            }
413        }
414        return false;
415    }
416
417    /**
418     * Updates InputMethodManager with the current fullscreen mode.
419     */
420    public boolean reportFullscreenMode(boolean enabled) {
421        mIMM.setFullscreenMode(enabled);
422        return true;
423    }
424
425    private void sendCurrentText() {
426        if (!mDummyMode) {
427            return;
428        }
429
430        Editable content = getEditable();
431        if (content != null) {
432            final int N = content.length();
433            if (N == 0) {
434                return;
435            }
436            if (N == 1) {
437                // If it's 1 character, we have a chance of being
438                // able to generate normal key events...
439                if (mKeyCharacterMap == null) {
440                    mKeyCharacterMap = KeyCharacterMap.load(
441                            KeyCharacterMap.BUILT_IN_KEYBOARD);
442                }
443                char[] chars = new char[1];
444                content.getChars(0, 1, chars, 0);
445                KeyEvent[] events = mKeyCharacterMap.getEvents(chars);
446                if (events != null) {
447                    for (int i=0; i<events.length; i++) {
448                        if (DEBUG) Log.v(TAG, "Sending: " + events[i]);
449                        sendKeyEvent(events[i]);
450                    }
451                    content.clear();
452                    return;
453                }
454            }
455
456            // Otherwise, revert to the special key event containing
457            // the actual characters.
458            KeyEvent event = new KeyEvent(SystemClock.uptimeMillis(),
459                    content.toString(), KeyCharacterMap.BUILT_IN_KEYBOARD, 0);
460            sendKeyEvent(event);
461            content.clear();
462        }
463    }
464
465    private void replaceText(CharSequence text, int newCursorPosition,
466            boolean composing) {
467        final Editable content = getEditable();
468        if (content == null) {
469            return;
470        }
471
472        beginBatchEdit();
473
474        // delete composing text set previously.
475        int a = getComposingSpanStart(content);
476        int b = getComposingSpanEnd(content);
477
478        if (DEBUG) Log.v(TAG, "Composing span: " + a + " to " + b);
479
480        if (b < a) {
481            int tmp = a;
482            a = b;
483            b = tmp;
484        }
485
486        if (a != -1 && b != -1) {
487            removeComposingSpans(content);
488        } else {
489            a = Selection.getSelectionStart(content);
490            b = Selection.getSelectionEnd(content);
491            if (a >=0 && b>= 0 && a != b) {
492                if (b < a) {
493                    int tmp = a;
494                    a = b;
495                    b = tmp;
496                }
497            }
498        }
499
500        if (composing) {
501            Spannable sp = null;
502            if (!(text instanceof Spannable)) {
503                sp = new SpannableStringBuilder(text);
504                text = sp;
505                if (mDefaultComposingSpans == null) {
506                    Context context;
507                    if (mTargetView != null) {
508                        context = mTargetView.getContext();
509                    } else if (mIMM.mServedView != null) {
510                        context = mIMM.mServedView.getContext();
511                    } else {
512                        context = null;
513                    }
514                    if (context != null) {
515                        TypedArray ta = context.getTheme()
516                                .obtainStyledAttributes(new int[] {
517                                        com.android.internal.R.attr.candidatesTextStyleSpans
518                                });
519                        CharSequence style = ta.getText(0);
520                        ta.recycle();
521                        if (style != null && style instanceof Spanned) {
522                            mDefaultComposingSpans = ((Spanned)style).getSpans(
523                                    0, style.length(), Object.class);
524                        }
525                    }
526                }
527                if (mDefaultComposingSpans != null) {
528                    for (int i = 0; i < mDefaultComposingSpans.length; ++i) {
529                        sp.setSpan(mDefaultComposingSpans[i], 0, sp.length(),
530                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
531                    }
532                }
533            } else {
534                sp = (Spannable)text;
535            }
536            setComposingSpans(sp);
537        }
538
539        if (DEBUG) Log.v(TAG, "Replacing from " + a + " to " + b + " with \""
540                + text + "\", composing=" + composing
541                + ", type=" + text.getClass().getCanonicalName());
542
543        if (DEBUG) {
544            LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG);
545            lp.println("Current text:");
546            TextUtils.dumpSpans(content, lp, "  ");
547            lp.println("Composing text:");
548            TextUtils.dumpSpans(text, lp, "  ");
549        }
550
551        // Position the cursor appropriately, so that after replacing the
552        // desired range of text it will be located in the correct spot.
553        // This allows us to deal with filters performing edits on the text
554        // we are providing here.
555        if (newCursorPosition > 0) {
556            newCursorPosition += b - 1;
557        } else {
558            newCursorPosition += a;
559        }
560        if (newCursorPosition < 0) newCursorPosition = 0;
561        if (newCursorPosition > content.length())
562            newCursorPosition = content.length();
563        Selection.setSelection(content, newCursorPosition);
564
565        content.replace(a, b, text);
566
567        if (DEBUG) {
568            LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG);
569            lp.println("Final text:");
570            TextUtils.dumpSpans(content, lp, "  ");
571        }
572
573        endBatchEdit();
574    }
575}
576