Home | History | Annotate | Download | only in compat
      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 
     17 package com.android.inputmethod.compat;
     18 
     19 import android.content.Intent;
     20 import android.text.TextUtils;
     21 import android.util.Log;
     22 
     23 import java.lang.reflect.Constructor;
     24 import java.lang.reflect.Field;
     25 import java.lang.reflect.Method;
     26 import java.util.ArrayList;
     27 import java.util.List;
     28 
     29 public 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     private static final String INPUT_LANGUAGE_SELECTION =
     36             "com.android.inputmethod.latin.INPUT_LANGUAGE_SELECTION";
     37 
     38     public static Intent getInputLanguageSelectionIntent(String inputMethodId,
     39             int flagsForSubtypeSettings) {
     40         final String action;
     41         Intent intent;
     42         if (InputMethodServiceCompatWrapper.CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED
     43                 /* android.os.Build.VERSION_CODES.HONEYCOMB */
     44                 && android.os.Build.VERSION.SDK_INT >=  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         } catch (NoSuchMethodException e) {
     77             // ignore
     78         }
     79         return null;
     80     }
     81 
     82     public static Field getField(Class<?> targetClass, String name) {
     83         if (targetClass == null || TextUtils.isEmpty(name)) return null;
     84         try {
     85             return targetClass.getField(name);
     86         } catch (SecurityException e) {
     87             // ignore
     88         } catch (NoSuchFieldException e) {
     89             // ignore
     90         }
     91         return null;
     92     }
     93 
     94     public static Constructor<?> getConstructor(Class<?> targetClass, Class<?> ... types) {
     95         if (targetClass == null || types == null) return null;
     96         try {
     97             return targetClass.getConstructor(types);
     98         } catch (SecurityException e) {
     99             // ignore
    100         } catch (NoSuchMethodException e) {
    101             // ignore
    102         }
    103         return null;
    104     }
    105 
    106     public static Object newInstance(Constructor<?> constructor, Object ... args) {
    107         if (constructor == null) return null;
    108         try {
    109             return constructor.newInstance(args);
    110         } catch (Exception e) {
    111             Log.e(TAG, "Exception in newInstance: " + e.getClass().getSimpleName());
    112         }
    113         return null;
    114     }
    115 
    116     public static Object invoke(
    117             Object receiver, Object defaultValue, Method method, Object... args) {
    118         if (method == null) return defaultValue;
    119         try {
    120             return method.invoke(receiver, args);
    121         } catch (Exception e) {
    122             Log.e(TAG, "Exception in invoke: " + e.getClass().getSimpleName());
    123         }
    124         return defaultValue;
    125     }
    126 
    127     public static Object getFieldValue(Object receiver, Object defaultValue, Field field) {
    128         if (field == null) return defaultValue;
    129         try {
    130             return field.get(receiver);
    131         } catch (Exception e) {
    132             Log.e(TAG, "Exception in getFieldValue: " + e.getClass().getSimpleName());
    133         }
    134         return defaultValue;
    135     }
    136 
    137     public static void setFieldValue(Object receiver, Field field, Object value) {
    138         if (field == null) return;
    139         try {
    140             field.set(receiver, value);
    141         } catch (Exception e) {
    142             Log.e(TAG, "Exception in setFieldValue: " + e.getClass().getSimpleName());
    143         }
    144     }
    145 
    146     public static List<InputMethodSubtypeCompatWrapper> copyInputMethodSubtypeListToWrapper(
    147             Object listObject) {
    148         if (!(listObject instanceof List<?>)) return null;
    149         final List<InputMethodSubtypeCompatWrapper> subtypes =
    150                 new ArrayList<InputMethodSubtypeCompatWrapper>();
    151         for (Object o: (List<?>)listObject) {
    152             subtypes.add(new InputMethodSubtypeCompatWrapper(o));
    153         }
    154         return subtypes;
    155     }
    156 }
    157