CompatUtils.java revision 19457316c1da5e4ca52d2fe8039d76469fbf1e61
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.Method;
26import java.util.ArrayList;
27import java.util.List;
28
29public class CompatUtils {
30    private static final String TAG = CompatUtils.class.getSimpleName();
31    private static final String EXTRA_INPUT_METHOD_ID = "input_method_id";
32    // TODO: Can these be constants instead of literal String constants?
33    private static final String INPUT_METHOD_SUBTYPE_SETTINGS =
34            "android.settings.INPUT_METHOD_SUBTYPE_SETTINGS";
35
36    public static Intent getInputLanguageSelectionIntent(String inputMethodId,
37            int flagsForSubtypeSettings) {
38        // Refer to android.provider.Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS
39        final String action = INPUT_METHOD_SUBTYPE_SETTINGS;
40        final Intent intent = new Intent(action);
41        if (!TextUtils.isEmpty(inputMethodId)) {
42            intent.putExtra(EXTRA_INPUT_METHOD_ID, inputMethodId);
43        }
44        if (flagsForSubtypeSettings > 0) {
45            intent.setFlags(flagsForSubtypeSettings);
46        }
47        return intent;
48    }
49
50    public static Class<?> getClass(String className) {
51        try {
52            return Class.forName(className);
53        } catch (ClassNotFoundException e) {
54            return null;
55        }
56    }
57
58    public static Method getMethod(Class<?> targetClass, String name,
59            Class<?>... parameterTypes) {
60        if (targetClass == null || TextUtils.isEmpty(name)) return null;
61        try {
62            return targetClass.getMethod(name, parameterTypes);
63        } catch (SecurityException e) {
64            // ignore
65        } catch (NoSuchMethodException e) {
66            // ignore
67        }
68        return null;
69    }
70
71    public static Field getField(Class<?> targetClass, String name) {
72        if (targetClass == null || TextUtils.isEmpty(name)) return null;
73        try {
74            return targetClass.getField(name);
75        } catch (SecurityException e) {
76            // ignore
77        } catch (NoSuchFieldException e) {
78            // ignore
79        }
80        return null;
81    }
82
83    public static Constructor<?> getConstructor(Class<?> targetClass, Class<?> ... types) {
84        if (targetClass == null || types == null) return null;
85        try {
86            return targetClass.getConstructor(types);
87        } catch (SecurityException e) {
88            // ignore
89        } catch (NoSuchMethodException e) {
90            // ignore
91        }
92        return null;
93    }
94
95    public static Object newInstance(Constructor<?> constructor, Object ... args) {
96        if (constructor == null) return null;
97        try {
98            return constructor.newInstance(args);
99        } catch (Exception e) {
100            Log.e(TAG, "Exception in newInstance: " + e.getClass().getSimpleName());
101        }
102        return null;
103    }
104
105    public static Object invoke(
106            Object receiver, Object defaultValue, Method method, Object... args) {
107        if (method == null) return defaultValue;
108        try {
109            return method.invoke(receiver, args);
110        } catch (Exception e) {
111            Log.e(TAG, "Exception in invoke: " + e.getClass().getSimpleName());
112        }
113        return defaultValue;
114    }
115
116    public static Object getFieldValue(Object receiver, Object defaultValue, Field field) {
117        if (field == null) return defaultValue;
118        try {
119            return field.get(receiver);
120        } catch (Exception e) {
121            Log.e(TAG, "Exception in getFieldValue: " + e.getClass().getSimpleName());
122        }
123        return defaultValue;
124    }
125
126    public static void setFieldValue(Object receiver, Field field, Object value) {
127        if (field == null) return;
128        try {
129            field.set(receiver, value);
130        } catch (Exception e) {
131            Log.e(TAG, "Exception in setFieldValue: " + e.getClass().getSimpleName());
132        }
133    }
134
135    public static List<InputMethodSubtypeCompatWrapper> copyInputMethodSubtypeListToWrapper(
136            Object listObject) {
137        if (!(listObject instanceof List<?>)) return null;
138        final List<InputMethodSubtypeCompatWrapper> subtypes =
139                new ArrayList<InputMethodSubtypeCompatWrapper>();
140        for (Object o: (List<?>)listObject) {
141            subtypes.add(new InputMethodSubtypeCompatWrapper(o));
142        }
143        return subtypes;
144    }
145}
146