diff options
author | Likai Ding <likaid@codeaurora.org> | 2016-05-18 13:47:56 +0800 |
---|---|---|
committer | Likai Ding <likaid@codeaurora.org> | 2016-06-03 15:34:47 +0800 |
commit | 711aee7790ab995e61231e836bfe36a38f65cfce (patch) | |
tree | 761cfc107c9d8647be8f19946c29bf540b30f24f /gallerycommon | |
parent | c83b5b7faee8117f7f83ca931db0040610ace976 (diff) | |
download | android_packages_apps_Gallery2-711aee7790ab995e61231e836bfe36a38f65cfce.tar.gz android_packages_apps_Gallery2-711aee7790ab995e61231e836bfe36a38f65cfce.tar.bz2 android_packages_apps_Gallery2-711aee7790ab995e61231e836bfe36a38f65cfce.zip |
Gallery: SystemProperties wrapper
CRs-Fixed: 986672
Change-Id: I84c1c24604ef2bb5890e8054f4a8ef9b43f1a07e
Diffstat (limited to 'gallerycommon')
-rw-r--r-- | gallerycommon/src/com/android/gallery3d/common/ApiHelper.java | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/gallerycommon/src/com/android/gallery3d/common/ApiHelper.java b/gallerycommon/src/com/android/gallery3d/common/ApiHelper.java index f4de5c9ff..1c3ffcd67 100644 --- a/gallerycommon/src/com/android/gallery3d/common/ApiHelper.java +++ b/gallerycommon/src/com/android/gallery3d/common/ApiHelper.java @@ -25,6 +25,8 @@ import android.view.View; import android.view.WindowManager; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; public class ApiHelper { public static interface VERSION_CODES { @@ -231,4 +233,72 @@ public class ApiHelper { return false; } } + + private static Class<?> getClassForName(String className) { + Class<?> klass = null; + try { + klass = Class.forName(className); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return klass; + } + + private static Method getMethod(Class<?> klass, String name, Class<?>... parameterTypes) { + Method method = null; + if (klass != null) { + try { + method = klass.getMethod(name, parameterTypes); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + return method; + } + + private static Object invoke(Method method, Object receiver, Object... args) { + Object obj = null; + if (method != null) { + try { + obj = method.invoke(receiver, args); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + return obj; + } + + public static class SystemProperties { + private static final Method getIntMethod; + private static final Method getBooleanMethod; + private static final Method getMethod; + private static final Method setMethod; + + static { + Class<?> klass = getClassForName("android.os.SystemProperties"); + getIntMethod = getMethod(klass, "getInt", String.class, int.class); + getBooleanMethod = getMethod(klass, "getBoolean", String.class, boolean.class); + getMethod = getMethod(klass, "get", String.class, String.class); + setMethod = getMethod(klass, "set", String.class, String.class); + } + + public static int getInt(String key, int def) { + Object obj = invoke(getIntMethod, null, key, def); + return obj == null ? def : (Integer) obj; + } + + public static boolean getBoolean(String key, boolean def) { + Object obj = invoke(getBooleanMethod, null, key, def); + return obj == null ? def : (Boolean) obj; + } + + public static String get(String key, String def) { + Object obj = invoke(getMethod, null, key, def); + return obj == null ? def : (String) obj; + } + + public static void set(String key, String val) { + invoke(setMethod, null, key, val); + } + } } |