InputConnectionCompatUtils.java revision 6f18a1fbcccf9cf5ca937b08098601a4fafead29
1/*
2 * Copyright (C) 2011 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 com.android.inputmethod.compat;
18
19import com.android.inputmethod.latin.EditingUtils.SelectedWord;
20
21import android.util.Log;
22import android.view.inputmethod.InputConnection;
23
24import java.lang.reflect.Constructor;
25import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Method;
27
28public class InputConnectionCompatUtils {
29    private static final String TAG = InputConnectionCompatUtils.class.getSimpleName();
30    private static final Class<?> CLASS_CorrectionInfo = CompatUtils
31            .getClass("android.view.inputmethod.CorrectionInfo");
32    private static final Class<?>[] INPUT_TYPE_CorrectionInfo = new Class<?>[] { int.class,
33            CharSequence.class, CharSequence.class };
34    private static final Constructor<?> CONSTRUCTOR_CorrectionInfo = CompatUtils
35            .getConstructor(CLASS_CorrectionInfo, INPUT_TYPE_CorrectionInfo);
36    private static final Method METHOD_InputConnection_commitCorrection = CompatUtils
37            .getMethod(InputConnection.class, "commitCorrection", CLASS_CorrectionInfo);
38    private static final Method METHOD_getSelectedText = CompatUtils
39            .getMethod(InputConnection.class, "getSelectedText", int.class);
40    private static final Method METHOD_setComposingRegion = CompatUtils
41            .getMethod(InputConnection.class, "setComposingRegion", int.class, int.class);
42    public static final boolean RECORRECTION_SUPPORTED;
43
44    static {
45        RECORRECTION_SUPPORTED = METHOD_getSelectedText != null
46                && METHOD_setComposingRegion != null;
47    }
48
49    public static void commitCorrection(InputConnection ic, int offset, CharSequence oldText,
50            CharSequence newText) {
51        if (ic == null || CONSTRUCTOR_CorrectionInfo == null
52                || METHOD_InputConnection_commitCorrection == null) {
53            return;
54        }
55        Object[] args = { offset, oldText, newText };
56        try {
57            Object correctionInfo = CONSTRUCTOR_CorrectionInfo.newInstance(args);
58            CompatUtils.invoke(ic, null, METHOD_InputConnection_commitCorrection,
59                    correctionInfo);
60        } catch (IllegalArgumentException e) {
61            Log.e(TAG, "Error in commitCorrection: IllegalArgumentException");
62        } catch (InstantiationException e) {
63            Log.e(TAG, "Error in commitCorrection: InstantiationException");
64        } catch (IllegalAccessException e) {
65            Log.e(TAG, "Error in commitCorrection: IllegalAccessException");
66        } catch (InvocationTargetException e) {
67            Log.e(TAG, "Error in commitCorrection: InvocationTargetException");
68        }
69    }
70
71
72    /**
73     * Returns the selected text between the selStart and selEnd positions.
74     */
75    public static CharSequence getSelectedText(InputConnection ic, int selStart, int selEnd) {
76        // Use reflection, for backward compatibility
77        return (CharSequence) CompatUtils.invoke(
78                ic, null, METHOD_getSelectedText, 0);
79    }
80
81    /**
82     * Tries to set the text into composition mode if there is support for it in the framework.
83     */
84    public static void underlineWord(InputConnection ic, SelectedWord word) {
85        // Use reflection, for backward compatibility
86        // If method not found, there's nothing we can do. It still works but just wont underline
87        // the word.
88        CompatUtils.invoke(
89                ic, null, METHOD_setComposingRegion, word.mStart, word.mEnd);
90    }
91}
92