diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2017-08-31 16:17:04 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-08-31 16:17:04 +0000 |
commit | c39ea3c55fac807c0b98aabdf56c70dc8a49036c (patch) | |
tree | e282668a9587cf6c1ec7b604dea860400c75c6c7 /java/com/android/dialer/compat/CompatUtils.java | |
parent | 68038172793ee0e2ab3e2e56ddfbeb82879d1f58 (diff) | |
parent | 2ca4318cc1ee57dda907ba2069bd61d162b1baef (diff) |
Merge "Update Dialer source to latest internal Google revision."
Diffstat (limited to 'java/com/android/dialer/compat/CompatUtils.java')
-rw-r--r-- | java/com/android/dialer/compat/CompatUtils.java | 167 |
1 files changed, 15 insertions, 152 deletions
diff --git a/java/com/android/dialer/compat/CompatUtils.java b/java/com/android/dialer/compat/CompatUtils.java index 673cb709b..584f20549 100644 --- a/java/com/android/dialer/compat/CompatUtils.java +++ b/java/com/android/dialer/compat/CompatUtils.java @@ -15,16 +15,15 @@ */ package com.android.dialer.compat; +import android.content.Context; import android.os.Build; -import android.support.annotation.Nullable; -import android.text.TextUtils; -import android.util.Log; -import java.lang.reflect.InvocationTargetException; +import android.os.Build.VERSION; +import android.os.Build.VERSION_CODES; +import android.os.LocaleList; +import java.util.Locale; public final class CompatUtils { - private static final String TAG = CompatUtils.class.getSimpleName(); - /** PrioritizedMimeType is added in API level 23. */ public static boolean hasPrioritizedMimeType() { return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.M) >= Build.VERSION_CODES.M; @@ -71,152 +70,16 @@ public final class CompatUtils { return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP) >= Build.VERSION_CODES.M; } - /** - * Determines if this version is compatible with a default dialer. Can also force the version to - * be lower through {@link SdkVersionOverride}. - * - * @return {@code true} if default dialer is a feature on this device, {@code false} otherwise. - */ - public static boolean isDefaultDialerCompatible() { - return isMarshmallowCompatible(); - } - - /** - * Determines if this version is compatible with Lollipop Mr1-specific APIs. Can also force the - * version to be lower through SdkVersionOverride. - * - * @return {@code true} if runtime sdk is compatible with Lollipop MR1, {@code false} otherwise. - */ - public static boolean isLollipopMr1Compatible() { - return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP_MR1) - >= Build.VERSION_CODES.LOLLIPOP_MR1; - } - - /** - * Determines if this version is compatible with Marshmallow-specific APIs. Can also force the - * version to be lower through SdkVersionOverride. - * - * @return {@code true} if runtime sdk is compatible with Marshmallow, {@code false} otherwise. - */ - public static boolean isMarshmallowCompatible() { - return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP) >= Build.VERSION_CODES.M; - } - - /** - * Determines if the given class is available. Can be used to check if system apis exist at - * runtime. - * - * @param className the name of the class to look for. - * @return {@code true} if the given class is available, {@code false} otherwise or if className - * is empty. - */ - public static boolean isClassAvailable(@Nullable String className) { - if (TextUtils.isEmpty(className)) { - return false; + /** Returns locale of the device. */ + public static Locale getLocale(Context context) { + if (VERSION.SDK_INT >= VERSION_CODES.N) { + LocaleList localList = context.getResources().getConfiguration().getLocales(); + if (!localList.isEmpty()) { + return localList.get(0); + } + return Locale.getDefault(); + } else { + return context.getResources().getConfiguration().locale; } - try { - Class.forName(className); - return true; - } catch (ClassNotFoundException e) { - return false; - } catch (Throwable t) { - Log.e( - TAG, - "Unexpected exception when checking if class:" + className + " exists at " + "runtime", - t); - return false; - } - } - - /** - * Determines if the given class's method is available to call. Can be used to check if system - * apis exist at runtime. - * - * @param className the name of the class to look for - * @param methodName the name of the method to look for - * @param parameterTypes the needed parameter types for the method to look for - * @return {@code true} if the given class is available, {@code false} otherwise or if className - * or methodName are empty. - */ - public static boolean isMethodAvailable( - @Nullable String className, @Nullable String methodName, Class<?>... parameterTypes) { - if (TextUtils.isEmpty(className) || TextUtils.isEmpty(methodName)) { - return false; - } - - try { - Class.forName(className).getMethod(methodName, parameterTypes); - return true; - } catch (ClassNotFoundException | NoSuchMethodException e) { - Log.v(TAG, "Could not find method: " + className + "#" + methodName); - return false; - } catch (Throwable t) { - Log.e( - TAG, - "Unexpected exception when checking if method: " - + className - + "#" - + methodName - + " exists at runtime", - t); - return false; - } - } - - /** - * Invokes a given class's method using reflection. Can be used to call system apis that exist at - * runtime but not in the SDK. - * - * @param instance The instance of the class to invoke the method on. - * @param methodName The name of the method to invoke. - * @param parameterTypes The needed parameter types for the method. - * @param parameters The parameter values to pass into the method. - * @return The result of the invocation or {@code null} if instance or methodName are empty, or if - * the reflection fails. - */ - @Nullable - public static Object invokeMethod( - @Nullable Object instance, - @Nullable String methodName, - Class<?>[] parameterTypes, - Object[] parameters) { - if (instance == null || TextUtils.isEmpty(methodName)) { - return null; - } - - String className = instance.getClass().getName(); - try { - return Class.forName(className) - .getMethod(methodName, parameterTypes) - .invoke(instance, parameters); - } catch (ClassNotFoundException - | NoSuchMethodException - | IllegalArgumentException - | IllegalAccessException - | InvocationTargetException e) { - Log.v(TAG, "Could not invoke method: " + className + "#" + methodName); - return null; - } catch (Throwable t) { - Log.e( - TAG, - "Unexpected exception when invoking method: " - + className - + "#" - + methodName - + " at runtime", - t); - return null; - } - } - - /** - * Determines if this version is compatible with Lollipop-specific APIs. Can also force the - * version to be lower through SdkVersionOverride. - * - * @return {@code true} if call subject is a feature on this device, {@code false} otherwise. - */ - public static boolean isLollipopCompatible() { - return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP) - >= Build.VERSION_CODES.LOLLIPOP; } } |