CompatUtils.java revision 24119dfd414f948b2da214c021cc76e06b3dd41c
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.Field; 24import java.lang.reflect.InvocationTargetException; 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 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 (android.os.Build.VERSION.SDK_INT 43 >= /* android.os.Build.VERSION_CODES.HONEYCOMB */ 11) { 44 // Refer to android.provider.Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS 45 action = INPUT_METHOD_SUBTYPE_SETTINGS; 46 intent = new Intent(action); 47 if (!TextUtils.isEmpty(inputMethodId)) { 48 intent.putExtra(EXTRA_INPUT_METHOD_ID, inputMethodId); 49 } 50 if (flagsForSubtypeSettings > 0) { 51 intent.setFlags(flagsForSubtypeSettings); 52 } 53 } else { 54 action = INPUT_LANGUAGE_SELECTION; 55 intent = new Intent(action); 56 } 57 return intent; 58 } 59 60 public static Class<?> getClass(String className) { 61 try { 62 return Class.forName(className); 63 } catch (ClassNotFoundException e) { 64 return null; 65 } 66 } 67 68 public static Method getMethod(Class<?> targetClass, String name, 69 Class<?>... parameterTypes) { 70 try { 71 return targetClass.getMethod(name, parameterTypes); 72 } catch (SecurityException e) { 73 // ignore 74 return null; 75 } catch (NoSuchMethodException e) { 76 // ignore 77 return null; 78 } 79 } 80 81 public static Field getField(Class<?> targetClass, String name) { 82 try { 83 return targetClass.getField(name); 84 } catch (SecurityException e) { 85 // ignore 86 return null; 87 } catch (NoSuchFieldException e) { 88 // ignore 89 return null; 90 } 91 } 92 93 public static Object invoke( 94 Object receiver, Object defaultValue, Method method, Object... args) { 95 if (receiver == null || method == null) return defaultValue; 96 try { 97 return method.invoke(receiver, args); 98 } catch (IllegalArgumentException e) { 99 Log.e(TAG, "Exception in invoke: IllegalArgmentException"); 100 return defaultValue; 101 } catch (IllegalAccessException e) { 102 Log.e(TAG, "Exception in invoke: IllegalAccessException"); 103 return defaultValue; 104 } catch (InvocationTargetException e) { 105 Log.e(TAG, "Exception in invoke: IllegalTargetException"); 106 return defaultValue; 107 } 108 } 109 110 public static Object getFieldValue(Object receiver, Object defaultValue, Field field) { 111 if (receiver == null || field == null) return defaultValue; 112 try { 113 return field.get(receiver); 114 } catch (IllegalArgumentException e) { 115 return defaultValue; 116 } catch (IllegalAccessException e) { 117 return defaultValue; 118 } 119 } 120 121 public static void setFieldValue(Object receiver, Field field, Object value) { 122 if (receiver == null || field == null) return; 123 try { 124 field.set(receiver, value); 125 } catch (IllegalArgumentException e) { 126 // ignore 127 } catch (IllegalAccessException e) { 128 // ignore 129 } 130 } 131 132 public static List<InputMethodSubtypeCompatWrapper> copyInputMethodSubtypeListToWrapper( 133 Object listObject) { 134 if (!(listObject instanceof List<?>)) return null; 135 final List<InputMethodSubtypeCompatWrapper> subtypes = 136 new ArrayList<InputMethodSubtypeCompatWrapper>(); 137 for (Object o: (List<?>)listObject) { 138 subtypes.add(new InputMethodSubtypeCompatWrapper(o)); 139 } 140 return subtypes; 141 } 142} 143