InputMethods.java (4654B)
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 2 * This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 package org.mozilla.gecko; 7 8 import android.content.Context; 9 import android.provider.Settings.Secure; 10 import android.view.View; 11 import android.view.inputmethod.InputMethodInfo; 12 import android.view.inputmethod.InputMethodManager; 13 import java.util.Collection; 14 15 public final class InputMethods { 16 public static final String METHOD_ANDROID_LATINIME = "com.android.inputmethod.latin/.LatinIME"; 17 // ATOK has a lot of package names since they release custom versions. 18 public static final String METHOD_ATOK_PREFIX = "com.justsystems.atokmobile"; 19 public static final String METHOD_ATOK_OEM_PREFIX = "com.atok.mobile."; 20 public static final String METHOD_GOOGLE_JAPANESE_INPUT = 21 "com.google.android.inputmethod.japanese/.MozcService"; 22 public static final String METHOD_ATOK_OEM_SOFTBANK = 23 "com.mobiroo.n.justsystems.atok/.AtokInputMethodService"; 24 public static final String METHOD_GOOGLE_LATINIME = 25 "com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME"; 26 public static final String METHOD_HTC_TOUCH_INPUT = "com.htc.android.htcime/.HTCIMEService"; 27 public static final String METHOD_IWNN = 28 "jp.co.omronsoft.iwnnime.ml/.standardcommon.IWnnLanguageSwitcher"; 29 public static final String METHOD_OPENWNN_PLUS = "com.owplus.ime.openwnnplus/.OpenWnnJAJP"; 30 public static final String METHOD_SAMSUNG = "com.sec.android.inputmethod/.SamsungKeypad"; 31 public static final String METHOD_SIMEJI = "com.adamrocker.android.input.simeji/.OpenWnnSimeji"; 32 public static final String METHOD_SONY = 33 "com.sonyericsson.textinput.uxp/.glue.InputMethodServiceGlue"; 34 public static final String METHOD_SWIFTKEY = 35 "com.touchtype.swiftkey/com.touchtype.KeyboardService"; 36 public static final String METHOD_SWYPE = "com.swype.android.inputmethod/.SwypeInputMethod"; 37 public static final String METHOD_SWYPE_BETA = "com.nuance.swype.input/.IME"; 38 public static final String METHOD_TOUCHPAL_KEYBOARD = 39 "com.cootek.smartinputv5/com.cootek.smartinput5.TouchPalIME"; 40 41 private InputMethods() {} 42 43 public static String getCurrentInputMethod(final Context context) { 44 final String inputMethod = 45 Secure.getString(context.getContentResolver(), Secure.DEFAULT_INPUT_METHOD); 46 return (inputMethod != null ? inputMethod : ""); 47 } 48 49 public static InputMethodInfo getInputMethodInfo( 50 final Context context, final String inputMethod) { 51 final InputMethodManager imm = getInputMethodManager(context); 52 final Collection<InputMethodInfo> infos = imm.getEnabledInputMethodList(); 53 for (final InputMethodInfo info : infos) { 54 if (info.getId().equals(inputMethod)) { 55 return info; 56 } 57 } 58 return null; 59 } 60 61 public static InputMethodManager getInputMethodManager(final Context context) { 62 return (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 63 } 64 65 public static void restartInput(final Context context, final View view) { 66 final InputMethodManager imm = getInputMethodManager(context); 67 if (imm != null) { 68 imm.restartInput(view); 69 } 70 } 71 72 public static boolean needsSoftResetWorkaround(final String inputMethod) { 73 // Stock latin IME on Android 4.2 and above 74 return (METHOD_ANDROID_LATINIME.equals(inputMethod) 75 || METHOD_GOOGLE_LATINIME.equals(inputMethod)); 76 } 77 78 /** 79 * Check input method if we require a workaround to remove composition in {@link 80 * android.view.inputmethod.InputMethodManager.updateSelection}. 81 * 82 * @param inputMethod The input method name by {@link #getCurrentInputMethod}. 83 * @return true if {@link android.view.inputmethod.InputMethodManager.updateSelection} doesn't 84 * remove the composition, use {@link 85 * android.view.inputmethod.InputMehtodManager.restartInput} to remove it in this case. 86 */ 87 public static boolean needsRestartInput(final String inputMethod) { 88 return inputMethod.startsWith(METHOD_ATOK_PREFIX) 89 || inputMethod.startsWith(METHOD_ATOK_OEM_PREFIX) 90 || METHOD_ATOK_OEM_SOFTBANK.equals(inputMethod); 91 } 92 93 public static boolean shouldCommitCharAsKey(final String inputMethod) { 94 return METHOD_HTC_TOUCH_INPUT.equals(inputMethod); 95 } 96 97 public static boolean needsRestartOnReplaceRemove(final Context context) { 98 final String inputMethod = getCurrentInputMethod(context); 99 return METHOD_SONY.equals(inputMethod); 100 } 101 }