CompatUtils.java revision 9115dd272328def247116ccf1ce458d360fec4ad
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.compat;
18
19import android.content.Intent;
20import android.text.TextUtils;
21import android.util.Log;
22
23import java.lang.reflect.Constructor;
24import java.lang.reflect.Field;
25import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Method;
27import java.util.ArrayList;
28import java.util.List;
29
30public class CompatUtils {
31    private static final String TAG = CompatUtils.class.getSimpleName();
32    private static final String EXTRA_INPUT_METHOD_ID = "input_method_id";
33    // TODO: Can these be constants instead of literal String constants?
34    private static final String INPUT_METHOD_SUBTYPE_SETTINGS =
35            "android.settings.INPUT_METHOD_SUBTYPE_SETTINGS";
36    private static final String INPUT_LANGUAGE_SELECTION =
37            "com.android.inputmethod.latin.INPUT_LANGUAGE_SELECTION";
38
39    public static Intent getInputLanguageSelectionIntent(String inputMethodId,
40            int flagsForSubtypeSettings) {
41        final String action;
42        Intent intent;
43        if (android.os.Build.VERSION.SDK_INT
44                >= /* android.os.Build.VERSION_CODES.HONEYCOMB */ 11) {
45            // Refer to android.provider.Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS
46            action = INPUT_METHOD_SUBTYPE_SETTINGS;
47            intent = new Intent(action);
48            if (!TextUtils.isEmpty(inputMethodId)) {
49                intent.putExtra(EXTRA_INPUT_METHOD_ID, inputMethodId);
50            }
51            if (flagsForSubtypeSettings > 0) {
52                intent.setFlags(flagsForSubtypeSettings);
53            }
54        } else {
55            action = INPUT_LANGUAGE_SELECTION;
56            intent = new Intent(action);
57        }
58        return intent;
59    }
60
61    public static Class<?> getClass(String className) {
62        try {
63            return Class.forName(className);
64        } catch (ClassNotFoundException e) {
65            return null;
66        }
67    }
68
69    public static Method getMethod(Class<?> targetClass, String name,
70            Class<?>... parameterTypes) {
71        if (targetClass == null || TextUtils.isEmpty(name)) return null;
72        try {
73            return targetClass.getMethod(name, parameterTypes);
74        } catch (SecurityException e) {
75            // ignore
76            return null;
77        } catch (NoSuchMethodException e) {
78            // ignore
79            return null;
80        }
81    }
82
83    public static Field getField(Class<?> targetClass, String name) {
84        try {
85            return targetClass.getField(name);
86        } catch (SecurityException e) {
87            // ignore
88            return null;
89        } catch (NoSuchFieldException e) {
90            // ignore
91            return null;
92        }
93    }
94
95    public static Constructor<?> getConstructor(Class<?> targetClass, Class<?>[] types) {
96        if (targetClass == null || types == null) return null;
97        try {
98            return targetClass.getConstructor(types);
99        } catch (SecurityException e) {
100            // ignore
101            return null;
102        } catch (NoSuchMethodException e) {
103            // ignore
104            return null;
105        }
106    }
107
108    public static Object invoke(
109            Object receiver, Object defaultValue, Method method, Object... args) {
110        if (receiver == null || method == null) return defaultValue;
111        try {
112            return method.invoke(receiver, args);
113        } catch (IllegalArgumentException e) {
114            Log.e(TAG, "Exception in invoke: IllegalArgumentException");
115            return defaultValue;
116        } catch (IllegalAccessException e) {
117            Log.e(TAG, "Exception in invoke: IllegalAccessException");
118            return defaultValue;
119        } catch (InvocationTargetException e) {
120            Log.e(TAG, "Exception in invoke: IllegalTargetException");
121            return defaultValue;
122        }
123    }
124
125    public static Object getFieldValue(Object receiver, Object defaultValue, Field field) {
126        if (receiver == null || field == null) return defaultValue;
127        try {
128            return field.get(receiver);
129        } catch (IllegalArgumentException e) {
130            Log.e(TAG, "Exception in getFieldValue: IllegalArgumentException");
131            return defaultValue;
132        } catch (IllegalAccessException e) {
133            Log.e(TAG, "Exception in getFieldValue: IllegalAccessException");
134            return defaultValue;
135        }
136    }
137
138    public static void setFieldValue(Object receiver, Field field, Object value) {
139        if (receiver == null || field == null) return;
140        try {
141            field.set(receiver, value);
142        } catch (IllegalArgumentException e) {
143            Log.e(TAG, "Exception in setFieldValue: IllegalArgumentException");
144        } catch (IllegalAccessException e) {
145            Log.e(TAG, "Exception in setFieldValue: IllegalAccessException");
146        }
147    }
148
149    public static List<InputMethodSubtypeCompatWrapper> copyInputMethodSubtypeListToWrapper(
150            Object listObject) {
151        if (!(listObject instanceof List<?>)) return null;
152        final List<InputMethodSubtypeCompatWrapper> subtypes =
153                new ArrayList<InputMethodSubtypeCompatWrapper>();
154        for (Object o: (List<?>)listObject) {
155            subtypes.add(new InputMethodSubtypeCompatWrapper(o));
156        }
157        return subtypes;
158    }
159}
160