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