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