diff options
Diffstat (limited to 'java/src')
35 files changed, 453 insertions, 1185 deletions
diff --git a/java/src/com/android/inputmethod/accessibility/AccessibilityUtils.java b/java/src/com/android/inputmethod/accessibility/AccessibilityUtils.java index 41da2aa4c..667b109cb 100644 --- a/java/src/com/android/inputmethod/accessibility/AccessibilityUtils.java +++ b/java/src/com/android/inputmethod/accessibility/AccessibilityUtils.java @@ -21,18 +21,15 @@ import android.inputmethodservice.InputMethodService; import android.media.AudioManager; import android.os.SystemClock; import android.provider.Settings; -import android.support.v4.view.MotionEventCompat; import android.util.Log; import android.view.MotionEvent; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityManager; import android.view.inputmethod.EditorInfo; -import com.android.inputmethod.compat.AccessibilityManagerCompatUtils; import com.android.inputmethod.compat.AudioManagerCompatWrapper; -import com.android.inputmethod.compat.InputTypeCompatUtils; -import com.android.inputmethod.compat.MotionEventCompatUtils; import com.android.inputmethod.compat.SettingsSecureCompatUtils; +import com.android.inputmethod.latin.InputTypeUtils; import com.android.inputmethod.latin.R; public class AccessibilityUtils { @@ -93,7 +90,7 @@ public class AccessibilityUtils { public boolean isTouchExplorationEnabled() { return ENABLE_ACCESSIBILITY && mAccessibilityManager.isEnabled() - && AccessibilityManagerCompatUtils.isTouchExplorationEnabled(mAccessibilityManager); + && mAccessibilityManager.isTouchExplorationEnabled(); } /** @@ -107,9 +104,9 @@ public class AccessibilityUtils { public boolean isTouchExplorationEvent(MotionEvent event) { final int action = event.getAction(); - return action == MotionEventCompatUtils.ACTION_HOVER_ENTER - || action == MotionEventCompatUtils.ACTION_HOVER_EXIT - || action == MotionEventCompat.ACTION_HOVER_MOVE; + return action == MotionEvent.ACTION_HOVER_ENTER + || action == MotionEvent.ACTION_HOVER_EXIT + || action == MotionEvent.ACTION_HOVER_MOVE; } /** @@ -135,7 +132,7 @@ public class AccessibilityUtils { return false; // Don't speak if the IME is connected to a password field. - return InputTypeCompatUtils.isPasswordInputType(editorInfo.inputType); + return InputTypeUtils.isPasswordInputType(editorInfo.inputType); } /** diff --git a/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java b/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java index 2401d93c6..c85a5514e 100644 --- a/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java +++ b/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java @@ -21,15 +21,12 @@ import android.graphics.Color; import android.graphics.Paint; import android.inputmethodservice.InputMethodService; import android.support.v4.view.AccessibilityDelegateCompat; -import android.support.v4.view.MotionEventCompat; import android.support.v4.view.ViewCompat; import android.support.v4.view.accessibility.AccessibilityEventCompat; import android.view.MotionEvent; import android.view.View; import android.view.accessibility.AccessibilityEvent; -import com.android.inputmethod.compat.MotionEventCompatUtils; -import com.android.inputmethod.compat.ViewParentCompatUtils; import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.KeyboardId; @@ -136,10 +133,10 @@ public class AccessibleKeyboardViewProxy extends AccessibilityDelegateCompat { mLastHoverKey = key; switch (event.getAction()) { - case MotionEventCompatUtils.ACTION_HOVER_ENTER: - case MotionEventCompatUtils.ACTION_HOVER_EXIT: + case MotionEvent.ACTION_HOVER_ENTER: + case MotionEvent.ACTION_HOVER_EXIT: return onHoverKey(key, event); - case MotionEventCompat.ACTION_HOVER_MOVE: + case MotionEvent.ACTION_HOVER_MOVE: if (key != previousKey) { return onTransitionKey(key, previousKey, event); } else { @@ -163,13 +160,13 @@ public class AccessibleKeyboardViewProxy extends AccessibilityDelegateCompat { private boolean onTransitionKey(Key currentKey, Key previousKey, MotionEvent event) { final int savedAction = event.getAction(); - event.setAction(MotionEventCompatUtils.ACTION_HOVER_EXIT); + event.setAction(MotionEvent.ACTION_HOVER_EXIT); onHoverKey(previousKey, event); - event.setAction(MotionEventCompatUtils.ACTION_HOVER_ENTER); + event.setAction(MotionEvent.ACTION_HOVER_ENTER); onHoverKey(currentKey, event); - event.setAction(MotionEventCompat.ACTION_HOVER_MOVE); + event.setAction(MotionEvent.ACTION_HOVER_MOVE); final boolean handled = onHoverKey(currentKey, event); event.setAction(savedAction); @@ -192,10 +189,10 @@ public class AccessibleKeyboardViewProxy extends AccessibilityDelegateCompat { } switch (event.getAction()) { - case MotionEventCompatUtils.ACTION_HOVER_ENTER: + case MotionEvent.ACTION_HOVER_ENTER: sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER); break; - case MotionEventCompatUtils.ACTION_HOVER_EXIT: + case MotionEvent.ACTION_HOVER_EXIT: sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_VIEW_HOVER_EXIT); break; } @@ -214,7 +211,7 @@ public class AccessibleKeyboardViewProxy extends AccessibilityDelegateCompat { final AccessibilityEvent event = nodeProvider.createAccessibilityEvent(key, eventType); // Propagates the event up the view hierarchy. - ViewParentCompatUtils.requestSendAccessibilityEvent(mView.getParent(), mView, event); + mView.getParent().requestSendAccessibilityEvent(mView, event); } private class KeyboardFlickGestureDetector extends FlickGestureDetector { diff --git a/java/src/com/android/inputmethod/accessibility/FlickGestureDetector.java b/java/src/com/android/inputmethod/accessibility/FlickGestureDetector.java index eaa4ddff6..e8ec37600 100644 --- a/java/src/com/android/inputmethod/accessibility/FlickGestureDetector.java +++ b/java/src/com/android/inputmethod/accessibility/FlickGestureDetector.java @@ -18,11 +18,9 @@ package com.android.inputmethod.accessibility; import android.content.Context; import android.os.Message; -import android.support.v4.view.MotionEventCompat; import android.view.MotionEvent; import android.view.ViewConfiguration; -import com.android.inputmethod.compat.MotionEventCompatUtils; import com.android.inputmethod.keyboard.PointerTracker; import com.android.inputmethod.latin.StaticInnerHandlerWrapper; @@ -32,10 +30,10 @@ import com.android.inputmethod.latin.StaticInnerHandlerWrapper; * A flick gesture is defined as a stream of hover events with the following * properties: * <ul> - * <li>Begins with a {@link MotionEventCompatUtils#ACTION_HOVER_ENTER} event - * <li>Contains any number of {@link MotionEventCompat#ACTION_HOVER_MOVE} + * <li>Begins with a {@link MotionEvent#ACTION_HOVER_ENTER} event + * <li>Contains any number of {@link MotionEvent#ACTION_HOVER_MOVE} * events - * <li>Ends with a {@link MotionEventCompatUtils#ACTION_HOVER_EXIT} event + * <li>Ends with a {@link MotionEvent#ACTION_HOVER_EXIT} event * <li>Maximum duration of 250 milliseconds * <li>Minimum distance between enter and exit points must be at least equal to * scaled double tap slop (see @@ -113,7 +111,7 @@ public abstract class FlickGestureDetector { public boolean onHoverEvent(MotionEvent event, AccessibleKeyboardViewProxy view, PointerTracker tracker) { // Always cache and consume the first hover event. - if (event.getAction() == MotionEventCompatUtils.ACTION_HOVER_ENTER) { + if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) { mCachedView = view; mCachedTracker = tracker; mCachedHoverEnter = MotionEvent.obtain(event); @@ -129,10 +127,10 @@ public abstract class FlickGestureDetector { final float distanceSquare = calculateDistanceSquare(mCachedHoverEnter, event); switch (event.getAction()) { - case MotionEventCompat.ACTION_HOVER_MOVE: + case MotionEvent.ACTION_HOVER_MOVE: // Consume all valid move events before timeout. return true; - case MotionEventCompatUtils.ACTION_HOVER_EXIT: + case MotionEvent.ACTION_HOVER_EXIT: // Ignore exit events outside the flick radius. if (distanceSquare < mFlickRadiusSquare) { clearFlick(true); @@ -171,10 +169,8 @@ public abstract class FlickGestureDetector { * Computes the direction of a flick gesture and forwards it to * {@link #onFlick(MotionEvent, MotionEvent, int)} for handling. * - * @param e1 The {@link MotionEventCompatUtils#ACTION_HOVER_ENTER} event - * where the flick started. - * @param e2 The {@link MotionEventCompatUtils#ACTION_HOVER_EXIT} event - * where the flick ended. + * @param e1 The {@link MotionEvent#ACTION_HOVER_ENTER} event where the flick started. + * @param e2 The {@link MotionEvent#ACTION_HOVER_EXIT} event where the flick ended. * @return {@code true} if the flick event was handled. */ private boolean dispatchFlick(MotionEvent e1, MotionEvent e2) { diff --git a/java/src/com/android/inputmethod/compat/AbstractCompatWrapper.java b/java/src/com/android/inputmethod/compat/AbstractCompatWrapper.java deleted file mode 100644 index 2c31c55b0..000000000 --- a/java/src/com/android/inputmethod/compat/AbstractCompatWrapper.java +++ /dev/null @@ -1,39 +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.util.Log; - -public abstract class AbstractCompatWrapper { - private static final String TAG = AbstractCompatWrapper.class.getSimpleName(); - protected final Object mObj; - - public AbstractCompatWrapper(Object obj) { - if (obj == null) { - Log.e(TAG, "Invalid input to AbstractCompatWrapper"); - } - mObj = obj; - } - - public Object getOriginalObject() { - return mObj; - } - - public boolean hasOriginalObject() { - return mObj != null; - } -} diff --git a/java/src/com/android/inputmethod/compat/AccessibilityManagerCompatUtils.java b/java/src/com/android/inputmethod/compat/AccessibilityManagerCompatUtils.java deleted file mode 100644 index 41b6a074d..000000000 --- a/java/src/com/android/inputmethod/compat/AccessibilityManagerCompatUtils.java +++ /dev/null @@ -1,34 +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.view.accessibility.AccessibilityManager; - -import java.lang.reflect.Method; - -public class AccessibilityManagerCompatUtils { - private static final Method METHOD_isTouchExplorationEnabled = CompatUtils.getMethod( - AccessibilityManager.class, "isTouchExplorationEnabled"); - - private AccessibilityManagerCompatUtils() { - // This class is non-instantiable. - } - - public static boolean isTouchExplorationEnabled(AccessibilityManager receiver) { - return (Boolean) CompatUtils.invoke(receiver, false, METHOD_isTouchExplorationEnabled); - } -} diff --git a/java/src/com/android/inputmethod/compat/CompatUtils.java b/java/src/com/android/inputmethod/compat/CompatUtils.java index ba82a06ac..ce427e9c9 100644 --- a/java/src/com/android/inputmethod/compat/CompatUtils.java +++ b/java/src/com/android/inputmethod/compat/CompatUtils.java @@ -23,8 +23,6 @@ import android.util.Log; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; public class CompatUtils { private static final String TAG = CompatUtils.class.getSimpleName(); @@ -35,23 +33,16 @@ public class CompatUtils { public static Intent getInputLanguageSelectionIntent(String inputMethodId, int flagsForSubtypeSettings) { - final String action; - Intent intent; - if (InputMethodServiceCompatWrapper.CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED - /* android.os.Build.VERSION_CODES.HONEYCOMB */ - && android.os.Build.VERSION.SDK_INT >= 11) { - // Refer to android.provider.Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS - action = INPUT_METHOD_SUBTYPE_SETTINGS; - intent = new Intent(action); - if (!TextUtils.isEmpty(inputMethodId)) { - intent.putExtra(EXTRA_INPUT_METHOD_ID, inputMethodId); - } - if (flagsForSubtypeSettings > 0) { - intent.setFlags(flagsForSubtypeSettings); - } - return intent; + // Refer to android.provider.Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS + final String action = INPUT_METHOD_SUBTYPE_SETTINGS; + final Intent intent = new Intent(action); + if (!TextUtils.isEmpty(inputMethodId)) { + intent.putExtra(EXTRA_INPUT_METHOD_ID, inputMethodId); } - throw new RuntimeException("Language selection doesn't supported on this platform"); + if (flagsForSubtypeSettings > 0) { + intent.setFlags(flagsForSubtypeSettings); + } + return intent; } public static Class<?> getClass(String className) { @@ -138,15 +129,4 @@ public class CompatUtils { Log.e(TAG, "Exception in setFieldValue: " + e.getClass().getSimpleName()); } } - - public static List<InputMethodSubtypeCompatWrapper> copyInputMethodSubtypeListToWrapper( - Object listObject) { - if (!(listObject instanceof List<?>)) return null; - final List<InputMethodSubtypeCompatWrapper> subtypes = - new ArrayList<InputMethodSubtypeCompatWrapper>(); - for (Object o: (List<?>)listObject) { - subtypes.add(new InputMethodSubtypeCompatWrapper(o)); - } - return subtypes; - } } diff --git a/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java b/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java index 938388d6c..08c246f8b 100644 --- a/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java @@ -17,56 +17,26 @@ package com.android.inputmethod.compat; import android.view.inputmethod.EditorInfo; -import android.view.inputmethod.InputConnection; import java.lang.reflect.Field; public class EditorInfoCompatUtils { - private static final Field FIELD_IME_FLAG_NAVIGATE_NEXT = CompatUtils.getField( - EditorInfo.class, "IME_FLAG_NAVIGATE_NEXT"); - private static final Field FIELD_IME_FLAG_NAVIGATE_PREVIOUS = CompatUtils.getField( - EditorInfo.class, "IME_FLAG_NAVIGATE_PREVIOUS"); + // EditorInfo.IME_FLAG_FORCE_ASCII has been introduced since API#16 (JellyBean). 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; + private EditorInfoCompatUtils() { + // This utility class is not publicly instantiable. } - // 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 || ic == null) - return; - ic.performEditorAction(OBJ_IME_ACTION_PREVIOUS); - } - public static String imeActionName(int imeOptions) { final int actionId = imeOptions & EditorInfo.IME_MASK_ACTION; switch (actionId) { @@ -84,12 +54,10 @@ public class EditorInfoCompatUtils { return "actionNext"; case EditorInfo.IME_ACTION_DONE: return "actionDone"; + case EditorInfo.IME_ACTION_PREVIOUS: + return "actionPrevious"; default: - if (OBJ_IME_ACTION_PREVIOUS != null && actionId == OBJ_IME_ACTION_PREVIOUS) { - return "actionPrevious"; - } else { - return "actionUnknown(" + actionId + ")"; - } + return "actionUnknown(" + actionId + ")"; } } @@ -99,10 +67,10 @@ public class EditorInfoCompatUtils { if ((imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) { flags.append("flagNoEnterAction|"); } - if (hasFlagNavigateNext(imeOptions)) { + if ((imeOptions & EditorInfo.IME_FLAG_NAVIGATE_NEXT) != 0) { flags.append("flagNavigateNext|"); } - if (hasFlagNavigatePrevious(imeOptions)) { + if ((imeOptions & EditorInfo.IME_FLAG_NAVIGATE_PREVIOUS) != 0) { flags.append("flagNavigatePrevious|"); } if (hasFlagForceAscii(imeOptions)) { diff --git a/java/src/com/android/inputmethod/compat/InputMethodInfoCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodInfoCompatWrapper.java deleted file mode 100644 index 831559809..000000000 --- a/java/src/com/android/inputmethod/compat/InputMethodInfoCompatWrapper.java +++ /dev/null @@ -1,77 +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.pm.PackageManager; -import android.content.pm.ServiceInfo; -import android.view.inputmethod.InputMethodInfo; - -import java.lang.reflect.Method; - -public class InputMethodInfoCompatWrapper { - private final InputMethodInfo mImi; - private static final Method METHOD_getSubtypeAt = CompatUtils.getMethod( - InputMethodInfo.class, "getSubtypeAt", int.class); - private static final Method METHOD_getSubtypeCount = CompatUtils.getMethod( - InputMethodInfo.class, "getSubtypeCount"); - - public InputMethodInfoCompatWrapper(InputMethodInfo imi) { - mImi = imi; - } - - public InputMethodInfo getInputMethodInfo() { - return mImi; - } - - public String getId() { - return mImi.getId(); - } - - public String getPackageName() { - return mImi.getPackageName(); - } - - public ServiceInfo getServiceInfo() { - return mImi.getServiceInfo(); - } - - public int getSubtypeCount() { - return (Integer) CompatUtils.invoke(mImi, 0, METHOD_getSubtypeCount); - } - - public InputMethodSubtypeCompatWrapper getSubtypeAt(int index) { - return new InputMethodSubtypeCompatWrapper(CompatUtils.invoke(mImi, null, - METHOD_getSubtypeAt, index)); - } - - public CharSequence loadLabel(PackageManager pm) { - return mImi.loadLabel(pm); - } - - @Override - public boolean equals(Object o) { - if (o instanceof InputMethodInfoCompatWrapper) { - return mImi.equals(((InputMethodInfoCompatWrapper)o).mImi); - } - return false; - } - - @Override - public int hashCode() { - return mImi.hashCode(); - } -} diff --git a/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java index 3df6bea4b..ffed8202d 100644 --- a/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java +++ b/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java @@ -16,69 +16,33 @@ package com.android.inputmethod.compat; -import android.app.AlertDialog; import android.content.Context; -import android.content.DialogInterface; -import android.content.DialogInterface.OnClickListener; -import android.content.Intent; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; +import android.inputmethodservice.InputMethodService; import android.os.IBinder; -import android.text.TextUtils; import android.util.Log; import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodManager; - -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.SubtypeSwitcher; -import com.android.inputmethod.latin.SubtypeUtils; +import android.view.inputmethod.InputMethodSubtype; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; import java.util.List; -import java.util.Locale; import java.util.Map; // TODO: Override this class with the concrete implementation if we need to take care of the // performance. 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); - private static final Method METHOD_getShortcutInputMethodsAndSubtypes = - CompatUtils.getMethod(InputMethodManager.class, "getShortcutInputMethodsAndSubtypes"); - private static final Method METHOD_setInputMethodAndSubtype = - CompatUtils.getMethod( - InputMethodManager.class, "setInputMethodAndSubtype", IBinder.class, - 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(); - // 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 = - !InputMethodServiceCompatWrapper.CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED; - private static final String VOICE_MODE = "voice"; - private static final String KEYBOARD_MODE = "keyboard"; - - private InputMethodServiceCompatWrapper mService; private InputMethodManager mImm; - private PackageManager mPackageManager; - private ApplicationInfo mApplicationInfo; - private String mLatinImePackageName; + + private InputMethodManagerCompatWrapper() { + // This wrapper class is not publicly instantiable. + } public static InputMethodManagerCompatWrapper getInstance() { if (sInstance.mImm == null) @@ -86,120 +50,35 @@ public class InputMethodManagerCompatWrapper { return sInstance; } - public static void init(InputMethodServiceCompatWrapper service) { - sInstance.mService = service; + public static void init(InputMethodService service) { sInstance.mImm = (InputMethodManager) service.getSystemService( Context.INPUT_METHOD_SERVICE); - sInstance.mLatinImePackageName = service.getPackageName(); - sInstance.mPackageManager = service.getPackageManager(); - sInstance.mApplicationInfo = service.getApplicationInfo(); - } - - public InputMethodSubtypeCompatWrapper getCurrentInputMethodSubtype() { - 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) { - Object retval = CompatUtils.invoke(mImm, null, METHOD_getEnabledInputMethodSubtypeList, - (imi != null ? imi.getInputMethodInfo() : null), allowsImplicitlySelectedSubtypes); - if (retval == null || !(retval instanceof List<?>) || ((List<?>)retval).isEmpty()) { - if (!FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES) { - // Returns an empty list - return Collections.emptyList(); - } - // Creates dummy subtypes - @SuppressWarnings("unused") - List<InputMethodSubtypeCompatWrapper> subtypeList = - new ArrayList<InputMethodSubtypeCompatWrapper>(); - InputMethodSubtypeCompatWrapper keyboardSubtype = getLastResortSubtype(KEYBOARD_MODE); - InputMethodSubtypeCompatWrapper voiceSubtype = getLastResortSubtype(VOICE_MODE); - if (keyboardSubtype != null) { - subtypeList.add(keyboardSubtype); - } - if (voiceSubtype != null) { - subtypeList.add(voiceSubtype); - } - return subtypeList; - } - return CompatUtils.copyInputMethodSubtypeListToWrapper(retval); + public InputMethodSubtype getCurrentInputMethodSubtype() { + return mImm.getCurrentInputMethodSubtype(); } - private InputMethodInfoCompatWrapper getLatinImeInputMethodInfo() { - if (TextUtils.isEmpty(mLatinImePackageName)) - return null; - return SubtypeUtils.getInputMethodInfo(mLatinImePackageName); + public InputMethodSubtype getLastInputMethodSubtype() { + return mImm.getLastInputMethodSubtype(); } - 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(); - if (inputLocale == null) - return null; - return new InputMethodSubtypeCompatWrapper(0, 0, inputLocale.toString(), mode, ""); + public List<InputMethodSubtype> getEnabledInputMethodSubtypeList( + InputMethodInfo imi, boolean allowsImplicitlySelectedSubtypes) { + return mImm.getEnabledInputMethodSubtypeList(imi, allowsImplicitlySelectedSubtypes); } - public Map<InputMethodInfoCompatWrapper, List<InputMethodSubtypeCompatWrapper>> - getShortcutInputMethodsAndSubtypes() { - Object retval = CompatUtils.invoke(mImm, null, METHOD_getShortcutInputMethodsAndSubtypes); - if (retval == null || !(retval instanceof Map<?, ?>) || ((Map<?, ?>)retval).isEmpty()) { - if (!FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES) { - // Returns an empty map - return Collections.emptyMap(); - } - // Creates dummy subtypes - @SuppressWarnings("unused") - InputMethodInfoCompatWrapper imi = getLatinImeInputMethodInfo(); - InputMethodSubtypeCompatWrapper voiceSubtype = getLastResortSubtype(VOICE_MODE); - if (imi != null && voiceSubtype != null) { - Map<InputMethodInfoCompatWrapper, List<InputMethodSubtypeCompatWrapper>> - shortcutMap = - new HashMap<InputMethodInfoCompatWrapper, - List<InputMethodSubtypeCompatWrapper>>(); - List<InputMethodSubtypeCompatWrapper> subtypeList = - new ArrayList<InputMethodSubtypeCompatWrapper>(); - subtypeList.add(voiceSubtype); - shortcutMap.put(imi, subtypeList); - return shortcutMap; - } else { - return Collections.emptyMap(); - } - } - Map<InputMethodInfoCompatWrapper, List<InputMethodSubtypeCompatWrapper>> shortcutMap = - new HashMap<InputMethodInfoCompatWrapper, List<InputMethodSubtypeCompatWrapper>>(); - final Map<?, ?> retvalMap = (Map<?, ?>)retval; - for (Object key : retvalMap.keySet()) { - if (!(key instanceof InputMethodInfo)) { - Log.e(TAG, "Class type error."); - return null; - } - shortcutMap.put(new InputMethodInfoCompatWrapper((InputMethodInfo)key), - CompatUtils.copyInputMethodSubtypeListToWrapper(retvalMap.get(key))); - } - return shortcutMap; + public Map<InputMethodInfo, List<InputMethodSubtype>> getShortcutInputMethodsAndSubtypes() { + return mImm.getShortcutInputMethodsAndSubtypes(); } // We don't call this method when we switch between subtypes within this IME. - public void setInputMethodAndSubtype( - IBinder token, String id, InputMethodSubtypeCompatWrapper subtype) { - // TODO: Support subtype change on non-subtype-supported platform. - if (subtype != null && subtype.hasOriginalObject()) { - CompatUtils.invoke(mImm, null, METHOD_setInputMethodAndSubtype, - token, id, subtype.getOriginalObject()); - } else { - mImm.setInputMethod(token, id); - } + public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) { + mImm.setInputMethodAndSubtype(token, id, subtype); } public boolean switchToLastInputMethod(IBinder token) { - return (Boolean)CompatUtils.invoke(mImm, false, METHOD_switchToLastInputMethod, token); + return mImm.switchToLastInputMethod(token); } public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) { @@ -207,13 +86,9 @@ public class InputMethodManagerCompatWrapper { onlyCurrentIme); } - public List<InputMethodInfoCompatWrapper> getEnabledInputMethodList() { + public List<InputMethodInfo> getEnabledInputMethodList() { if (mImm == null) return null; - List<InputMethodInfoCompatWrapper> imis = new ArrayList<InputMethodInfoCompatWrapper>(); - for (InputMethodInfo imi : mImm.getEnabledInputMethodList()) { - imis.add(new InputMethodInfoCompatWrapper(imi)); - } - return imis; + return mImm.getEnabledInputMethodList(); } public void showInputMethodPicker() { diff --git a/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java deleted file mode 100644 index 7c15be300..000000000 --- a/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java +++ /dev/null @@ -1,109 +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.app.AlertDialog; -import android.inputmethodservice.InputMethodService; -import android.os.IBinder; -import android.view.Window; -import android.view.WindowManager; -import android.view.inputmethod.InputMethodSubtype; - -import com.android.inputmethod.keyboard.KeyboardSwitcher; -import com.android.inputmethod.latin.SubtypeSwitcher; - -public class InputMethodServiceCompatWrapper extends InputMethodService { - // CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED needs to be false if the API level is 10 - // or previous. Note that InputMethodSubtype was added in the API level 11. - // For the API level 11 or later, LatinIME should override onCurrentInputMethodSubtypeChanged(). - // For the API level 10 or previous, we handle the "subtype changed" events by ourselves - // without having support from framework -- onCurrentInputMethodSubtypeChanged(). - public static final boolean CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED = true; - - private InputMethodManagerCompatWrapper mImm; - - // For compatibility of {@link InputMethodManager#showInputMethodPicker}. - // TODO: Move this variable back to LatinIME when this compatibility wrapper is removed. - protected AlertDialog mOptionsDialog; - - public void showOptionDialogInternal(AlertDialog dialog) { - final IBinder windowToken = KeyboardSwitcher.getInstance().getKeyboardView() - .getWindowToken(); - if (windowToken == null) return; - - dialog.setCancelable(true); - dialog.setCanceledOnTouchOutside(true); - - final Window window = dialog.getWindow(); - final WindowManager.LayoutParams lp = window.getAttributes(); - lp.token = windowToken; - lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG; - window.setAttributes(lp); - window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); - - mOptionsDialog = dialog; - dialog.show(); - } - - @Override - public void onCreate() { - super.onCreate(); - mImm = InputMethodManagerCompatWrapper.getInstance(); - } - - // When the API level is 10 or previous, notifyOnCurrentInputMethodSubtypeChanged should - // handle the event the current subtype was changed. LatinIME calls - // notifyOnCurrentInputMethodSubtypeChanged every time LatinIME - // changes the current subtype. - // This call is required to let LatinIME itself know a subtype changed - // event when the API level is 10 or previous. - @SuppressWarnings("unused") - public void notifyOnCurrentInputMethodSubtypeChanged( - InputMethodSubtypeCompatWrapper newSubtype) { - // Do nothing when the API level is 11 or later - // and FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES is not true - if (CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED && !InputMethodManagerCompatWrapper. - FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES) { - return; - } - final InputMethodSubtypeCompatWrapper subtype = (newSubtype == null) - ? mImm.getCurrentInputMethodSubtype() - : newSubtype; - if (subtype != null) { - if (!InputMethodManagerCompatWrapper.FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES - && !subtype.isDummy()) return; - SubtypeSwitcher.getInstance().updateSubtype(subtype); - } - } - - ////////////////////////////////////// - // Functions using API v11 or later // - ////////////////////////////////////// - @Override - public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype) { - // Do nothing when the API level is 10 or previous - if (!CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED) return; - SubtypeSwitcher.getInstance().updateSubtype( - new InputMethodSubtypeCompatWrapper(subtype)); - } - - protected static void setTouchableRegionCompat(InputMethodService.Insets outInsets, - int x, int y, int width, int height) { - outInsets.touchableInsets = InputMethodService.Insets.TOUCHABLE_INSETS_REGION; - outInsets.touchableRegion.set(x, y, width, height); - } -} diff --git a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java deleted file mode 100644 index 574158825..000000000 --- a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java +++ /dev/null @@ -1,188 +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.content.pm.ApplicationInfo; -import android.text.TextUtils; -import android.util.Log; - -import com.android.inputmethod.latin.LatinImeLogger; - -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.Locale; - -// TODO: Override this class with the concrete implementation if we need to take care of the -// performance. -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"; - private static final String DEFAULT_MODE = "keyboard"; - - public static final Class<?> CLASS_InputMethodSubtype = - CompatUtils.getClass("android.view.inputmethod.InputMethodSubtype"); - private static final Method METHOD_getNameResId = - CompatUtils.getMethod(CLASS_InputMethodSubtype, "getNameResId"); - private static final Method METHOD_getIconResId = - CompatUtils.getMethod(CLASS_InputMethodSubtype, "getIconResId"); - private static final Method METHOD_getLocale = - CompatUtils.getMethod(CLASS_InputMethodSubtype, "getLocale"); - private static final Method METHOD_getMode = - CompatUtils.getMethod(CLASS_InputMethodSubtype, "getMode"); - private static final Method METHOD_getExtraValue = - CompatUtils.getMethod(CLASS_InputMethodSubtype, "getExtraValue"); - private static final Method METHOD_containsExtraValueKey = - CompatUtils.getMethod(CLASS_InputMethodSubtype, "containsExtraValueKey", String.class); - private static final Method METHOD_getExtraValueOf = - CompatUtils.getMethod(CLASS_InputMethodSubtype, "getExtraValueOf", String.class); - private static final Method METHOD_isAuxiliary = - CompatUtils.getMethod(CLASS_InputMethodSubtype, "isAuxiliary"); - private static final Method METHOD_getDisplayName = - CompatUtils.getMethod(CLASS_InputMethodSubtype, "getDisplayName", Context.class, - String.class, ApplicationInfo.class); - - private final int mDummyNameResId; - private final int mDummyIconResId; - private final String mDummyLocale; - private final String mDummyMode; - private final String mDummyExtraValues; - - public InputMethodSubtypeCompatWrapper(Object subtype) { - super((CLASS_InputMethodSubtype != null && CLASS_InputMethodSubtype.isInstance(subtype)) - ? subtype : new Object()); - mDummyNameResId = 0; - mDummyIconResId = 0; - mDummyLocale = DEFAULT_LOCALE; - mDummyMode = DEFAULT_MODE; - mDummyExtraValues = ""; - } - - // Constructor for creating a dummy subtype. - public InputMethodSubtypeCompatWrapper(int nameResId, int iconResId, String locale, - String mode, String extraValues) { - super(new Object()); - if (DBG) { - Log.d(TAG, "CreateInputMethodSubtypeCompatWrapper"); - } - mDummyNameResId = nameResId; - mDummyIconResId = iconResId; - mDummyLocale = locale != null ? locale : ""; - mDummyMode = mode != null ? mode : ""; - mDummyExtraValues = extraValues != null ? extraValues : ""; - } - - public int getNameResId() { - if (mObj == null) return mDummyNameResId; - return (Integer)CompatUtils.invoke(mObj, 0, METHOD_getNameResId); - } - - public int getIconResId() { - if (mObj == null) return mDummyIconResId; - return (Integer)CompatUtils.invoke(mObj, 0, METHOD_getIconResId); - } - - public String getLocale() { - if (mObj == null) return mDummyLocale; - final String s = (String)CompatUtils.invoke(mObj, null, METHOD_getLocale); - return s != null ? s : DEFAULT_LOCALE; - } - - public String getMode() { - if (mObj == null) return mDummyMode; - String s = (String)CompatUtils.invoke(mObj, null, METHOD_getMode); - if (TextUtils.isEmpty(s)) return DEFAULT_MODE; - return s; - } - - public String getExtraValue() { - if (mObj == null) return mDummyExtraValues; - return (String)CompatUtils.invoke(mObj, null, METHOD_getExtraValue); - } - - public boolean containsExtraValueKey(String key) { - return (Boolean)CompatUtils.invoke(mObj, false, METHOD_containsExtraValueKey, key); - } - - public String getExtraValueOf(String key) { - return (String)CompatUtils.invoke(mObj, null, METHOD_getExtraValueOf, key); - } - - public boolean isAuxiliary() { - return (Boolean)CompatUtils.invoke(mObj, false, METHOD_isAuxiliary); - } - - public CharSequence getDisplayName(Context context, String packageName, - ApplicationInfo appInfo) { - if (mObj != null) { - return (CharSequence)CompatUtils.invoke( - mObj, "", METHOD_getDisplayName, context, packageName, appInfo); - } - - // The code below are based on {@link InputMethodSubtype#getDisplayName}. - - final Locale locale = new Locale(getLocale()); - final String localeStr = locale.getDisplayName(); - if (getNameResId() == 0) { - return localeStr; - } - final CharSequence subtypeName = context.getText(getNameResId()); - if (!TextUtils.isEmpty(localeStr)) { - return String.format(subtypeName.toString(), localeStr); - } else { - return localeStr; - } - } - - public boolean isDummy() { - return !hasOriginalObject(); - } - - @Override - public boolean equals(Object o) { - if (o instanceof InputMethodSubtypeCompatWrapper) { - InputMethodSubtypeCompatWrapper subtype = (InputMethodSubtypeCompatWrapper)o; - if (mObj == null) { - // easy check of dummy subtypes - return (mDummyNameResId == subtype.mDummyNameResId - && mDummyIconResId == subtype.mDummyIconResId - && mDummyLocale.equals(subtype.mDummyLocale) - && mDummyMode.equals(subtype.mDummyMode) - && mDummyExtraValues.equals(subtype.mDummyExtraValues)); - } - return mObj.equals(subtype.getOriginalObject()); - } else { - return mObj.equals(o); - } - } - - @Override - public int hashCode() { - if (mObj == null) { - return hashCodeInternal(mDummyNameResId, mDummyIconResId, mDummyLocale, - mDummyMode, mDummyExtraValues); - } - return mObj.hashCode(); - } - - private static int hashCodeInternal(int nameResId, int iconResId, String locale, - String mode, String extraValue) { - return Arrays - .hashCode(new Object[] { nameResId, iconResId, locale, mode, extraValue }); - } -} diff --git a/java/src/com/android/inputmethod/compat/InputTypeCompatUtils.java b/java/src/com/android/inputmethod/compat/InputTypeCompatUtils.java deleted file mode 100644 index 6c2f0f799..000000000 --- a/java/src/com/android/inputmethod/compat/InputTypeCompatUtils.java +++ /dev/null @@ -1,118 +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.text.InputType; - -import java.lang.reflect.Field; - -public class InputTypeCompatUtils { - private static final Field FIELD_InputType_TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS = - CompatUtils.getField(InputType.class, "TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS"); - private static final Field FIELD_InputType_TYPE_TEXT_VARIATION_WEB_PASSWORD = CompatUtils - .getField(InputType.class, "TYPE_TEXT_VARIATION_WEB_PASSWORD"); - private static final Field FIELD_InputType_TYPE_NUMBER_VARIATION_PASSWORD = CompatUtils - .getField(InputType.class, "TYPE_NUMBER_VARIATION_PASSWORD"); - private static final Integer OBJ_InputType_TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS = - (Integer) CompatUtils.getFieldValue(null, null, - FIELD_InputType_TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS); - private static final Integer OBJ_InputType_TYPE_TEXT_VARIATION_WEB_PASSWORD = - (Integer) CompatUtils.getFieldValue(null, null, - FIELD_InputType_TYPE_TEXT_VARIATION_WEB_PASSWORD); - private static final Integer OBJ_InputType_TYPE_NUMBER_VARIATION_PASSWORD = - (Integer) CompatUtils.getFieldValue(null, null, - FIELD_InputType_TYPE_NUMBER_VARIATION_PASSWORD); - private static final int WEB_TEXT_PASSWORD_INPUT_TYPE; - private static final int WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE; - private static final int NUMBER_PASSWORD_INPUT_TYPE; - private static final int TEXT_PASSWORD_INPUT_TYPE = - InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD; - private static final int TEXT_VISIBLE_PASSWORD_INPUT_TYPE = - InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD; - - static { - WEB_TEXT_PASSWORD_INPUT_TYPE = - OBJ_InputType_TYPE_TEXT_VARIATION_WEB_PASSWORD != null - ? InputType.TYPE_CLASS_TEXT | OBJ_InputType_TYPE_TEXT_VARIATION_WEB_PASSWORD - : 0; - WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE = - OBJ_InputType_TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS != null - ? InputType.TYPE_CLASS_TEXT - | OBJ_InputType_TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS - : 0; - NUMBER_PASSWORD_INPUT_TYPE = - OBJ_InputType_TYPE_NUMBER_VARIATION_PASSWORD != null - ? InputType.TYPE_CLASS_NUMBER | OBJ_InputType_TYPE_NUMBER_VARIATION_PASSWORD - : 0; - } - - private static boolean isWebEditTextInputType(int inputType) { - return inputType == (InputType.TYPE_CLASS_TEXT - | InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT); - } - - private static boolean isWebPasswordInputType(int inputType) { - return WEB_TEXT_PASSWORD_INPUT_TYPE != 0 - && inputType == WEB_TEXT_PASSWORD_INPUT_TYPE; - } - - private static boolean isWebEmailAddressInputType(int inputType) { - return WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE != 0 - && inputType == WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE; - } - - private static boolean isNumberPasswordInputType(int inputType) { - return NUMBER_PASSWORD_INPUT_TYPE != 0 - && inputType == NUMBER_PASSWORD_INPUT_TYPE; - } - - private static boolean isTextPasswordInputType(int inputType) { - return inputType == TEXT_PASSWORD_INPUT_TYPE; - } - - private static boolean isWebEmailAddressVariation(int variation) { - return OBJ_InputType_TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS != null - && variation == OBJ_InputType_TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS; - } - - public static boolean isEmailVariation(int variation) { - return variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS - || isWebEmailAddressVariation(variation); - } - - public static boolean isWebInputType(int inputType) { - final int maskedInputType = - inputType & (InputType.TYPE_MASK_CLASS | InputType.TYPE_MASK_VARIATION); - return isWebEditTextInputType(maskedInputType) || isWebPasswordInputType(maskedInputType) - || isWebEmailAddressInputType(maskedInputType); - } - - // Please refer to TextView.isPasswordInputType - public static boolean isPasswordInputType(int inputType) { - final int maskedInputType = - inputType & (InputType.TYPE_MASK_CLASS | InputType.TYPE_MASK_VARIATION); - return isTextPasswordInputType(maskedInputType) || isWebPasswordInputType(maskedInputType) - || isNumberPasswordInputType(maskedInputType); - } - - // Please refer to TextView.isVisiblePasswordInputType - public static boolean isVisiblePasswordInputType(int inputType) { - final int maskedInputType = - inputType & (InputType.TYPE_MASK_CLASS | InputType.TYPE_MASK_VARIATION); - return maskedInputType == TEXT_VISIBLE_PASSWORD_INPUT_TYPE; - } -} diff --git a/java/src/com/android/inputmethod/compat/MotionEventCompatUtils.java b/java/src/com/android/inputmethod/compat/MotionEventCompatUtils.java deleted file mode 100644 index 9a523011a..000000000 --- a/java/src/com/android/inputmethod/compat/MotionEventCompatUtils.java +++ /dev/null @@ -1,23 +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 MotionEventCompatUtils { - // 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/SuggestionSpanUtils.java b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java index df55aee94..5c351e41f 100644 --- a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java +++ b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java @@ -77,6 +77,10 @@ public class SuggestionSpanUtils { } } + private SuggestionSpanUtils() { + // This utility class is not publicly instantiable. + } + public static CharSequence getTextWithAutoCorrectionIndicatorUnderline( Context context, CharSequence text) { if (TextUtils.isEmpty(text) || CONSTRUCTOR_SuggestionSpan == null diff --git a/java/src/com/android/inputmethod/compat/SuggestionsInfoCompatUtils.java b/java/src/com/android/inputmethod/compat/SuggestionsInfoCompatUtils.java index 723ec2862..e5f9db27c 100644 --- a/java/src/com/android/inputmethod/compat/SuggestionsInfoCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/SuggestionsInfoCompatUtils.java @@ -30,6 +30,7 @@ public class SuggestionsInfoCompatUtils { ? OBJ_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS : 0; private SuggestionsInfoCompatUtils() { + // This utility class is not publicly instantiable. } /** diff --git a/java/src/com/android/inputmethod/compat/ViewParentCompatUtils.java b/java/src/com/android/inputmethod/compat/ViewParentCompatUtils.java deleted file mode 100644 index d19bc3af1..000000000 --- a/java/src/com/android/inputmethod/compat/ViewParentCompatUtils.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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); - } -} diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardId.java b/java/src/com/android/inputmethod/keyboard/KeyboardId.java index 3b2b11e4e..e35081867 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardId.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardId.java @@ -21,7 +21,7 @@ import android.text.TextUtils; import android.view.inputmethod.EditorInfo; import com.android.inputmethod.compat.EditorInfoCompatUtils; -import com.android.inputmethod.compat.InputTypeCompatUtils; +import com.android.inputmethod.latin.InputTypeUtils; import java.util.Arrays; import java.util.Locale; @@ -131,17 +131,17 @@ public class KeyboardId { } public boolean navigateNext() { - return EditorInfoCompatUtils.hasFlagNavigateNext(mEditorInfo.imeOptions); + return (mEditorInfo.imeOptions & EditorInfo.IME_FLAG_NAVIGATE_NEXT) != 0; } public boolean navigatePrevious() { - return EditorInfoCompatUtils.hasFlagNavigatePrevious(mEditorInfo.imeOptions); + return (mEditorInfo.imeOptions & EditorInfo.IME_FLAG_NAVIGATE_PREVIOUS) != 0; } public boolean passwordInput() { final int inputType = mEditorInfo.inputType; - return InputTypeCompatUtils.isPasswordInputType(inputType) - || InputTypeCompatUtils.isVisiblePasswordInputType(inputType); + return InputTypeUtils.isPasswordInputType(inputType) + || InputTypeUtils.isVisiblePasswordInputType(inputType); } public boolean isMultiLine() { diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSet.java b/java/src/com/android/inputmethod/keyboard/KeyboardSet.java index f0c773ff4..263f17f74 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSet.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSet.java @@ -27,11 +27,11 @@ import android.util.Xml; import android.view.inputmethod.EditorInfo; import com.android.inputmethod.compat.EditorInfoCompatUtils; -import com.android.inputmethod.compat.InputTypeCompatUtils; import com.android.inputmethod.keyboard.KeyboardSet.Params.ElementParams; +import com.android.inputmethod.latin.InputTypeUtils; import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinImeLogger; -import com.android.inputmethod.latin.LocaleUtils; +import com.android.inputmethod.latin.LocaleUtils.RunInLocale; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.StringUtils; import com.android.inputmethod.latin.XmlParseUtils; @@ -66,6 +66,7 @@ public class KeyboardSet { public static class KeyboardSetException extends RuntimeException { public final KeyboardId mKeyboardId; + public KeyboardSetException(Throwable cause, KeyboardId keyboardId) { super(cause); mKeyboardId = keyboardId; @@ -161,26 +162,29 @@ public class KeyboardSet { } } - private Keyboard getKeyboard(Context context, ElementParams elementParams, KeyboardId id) { - final Resources res = context.getResources(); + private Keyboard getKeyboard(Context context, ElementParams elementParams, + final KeyboardId id) { final SoftReference<Keyboard> ref = sKeyboardCache.get(id); Keyboard keyboard = (ref == null) ? null : ref.get(); if (keyboard == null) { - final Locale savedLocale = LocaleUtils.setSystemLocale(res, id.mLocale); - try { - final Keyboard.Builder<Keyboard.Params> builder = - new Keyboard.Builder<Keyboard.Params>(context, new Keyboard.Params()); - if (id.isAlphabetKeyboard()) { - builder.setAutoGenerate(sKeysCache); - } - builder.load(elementParams.mKeyboardXmlId, id); - builder.setTouchPositionCorrectionEnabled(mParams.mTouchPositionCorrectionEnabled); - builder.setProximityCharsCorrectionEnabled( - elementParams.mProximityCharsCorrectionEnabled); - keyboard = builder.build(); - } finally { - LocaleUtils.setSystemLocale(res, savedLocale); + final Keyboard.Builder<Keyboard.Params> builder = + new Keyboard.Builder<Keyboard.Params>(mContext, new Keyboard.Params()); + if (id.isAlphabetKeyboard()) { + builder.setAutoGenerate(sKeysCache); } + final int keyboardXmlId = elementParams.mKeyboardXmlId; + final RunInLocale<Void> job = new RunInLocale<Void>() { + @Override + protected Void job(Resources res) { + builder.load(keyboardXmlId, id); + return null; + } + }; + job.runInLocale(context.getResources(), id.mLocale); + builder.setTouchPositionCorrectionEnabled(mParams.mTouchPositionCorrectionEnabled); + builder.setProximityCharsCorrectionEnabled( + elementParams.mProximityCharsCorrectionEnabled); + keyboard = builder.build(); sKeyboardCache.put(id, new SoftReference<Keyboard>(keyboard)); if (DEBUG_CACHE) { @@ -237,7 +241,6 @@ public class KeyboardSet { return this; } - // TODO: Use InputMethodSubtype object as argument. public Builder setSubtype(Locale inputLocale, boolean asciiCapable) { final boolean deprecatedForceAscii = StringUtils.inPrivateImeOptions( mPackageName, LatinIME.IME_OPTION_FORCE_ASCII, mEditorInfo); @@ -272,16 +275,20 @@ public class KeyboardSet { if (mParams.mLocale == null) throw new RuntimeException("KeyboardSet subtype is not specified"); - final Locale savedLocale = LocaleUtils.setSystemLocale(mResources, mParams.mLocale); - try { - parseKeyboardSet(mResources, R.xml.keyboard_set); - } catch (Exception e) { - throw new RuntimeException(e.getMessage() + " in " - + mResources.getResourceName(R.xml.keyboard_set) - + " of locale " + mParams.mLocale); - } finally { - LocaleUtils.setSystemLocale(mResources, savedLocale); - } + final RunInLocale<Void> job = new RunInLocale<Void>() { + @Override + protected Void job(Resources res) { + try { + parseKeyboardSet(res, R.xml.keyboard_set); + } catch (Exception e) { + throw new RuntimeException(e.getMessage() + " in " + + res.getResourceName(R.xml.keyboard_set) + + " of locale " + mParams.mLocale); + } + return null; + } + }; + job.runInLocale(mResources, mParams.mLocale); return new KeyboardSet(mContext, mParams); } @@ -380,7 +387,7 @@ public class KeyboardSet { case InputType.TYPE_CLASS_PHONE: return KeyboardId.MODE_PHONE; case InputType.TYPE_CLASS_TEXT: - if (InputTypeCompatUtils.isEmailVariation(variation)) { + if (InputTypeUtils.isEmailVariation(variation)) { return KeyboardId.MODE_EMAIL; } else if (variation == InputType.TYPE_TEXT_VARIATION_URI) { return KeyboardId.MODE_URL; diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java index c1d11a086..b51dbb906 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java @@ -37,7 +37,6 @@ import android.view.ViewGroup; import android.widget.RelativeLayout; import android.widget.TextView; -import com.android.inputmethod.compat.FrameLayoutCompatUtils; import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.StaticInnerHandlerWrapper; @@ -853,7 +852,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { windowContentView.addView(mPreviewPlacer); } mPreviewPlacer.addView( - keyPreview, FrameLayoutCompatUtils.newLayoutParam(mPreviewPlacer, 0, 0)); + keyPreview, ViewLayoutUtils.newLayoutParam(mPreviewPlacer, 0, 0)); } private void showKey(PointerTracker tracker) { @@ -919,7 +918,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { previewText.getBackground().setState( key.mMoreKeys != null ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET); previewText.setTextColor(params.mPreviewTextColor); - FrameLayoutCompatUtils.placeViewAt( + ViewLayoutUtils.placeViewAt( previewText, previewX, previewY, previewWidth, previewHeight); previewText.setVisibility(VISIBLE); } diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java index 62bcf6ca8..e2af97185 100644 --- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java @@ -828,15 +828,13 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke } /** - * Receives hover events from the input framework. This method overrides - * View.dispatchHoverEvent(MotionEvent) on SDK version ICS or higher. On - * lower SDK versions, this method is never called. + * Receives hover events from the input framework. * * @param event The motion event to be dispatched. * @return {@code true} if the event was handled by the view, {@code false} * otherwise */ - //Should not annotate @override + @Override public boolean dispatchHoverEvent(MotionEvent event) { if (AccessibilityUtils.getInstance().isTouchExplorationEnabled()) { final PointerTracker tracker = PointerTracker.getPointerTracker(0, this); diff --git a/java/src/com/android/inputmethod/compat/FrameLayoutCompatUtils.java b/java/src/com/android/inputmethod/keyboard/ViewLayoutUtils.java index 523bf7d0e..ee5047083 100644 --- a/java/src/com/android/inputmethod/compat/FrameLayoutCompatUtils.java +++ b/java/src/com/android/inputmethod/keyboard/ViewLayoutUtils.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.android.inputmethod.compat; +package com.android.inputmethod.keyboard; import android.view.View; import android.view.ViewGroup; @@ -22,20 +22,9 @@ import android.view.ViewGroup.MarginLayoutParams; import android.widget.FrameLayout; import android.widget.RelativeLayout; -public class FrameLayoutCompatUtils { - private static final boolean NEEDS_FRAME_LAYOUT_HACK = ( - android.os.Build.VERSION.SDK_INT < 11 /* Honeycomb */); - - public static ViewGroup getPlacer(ViewGroup container) { - if (NEEDS_FRAME_LAYOUT_HACK) { - // Insert RelativeLayout to be able to setMargin because pre-Honeycomb FrameLayout - // could not handle setMargin properly. - final ViewGroup placer = new RelativeLayout(container.getContext()); - container.addView(placer); - return placer; - } else { - return container; - } +public class ViewLayoutUtils { + private ViewLayoutUtils() { + // This utility class is not publicly instantiable. } public static MarginLayoutParams newLayoutParam(ViewGroup placer, int width, int height) { diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index 1c24cd11d..e4d839690 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -23,6 +23,8 @@ import android.content.res.AssetFileDescriptor; import android.content.res.Resources; import android.util.Log; +import com.android.inputmethod.latin.LocaleUtils.RunInLocale; + import java.io.File; import java.util.ArrayList; import java.util.Locale; @@ -154,11 +156,13 @@ class BinaryDictionaryGetter { */ private static AssetFileAddress loadFallbackResource(final Context context, final int fallbackResId, final Locale locale) { - final Resources res = context.getResources(); - final Locale savedLocale = LocaleUtils.setSystemLocale(res, locale); - final AssetFileDescriptor afd = res.openRawResourceFd(fallbackResId); - LocaleUtils.setSystemLocale(res, savedLocale); - + final RunInLocale<AssetFileDescriptor> job = new RunInLocale<AssetFileDescriptor>() { + @Override + protected AssetFileDescriptor job(Resources res) { + return res.openRawResourceFd(fallbackResId); + } + }; + final AssetFileDescriptor afd = job.runInLocale(context.getResources(), locale); if (afd == null) { Log.e(TAG, "Found the resource but cannot read it. Is it compressed? resId=" + fallbackResId); diff --git a/java/src/com/android/inputmethod/latin/DictionaryFactory.java b/java/src/com/android/inputmethod/latin/DictionaryFactory.java index 77c685c50..7be374db5 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFactory.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFactory.java @@ -21,6 +21,8 @@ import android.content.res.AssetFileDescriptor; import android.content.res.Resources; import android.util.Log; +import com.android.inputmethod.latin.LocaleUtils.RunInLocale; + import java.io.File; import java.util.ArrayList; import java.util.LinkedList; @@ -30,7 +32,6 @@ import java.util.Locale; * Factory for dictionary instances. */ public class DictionaryFactory { - private static String TAG = DictionaryFactory.class.getSimpleName(); /** @@ -98,14 +99,13 @@ public class DictionaryFactory { final int resId, final Locale locale) { AssetFileDescriptor afd = null; try { - final Resources res = context.getResources(); - if (null != locale) { - final Locale savedLocale = LocaleUtils.setSystemLocale(res, locale); - afd = res.openRawResourceFd(resId); - LocaleUtils.setSystemLocale(res, savedLocale); - } else { - afd = res.openRawResourceFd(resId); - } + final RunInLocale<AssetFileDescriptor> job = new RunInLocale<AssetFileDescriptor>() { + @Override + protected AssetFileDescriptor job(Resources res) { + return res.openRawResourceFd(resId); + } + }; + afd = job.runInLocale(context.getResources(), locale); if (afd == null) { Log.e(TAG, "Found the resource but it is compressed. resId=" + resId); return null; @@ -161,39 +161,41 @@ public class DictionaryFactory { * @return whether a (non-placeholder) dictionary is available or not. */ public static boolean isDictionaryAvailable(Context context, Locale locale) { - final Resources res = context.getResources(); - final Locale saveLocale = LocaleUtils.setSystemLocale(res, locale); - - final int resourceId = getMainDictionaryResourceId(res); - final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); - final boolean hasDictionary = isFullDictionary(afd); - try { - if (null != afd) afd.close(); - } catch (java.io.IOException e) { - /* Um, what can we do here exactly? */ - } - - LocaleUtils.setSystemLocale(res, saveLocale); - return hasDictionary; + final RunInLocale<Boolean> job = new RunInLocale<Boolean>() { + @Override + protected Boolean job(Resources res) { + final int resourceId = getMainDictionaryResourceId(res); + final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); + final boolean hasDictionary = isFullDictionary(afd); + try { + if (null != afd) afd.close(); + } catch (java.io.IOException e) { + /* Um, what can we do here exactly? */ + } + return hasDictionary; + } + }; + return job.runInLocale(context.getResources(), locale); } // TODO: Do not use the size of the dictionary as an unique dictionary ID. public static Long getDictionaryId(final Context context, final Locale locale) { - final Resources res = context.getResources(); - final Locale saveLocale = LocaleUtils.setSystemLocale(res, locale); - - final int resourceId = getMainDictionaryResourceId(res); - final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); - final Long size = (afd != null && afd.getLength() > PLACEHOLDER_LENGTH) - ? afd.getLength() - : null; - try { - if (null != afd) afd.close(); - } catch (java.io.IOException e) { - } - - LocaleUtils.setSystemLocale(res, saveLocale); - return size; + final RunInLocale<Long> job = new RunInLocale<Long>() { + @Override + protected Long job(Resources res) { + final int resourceId = getMainDictionaryResourceId(res); + final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); + final Long size = (afd != null && afd.getLength() > PLACEHOLDER_LENGTH) + ? afd.getLength() + : null; + try { + if (null != afd) afd.close(); + } catch (java.io.IOException e) { + } + return size; + } + }; + return job.runInLocale(context.getResources(), locale); } // TODO: Find the Right Way to find out whether the resource is a placeholder or not. diff --git a/java/src/com/android/inputmethod/latin/InputAttributes.java b/java/src/com/android/inputmethod/latin/InputAttributes.java index 06c70c42c..a6ce04069 100644 --- a/java/src/com/android/inputmethod/latin/InputAttributes.java +++ b/java/src/com/android/inputmethod/latin/InputAttributes.java @@ -20,8 +20,6 @@ import android.text.InputType; import android.util.Log; import android.view.inputmethod.EditorInfo; -import com.android.inputmethod.compat.InputTypeCompatUtils; - /** * Class to hold attributes of the input field. */ @@ -66,9 +64,9 @@ public class InputAttributes { 0 != (inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE); // Make sure that passwords are not displayed in {@link SuggestionsView}. - if (InputTypeCompatUtils.isPasswordInputType(inputType) - || InputTypeCompatUtils.isVisiblePasswordInputType(inputType) - || InputTypeCompatUtils.isEmailVariation(variation) + if (InputTypeUtils.isPasswordInputType(inputType) + || InputTypeUtils.isVisiblePasswordInputType(inputType) + || InputTypeUtils.isEmailVariation(variation) || InputType.TYPE_TEXT_VARIATION_URI == variation || InputType.TYPE_TEXT_VARIATION_FILTER == variation || flagNoSuggestions diff --git a/java/src/com/android/inputmethod/latin/InputTypeUtils.java b/java/src/com/android/inputmethod/latin/InputTypeUtils.java new file mode 100644 index 000000000..40c3b765e --- /dev/null +++ b/java/src/com/android/inputmethod/latin/InputTypeUtils.java @@ -0,0 +1,90 @@ +/* + * 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.latin; + +import android.text.InputType; + +public class InputTypeUtils implements InputType { + private static final int WEB_TEXT_PASSWORD_INPUT_TYPE = + TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_PASSWORD; + private static final int WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE = + TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS; + private static final int NUMBER_PASSWORD_INPUT_TYPE = + TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_PASSWORD; + private static final int TEXT_PASSWORD_INPUT_TYPE = + TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD; + private static final int TEXT_VISIBLE_PASSWORD_INPUT_TYPE = + TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD; + + private InputTypeUtils() { + // This utility class is not publicly instantiable. + } + + private static boolean isWebEditTextInputType(int inputType) { + return inputType == (TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_EDIT_TEXT); + } + + private static boolean isWebPasswordInputType(int inputType) { + return WEB_TEXT_PASSWORD_INPUT_TYPE != 0 + && inputType == WEB_TEXT_PASSWORD_INPUT_TYPE; + } + + private static boolean isWebEmailAddressInputType(int inputType) { + return WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE != 0 + && inputType == WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE; + } + + private static boolean isNumberPasswordInputType(int inputType) { + return NUMBER_PASSWORD_INPUT_TYPE != 0 + && inputType == NUMBER_PASSWORD_INPUT_TYPE; + } + + private static boolean isTextPasswordInputType(int inputType) { + return inputType == TEXT_PASSWORD_INPUT_TYPE; + } + + private static boolean isWebEmailAddressVariation(int variation) { + return variation == TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS; + } + + public static boolean isEmailVariation(int variation) { + return variation == TYPE_TEXT_VARIATION_EMAIL_ADDRESS + || isWebEmailAddressVariation(variation); + } + + public static boolean isWebInputType(int inputType) { + final int maskedInputType = + inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION); + return isWebEditTextInputType(maskedInputType) || isWebPasswordInputType(maskedInputType) + || isWebEmailAddressInputType(maskedInputType); + } + + // Please refer to TextView.isPasswordInputType + public static boolean isPasswordInputType(int inputType) { + final int maskedInputType = + inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION); + return isTextPasswordInputType(maskedInputType) || isWebPasswordInputType(maskedInputType) + || isNumberPasswordInputType(maskedInputType); + } + + // Please refer to TextView.isVisiblePasswordInputType + public static boolean isVisiblePasswordInputType(int inputType) { + final int maskedInputType = + inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION); + return maskedInputType == TEXT_VISIBLE_PASSWORD_INPUT_TYPE; + } +} diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 69780d0fd..db57044e9 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -45,18 +45,18 @@ import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.ViewParent; +import android.view.Window; +import android.view.WindowManager; import android.view.inputmethod.CompletionInfo; import android.view.inputmethod.CorrectionInfo; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; +import android.view.inputmethod.InputMethodSubtype; import com.android.inputmethod.accessibility.AccessibilityUtils; import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy; import com.android.inputmethod.compat.CompatUtils; -import com.android.inputmethod.compat.EditorInfoCompatUtils; import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; -import com.android.inputmethod.compat.InputMethodServiceCompatWrapper; -import com.android.inputmethod.compat.InputMethodSubtypeCompatWrapper; import com.android.inputmethod.compat.SuggestionSpanUtils; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.KeyboardActionListener; @@ -64,6 +64,7 @@ import com.android.inputmethod.keyboard.KeyboardId; import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.keyboard.KeyboardView; import com.android.inputmethod.keyboard.LatinKeyboardView; +import com.android.inputmethod.latin.LocaleUtils.RunInLocale; import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.suggestions.SuggestionsView; @@ -75,7 +76,7 @@ import java.util.Locale; /** * Input method implementation for Qwerty'ish keyboard. */ -public class LatinIME extends InputMethodServiceCompatWrapper implements KeyboardActionListener, +public class LatinIME extends InputMethodService implements KeyboardActionListener, SuggestionsView.Listener { private static final String TAG = LatinIME.class.getSimpleName(); private static final boolean TRACE = false; @@ -225,6 +226,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar private boolean mIsAutoCorrectionIndicatorOn; + private AlertDialog mOptionsDialog; + public final UIHandler mHandler = new UIHandler(this); public static class UIHandler extends StaticInnerHandlerWrapper<LatinIME> { @@ -476,7 +479,13 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // Has to be package-visible for unit tests /* package */ void loadSettings() { if (null == mPrefs) mPrefs = PreferenceManager.getDefaultSharedPreferences(this); - mSettingsValues = new SettingsValues(mPrefs, this, mSubtypeSwitcher.getInputLocaleStr()); + final RunInLocale<SettingsValues> job = new RunInLocale<SettingsValues>() { + @Override + protected SettingsValues job(Resources res) { + return new SettingsValues(mPrefs, LatinIME.this); + } + }; + mSettingsValues = job.runInLocale(mResources, mSubtypeSwitcher.getInputLocale()); mFeedbackManager = new AudioAndHapticFeedbackManager(this, mSettingsValues); resetContactsDictionary(null == mSuggest ? null : mSuggest.getContactsDictionary()); } @@ -485,33 +494,37 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar final String localeStr = mSubtypeSwitcher.getInputLocaleStr(); final Locale keyboardLocale = mSubtypeSwitcher.getInputLocale(); - final Resources res = mResources; - final Locale savedLocale = LocaleUtils.setSystemLocale(res, keyboardLocale); - final ContactsDictionary oldContactsDictionary; - if (mSuggest != null) { - oldContactsDictionary = mSuggest.getContactsDictionary(); - mSuggest.close(); - } else { - oldContactsDictionary = null; - } - - int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(res); - mSuggest = new Suggest(this, mainDicResId, keyboardLocale); - if (mSettingsValues.mAutoCorrectEnabled) { - mSuggest.setAutoCorrectionThreshold(mSettingsValues.mAutoCorrectionThreshold); - } + final Context context = this; + final RunInLocale<Void> job = new RunInLocale<Void>() { + @Override + protected Void job(Resources res) { + final ContactsDictionary oldContactsDictionary; + if (mSuggest != null) { + oldContactsDictionary = mSuggest.getContactsDictionary(); + mSuggest.close(); + } else { + oldContactsDictionary = null; + } - mUserDictionary = new UserDictionary(this, localeStr); - mSuggest.setUserDictionary(mUserDictionary); - mIsUserDictionaryAvailable = mUserDictionary.isEnabled(); + int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(res); + mSuggest = new Suggest(context, mainDicResId, keyboardLocale); + if (mSettingsValues.mAutoCorrectEnabled) { + mSuggest.setAutoCorrectionThreshold(mSettingsValues.mAutoCorrectionThreshold); + } - resetContactsDictionary(oldContactsDictionary); + mUserDictionary = new UserDictionary(context, localeStr); + mSuggest.setUserDictionary(mUserDictionary); + mIsUserDictionaryAvailable = mUserDictionary.isEnabled(); - mUserHistoryDictionary - = new UserHistoryDictionary(this, localeStr, Suggest.DIC_USER_HISTORY); - mSuggest.setUserHistoryDictionary(mUserHistoryDictionary); + resetContactsDictionary(oldContactsDictionary); - LocaleUtils.setSystemLocale(res, savedLocale); + mUserHistoryDictionary + = new UserHistoryDictionary(context, localeStr, Suggest.DIC_USER_HISTORY); + mSuggest.setUserHistoryDictionary(mUserHistoryDictionary); + return null; + } + }; + job.runInLocale(mResources, keyboardLocale); } /** @@ -627,6 +640,11 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar mHandler.onFinishInput(); } + @Override + public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype) { + SubtypeSwitcher.getInstance().updateSubtype(subtype); + } + private void onStartInputInternal(EditorInfo editorInfo, boolean restarting) { super.onStartInput(editorInfo, restarting); } @@ -968,7 +986,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar final int touchHeight = inputView.getHeight() + extraHeight // Extend touchable region below the keyboard. + EXTENDED_TOUCHABLE_REGION_HEIGHT; - setTouchableRegionCompat(outInsets, 0, touchY, touchWidth, touchHeight); + outInsets.touchableInsets = InputMethodService.Insets.TOUCHABLE_INSETS_REGION; + outInsets.touchableRegion.set(0, touchY, touchWidth, touchHeight); } outInsets.contentTopInsets = touchY; outInsets.visibleTopInsets = touchY; @@ -1167,7 +1186,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar return keyboard != null ? keyboard.mId.imeActionId() : EditorInfo.IME_ACTION_NONE; } - private void performeEditorAction(int actionId) { + private void performEditorAction(int actionId) { final InputConnection ic = getCurrentInputConnection(); if (ic != null) { ic.performEditorAction(actionId); @@ -1178,7 +1197,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar final boolean includesOtherImes = mSettingsValues.mIncludesOtherImesInLanguageSwitchList; final IBinder token = getWindow().getWindow().getAttributes().token; if (mShouldSwitchToLastSubtype) { - final InputMethodSubtypeCompatWrapper lastSubtype = mImm.getLastInputMethodSubtype(); + final InputMethodSubtype lastSubtype = mImm.getLastInputMethodSubtype(); final boolean lastSubtypeBelongsToThisIme = SubtypeUtils.checkIfSubtypeBelongsToThisIme( this, lastSubtype); if ((includesOtherImes || lastSubtypeBelongsToThisIme) @@ -1258,13 +1277,13 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar mSubtypeSwitcher.switchToShortcutIME(); break; case Keyboard.CODE_ACTION_ENTER: - performeEditorAction(getActionId(switcher.getKeyboard())); + performEditorAction(getActionId(switcher.getKeyboard())); break; case Keyboard.CODE_ACTION_NEXT: - performeEditorAction(EditorInfo.IME_ACTION_NEXT); + performEditorAction(EditorInfo.IME_ACTION_NEXT); break; case Keyboard.CODE_ACTION_PREVIOUS: - EditorInfoCompatUtils.performEditorActionPrevious(getCurrentInputConnection()); + performEditorAction(EditorInfo.IME_ACTION_PREVIOUS); break; case Keyboard.CODE_LANGUAGE_SWITCH: handleLanguageSwitchKey(); @@ -2263,6 +2282,25 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar showOptionDialogInternal(builder.create()); } + private void showOptionDialogInternal(AlertDialog dialog) { + final IBinder windowToken = KeyboardSwitcher.getInstance().getKeyboardView() + .getWindowToken(); + if (windowToken == null) return; + + dialog.setCancelable(true); + dialog.setCanceledOnTouchOutside(true); + + final Window window = dialog.getWindow(); + final WindowManager.LayoutParams lp = window.getAttributes(); + lp.token = windowToken; + lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG; + window.setAttributes(lp); + window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); + + mOptionsDialog = dialog; + dialog.show(); + } + @Override protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { super.dump(fd, fout, args); diff --git a/java/src/com/android/inputmethod/latin/LocaleUtils.java b/java/src/com/android/inputmethod/latin/LocaleUtils.java index cf60089c5..f19c59a6a 100644 --- a/java/src/com/android/inputmethod/latin/LocaleUtils.java +++ b/java/src/com/android/inputmethod/latin/LocaleUtils.java @@ -161,21 +161,36 @@ public class LocaleUtils { return LOCALE_MATCH <= level; } - /** - * Sets the system locale for this process. - * - * @param res the resources to use. Pass current resources. - * @param newLocale the locale to change to. - * @return the old locale. - */ - public static synchronized Locale setSystemLocale(final Resources res, final Locale newLocale) { - final Configuration conf = res.getConfiguration(); - final Locale oldLocale = conf.locale; - if (newLocale != null && !newLocale.equals(oldLocale)) { - conf.locale = newLocale; - res.updateConfiguration(conf, res.getDisplayMetrics()); + static final Object sLockForRunInLocale = new Object(); + + public abstract static class RunInLocale<T> { + protected abstract T job(Resources res); + + /** + * Execute {@link #job(Resources)} method in specified system locale exclusively. + * + * @param res the resources to use. Pass current resources. + * @param newLocale the locale to change to + * @return the value returned from {@link #job(Resources)}. + */ + public T runInLocale(final Resources res, final Locale newLocale) { + synchronized (sLockForRunInLocale) { + final Configuration conf = res.getConfiguration(); + final Locale oldLocale = conf.locale; + try { + if (newLocale != null && !newLocale.equals(oldLocale)) { + conf.locale = newLocale; + res.updateConfiguration(conf, res.getDisplayMetrics()); + } + return job(res); + } finally { + if (newLocale != null && !newLocale.equals(oldLocale)) { + conf.locale = oldLocale; + res.updateConfiguration(conf, res.getDisplayMetrics()); + } + } + } } - return oldLocale; } private static final HashMap<String, Locale> sLocaleCache = new HashMap<String, Locale>(); diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java index fd61292df..7b98a7188 100644 --- a/java/src/com/android/inputmethod/latin/Settings.java +++ b/java/src/com/android/inputmethod/latin/Settings.java @@ -18,7 +18,6 @@ package com.android.inputmethod.latin; import android.app.Activity; import android.app.AlertDialog; -import android.app.Dialog; import android.app.Fragment; import android.app.backup.BackupManager; import android.content.Context; @@ -34,31 +33,20 @@ import android.preference.Preference; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceGroup; import android.preference.PreferenceScreen; -import android.text.TextUtils; -import android.text.method.LinkMovementMethod; -import android.util.Log; import android.view.View; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; -import com.android.inputmethod.compat.CompatUtils; -import com.android.inputmethod.compat.InputMethodServiceCompatWrapper; -import com.android.inputmethod.latin.VibratorUtils; import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethodcommon.InputMethodSettingsActivity; -import java.util.Locale; - public class Settings extends InputMethodSettingsActivity - implements SharedPreferences.OnSharedPreferenceChangeListener, OnPreferenceClickListener { - private static final String TAG = Settings.class.getSimpleName(); - + implements SharedPreferences.OnSharedPreferenceChangeListener { public static final boolean ENABLE_EXPERIMENTAL_SETTINGS = false; // In the same order as xml/prefs.xml public static final String PREF_GENERAL_SETTINGS = "general_settings"; - public static final String PREF_SUBTYPES_SETTINGS = "subtype_settings"; public static final String PREF_AUTO_CAP = "auto_cap"; public static final String PREF_VIBRATE_ON = "vibrate_on"; public static final String PREF_SOUND_ON = "sound_on"; @@ -90,7 +78,6 @@ public class Settings extends InputMethodSettingsActivity public static final String PREF_SELECTED_LANGUAGES = "selected_languages"; public static final String PREF_DEBUG_SETTINGS = "debug_settings"; - private PreferenceScreen mInputLanguageSelection; private PreferenceScreen mKeypressVibrationDurationSettingsPref; private PreferenceScreen mKeypressSoundVolumeSettingsPref; private ListPreference mVoicePreference; @@ -102,14 +89,10 @@ public class Settings extends InputMethodSettingsActivity // Prediction: use bigrams to predict the next word when there is no input for it yet private CheckBoxPreference mBigramPrediction; private Preference mDebugSettingsPreference; - private boolean mVoiceOn; - private AlertDialog mDialog; private TextView mKeypressVibrationDurationSettingsTextView; private TextView mKeypressSoundVolumeSettingsTextView; - private String mVoiceModeOff; - private void ensureConsistencyOfAutoCorrectionSettings() { final String autoCorrectionOff = getResources().getString( R.string.auto_correction_threshold_mode_index_off); @@ -140,18 +123,12 @@ public class Settings extends InputMethodSettingsActivity final Context context = getActivityInternal(); addPreferencesFromResource(R.xml.prefs); - mInputLanguageSelection = (PreferenceScreen) findPreference(PREF_SUBTYPES_SETTINGS); - mInputLanguageSelection.setOnPreferenceClickListener(this); mVoicePreference = (ListPreference) findPreference(PREF_VOICE_MODE); mShowCorrectionSuggestionsPreference = (ListPreference) findPreference(PREF_SHOW_SUGGESTIONS_SETTING); SharedPreferences prefs = getPreferenceManager().getSharedPreferences(); prefs.registerOnSharedPreferenceChangeListener(this); - mVoiceModeOff = getString(R.string.voice_mode_off); - mVoiceOn = !(prefs.getString(PREF_VOICE_MODE, mVoiceModeOff) - .equals(mVoiceModeOff)); - mAutoCorrectionThresholdPreference = (ListPreference) findPreference(PREF_AUTO_CORRECTION_THRESHOLD); mBigramSuggestion = (CheckBoxPreference) findPreference(PREF_BIGRAM_SUGGESTION); @@ -183,10 +160,6 @@ public class Settings extends InputMethodSettingsActivity generalSettings.removePreference(findPreference(PREF_VIBRATE_ON)); } - if (InputMethodServiceCompatWrapper.CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED) { - generalSettings.removePreference(findPreference(PREF_SUBTYPES_SETTINGS)); - } - final boolean showPopupOption = res.getBoolean( R.bool.config_enable_show_popup_on_keypress_option); if (!showPopupOption) { @@ -318,25 +291,12 @@ public class Settings extends InputMethodSettingsActivity !SettingsValues.isLanguageSwitchKeySupressed(prefs)); } ensureConsistencyOfAutoCorrectionSettings(); - mVoiceOn = !(prefs.getString(PREF_VOICE_MODE, mVoiceModeOff) - .equals(mVoiceModeOff)); updateVoiceModeSummary(); updateShowCorrectionSuggestionsSummary(); updateKeyPreviewPopupDelaySummary(); refreshEnablingsOfKeypressSoundAndVibrationSettings(prefs, getResources()); } - @Override - public boolean onPreferenceClick(Preference pref) { - if (pref == mInputLanguageSelection) { - final String imeId = SubtypeUtils.getInputMethodId( - getActivityInternal().getApplicationInfo().packageName); - startActivity(CompatUtils.getInputLanguageSelectionIntent(imeId, 0)); - return true; - } - return false; - } - private void updateShowCorrectionSuggestionsSummary() { mShowCorrectionSuggestionsPreference.setSummary( getResources().getStringArray(R.array.prefs_suggestion_visibilities) diff --git a/java/src/com/android/inputmethod/latin/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java index f76cc7e44..103678403 100644 --- a/java/src/com/android/inputmethod/latin/SettingsValues.java +++ b/java/src/com/android/inputmethod/latin/SettingsValues.java @@ -22,15 +22,16 @@ import android.content.res.Resources; import android.util.Log; import android.view.inputmethod.EditorInfo; -import com.android.inputmethod.compat.InputTypeCompatUtils; import com.android.inputmethod.keyboard.internal.KeySpecParser; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.VibratorUtils; import java.util.ArrayList; import java.util.Arrays; -import java.util.Locale; +/** + * When you call the constructor of this class, you may want to change the current system locale by + * using {@link LocaleUtils.RunInLocale}. + */ public class SettingsValues { private static final String TAG = SettingsValues.class.getSimpleName(); @@ -78,16 +79,8 @@ public class SettingsValues { private final boolean mVoiceKeyEnabled; private final boolean mVoiceKeyOnMain; - public SettingsValues(final SharedPreferences prefs, final Context context, - final String localeStr) { + public SettingsValues(final SharedPreferences prefs, final Context context) { final Resources res = context.getResources(); - final Locale savedLocale; - if (null != localeStr) { - final Locale keyboardLocale = LocaleUtils.constructLocaleFromString(localeStr); - savedLocale = LocaleUtils.setSystemLocale(res, keyboardLocale); - } else { - savedLocale = null; - } // Get the resources mDelayUpdateOldSuggestions = res.getInteger(R.integer.config_delay_update_old_suggestions); @@ -152,8 +145,6 @@ public class SettingsValues { mAutoCorrectionThresholdRawValue); mVoiceKeyEnabled = mVoiceMode != null && !mVoiceMode.equals(voiceModeOff); mVoiceKeyOnMain = mVoiceMode != null && mVoiceMode.equals(voiceModeMain); - - LocaleUtils.setSystemLocale(res, savedLocale); } // Helper functions to create member values. @@ -286,7 +277,7 @@ public class SettingsValues { final boolean shortcutImeEnabled = SubtypeSwitcher.getInstance().isShortcutImeEnabled(); final int inputType = (editorInfo != null) ? editorInfo.inputType : 0; return shortcutImeEnabled && mVoiceKeyEnabled - && !InputTypeCompatUtils.isPasswordInputType(inputType); + && !InputTypeUtils.isPasswordInputType(inputType); } public boolean isVoiceKeyOnMain() { diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index de2e8be3d..e35364420 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -29,14 +29,13 @@ import android.os.AsyncTask; import android.os.IBinder; import android.text.TextUtils; import android.util.Log; +import android.view.inputmethod.InputMethodInfo; +import android.view.inputmethod.InputMethodSubtype; -import com.android.inputmethod.compat.InputMethodInfoCompatWrapper; import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; -import com.android.inputmethod.compat.InputMethodSubtypeCompatWrapper; import com.android.inputmethod.keyboard.KeyboardSwitcher; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.Map; @@ -47,7 +46,6 @@ public class SubtypeSwitcher { public static final String KEYBOARD_MODE = "keyboard"; private static final char LOCALE_SEPARATOR = '_'; - private static final String VOICE_MODE = "voice"; private static final String SUBTYPE_EXTRAVALUE_REQUIRE_NETWORK_CONNECTIVITY = "requireNetworkConnectivity"; @@ -59,19 +57,19 @@ public class SubtypeSwitcher { private /* final */ InputMethodManagerCompatWrapper mImm; private /* final */ Resources mResources; private /* final */ ConnectivityManager mConnectivityManager; - private final ArrayList<InputMethodSubtypeCompatWrapper> - mEnabledKeyboardSubtypesOfCurrentInputMethod = - new ArrayList<InputMethodSubtypeCompatWrapper>(); + private final ArrayList<InputMethodSubtype> mEnabledKeyboardSubtypesOfCurrentInputMethod = + new ArrayList<InputMethodSubtype>(); private final ArrayList<String> mEnabledLanguagesOfCurrentInputMethod = new ArrayList<String>(); /*-----------------------------------------------------------*/ // Variants which should be changed only by reload functions. private boolean mNeedsToDisplayLanguage; private boolean mIsSystemLanguageSameAsInputLanguage; - private InputMethodInfoCompatWrapper mShortcutInputMethodInfo; - private InputMethodSubtypeCompatWrapper mShortcutSubtype; - private List<InputMethodSubtypeCompatWrapper> mAllEnabledSubtypesOfCurrentInputMethod; - private InputMethodSubtypeCompatWrapper mCurrentSubtype; + private InputMethodInfo mShortcutInputMethodInfo; + private InputMethodSubtype mShortcutSubtype; + private List<InputMethodSubtype> mAllEnabledSubtypesOfCurrentInputMethod; + // Note: This variable is always non-null after {@link #initialize(LatinIME)}. + private InputMethodSubtype mCurrentSubtype; private Locale mSystemLocale; private Locale mInputLocale; private String mInputLocaleStr; @@ -104,7 +102,7 @@ public class SubtypeSwitcher { mSystemLocale = null; mInputLocale = null; mInputLocaleStr = null; - mCurrentSubtype = null; + mCurrentSubtype = mImm.getCurrentInputMethodSubtype(); mAllEnabledSubtypesOfCurrentInputMethod = null; final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo(); @@ -134,7 +132,7 @@ public class SubtypeSwitcher { null, true); mEnabledLanguagesOfCurrentInputMethod.clear(); mEnabledKeyboardSubtypesOfCurrentInputMethod.clear(); - for (InputMethodSubtypeCompatWrapper ims : mAllEnabledSubtypesOfCurrentInputMethod) { + for (InputMethodSubtype ims : mAllEnabledSubtypesOfCurrentInputMethod) { final String locale = getSubtypeLocale(ims); final String mode = ims.getMode(); mLocaleSplitter.setString(locale); @@ -168,12 +166,12 @@ public class SubtypeSwitcher { + ", " + mShortcutSubtype.getMode()))); } // TODO: Update an icon for shortcut IME - final Map<InputMethodInfoCompatWrapper, List<InputMethodSubtypeCompatWrapper>> shortcuts = + final Map<InputMethodInfo, List<InputMethodSubtype>> shortcuts = mImm.getShortcutInputMethodsAndSubtypes(); mShortcutInputMethodInfo = null; mShortcutSubtype = null; - for (InputMethodInfoCompatWrapper imi : shortcuts.keySet()) { - List<InputMethodSubtypeCompatWrapper> subtypes = shortcuts.get(imi); + for (InputMethodInfo imi : shortcuts.keySet()) { + List<InputMethodSubtype> subtypes = shortcuts.get(imi); // TODO: Returns the first found IMI for now. Should handle all shortcuts as // appropriate. mShortcutInputMethodInfo = imi; @@ -191,27 +189,17 @@ public class SubtypeSwitcher { } } - private static String getSubtypeLocale(InputMethodSubtypeCompatWrapper subtype) { + private static String getSubtypeLocale(InputMethodSubtype subtype) { final String keyboardLocale = subtype.getExtraValueOf( LatinIME.SUBTYPE_EXTRA_VALUE_KEYBOARD_LOCALE); return keyboardLocale != null ? keyboardLocale : subtype.getLocale(); } // Update the current subtype. LatinIME.onCurrentInputMethodSubtypeChanged calls this function. - public void updateSubtype(InputMethodSubtypeCompatWrapper newSubtype) { - final String newLocale; - final String newMode; + public void updateSubtype(InputMethodSubtype newSubtype) { + final String newLocale = getSubtypeLocale(newSubtype); + final String newMode = newSubtype.getMode(); final String oldMode = getCurrentSubtypeMode(); - if (newSubtype == null) { - // Normally, newSubtype shouldn't be null. But just in case newSubtype was null, - // fallback to the default locale. - Log.w(TAG, "Couldn't get the current subtype."); - newLocale = "en_US"; - newMode = KEYBOARD_MODE; - } else { - newLocale = getSubtypeLocale(newSubtype); - newMode = newSubtype.getMode(); - } if (DBG) { Log.w(TAG, "Update subtype to:" + newLocale + "," + newMode + ", from: " + mInputLocaleStr + ", " + oldMode); @@ -286,12 +274,10 @@ public class SubtypeSwitcher { } final String imiId = mShortcutInputMethodInfo.getId(); - final InputMethodSubtypeCompatWrapper subtype = mShortcutSubtype; - switchToTargetIME(imiId, subtype); + switchToTargetIME(imiId, mShortcutSubtype); } - private void switchToTargetIME( - final String imiId, final InputMethodSubtypeCompatWrapper subtype) { + private void switchToTargetIME(final String imiId, final InputMethodSubtype subtype) { final IBinder token = mService.getWindow().getWindow().getAttributes().token; if (token == null) { return; @@ -302,17 +288,6 @@ public class SubtypeSwitcher { mImm.setInputMethodAndSubtype(token, imiId, subtype); return null; } - - @Override - protected void onPostExecute(Void result) { - // Calls in this method need to be done in the same thread as the thread which - // called switchToShortcutIME(). - - // Notify an event that the current subtype was changed. This event will be - // handled if "onCurrentInputMethodSubtypeChanged" can't be implemented - // when the API level is 10 or previous. - mService.notifyOnCurrentInputMethodSubtypeChanged(subtype); - } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @@ -320,8 +295,7 @@ public class SubtypeSwitcher { return getSubtypeIcon(mShortcutInputMethodInfo, mShortcutSubtype); } - private Drawable getSubtypeIcon( - InputMethodInfoCompatWrapper imi, InputMethodSubtypeCompatWrapper subtype) { + private Drawable getSubtypeIcon(InputMethodInfo imi, InputMethodSubtype subtype) { final PackageManager pm = mService.getPackageManager(); if (imi != null) { final String imiPackageName = imi.getPackageName(); @@ -362,15 +336,9 @@ public class SubtypeSwitcher { if (mShortcutSubtype == null) { return true; } - // For compatibility, if the shortcut subtype is dummy, we assume the shortcut IME - // (built-in voice dummy subtype) is available. - if (!mShortcutSubtype.hasOriginalObject()) { - return true; - } final boolean allowsImplicitlySelectedSubtypes = true; - for (final InputMethodSubtypeCompatWrapper enabledSubtype : - mImm.getEnabledInputMethodSubtypeList( - mShortcutInputMethodInfo, allowsImplicitlySelectedSubtypes)) { + for (final InputMethodSubtype enabledSubtype : mImm.getEnabledInputMethodSubtypeList( + mShortcutInputMethodInfo, allowsImplicitlySelectedSubtypes)) { if (enabledSubtype.equals(mShortcutSubtype)) { return true; } @@ -462,20 +430,20 @@ public class SubtypeSwitcher { public String getCurrentSubtypeExtraValue() { // If null, return what an empty ExtraValue would return : the empty string. - return null != mCurrentSubtype ? mCurrentSubtype.getExtraValue() : ""; + return mCurrentSubtype.getExtraValue(); } public boolean currentSubtypeContainsExtraValueKey(String key) { // If null, return what an empty ExtraValue would return : false. - return null != mCurrentSubtype ? mCurrentSubtype.containsExtraValueKey(key) : false; + return mCurrentSubtype.containsExtraValueKey(key); } public String getCurrentSubtypeExtraValueOf(String key) { // If null, return what an empty ExtraValue would return : null. - return null != mCurrentSubtype ? mCurrentSubtype.getExtraValueOf(key) : null; + return mCurrentSubtype.getExtraValueOf(key); } public String getCurrentSubtypeMode() { - return null != mCurrentSubtype ? mCurrentSubtype.getMode() : KEYBOARD_MODE; + return mCurrentSubtype.getMode(); } } diff --git a/java/src/com/android/inputmethod/latin/SubtypeUtils.java b/java/src/com/android/inputmethod/latin/SubtypeUtils.java index cb2bcf43f..2c5d58200 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeUtils.java +++ b/java/src/com/android/inputmethod/latin/SubtypeUtils.java @@ -17,10 +17,10 @@ package com.android.inputmethod.latin; import android.content.Context; +import android.view.inputmethod.InputMethodInfo; +import android.view.inputmethod.InputMethodSubtype; -import com.android.inputmethod.compat.InputMethodInfoCompatWrapper; import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; -import com.android.inputmethod.compat.InputMethodSubtypeCompatWrapper; import java.util.Collections; import java.util.List; @@ -31,15 +31,13 @@ public class SubtypeUtils { } // TODO: Cache my InputMethodInfo and/or InputMethodSubtype list. - public static boolean checkIfSubtypeBelongsToThisIme(Context context, - InputMethodSubtypeCompatWrapper ims) { + public static boolean checkIfSubtypeBelongsToThisIme(Context context, InputMethodSubtype ims) { final InputMethodManagerCompatWrapper imm = InputMethodManagerCompatWrapper.getInstance(); if (imm == null) return false; - final InputMethodInfoCompatWrapper myImi = getInputMethodInfo(context.getPackageName()); - final List<InputMethodSubtypeCompatWrapper> subtypes = - imm.getEnabledInputMethodSubtypeList(myImi, true); - for (final InputMethodSubtypeCompatWrapper subtype : subtypes) { + final InputMethodInfo myImi = getInputMethodInfo(context.getPackageName()); + final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(myImi, true); + for (final InputMethodSubtype subtype : subtypes) { if (subtype.equals(ims)) { return true; } @@ -52,29 +50,29 @@ public class SubtypeUtils { final InputMethodManagerCompatWrapper imm = InputMethodManagerCompatWrapper.getInstance(); if (imm == null) return false; - final List<InputMethodInfoCompatWrapper> enabledImis = imm.getEnabledInputMethodList(); + final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList(); return hasMultipleEnabledSubtypes(shouldIncludeAuxiliarySubtypes, enabledImis); } public static boolean hasMultipleEnabledSubtypesInThisIme(Context context, final boolean shouldIncludeAuxiliarySubtypes) { - final InputMethodInfoCompatWrapper myImi = getInputMethodInfo(context.getPackageName()); - final List<InputMethodInfoCompatWrapper> imiList = Collections.singletonList(myImi); + final InputMethodInfo myImi = getInputMethodInfo(context.getPackageName()); + final List<InputMethodInfo> imiList = Collections.singletonList(myImi); return hasMultipleEnabledSubtypes(shouldIncludeAuxiliarySubtypes, imiList); } private static boolean hasMultipleEnabledSubtypes(final boolean shouldIncludeAuxiliarySubtypes, - List<InputMethodInfoCompatWrapper> imiList) { + List<InputMethodInfo> imiList) { final InputMethodManagerCompatWrapper imm = InputMethodManagerCompatWrapper.getInstance(); if (imm == null) return false; // Number of the filtered IMEs int filteredImisCount = 0; - for (InputMethodInfoCompatWrapper imi : imiList) { + for (InputMethodInfo imi : imiList) { // We can return true immediately after we find two or more filtered IMEs. if (filteredImisCount > 1) return true; - final List<InputMethodSubtypeCompatWrapper> subtypes = + final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(imi, true); // IMEs that have no subtypes should be counted. if (subtypes.isEmpty()) { @@ -83,7 +81,7 @@ public class SubtypeUtils { } int auxCount = 0; - for (InputMethodSubtypeCompatWrapper subtype : subtypes) { + for (InputMethodSubtype subtype : subtypes) { if (subtype.isAuxiliary()) { ++auxCount; } @@ -102,13 +100,12 @@ public class SubtypeUtils { if (filteredImisCount > 1) { return true; } - final List<InputMethodSubtypeCompatWrapper> subtypes = - imm.getEnabledInputMethodSubtypeList(null, true); + final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(null, true); int keyboardCount = 0; // imm.getEnabledInputMethodSubtypeList(null, true) will return the current IME's // both explicitly and implicitly enabled input method subtype. // (The current IME should be LatinIME.) - for (InputMethodSubtypeCompatWrapper subtype : subtypes) { + for (InputMethodSubtype subtype : subtypes) { if (SubtypeSwitcher.KEYBOARD_MODE.equals(subtype.getMode())) { ++keyboardCount; } @@ -120,13 +117,13 @@ public class SubtypeUtils { return getInputMethodInfo(packageName).getId(); } - public static InputMethodInfoCompatWrapper getInputMethodInfo(String packageName) { + public static InputMethodInfo getInputMethodInfo(String packageName) { final InputMethodManagerCompatWrapper imm = InputMethodManagerCompatWrapper.getInstance(); if (imm == null) { throw new RuntimeException("Input method manager not found"); } - for (final InputMethodInfoCompatWrapper imi : imm.getEnabledInputMethodList()) { + for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) { if (imi.getPackageName().equals(packageName)) return imi; } diff --git a/java/src/com/android/inputmethod/latin/WhitelistDictionary.java b/java/src/com/android/inputmethod/latin/WhitelistDictionary.java index a90ef290b..7bb307662 100644 --- a/java/src/com/android/inputmethod/latin/WhitelistDictionary.java +++ b/java/src/com/android/inputmethod/latin/WhitelistDictionary.java @@ -22,6 +22,8 @@ import android.text.TextUtils; import android.util.Log; import android.util.Pair; +import com.android.inputmethod.latin.LocaleUtils.RunInLocale; + import java.util.HashMap; import java.util.Locale; @@ -36,10 +38,14 @@ public class WhitelistDictionary extends ExpandableDictionary { // TODO: Conform to the async load contact of ExpandableDictionary public WhitelistDictionary(final Context context, final Locale locale) { super(context, Suggest.DIC_WHITELIST); - final Resources res = context.getResources(); - final Locale previousLocale = LocaleUtils.setSystemLocale(res, locale); - initWordlist(res.getStringArray(R.array.wordlist_whitelist)); - LocaleUtils.setSystemLocale(res, previousLocale); + final RunInLocale<Void> job = new RunInLocale<Void>() { + @Override + protected Void job(Resources res) { + initWordlist(res.getStringArray(R.array.wordlist_whitelist)); + return null; + } + }; + job.runInLocale(context.getResources(), locale); } private void initWordlist(String[] wordlist) { diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java index af7f863ee..010ea6813 100644 --- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java +++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java @@ -26,6 +26,7 @@ import java.io.OutputStream; import java.io.RandomAccessFile; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; @@ -922,7 +923,14 @@ public class BinaryDictInputOutput { if (version >= FIRST_VERSION_WITH_HEADER_SIZE) { final int headerSizeOffset = index; index += 4; // Size of the header size - // TODO: Write out the header contents here. + + // Write out the options. + for (final String key : dict.mOptions.mAttributes.keySet()) { + final String value = dict.mOptions.mAttributes.get(key); + index += CharEncoding.writeString(buffer, index, key); + index += CharEncoding.writeString(buffer, index, value); + } + // Write out the header size. buffer[headerSizeOffset] = (byte) (0xFF & (index >> 24)); buffer[headerSizeOffset + 1] = (byte) (0xFF & (index >> 16)); @@ -1214,12 +1222,17 @@ public class BinaryDictInputOutput { source.readUnsignedShort(); final long headerSize; + final HashMap<String, String> options = new HashMap<String, String>(); if (version < FIRST_VERSION_WITH_HEADER_SIZE) { headerSize = source.getFilePointer(); } else { headerSize = (source.readUnsignedByte() << 24) + (source.readUnsignedByte() << 16) + (source.readUnsignedByte() << 8) + source.readUnsignedByte(); - // read the header body + while (source.getFilePointer() < headerSize) { + final String key = CharEncoding.readString(source); + final String value = CharEncoding.readString(source); + options.put(key, value); + } source.seek(headerSize); } @@ -1228,7 +1241,7 @@ public class BinaryDictInputOutput { final Node root = readNode(source, headerSize, reverseNodeMapping, reverseGroupMapping); FusionDictionary newDict = new FusionDictionary(root, - new FusionDictionary.DictionaryOptions()); + new FusionDictionary.DictionaryOptions(options)); if (null != dict) { for (Word w : dict) { newDict.add(w.mWord, w.mFrequency, w.mShortcutTargets, w.mBigrams); diff --git a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java index d3ffb47ad..99b17048d 100644 --- a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java +++ b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java @@ -19,6 +19,7 @@ package com.android.inputmethod.latin.makedict; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; @@ -250,6 +251,10 @@ public class FusionDictionary implements Iterable<Word> { * There are no options at the moment, so this class is empty. */ public static class DictionaryOptions { + final HashMap<String, String> mAttributes; + public DictionaryOptions(final HashMap<String, String> attributes) { + mAttributes = attributes; + } } @@ -257,8 +262,13 @@ public class FusionDictionary implements Iterable<Word> { public final Node mRoot; public FusionDictionary() { - mOptions = new DictionaryOptions(); mRoot = new Node(); + mOptions = new DictionaryOptions(new HashMap<String, String>()); + } + + public FusionDictionary(final HashMap<String, String> attributes) { + mRoot = new Node(); + mOptions = new DictionaryOptions(attributes); } public FusionDictionary(final Node root, final DictionaryOptions options) { @@ -266,6 +276,10 @@ public class FusionDictionary implements Iterable<Word> { mOptions = options; } + public void addOptionAttribute(final String key, final String value) { + mOptions.mAttributes.put(key, value); + } + /** * Helper method to convert a String to an int array. */ diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionsView.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionsView.java index ca253543e..1ad37b933 100644 --- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionsView.java +++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionsView.java @@ -52,11 +52,11 @@ import android.widget.PopupWindow; import android.widget.RelativeLayout; import android.widget.TextView; -import com.android.inputmethod.compat.FrameLayoutCompatUtils; import com.android.inputmethod.keyboard.KeyboardActionListener; import com.android.inputmethod.keyboard.KeyboardView; import com.android.inputmethod.keyboard.MoreKeysPanel; import com.android.inputmethod.keyboard.PointerTracker; +import com.android.inputmethod.keyboard.ViewLayoutUtils; import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.StaticInnerHandlerWrapper; @@ -416,7 +416,7 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, ViewGroup.LayoutParams.WRAP_CONTENT); final int infoWidth = info.getMeasuredWidth(); final int y = info.getMeasuredHeight(); - FrameLayoutCompatUtils.placeViewAt( + ViewLayoutUtils.placeViewAt( info, x - infoWidth, y, infoWidth, info.getMeasuredHeight()); } } |