diff options
author | 2011-03-17 22:49:06 -0700 | |
---|---|---|
committer | 2011-03-18 22:49:57 -0700 | |
commit | 610f1dc8553cf2ed97e763a06a19380c4a6cd636 (patch) | |
tree | cec8e4165fc5f72a57ca52409668bcfa1c314e77 /java/src/com/android/inputmethod/compat/CompatUtils.java | |
parent | 0a7cf81ca297f511e0d0d2478a792014d0b62945 (diff) | |
download | latinime-610f1dc8553cf2ed97e763a06a19380c4a6cd636.tar.gz latinime-610f1dc8553cf2ed97e763a06a19380c4a6cd636.tar.xz latinime-610f1dc8553cf2ed97e763a06a19380c4a6cd636.zip |
Use reflections for classes related to InputMethodSubtype
Change-Id: Ica53ce879c2b4c5eb47f757fb788a795a881c30e
Diffstat (limited to 'java/src/com/android/inputmethod/compat/CompatUtils.java')
-rw-r--r-- | java/src/com/android/inputmethod/compat/CompatUtils.java | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/compat/CompatUtils.java b/java/src/com/android/inputmethod/compat/CompatUtils.java index e1e812d83..157446654 100644 --- a/java/src/com/android/inputmethod/compat/CompatUtils.java +++ b/java/src/com/android/inputmethod/compat/CompatUtils.java @@ -18,8 +18,15 @@ package com.android.inputmethod.compat; import android.content.Intent; import android.text.TextUtils; +import android.util.Log; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; public class CompatUtils { + private static final String TAG = CompatUtils.class.getSimpleName(); private static final String EXTRA_INPUT_METHOD_ID = "input_method_id"; // TODO: Can these be constants instead of literal String constants? private static final String INPUT_METHOD_SUBTYPE_SETTINGS = @@ -48,4 +55,53 @@ public class CompatUtils { } return intent; } + + public static Class<?> getClass(String className) { + try { + return Class.forName(className); + } catch (ClassNotFoundException e) { + return null; + } + } + + public static Method getMethod(Class<?> targetClass, String name, + Class<?>... parameterTypes) { + try { + return targetClass.getMethod(name, parameterTypes); + } catch (SecurityException e) { + // ignore + return null; + } catch (NoSuchMethodException e) { + // ignore + return null; + } + } + + public static Object invoke( + Object receiver, Object defaultValue, Method method, Object... args) { + if (receiver == null || method == null) return defaultValue; + try { + return method.invoke(receiver, args); + } catch (IllegalArgumentException e) { + Log.e(TAG, "Exception in invoke: IllegalArgmentException"); + 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; + } + } + + public static List<InputMethodSubtypeCompatWrapper> copyInputMethodSubtypeListToWrappler( + 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; + } } |