am 530845cb: am 9681027a: am 03a1c442: Clean up compatible utility classes
* commit '530845cb5e03be07badba2a56e567519ae16bd60': Clean up compatible utility classesmain
commit
aaa0f84053
|
@ -21,6 +21,7 @@ import android.util.Log;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
public final class CompatUtils {
|
public final class CompatUtils {
|
||||||
|
@ -33,31 +34,31 @@ public final class CompatUtils {
|
||||||
public static Class<?> getClass(final String className) {
|
public static Class<?> getClass(final String className) {
|
||||||
try {
|
try {
|
||||||
return Class.forName(className);
|
return Class.forName(className);
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (final ClassNotFoundException e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Method getMethod(final Class<?> targetClass, final String name,
|
public static Method getMethod(final Class<?> targetClass, final String name,
|
||||||
final Class<?>... parameterTypes) {
|
final Class<?>... parameterTypes) {
|
||||||
if (targetClass == null || TextUtils.isEmpty(name)) return null;
|
if (targetClass == null || TextUtils.isEmpty(name)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return targetClass.getMethod(name, parameterTypes);
|
return targetClass.getMethod(name, parameterTypes);
|
||||||
} catch (SecurityException e) {
|
} catch (final SecurityException | NoSuchMethodException e) {
|
||||||
// ignore
|
|
||||||
} catch (NoSuchMethodException e) {
|
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Field getField(final Class<?> targetClass, final String name) {
|
public static Field getField(final Class<?> targetClass, final String name) {
|
||||||
if (targetClass == null || TextUtils.isEmpty(name)) return null;
|
if (targetClass == null || TextUtils.isEmpty(name)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return targetClass.getField(name);
|
return targetClass.getField(name);
|
||||||
} catch (SecurityException e) {
|
} catch (final SecurityException | NoSuchFieldException e) {
|
||||||
// ignore
|
|
||||||
} catch (NoSuchFieldException e) {
|
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -65,22 +66,25 @@ public final class CompatUtils {
|
||||||
|
|
||||||
public static Constructor<?> getConstructor(final Class<?> targetClass,
|
public static Constructor<?> getConstructor(final Class<?> targetClass,
|
||||||
final Class<?> ... types) {
|
final Class<?> ... types) {
|
||||||
if (targetClass == null || types == null) return null;
|
if (targetClass == null || types == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return targetClass.getConstructor(types);
|
return targetClass.getConstructor(types);
|
||||||
} catch (SecurityException e) {
|
} catch (final SecurityException | NoSuchMethodException e) {
|
||||||
// ignore
|
|
||||||
} catch (NoSuchMethodException e) {
|
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object newInstance(final Constructor<?> constructor, final Object ... args) {
|
public static Object newInstance(final Constructor<?> constructor, final Object ... args) {
|
||||||
if (constructor == null) return null;
|
if (constructor == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return constructor.newInstance(args);
|
return constructor.newInstance(args);
|
||||||
} catch (Exception e) {
|
} catch (final InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||||
|
| InvocationTargetException e) {
|
||||||
Log.e(TAG, "Exception in newInstance", e);
|
Log.e(TAG, "Exception in newInstance", e);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -88,10 +92,13 @@ public final class CompatUtils {
|
||||||
|
|
||||||
public static Object invoke(final Object receiver, final Object defaultValue,
|
public static Object invoke(final Object receiver, final Object defaultValue,
|
||||||
final Method method, final Object... args) {
|
final Method method, final Object... args) {
|
||||||
if (method == null) return defaultValue;
|
if (method == null) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return method.invoke(receiver, args);
|
return method.invoke(receiver, args);
|
||||||
} catch (Exception e) {
|
} catch (final IllegalAccessException | IllegalArgumentException
|
||||||
|
| InvocationTargetException e) {
|
||||||
Log.e(TAG, "Exception in invoke", e);
|
Log.e(TAG, "Exception in invoke", e);
|
||||||
}
|
}
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
|
@ -99,20 +106,24 @@ public final class CompatUtils {
|
||||||
|
|
||||||
public static Object getFieldValue(final Object receiver, final Object defaultValue,
|
public static Object getFieldValue(final Object receiver, final Object defaultValue,
|
||||||
final Field field) {
|
final Field field) {
|
||||||
if (field == null) return defaultValue;
|
if (field == null) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return field.get(receiver);
|
return field.get(receiver);
|
||||||
} catch (Exception e) {
|
} catch (final IllegalAccessException | IllegalArgumentException e) {
|
||||||
Log.e(TAG, "Exception in getFieldValue", e);
|
Log.e(TAG, "Exception in getFieldValue", e);
|
||||||
}
|
}
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setFieldValue(final Object receiver, final Field field, final Object value) {
|
public static void setFieldValue(final Object receiver, final Field field, final Object value) {
|
||||||
if (field == null) return;
|
if (field == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
field.set(receiver, value);
|
field.set(receiver, value);
|
||||||
} catch (Exception e) {
|
} catch (final IllegalAccessException | IllegalArgumentException e) {
|
||||||
Log.e(TAG, "Exception in setFieldValue", e);
|
Log.e(TAG, "Exception in setFieldValue", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ public final class DownloadManagerCompatUtils {
|
||||||
// DownloadManager.Request#setAllowedOverMetered() has been introduced
|
// DownloadManager.Request#setAllowedOverMetered() has been introduced
|
||||||
// in API level 16 (Build.VERSION_CODES.JELLY_BEAN).
|
// in API level 16 (Build.VERSION_CODES.JELLY_BEAN).
|
||||||
private static final Method METHOD_setAllowedOverMetered = CompatUtils.getMethod(
|
private static final Method METHOD_setAllowedOverMetered = CompatUtils.getMethod(
|
||||||
DownloadManager.Request.class, "setAllowedOverMetered", Boolean.TYPE);
|
DownloadManager.Request.class, "setAllowedOverMetered", boolean.class);
|
||||||
|
|
||||||
public static DownloadManager.Request setAllowedOverMetered(
|
public static DownloadManager.Request setAllowedOverMetered(
|
||||||
final DownloadManager.Request request, final boolean allowOverMetered) {
|
final DownloadManager.Request request, final boolean allowOverMetered) {
|
||||||
|
|
|
@ -26,7 +26,7 @@ public final class InputMethodManagerCompatWrapper {
|
||||||
// Note that InputMethodManager.switchToNextInputMethod() has been introduced
|
// Note that InputMethodManager.switchToNextInputMethod() has been introduced
|
||||||
// in API level 16 (Build.VERSION_CODES.JELLY_BEAN).
|
// in API level 16 (Build.VERSION_CODES.JELLY_BEAN).
|
||||||
private static final Method METHOD_switchToNextInputMethod = CompatUtils.getMethod(
|
private static final Method METHOD_switchToNextInputMethod = CompatUtils.getMethod(
|
||||||
InputMethodManager.class, "switchToNextInputMethod", IBinder.class, Boolean.TYPE);
|
InputMethodManager.class, "switchToNextInputMethod", IBinder.class, boolean.class);
|
||||||
|
|
||||||
// Note that InputMethodManager.shouldOfferSwitchingToNextInputMethod() has been introduced
|
// Note that InputMethodManager.shouldOfferSwitchingToNextInputMethod() has been introduced
|
||||||
// in API level 19 (Build.VERSION_CODES.KITKAT).
|
// in API level 19 (Build.VERSION_CODES.KITKAT).
|
||||||
|
|
|
@ -32,8 +32,8 @@ public final class InputMethodSubtypeCompatUtils {
|
||||||
// has been introduced in API level 17 (Build.VERSION_CODE.JELLY_BEAN_MR1).
|
// has been introduced in API level 17 (Build.VERSION_CODE.JELLY_BEAN_MR1).
|
||||||
private static final Constructor<?> CONSTRUCTOR_INPUT_METHOD_SUBTYPE =
|
private static final Constructor<?> CONSTRUCTOR_INPUT_METHOD_SUBTYPE =
|
||||||
CompatUtils.getConstructor(InputMethodSubtype.class,
|
CompatUtils.getConstructor(InputMethodSubtype.class,
|
||||||
Integer.TYPE, Integer.TYPE, String.class, String.class, String.class,
|
int.class, int.class, String.class, String.class, String.class, boolean.class,
|
||||||
Boolean.TYPE, Boolean.TYPE, Integer.TYPE);
|
boolean.class, int.class);
|
||||||
static {
|
static {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||||
if (CONSTRUCTOR_INPUT_METHOD_SUBTYPE == null) {
|
if (CONSTRUCTOR_INPUT_METHOD_SUBTYPE == null) {
|
||||||
|
|
|
@ -34,9 +34,9 @@ import java.util.ArrayList;
|
||||||
public final class SuggestionSpanUtils {
|
public final class SuggestionSpanUtils {
|
||||||
// Note that SuggestionSpan.FLAG_AUTO_CORRECTION has been introduced
|
// Note that SuggestionSpan.FLAG_AUTO_CORRECTION has been introduced
|
||||||
// in API level 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1).
|
// in API level 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1).
|
||||||
public static final Field FIELD_FLAG_AUTO_CORRECTION = CompatUtils.getField(
|
private static final Field FIELD_FLAG_AUTO_CORRECTION = CompatUtils.getField(
|
||||||
SuggestionSpan.class, "FLAG_AUTO_CORRECTION");
|
SuggestionSpan.class, "FLAG_AUTO_CORRECTION");
|
||||||
public static final Integer OBJ_FLAG_AUTO_CORRECTION = (Integer) CompatUtils.getFieldValue(
|
private static final Integer OBJ_FLAG_AUTO_CORRECTION = (Integer) CompatUtils.getFieldValue(
|
||||||
null /* receiver */, null /* defaultValue */, FIELD_FLAG_AUTO_CORRECTION);
|
null /* receiver */, null /* defaultValue */, FIELD_FLAG_AUTO_CORRECTION);
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
|
|
@ -26,7 +26,7 @@ public final class UserDictionaryCompatUtils {
|
||||||
// UserDictionary.Words#addWord(Context, String, int, String, Locale) was introduced
|
// UserDictionary.Words#addWord(Context, String, int, String, Locale) was introduced
|
||||||
// in API level 16 (Build.VERSION_CODES.JELLY_BEAN).
|
// in API level 16 (Build.VERSION_CODES.JELLY_BEAN).
|
||||||
private static final Method METHOD_addWord = CompatUtils.getMethod(Words.class, "addWord",
|
private static final Method METHOD_addWord = CompatUtils.getMethod(Words.class, "addWord",
|
||||||
Context.class, String.class, Integer.TYPE, String.class, Locale.class);
|
Context.class, String.class, int.class, String.class, Locale.class);
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public static void addWord(final Context context, final String word,
|
public static void addWord(final Context context, final String word,
|
||||||
|
|
|
@ -30,7 +30,7 @@ public final class ViewCompatUtils {
|
||||||
View.class, "getPaddingEnd");
|
View.class, "getPaddingEnd");
|
||||||
private static final Method METHOD_setPaddingRelative = CompatUtils.getMethod(
|
private static final Method METHOD_setPaddingRelative = CompatUtils.getMethod(
|
||||||
View.class, "setPaddingRelative",
|
View.class, "setPaddingRelative",
|
||||||
Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE);
|
int.class, int.class, int.class, int.class);
|
||||||
|
|
||||||
private ViewCompatUtils() {
|
private ViewCompatUtils() {
|
||||||
// This utility class is not publicly instantiable.
|
// This utility class is not publicly instantiable.
|
||||||
|
|
|
@ -53,7 +53,7 @@ public class MoreKeySpecSplitTests extends InstrumentationTestCase {
|
||||||
private static String[] getAllResourceIdNames(final Class<?> resourceIdClass) {
|
private static String[] getAllResourceIdNames(final Class<?> resourceIdClass) {
|
||||||
final ArrayList<String> names = new ArrayList<>();
|
final ArrayList<String> names = new ArrayList<>();
|
||||||
for (final Field field : resourceIdClass.getFields()) {
|
for (final Field field : resourceIdClass.getFields()) {
|
||||||
if (field.getType() == Integer.TYPE) {
|
if (field.getType() == int.class) {
|
||||||
names.add(field.getName());
|
names.add(field.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue