diff options
Diffstat (limited to 'java/src/com/android/inputmethod/compat')
13 files changed, 426 insertions, 50 deletions
diff --git a/java/src/com/android/inputmethod/compat/AccessibilityEventCompatUtils.java b/java/src/com/android/inputmethod/compat/AccessibilityEventCompatUtils.java new file mode 100644 index 000000000..50057727a --- /dev/null +++ b/java/src/com/android/inputmethod/compat/AccessibilityEventCompatUtils.java @@ -0,0 +1,39 @@ +/* + * 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.AccessibilityEvent; + +import java.lang.reflect.Field; + +public class AccessibilityEventCompatUtils { + public static final int TYPE_VIEW_HOVER_ENTER = 0x80; + public static final int TYPE_VIEW_HOVER_EXIT = 0x100; + + private static final Field FIELD_TYPE_VIEW_HOVER_ENTER = CompatUtils.getField( + AccessibilityEvent.class, "TYPE_VIEW_HOVER_ENTER"); + private static final Field FIELD_TYPE_VIEW_HOVER_EXIT = CompatUtils.getField( + AccessibilityEvent.class, "TYPE_VIEW_HOVER_EXIT"); + private static final Integer OBJ_TYPE_VIEW_HOVER_ENTER = (Integer) CompatUtils + .getFieldValue(null, null, FIELD_TYPE_VIEW_HOVER_ENTER); + private static final Integer OBJ_TYPE_VIEW_HOVER_EXIT = (Integer) CompatUtils + .getFieldValue(null, null, FIELD_TYPE_VIEW_HOVER_EXIT); + + public static boolean supportsTouchExploration() { + return OBJ_TYPE_VIEW_HOVER_ENTER != null && OBJ_TYPE_VIEW_HOVER_EXIT != null; + } +} diff --git a/java/src/com/android/inputmethod/compat/AccessibilityManagerCompatWrapper.java b/java/src/com/android/inputmethod/compat/AccessibilityManagerCompatWrapper.java new file mode 100644 index 000000000..4db1c7a24 --- /dev/null +++ b/java/src/com/android/inputmethod/compat/AccessibilityManagerCompatWrapper.java @@ -0,0 +1,42 @@ +/* + * 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.accessibilityservice.AccessibilityServiceInfo; +import android.view.accessibility.AccessibilityManager; + +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.List; + +public class AccessibilityManagerCompatWrapper { + private static final Method METHOD_getEnabledAccessibilityServiceList = CompatUtils.getMethod( + AccessibilityManager.class, "getEnabledAccessibilityServiceList", int.class); + + private final AccessibilityManager mManager; + + public AccessibilityManagerCompatWrapper(AccessibilityManager manager) { + mManager = manager; + } + + @SuppressWarnings("unchecked") + public List<AccessibilityServiceInfo> getEnabledAccessibilityServiceList(int feedbackType) { + return (List<AccessibilityServiceInfo>) CompatUtils.invoke(mManager, + Collections.<AccessibilityServiceInfo>emptyList(), + METHOD_getEnabledAccessibilityServiceList, feedbackType); + } +} diff --git a/java/src/com/android/inputmethod/compat/ArraysCompatUtils.java b/java/src/com/android/inputmethod/compat/ArraysCompatUtils.java new file mode 100644 index 000000000..f6afbcfe2 --- /dev/null +++ b/java/src/com/android/inputmethod/compat/ArraysCompatUtils.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.compat; + +import java.lang.reflect.Method; +import java.util.Arrays; + +public class ArraysCompatUtils { + private static final Method METHOD_Arrays_binarySearch = CompatUtils + .getMethod(Arrays.class, "binarySearch", int[].class, int.class, int.class, int.class); + + public static int binarySearch(int[] array, int startIndex, int endIndex, int value) { + if (METHOD_Arrays_binarySearch != null) { + final Object index = CompatUtils.invoke(null, 0, METHOD_Arrays_binarySearch, + array, startIndex, endIndex, value); + return (Integer)index; + } else { + return compatBinarySearch(array, startIndex, endIndex, value); + } + } + + /* package */ static int compatBinarySearch(int[] array, int startIndex, int endIndex, + int value) { + if (startIndex > endIndex) throw new IllegalArgumentException(); + if (startIndex < 0 || endIndex > array.length) throw new ArrayIndexOutOfBoundsException(); + + final int work[] = new int[endIndex - startIndex]; + System.arraycopy(array, startIndex, work, 0, work.length); + final int index = Arrays.binarySearch(work, value); + if (index >= 0) { + return index + startIndex; + } else { + return ~(~index + startIndex); + } + } +} diff --git a/java/src/com/android/inputmethod/compat/CompatUtils.java b/java/src/com/android/inputmethod/compat/CompatUtils.java index 0b532f7f0..b42633cd9 100644 --- a/java/src/com/android/inputmethod/compat/CompatUtils.java +++ b/java/src/com/android/inputmethod/compat/CompatUtils.java @@ -22,7 +22,6 @@ import android.util.Log; import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @@ -74,76 +73,73 @@ public class CompatUtils { return targetClass.getMethod(name, parameterTypes); } catch (SecurityException e) { // ignore - return null; } catch (NoSuchMethodException e) { // ignore - return null; } + return null; } public static Field getField(Class<?> targetClass, String name) { + if (targetClass == null || TextUtils.isEmpty(name)) return null; try { return targetClass.getField(name); } catch (SecurityException e) { // ignore - return null; } catch (NoSuchFieldException e) { // ignore - return null; } + return null; } - public static Constructor<?> getConstructor(Class<?> targetClass, Class<?>[] types) { + public static Constructor<?> getConstructor(Class<?> targetClass, Class<?> ... types) { if (targetClass == null || types == null) return null; try { return targetClass.getConstructor(types); } catch (SecurityException e) { // ignore - return null; } catch (NoSuchMethodException e) { // ignore - return null; } + return null; + } + + public static Object newInstance(Constructor<?> constructor, Object ... args) { + if (constructor == null) return null; + try { + return constructor.newInstance(args); + } catch (Exception e) { + Log.e(TAG, "Exception in newInstance: " + e.getClass().getSimpleName()); + } + return null; } public static Object invoke( Object receiver, Object defaultValue, Method method, Object... args) { - if (receiver == null || method == null) return defaultValue; + if (method == null) return defaultValue; try { return method.invoke(receiver, args); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Exception in invoke: IllegalArgumentException"); - return defaultValue; - } catch (IllegalAccessException e) { - Log.e(TAG, "Exception in invoke: IllegalAccessException"); - return defaultValue; - } catch (InvocationTargetException e) { - Log.e(TAG, "Exception in invoke: IllegalTargetException"); - return defaultValue; + } catch (Exception e) { + Log.e(TAG, "Exception in invoke: " + e.getClass().getSimpleName()); } + return defaultValue; } public static Object getFieldValue(Object receiver, Object defaultValue, Field field) { - if (receiver == null || field == null) return defaultValue; + if (field == null) return defaultValue; try { return field.get(receiver); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Exception in getFieldValue: IllegalArgumentException"); - return defaultValue; - } catch (IllegalAccessException e) { - Log.e(TAG, "Exception in getFieldValue: IllegalAccessException"); - return defaultValue; + } catch (Exception e) { + Log.e(TAG, "Exception in getFieldValue: " + e.getClass().getSimpleName()); } + return defaultValue; } public static void setFieldValue(Object receiver, Field field, Object value) { - if (receiver == null || field == null) return; + if (field == null) return; try { field.set(receiver, value); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Exception in setFieldValue: IllegalArgumentException"); - } catch (IllegalAccessException e) { - Log.e(TAG, "Exception in setFieldValue: IllegalAccessException"); + } catch (Exception e) { + Log.e(TAG, "Exception in setFieldValue: " + e.getClass().getSimpleName()); } } diff --git a/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java b/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java index f6f4f7a59..bcdcef7dc 100644 --- a/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java @@ -27,7 +27,7 @@ public class EditorInfoCompatUtils { private static final Field FIELD_IME_FLAG_NAVIGATE_PREVIOUS = CompatUtils.getField( EditorInfo.class, "IME_FLAG_NAVIGATE_PREVIOUS"); private static final Field FIELD_IME_ACTION_PREVIOUS = CompatUtils.getField( - EditorInfo.class, "IME_FLAG_ACTION_PREVIOUS"); + 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 @@ -78,6 +78,9 @@ public class EditorInfoCompatUtils { case EditorInfo.IME_ACTION_SEND: action = "actionSend"; break; + case EditorInfo.IME_ACTION_NEXT: + action = "actionNext"; + break; case EditorInfo.IME_ACTION_DONE: action = "actionDone"; break; diff --git a/java/src/com/android/inputmethod/compat/FrameLayoutCompatUtils.java b/java/src/com/android/inputmethod/compat/FrameLayoutCompatUtils.java new file mode 100644 index 000000000..523bf7d0e --- /dev/null +++ b/java/src/com/android/inputmethod/compat/FrameLayoutCompatUtils.java @@ -0,0 +1,63 @@ +/* + * 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.View; +import android.view.ViewGroup; +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 static MarginLayoutParams newLayoutParam(ViewGroup placer, int width, int height) { + if (placer instanceof FrameLayout) { + return new FrameLayout.LayoutParams(width, height); + } else if (placer instanceof RelativeLayout) { + return new RelativeLayout.LayoutParams(width, height); + } else if (placer == null) { + throw new NullPointerException("placer is null"); + } else { + throw new IllegalArgumentException("placer is neither FrameLayout nor RelativeLayout: " + + placer.getClass().getName()); + } + } + + public static void placeViewAt(View view, int x, int y, int w, int h) { + final ViewGroup.LayoutParams lp = view.getLayoutParams(); + if (lp instanceof MarginLayoutParams) { + final MarginLayoutParams marginLayoutParams = (MarginLayoutParams)lp; + marginLayoutParams.width = w; + marginLayoutParams.height = h; + marginLayoutParams.setMargins(x, y, 0, 0); + } + } +} diff --git a/java/src/com/android/inputmethod/compat/InputConnectionCompatUtils.java b/java/src/com/android/inputmethod/compat/InputConnectionCompatUtils.java index c926be06f..7d00b6007 100644 --- a/java/src/com/android/inputmethod/compat/InputConnectionCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/InputConnectionCompatUtils.java @@ -18,15 +18,12 @@ package com.android.inputmethod.compat; import com.android.inputmethod.latin.EditingUtils.SelectedWord; -import android.util.Log; import android.view.inputmethod.InputConnection; import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class InputConnectionCompatUtils { - private static final String TAG = InputConnectionCompatUtils.class.getSimpleName(); private static final Class<?> CLASS_CorrectionInfo = CompatUtils .getClass("android.view.inputmethod.CorrectionInfo"); private static final Class<?>[] INPUT_TYPE_CorrectionInfo = new Class<?>[] { int.class, @@ -53,18 +50,10 @@ public class InputConnectionCompatUtils { return; } Object[] args = { offset, oldText, newText }; - try { - Object correctionInfo = CONSTRUCTOR_CorrectionInfo.newInstance(args); + Object correctionInfo = CompatUtils.newInstance(CONSTRUCTOR_CorrectionInfo, args); + if (correctionInfo != null) { CompatUtils.invoke(ic, null, METHOD_InputConnection_commitCorrection, correctionInfo); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Error in commitCorrection: IllegalArgumentException"); - } catch (InstantiationException e) { - Log.e(TAG, "Error in commitCorrection: InstantiationException"); - } catch (IllegalAccessException e) { - Log.e(TAG, "Error in commitCorrection: IllegalAccessException"); - } catch (InvocationTargetException e) { - Log.e(TAG, "Error in commitCorrection: InvocationTargetException"); } } diff --git a/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java index c04181b55..ec7250c2a 100644 --- a/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java +++ b/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java @@ -16,12 +16,12 @@ package com.android.inputmethod.compat; -import com.android.inputmethod.deprecated.LanguageSwitcherProxy; -import com.android.inputmethod.latin.SubtypeSwitcher; - import android.inputmethodservice.InputMethodService; // import android.view.inputmethod.InputMethodSubtype; +import com.android.inputmethod.deprecated.LanguageSwitcherProxy; +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. diff --git a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java index 806c355a9..667d86c42 100644 --- a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java +++ b/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java @@ -48,6 +48,8 @@ public final class InputMethodSubtypeCompatWrapper extends AbstractCompatWrapper 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 final int mDummyNameResId; private final int mDummyIconResId; @@ -116,6 +118,10 @@ public final class InputMethodSubtypeCompatWrapper extends AbstractCompatWrapper return (String)CompatUtils.invoke(mObj, null, METHOD_getExtraValueOf, key); } + public boolean isAuxiliary() { + return (Boolean)CompatUtils.invoke(mObj, false, METHOD_isAuxiliary); + } + public boolean isDummy() { return !hasOriginalObject(); } diff --git a/java/src/com/android/inputmethod/compat/InputTypeCompatUtils.java b/java/src/com/android/inputmethod/compat/InputTypeCompatUtils.java index d85174188..6c2f0f799 100644 --- a/java/src/com/android/inputmethod/compat/InputTypeCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/InputTypeCompatUtils.java @@ -37,6 +37,7 @@ public class InputTypeCompatUtils { (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; @@ -45,20 +46,35 @@ public class InputTypeCompatUtils { 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; + 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; @@ -78,6 +94,13 @@ public class InputTypeCompatUtils { || 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 = diff --git a/java/src/com/android/inputmethod/compat/LinearLayoutCompatUtils.java b/java/src/com/android/inputmethod/compat/LinearLayoutCompatUtils.java new file mode 100644 index 000000000..674cbe74b --- /dev/null +++ b/java/src/com/android/inputmethod/compat/LinearLayoutCompatUtils.java @@ -0,0 +1,55 @@ +/* + * 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.res.TypedArray; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.util.Log; + +import java.lang.reflect.Field; + +public class LinearLayoutCompatUtils { + private static final String TAG = LinearLayoutCompatUtils.class.getSimpleName(); + + private static final Class<?> CLASS_R_STYLEABLE = CompatUtils.getClass( + "com.android.internal.R$styleable"); + private static final Field STYLEABLE_VIEW = CompatUtils.getField( + CLASS_R_STYLEABLE, "View"); + private static final Field STYLEABLE_VIEW_BACKGROUND = CompatUtils.getField( + CLASS_R_STYLEABLE, "View_background"); + private static final Object VALUE_STYLEABLE_VIEW = CompatUtils.getFieldValue( + null, null, STYLEABLE_VIEW); + private static final Integer VALUE_STYLEABLE_VIEW_BACKGROUND = + (Integer)CompatUtils.getFieldValue(null, null, STYLEABLE_VIEW_BACKGROUND); + + public static Drawable getBackgroundDrawable(Context context, AttributeSet attrs, + int defStyleAttr, int defStyleRes) { + if (!(VALUE_STYLEABLE_VIEW instanceof int[]) || VALUE_STYLEABLE_VIEW_BACKGROUND == null) { + Log.w(TAG, "Can't get View background attribute using reflection"); + return null; + } + + final int[] styleableView = (int[])VALUE_STYLEABLE_VIEW; + final TypedArray a = context.obtainStyledAttributes( + attrs, styleableView, defStyleAttr, defStyleRes); + final Drawable background = a.getDrawable(VALUE_STYLEABLE_VIEW_BACKGROUND); + a.recycle(); + return background; + } +} diff --git a/java/src/com/android/inputmethod/compat/MotionEventCompatUtils.java b/java/src/com/android/inputmethod/compat/MotionEventCompatUtils.java new file mode 100644 index 000000000..8518a4a78 --- /dev/null +++ b/java/src/com/android/inputmethod/compat/MotionEventCompatUtils.java @@ -0,0 +1,23 @@ +/* + * 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 { + public static final int ACTION_HOVER_MOVE = 0x7; + 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 new file mode 100644 index 000000000..4929dd948 --- /dev/null +++ b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java @@ -0,0 +1,87 @@ +/* + * 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 com.android.inputmethod.latin.SuggestedWords; +import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver; + +import android.content.Context; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.Spanned; +import android.text.TextUtils; + +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.Locale; + +public class SuggestionSpanUtils { + // TODO: Use reflection to get field values + public static final String ACTION_SUGGESTION_PICKED = + "android.text.style.SUGGESTION_PICKED"; + public static final String SUGGESTION_SPAN_PICKED_AFTER = "after"; + public static final String SUGGESTION_SPAN_PICKED_BEFORE = "before"; + public static final String SUGGESTION_SPAN_PICKED_HASHCODE = "hashcode"; + public static final int SUGGESTION_MAX_SIZE = 5; + public static final boolean SUGGESTION_SPAN_IS_SUPPORTED; + + private static final Class<?> CLASS_SuggestionSpan = CompatUtils + .getClass("android.text.style.SuggestionSpan"); + private static final Class<?>[] INPUT_TYPE_SuggestionSpan = new Class<?>[] { + Context.class, Locale.class, String[].class, int.class, Class.class }; + private static final Constructor<?> CONSTRUCTOR_SuggestionSpan = CompatUtils + .getConstructor(CLASS_SuggestionSpan, INPUT_TYPE_SuggestionSpan); + static { + SUGGESTION_SPAN_IS_SUPPORTED = + CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null; + } + + public static CharSequence getTextWithSuggestionSpan(Context context, + CharSequence pickedWord, SuggestedWords suggestedWords) { + if (TextUtils.isEmpty(pickedWord) || CONSTRUCTOR_SuggestionSpan == null + || suggestedWords == null || suggestedWords.size() == 0) { + return pickedWord; + } + + final Spannable spannable; + if (pickedWord instanceof Spannable) { + spannable = (Spannable) pickedWord; + } else { + spannable = new SpannableString(pickedWord); + } + final ArrayList<String> suggestionsList = new ArrayList<String>(); + for (int i = 0; i < suggestedWords.size(); ++i) { + if (suggestionsList.size() >= SUGGESTION_MAX_SIZE) { + break; + } + final CharSequence word = suggestedWords.getWord(i); + if (!TextUtils.equals(pickedWord, word)) { + suggestionsList.add(word.toString()); + } + } + + final Object[] args = + { context, null, suggestionsList.toArray(new String[suggestionsList.size()]), 0, + (Class<?>) SuggestionSpanPickedNotificationReceiver.class }; + final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args); + if (ss == null) { + return pickedWord; + } + spannable.setSpan(ss, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + return spannable; + } +} |