aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/compat
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/compat')
-rw-r--r--java/src/com/android/inputmethod/compat/CompatUtils.java54
-rw-r--r--java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java3
-rw-r--r--java/src/com/android/inputmethod/compat/InputConnectionCompatUtils.java15
-rw-r--r--java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java6
-rw-r--r--java/src/com/android/inputmethod/compat/InputTypeCompatUtils.java29
-rw-r--r--java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java78
6 files changed, 140 insertions, 45 deletions
diff --git a/java/src/com/android/inputmethod/compat/CompatUtils.java b/java/src/com/android/inputmethod/compat/CompatUtils.java
index 0b532f7f0..6e14bfa8b 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,23 +73,22 @@ 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) {
@@ -99,51 +97,49 @@ public class CompatUtils {
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..2789bcb39 100644
--- a/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java
+++ b/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java
@@ -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/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/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/SuggestionSpanUtils.java b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java
new file mode 100644
index 000000000..5b02de36e
--- /dev/null
+++ b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java
@@ -0,0 +1,78 @@
+/*
+ * 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 java.lang.reflect.Constructor;
+import java.util.Locale;
+
+public class SuggestionSpanUtils {
+ 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";
+
+ 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);
+ public static final boolean SUGGESTION_SPAN_IS_SUPPORTED;
+ static {
+ SUGGESTION_SPAN_IS_SUPPORTED =
+ CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null;
+ }
+
+ public static CharSequence getTextWithSuggestionSpan(Context context,
+ CharSequence suggestion, SuggestedWords suggestedWords) {
+ if (CONSTRUCTOR_SuggestionSpan == null || suggestedWords == null
+ || suggestedWords.size() == 0) {
+ return suggestion;
+ }
+
+ final Spannable spannable;
+ if (suggestion instanceof Spannable) {
+ spannable = (Spannable) suggestion;
+ } else {
+ spannable = new SpannableString(suggestion);
+ }
+ // TODO: Use SUGGESTIONS_MAX_SIZE instead of 5.
+ final int N = Math.min(5, suggestedWords.size());
+ final String[] suggestionsArray = new String[N];
+ for (int i = 0; i < N; ++i) {
+ suggestionsArray[i] = suggestedWords.getWord(i).toString();
+ }
+ final Object[] args =
+ { context, null, suggestionsArray, 0,
+ (Class<?>) SuggestionSpanPickedNotificationReceiver.class };
+ final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args);
+ if (ss == null) {
+ return suggestion;
+ }
+ spannable.setSpan(ss, 0, suggestion.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+ return spannable;
+ }
+}