diff options
Diffstat (limited to 'java/src/com/android/inputmethod/compat')
15 files changed, 202 insertions, 362 deletions
diff --git a/java/src/com/android/inputmethod/compat/AbstractCompatWrapper.java b/java/src/com/android/inputmethod/compat/AbstractCompatWrapper.java index 65949357f..2c31c55b0 100644 --- a/java/src/com/android/inputmethod/compat/AbstractCompatWrapper.java +++ b/java/src/com/android/inputmethod/compat/AbstractCompatWrapper.java @@ -24,7 +24,7 @@ public abstract class AbstractCompatWrapper { public AbstractCompatWrapper(Object obj) { if (obj == null) { - Log.e(TAG, "Invalid input to AbstructCompatWrapper"); + Log.e(TAG, "Invalid input to AbstractCompatWrapper"); } mObj = obj; } diff --git a/java/src/com/android/inputmethod/compat/AccessibilityEventCompatUtils.java b/java/src/com/android/inputmethod/compat/AccessibilityEventCompatUtils.java deleted file mode 100644 index 2fa9d87d8..000000000 --- a/java/src/com/android/inputmethod/compat/AccessibilityEventCompatUtils.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.compat; - -public class AccessibilityEventCompatUtils { - public static final int TYPE_VIEW_HOVER_ENTER = 0x80; - public static final int TYPE_VIEW_HOVER_EXIT = 0x100; -} diff --git a/java/src/com/android/inputmethod/compat/AccessibilityManagerCompatWrapper.java b/java/src/com/android/inputmethod/compat/AccessibilityManagerCompatUtils.java index a30af0faf..41b6a074d 100644 --- a/java/src/com/android/inputmethod/compat/AccessibilityManagerCompatWrapper.java +++ b/java/src/com/android/inputmethod/compat/AccessibilityManagerCompatUtils.java @@ -20,17 +20,15 @@ import android.view.accessibility.AccessibilityManager; import java.lang.reflect.Method; -public class AccessibilityManagerCompatWrapper { +public class AccessibilityManagerCompatUtils { private static final Method METHOD_isTouchExplorationEnabled = CompatUtils.getMethod( AccessibilityManager.class, "isTouchExplorationEnabled"); - private final AccessibilityManager mManager; - - public AccessibilityManagerCompatWrapper(AccessibilityManager manager) { - mManager = manager; + private AccessibilityManagerCompatUtils() { + // This class is non-instantiable. } - public boolean isTouchExplorationEnabled() { - return (Boolean) CompatUtils.invoke(mManager, false, METHOD_isTouchExplorationEnabled); + public static boolean isTouchExplorationEnabled(AccessibilityManager receiver) { + return (Boolean) CompatUtils.invoke(receiver, false, METHOD_isTouchExplorationEnabled); } } diff --git a/java/src/com/android/inputmethod/compat/ArraysCompatUtils.java b/java/src/com/android/inputmethod/compat/ArraysCompatUtils.java deleted file mode 100644 index f6afbcfe2..000000000 --- a/java/src/com/android/inputmethod/compat/ArraysCompatUtils.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.compat; - -import java.lang.reflect.Method; -import java.util.Arrays; - -public class ArraysCompatUtils { - private static final Method METHOD_Arrays_binarySearch = CompatUtils - .getMethod(Arrays.class, "binarySearch", int[].class, int.class, int.class, int.class); - - public static int binarySearch(int[] array, int startIndex, int endIndex, int value) { - if (METHOD_Arrays_binarySearch != null) { - final Object index = CompatUtils.invoke(null, 0, METHOD_Arrays_binarySearch, - array, startIndex, endIndex, value); - return (Integer)index; - } else { - return compatBinarySearch(array, startIndex, endIndex, value); - } - } - - /* package */ static int compatBinarySearch(int[] array, int startIndex, int endIndex, - int value) { - if (startIndex > endIndex) throw new IllegalArgumentException(); - if (startIndex < 0 || endIndex > array.length) throw new ArrayIndexOutOfBoundsException(); - - final int work[] = new int[endIndex - startIndex]; - System.arraycopy(array, startIndex, work, 0, work.length); - final int index = Arrays.binarySearch(work, value); - if (index >= 0) { - return index + startIndex; - } else { - return ~(~index + startIndex); - } - } -} diff --git a/java/src/com/android/inputmethod/compat/CompatUtils.java b/java/src/com/android/inputmethod/compat/CompatUtils.java index b42633cd9..ba82a06ac 100644 --- a/java/src/com/android/inputmethod/compat/CompatUtils.java +++ b/java/src/com/android/inputmethod/compat/CompatUtils.java @@ -32,8 +32,6 @@ public class CompatUtils { // TODO: Can these be constants instead of literal String constants? private static final String INPUT_METHOD_SUBTYPE_SETTINGS = "android.settings.INPUT_METHOD_SUBTYPE_SETTINGS"; - private static final String INPUT_LANGUAGE_SELECTION = - "com.android.inputmethod.latin.INPUT_LANGUAGE_SELECTION"; public static Intent getInputLanguageSelectionIntent(String inputMethodId, int flagsForSubtypeSettings) { @@ -51,11 +49,9 @@ public class CompatUtils { if (flagsForSubtypeSettings > 0) { intent.setFlags(flagsForSubtypeSettings); } - } else { - action = INPUT_LANGUAGE_SELECTION; - intent = new Intent(action); + return intent; } - return intent; + throw new RuntimeException("Language selection doesn't supported on this platform"); } public static Class<?> getClass(String className) { diff --git a/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java b/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java index bcdcef7dc..938388d6c 100644 --- a/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java @@ -26,77 +26,88 @@ public class EditorInfoCompatUtils { EditorInfo.class, "IME_FLAG_NAVIGATE_NEXT"); private static final Field FIELD_IME_FLAG_NAVIGATE_PREVIOUS = CompatUtils.getField( EditorInfo.class, "IME_FLAG_NAVIGATE_PREVIOUS"); + private static final Field FIELD_IME_FLAG_FORCE_ASCII = CompatUtils.getField( + EditorInfo.class, "IME_FLAG_FORCE_ASCII"); private static final Field FIELD_IME_ACTION_PREVIOUS = CompatUtils.getField( EditorInfo.class, "IME_ACTION_PREVIOUS"); private static final Integer OBJ_IME_FLAG_NAVIGATE_NEXT = (Integer) CompatUtils .getFieldValue(null, null, FIELD_IME_FLAG_NAVIGATE_NEXT); private static final Integer OBJ_IME_FLAG_NAVIGATE_PREVIOUS = (Integer) CompatUtils .getFieldValue(null, null, FIELD_IME_FLAG_NAVIGATE_PREVIOUS); + private static final Integer OBJ_IME_FLAG_FORCE_ASCII = (Integer) CompatUtils + .getFieldValue(null, null, FIELD_IME_FLAG_FORCE_ASCII); private static final Integer OBJ_IME_ACTION_PREVIOUS = (Integer) CompatUtils .getFieldValue(null, null, FIELD_IME_ACTION_PREVIOUS); + // EditorInfo.IME_FLAG_NAVIGATE_NEXT has been introduced since API#11 (Honeycomb). public static boolean hasFlagNavigateNext(int imeOptions) { if (OBJ_IME_FLAG_NAVIGATE_NEXT == null) return false; return (imeOptions & OBJ_IME_FLAG_NAVIGATE_NEXT) != 0; } + // EditorInfo.IME_FLAG_NAVIGATE_PREVIOUS has been introduced since API#11 (Honeycomb). public static boolean hasFlagNavigatePrevious(int imeOptions) { if (OBJ_IME_FLAG_NAVIGATE_PREVIOUS == null) return false; return (imeOptions & OBJ_IME_FLAG_NAVIGATE_PREVIOUS) != 0; } - public static void performEditorActionNext(InputConnection ic) { - ic.performEditorAction(EditorInfo.IME_ACTION_NEXT); + // EditorInfo.IME_FLAG_FORCE_ASCII has been introduced since API#16 (JellyBean). + public static boolean hasFlagForceAscii(int imeOptions) { + if (OBJ_IME_FLAG_FORCE_ASCII == null) + return false; + return (imeOptions & OBJ_IME_FLAG_FORCE_ASCII) != 0; } + // EditorInfo.IME_ACTION_PREVIOUS has been introduced since API#11 (Honeycomb). public static void performEditorActionPrevious(InputConnection ic) { - if (OBJ_IME_ACTION_PREVIOUS == null) + if (OBJ_IME_ACTION_PREVIOUS == null || ic == null) return; ic.performEditorAction(OBJ_IME_ACTION_PREVIOUS); } - public static String imeOptionsName(int imeOptions) { - if (imeOptions == -1) - return null; + public static String imeActionName(int imeOptions) { final int actionId = imeOptions & EditorInfo.IME_MASK_ACTION; - final String action; switch (actionId) { - case EditorInfo.IME_ACTION_UNSPECIFIED: - action = "actionUnspecified"; - break; - case EditorInfo.IME_ACTION_NONE: - action = "actionNone"; - break; - case EditorInfo.IME_ACTION_GO: - action = "actionGo"; - break; - case EditorInfo.IME_ACTION_SEARCH: - action = "actionSearch"; - break; - case EditorInfo.IME_ACTION_SEND: - action = "actionSend"; - break; - case EditorInfo.IME_ACTION_NEXT: - action = "actionNext"; - break; - case EditorInfo.IME_ACTION_DONE: - action = "actionDone"; - break; - default: { - if (OBJ_IME_ACTION_PREVIOUS != null && actionId == OBJ_IME_ACTION_PREVIOUS) { - action = "actionPrevious"; - } else { - action = "actionUnknown(" + actionId + ")"; - } - break; + case EditorInfo.IME_ACTION_UNSPECIFIED: + return "actionUnspecified"; + case EditorInfo.IME_ACTION_NONE: + return "actionNone"; + case EditorInfo.IME_ACTION_GO: + return "actionGo"; + case EditorInfo.IME_ACTION_SEARCH: + return "actionSearch"; + case EditorInfo.IME_ACTION_SEND: + return "actionSend"; + case EditorInfo.IME_ACTION_NEXT: + return "actionNext"; + case EditorInfo.IME_ACTION_DONE: + return "actionDone"; + default: + if (OBJ_IME_ACTION_PREVIOUS != null && actionId == OBJ_IME_ACTION_PREVIOUS) { + return "actionPrevious"; + } else { + return "actionUnknown(" + actionId + ")"; } } + } + + public static String imeOptionsName(int imeOptions) { + final String action = imeActionName(imeOptions); + final StringBuilder flags = new StringBuilder(); if ((imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) { - return "flagNoEnterAction|" + action; - } else { - return action; + flags.append("flagNoEnterAction|"); + } + if (hasFlagNavigateNext(imeOptions)) { + flags.append("flagNavigateNext|"); + } + if (hasFlagNavigatePrevious(imeOptions)) { + flags.append("flagNavigatePrevious|"); + } + if (hasFlagForceAscii(imeOptions)) { + flags.append("flagForceAscii|"); } + return (action != null) ? flags + action : flags.toString(); } } diff --git a/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java index 51dc4cd37..3df6bea4b 100644 --- a/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java +++ b/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java @@ -29,10 +29,9 @@ import android.util.Log; import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodManager; -import com.android.inputmethod.deprecated.LanguageSwitcherProxy; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.SubtypeSwitcher; -import com.android.inputmethod.latin.Utils; +import com.android.inputmethod.latin.SubtypeUtils; import java.lang.reflect.Method; import java.util.ArrayList; @@ -49,6 +48,8 @@ public class InputMethodManagerCompatWrapper { private static final String TAG = InputMethodManagerCompatWrapper.class.getSimpleName(); private static final Method METHOD_getCurrentInputMethodSubtype = CompatUtils.getMethod(InputMethodManager.class, "getCurrentInputMethodSubtype"); + private static final Method METHOD_getLastInputMethodSubtype = + CompatUtils.getMethod(InputMethodManager.class, "getLastInputMethodSubtype"); private static final Method METHOD_getEnabledInputMethodSubtypeList = CompatUtils.getMethod(InputMethodManager.class, "getEnabledInputMethodSubtypeList", InputMethodInfo.class, boolean.class); @@ -60,18 +61,12 @@ public class InputMethodManagerCompatWrapper { String.class, InputMethodSubtypeCompatWrapper.CLASS_InputMethodSubtype); private static final Method METHOD_switchToLastInputMethod = CompatUtils.getMethod( InputMethodManager.class, "switchToLastInputMethod", IBinder.class); + private static final Method METHOD_switchToNextInputMethod = CompatUtils.getMethod( + InputMethodManager.class, "switchToNextInputMethod", IBinder.class, Boolean.TYPE); private static final InputMethodManagerCompatWrapper sInstance = new InputMethodManagerCompatWrapper(); - public static final boolean SUBTYPE_SUPPORTED; - - static { - // This static initializer guarantees that METHOD_getShortcutInputMethodsAndSubtypes is - // already instantiated. - SUBTYPE_SUPPORTED = METHOD_getShortcutInputMethodsAndSubtypes != null; - } - // For the compatibility, IMM will create dummy subtypes if subtypes are not found. // This is required to be false if the current behavior is broken. For now, it's ok to be true. public static final boolean FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES = @@ -83,7 +78,6 @@ public class InputMethodManagerCompatWrapper { private InputMethodManager mImm; private PackageManager mPackageManager; private ApplicationInfo mApplicationInfo; - private LanguageSwitcherProxy mLanguageSwitcherProxy; private String mLatinImePackageName; public static InputMethodManagerCompatWrapper getInstance() { @@ -99,30 +93,20 @@ public class InputMethodManagerCompatWrapper { sInstance.mLatinImePackageName = service.getPackageName(); sInstance.mPackageManager = service.getPackageManager(); sInstance.mApplicationInfo = service.getApplicationInfo(); - sInstance.mLanguageSwitcherProxy = LanguageSwitcherProxy.getInstance(); } public InputMethodSubtypeCompatWrapper getCurrentInputMethodSubtype() { - if (!SUBTYPE_SUPPORTED) { - return new InputMethodSubtypeCompatWrapper( - 0, 0, mLanguageSwitcherProxy.getInputLocale().toString(), KEYBOARD_MODE, ""); - } Object o = CompatUtils.invoke(mImm, null, METHOD_getCurrentInputMethodSubtype); return new InputMethodSubtypeCompatWrapper(o); } + public InputMethodSubtypeCompatWrapper getLastInputMethodSubtype() { + Object o = CompatUtils.invoke(mImm, null, METHOD_getLastInputMethodSubtype); + return new InputMethodSubtypeCompatWrapper(o); + } + public List<InputMethodSubtypeCompatWrapper> getEnabledInputMethodSubtypeList( InputMethodInfoCompatWrapper imi, boolean allowsImplicitlySelectedSubtypes) { - if (!SUBTYPE_SUPPORTED) { - String[] languages = mLanguageSwitcherProxy.getEnabledLanguages( - allowsImplicitlySelectedSubtypes); - List<InputMethodSubtypeCompatWrapper> subtypeList = - new ArrayList<InputMethodSubtypeCompatWrapper>(); - for (String lang: languages) { - subtypeList.add(new InputMethodSubtypeCompatWrapper(0, 0, lang, KEYBOARD_MODE, "")); - } - return subtypeList; - } Object retval = CompatUtils.invoke(mImm, null, METHOD_getEnabledInputMethodSubtypeList, (imi != null ? imi.getInputMethodInfo() : null), allowsImplicitlySelectedSubtypes); if (retval == null || !(retval instanceof List<?>) || ((List<?>)retval).isEmpty()) { @@ -150,11 +134,10 @@ public class InputMethodManagerCompatWrapper { private InputMethodInfoCompatWrapper getLatinImeInputMethodInfo() { if (TextUtils.isEmpty(mLatinImePackageName)) return null; - return Utils.getInputMethodInfo(this, mLatinImePackageName); + return SubtypeUtils.getInputMethodInfo(mLatinImePackageName); } - @SuppressWarnings("unused") - private InputMethodSubtypeCompatWrapper getLastResortSubtype(String mode) { + private static InputMethodSubtypeCompatWrapper getLastResortSubtype(String mode) { if (VOICE_MODE.equals(mode) && !FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES) return null; Locale inputLocale = SubtypeSwitcher.getInstance().getInputLocale(); @@ -216,12 +199,14 @@ public class InputMethodManagerCompatWrapper { } public boolean switchToLastInputMethod(IBinder token) { - if (SubtypeSwitcher.getInstance().isDummyVoiceMode()) { - return true; - } return (Boolean)CompatUtils.invoke(mImm, false, METHOD_switchToLastInputMethod, token); } + public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) { + return (Boolean)CompatUtils.invoke(mImm, false, METHOD_switchToNextInputMethod, token, + onlyCurrentIme); + } + public List<InputMethodInfoCompatWrapper> getEnabledInputMethodList() { if (mImm == null) return null; List<InputMethodInfoCompatWrapper> imis = new ArrayList<InputMethodInfoCompatWrapper>(); @@ -233,87 +218,6 @@ public class InputMethodManagerCompatWrapper { public void showInputMethodPicker() { if (mImm == null) return; - if (SUBTYPE_SUPPORTED) { - mImm.showInputMethodPicker(); - return; - } - - // The code below are based on {@link InputMethodManager#showInputMethodMenuInternal}. - - final InputMethodInfoCompatWrapper myImi = Utils.getInputMethodInfo( - this, mLatinImePackageName); - final List<InputMethodSubtypeCompatWrapper> myImsList = getEnabledInputMethodSubtypeList( - myImi, true); - final InputMethodSubtypeCompatWrapper currentIms = getCurrentInputMethodSubtype(); - final List<InputMethodInfoCompatWrapper> imiList = getEnabledInputMethodList(); - imiList.remove(myImi); - Collections.sort(imiList, new Comparator<InputMethodInfoCompatWrapper>() { - @Override - public int compare(InputMethodInfoCompatWrapper imi1, - InputMethodInfoCompatWrapper imi2) { - final CharSequence imiId1 = imi1.loadLabel(mPackageManager) + "/" + imi1.getId(); - final CharSequence imiId2 = imi2.loadLabel(mPackageManager) + "/" + imi2.getId(); - return imiId1.toString().compareTo(imiId2.toString()); - } - }); - - final int myImsCount = myImsList.size(); - final int imiCount = imiList.size(); - final CharSequence[] items = new CharSequence[myImsCount + imiCount]; - - int checkedItem = 0; - int index = 0; - final CharSequence myImiLabel = myImi.loadLabel(mPackageManager); - for (int i = 0; i < myImsCount; i++) { - InputMethodSubtypeCompatWrapper ims = myImsList.get(i); - if (currentIms.equals(ims)) - checkedItem = index; - final CharSequence title = TextUtils.concat( - ims.getDisplayName(mService, mLatinImePackageName, mApplicationInfo), - " (" + myImiLabel, ")"); - items[index] = title; - index++; - } - - for (int i = 0; i < imiCount; i++) { - final InputMethodInfoCompatWrapper imi = imiList.get(i); - final CharSequence title = imi.loadLabel(mPackageManager); - items[index] = title; - index++; - } - - final OnClickListener buttonListener = new OnClickListener() { - @Override - public void onClick(DialogInterface di, int whichButton) { - final Intent intent = new Intent("android.settings.INPUT_METHOD_SETTINGS"); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK - | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED - | Intent.FLAG_ACTIVITY_CLEAR_TOP); - mService.startActivity(intent); - } - }; - final InputMethodServiceCompatWrapper service = mService; - final IBinder token = service.getWindow().getWindow().getAttributes().token; - final OnClickListener selectionListener = new OnClickListener() { - @Override - public void onClick(DialogInterface di, int which) { - di.dismiss(); - if (which < myImsCount) { - final int imsIndex = which; - final InputMethodSubtypeCompatWrapper ims = myImsList.get(imsIndex); - service.notifyOnCurrentInputMethodSubtypeChanged(ims); - } else { - final int imiIndex = which - myImsCount; - final InputMethodInfoCompatWrapper imi = imiList.get(imiIndex); - setInputMethodAndSubtype(token, imi.getId(), null); - } - } - }; - - final AlertDialog.Builder builder = new AlertDialog.Builder(mService) - .setTitle(mService.getString(R.string.selectInputMethod)) - .setNeutralButton(R.string.configure_input_method, buttonListener) - .setSingleChoiceItems(items, checkedItem, selectionListener); - mService.showOptionDialogInternal(builder.create()); + mImm.showInputMethodPicker(); } } diff --git a/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java index 8e2ee0f7a..7c15be300 100644 --- a/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java +++ b/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java @@ -23,7 +23,6 @@ import android.view.Window; import android.view.WindowManager; import android.view.inputmethod.InputMethodSubtype; -import com.android.inputmethod.deprecated.LanguageSwitcherProxy; import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.latin.SubtypeSwitcher; @@ -87,9 +86,6 @@ public class InputMethodServiceCompatWrapper extends InputMethodService { if (subtype != null) { if (!InputMethodManagerCompatWrapper.FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES && !subtype.isDummy()) return; - if (!InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) { - LanguageSwitcherProxy.getInstance().setLocale(subtype.getLocale()); - } SubtypeSwitcher.getInstance().updateSubtype(subtype); } } diff --git a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java index a6bb83adf..574158825 100644 --- a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java +++ b/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java @@ -29,7 +29,7 @@ import java.util.Locale; // TODO: Override this class with the concrete implementation if we need to take care of the // performance. -public final class InputMethodSubtypeCompatWrapper extends AbstractCompatWrapper { +public class InputMethodSubtypeCompatWrapper extends AbstractCompatWrapper { private static final boolean DBG = LatinImeLogger.sDBG; private static final String TAG = InputMethodSubtypeCompatWrapper.class.getSimpleName(); private static final String DEFAULT_LOCALE = "en_US"; @@ -65,7 +65,7 @@ public final class InputMethodSubtypeCompatWrapper extends AbstractCompatWrapper public InputMethodSubtypeCompatWrapper(Object subtype) { super((CLASS_InputMethodSubtype != null && CLASS_InputMethodSubtype.isInstance(subtype)) - ? subtype : null); + ? subtype : new Object()); mDummyNameResId = 0; mDummyIconResId = 0; mDummyLocale = DEFAULT_LOCALE; @@ -76,7 +76,7 @@ public final class InputMethodSubtypeCompatWrapper extends AbstractCompatWrapper // Constructor for creating a dummy subtype. public InputMethodSubtypeCompatWrapper(int nameResId, int iconResId, String locale, String mode, String extraValues) { - super(null); + super(new Object()); if (DBG) { Log.d(TAG, "CreateInputMethodSubtypeCompatWrapper"); } diff --git a/java/src/com/android/inputmethod/compat/MotionEventCompatUtils.java b/java/src/com/android/inputmethod/compat/MotionEventCompatUtils.java index 8518a4a78..9a523011a 100644 --- a/java/src/com/android/inputmethod/compat/MotionEventCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/MotionEventCompatUtils.java @@ -17,7 +17,7 @@ package com.android.inputmethod.compat; public class MotionEventCompatUtils { - public static final int ACTION_HOVER_MOVE = 0x7; + // TODO: Remove after these are added to MotionEventCompat. public static final int ACTION_HOVER_ENTER = 0x9; public static final int ACTION_HOVER_EXIT = 0xA; } diff --git a/java/src/com/android/inputmethod/compat/SharedPreferencesCompat.java b/java/src/com/android/inputmethod/compat/SharedPreferencesCompat.java deleted file mode 100644 index 38736f3a1..000000000 --- a/java/src/com/android/inputmethod/compat/SharedPreferencesCompat.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.compat; - -import android.content.SharedPreferences; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -/** - * Reflection utils to call SharedPreferences$Editor.apply when possible, - * falling back to commit when apply isn't available. - */ -public class SharedPreferencesCompat { - private static final Method sApplyMethod = findApplyMethod(); - - private static Method findApplyMethod() { - try { - return SharedPreferences.Editor.class.getMethod("apply"); - } catch (NoSuchMethodException unused) { - // fall through - } - return null; - } - - public static void apply(SharedPreferences.Editor editor) { - if (sApplyMethod != null) { - try { - sApplyMethod.invoke(editor); - return; - } catch (InvocationTargetException unused) { - // fall through - } catch (IllegalAccessException unused) { - // fall through - } - } - editor.commit(); - } -} diff --git a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java index 161ef09b8..df55aee94 100644 --- a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java +++ b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java @@ -48,21 +48,30 @@ public class SuggestionSpanUtils { Context.class, Locale.class, String[].class, int.class, Class.class }; private static final Constructor<?> CONSTRUCTOR_SuggestionSpan = CompatUtils .getConstructor(CLASS_SuggestionSpan, INPUT_TYPE_SuggestionSpan); - public static final Field FIELD_FLAG_AUTO_CORRECTION - = CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_AUTO_CORRECTION"); - public static final Field FIELD_SUGGESTION_MAX_SIZE + public static final Field FIELD_FLAG_EASY_CORRECT = + CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_EASY_CORRECT"); + public static final Field FIELD_FLAG_MISSPELLED = + CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_MISSPELLED"); + public static final Field FIELD_FLAG_AUTO_CORRECTION = + CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_AUTO_CORRECTION"); + public static final Field FIELD_SUGGESTIONS_MAX_SIZE = CompatUtils.getField(CLASS_SuggestionSpan, "SUGGESTIONS_MAX_SIZE"); + public static final Integer OBJ_FLAG_EASY_CORRECT = (Integer) CompatUtils + .getFieldValue(null, null, FIELD_FLAG_EASY_CORRECT); + public static final Integer OBJ_FLAG_MISSPELLED = (Integer) CompatUtils + .getFieldValue(null, null, FIELD_FLAG_MISSPELLED); public static final Integer OBJ_FLAG_AUTO_CORRECTION = (Integer) CompatUtils - .getFieldValue(null, null, FIELD_FLAG_AUTO_CORRECTION);; - public static final Integer OBJ_SUGGESTION_MAX_SIZE = (Integer) CompatUtils - .getFieldValue(null, null, FIELD_SUGGESTION_MAX_SIZE);; + .getFieldValue(null, null, FIELD_FLAG_AUTO_CORRECTION); + public static final Integer OBJ_SUGGESTIONS_MAX_SIZE = (Integer) CompatUtils + .getFieldValue(null, null, FIELD_SUGGESTIONS_MAX_SIZE); static { SUGGESTION_SPAN_IS_SUPPORTED = CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null; if (LatinImeLogger.sDBG) { if (SUGGESTION_SPAN_IS_SUPPORTED - && (OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTION_MAX_SIZE == null)) { + && (OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTIONS_MAX_SIZE == null + || OBJ_FLAG_MISSPELLED == null || OBJ_FLAG_EASY_CORRECT == null)) { throw new RuntimeException("Field is accidentially null."); } } @@ -71,7 +80,8 @@ public class SuggestionSpanUtils { public static CharSequence getTextWithAutoCorrectionIndicatorUnderline( Context context, CharSequence text) { if (TextUtils.isEmpty(text) || CONSTRUCTOR_SuggestionSpan == null - || OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTION_MAX_SIZE == null) { + || OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTIONS_MAX_SIZE == null + || OBJ_FLAG_MISSPELLED == null || OBJ_FLAG_EASY_CORRECT == null) { return text; } final Spannable spannable = text instanceof Spannable @@ -93,8 +103,7 @@ public class SuggestionSpanUtils { CharSequence pickedWord, SuggestedWords suggestedWords) { if (TextUtils.isEmpty(pickedWord) || CONSTRUCTOR_SuggestionSpan == null || suggestedWords == null || suggestedWords.size() == 0 - || suggestedWords.getInfo(0).isObsoleteSuggestedWord() - || OBJ_SUGGESTION_MAX_SIZE == null) { + || OBJ_SUGGESTIONS_MAX_SIZE == null) { return pickedWord; } @@ -105,18 +114,26 @@ public class SuggestionSpanUtils { spannable = new SpannableString(pickedWord); } final ArrayList<String> suggestionsList = new ArrayList<String>(); + boolean sameAsTyped = false; for (int i = 0; i < suggestedWords.size(); ++i) { - if (suggestionsList.size() >= OBJ_SUGGESTION_MAX_SIZE) { + if (suggestionsList.size() >= OBJ_SUGGESTIONS_MAX_SIZE) { break; } final CharSequence word = suggestedWords.getWord(i); if (!TextUtils.equals(pickedWord, word)) { suggestionsList.add(word.toString()); + } else if (i == 0) { + sameAsTyped = true; } } + // TODO: Share the implementation for checking typed word validity between the IME + // and the spell checker. + final int flag = (sameAsTyped && !suggestedWords.mTypedWordValid) + ? (OBJ_FLAG_EASY_CORRECT | OBJ_FLAG_MISSPELLED) + : 0; final Object[] args = - { context, null, suggestionsList.toArray(new String[suggestionsList.size()]), 0, + { context, null, suggestionsList.toArray(new String[suggestionsList.size()]), flag, (Class<?>) SuggestionSpanPickedNotificationReceiver.class }; final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args); if (ss == null) { diff --git a/java/src/com/android/inputmethod/compat/SuggestionsInfoCompatUtils.java b/java/src/com/android/inputmethod/compat/SuggestionsInfoCompatUtils.java new file mode 100644 index 000000000..723ec2862 --- /dev/null +++ b/java/src/com/android/inputmethod/compat/SuggestionsInfoCompatUtils.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.compat; + +import android.view.textservice.SuggestionsInfo; + +import java.lang.reflect.Field; + +public class SuggestionsInfoCompatUtils { + private static final Field FIELD_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS = CompatUtils.getField( + SuggestionsInfo.class, "RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS"); + private static final Integer OBJ_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS = (Integer) CompatUtils + .getFieldValue(null, null, FIELD_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS); + private static final int RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS = + OBJ_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS != null + ? OBJ_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS : 0; + + private SuggestionsInfoCompatUtils() { + } + + /** + * Returns the flag value of the attributes of the suggestions that can be obtained by + * {@link SuggestionsInfo#getSuggestionsAttributes()}: this tells that the text service thinks + * the result suggestions include highly recommended ones. + */ + public static int getValueOf_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS() { + return RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS; + } +} diff --git a/java/src/com/android/inputmethod/compat/VibratorCompatWrapper.java b/java/src/com/android/inputmethod/compat/VibratorCompatWrapper.java deleted file mode 100644 index 2fb8b8710..000000000 --- a/java/src/com/android/inputmethod/compat/VibratorCompatWrapper.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.compat; - -import android.content.Context; -import android.os.Vibrator; - -import java.lang.reflect.Method; - -public class VibratorCompatWrapper { - private static final Method METHOD_hasVibrator = CompatUtils.getMethod(Vibrator.class, - "hasVibrator"); - - private static final VibratorCompatWrapper sInstance = new VibratorCompatWrapper(); - private Vibrator mVibrator; - - private VibratorCompatWrapper() { - } - - public static VibratorCompatWrapper getInstance(Context context) { - if (sInstance.mVibrator == null) { - sInstance.mVibrator = - (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); - } - return sInstance; - } - - public boolean hasVibrator() { - if (mVibrator == null) - return false; - return (Boolean) CompatUtils.invoke(mVibrator, true, METHOD_hasVibrator); - } - - public void vibrate(long milliseconds) { - mVibrator.vibrate(milliseconds); - } -} diff --git a/java/src/com/android/inputmethod/compat/ViewParentCompatUtils.java b/java/src/com/android/inputmethod/compat/ViewParentCompatUtils.java new file mode 100644 index 000000000..d19bc3af1 --- /dev/null +++ b/java/src/com/android/inputmethod/compat/ViewParentCompatUtils.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.compat; + +import android.view.View; +import android.view.ViewParent; +import android.view.accessibility.AccessibilityEvent; + +import java.lang.reflect.Method; + +public class ViewParentCompatUtils { + private static final Method METHOD_requestSendAccessibilityEvent = CompatUtils.getMethod( + ViewParent.class, "requestSendAccessibilityEvent", View.class, + AccessibilityEvent.class); + + /** + * Called by a child to request from its parent to send an {@link AccessibilityEvent}. + * The child has already populated a record for itself in the event and is delegating + * to its parent to send the event. The parent can optionally add a record for itself. + * <p> + * Note: An accessibility event is fired by an individual view which populates the + * event with a record for its state and requests from its parent to perform + * the sending. The parent can optionally add a record for itself before + * dispatching the request to its parent. A parent can also choose not to + * respect the request for sending the event. The accessibility event is sent + * by the topmost view in the view tree.</p> + * + * @param child The child which requests sending the event. + * @param event The event to be sent. + * @return True if the event was sent. + */ + public static boolean requestSendAccessibilityEvent( + ViewParent receiver, View child, AccessibilityEvent event) { + return (Boolean) CompatUtils.invoke( + receiver, false, METHOD_requestSendAccessibilityEvent, child, event); + } +} |