diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2017-09-28 20:59:07 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-09-28 20:59:07 +0000 |
commit | ea2f430dd5f5f5f08bb5c9438696112f44423abe (patch) | |
tree | eb2b91eaa8fafe6041c27dae82a68888ba771a93 /java/com/android/dialer/strictmode/StrictModeUtils.java | |
parent | 126469d1020dbcb88146d595414faf9a0c4a1a28 (diff) | |
parent | 96aea2a8f42bfc55dd2b08c51698efb0d0559109 (diff) |
Merge changes I0ff396b5,I3b668c3a,Iab630f19,I6ef204ac
* changes:
Use goAsync() and dialer executor to insert post call notifications
Add the assisted dialing logic module.
Refactor DialerStrictMode into an interface.
Adding logging for transcription polling requests
Diffstat (limited to 'java/com/android/dialer/strictmode/StrictModeUtils.java')
-rw-r--r-- | java/com/android/dialer/strictmode/StrictModeUtils.java | 132 |
1 files changed, 52 insertions, 80 deletions
diff --git a/java/com/android/dialer/strictmode/StrictModeUtils.java b/java/com/android/dialer/strictmode/StrictModeUtils.java index 6944fd487..c83beb0b6 100644 --- a/java/com/android/dialer/strictmode/StrictModeUtils.java +++ b/java/com/android/dialer/strictmode/StrictModeUtils.java @@ -16,104 +16,76 @@ package com.android.dialer.strictmode; -import android.os.Build; +import android.os.Looper; import android.os.StrictMode; -import android.support.annotation.Nullable; -import com.android.dialer.common.Assert; -import com.google.auto.value.AutoValue; -import java.util.Map; -import java.util.Map.Entry; +import android.os.StrictMode.ThreadPolicy; +import android.support.annotation.AnyThread; +import com.android.dialer.buildtype.BuildType; +import com.android.dialer.function.Supplier; /** Utilities for enforcing strict-mode in an app. */ -final class StrictModeUtils { +public final class StrictModeUtils { - /** - * Set the recommended policy for the app. - * - * @param threadPenalties policy with preferred penalties. Detection bits will be ignored. - */ - static void setRecommendedMainThreadPolicy(StrictMode.ThreadPolicy threadPenalties) { - StrictMode.ThreadPolicy threadPolicy = - new StrictMode.ThreadPolicy.Builder(threadPenalties).detectAll().build(); - StrictMode.setThreadPolicy(threadPolicy); - } + private static final ThreadPolicy THREAD_NO_PENALTY = + new StrictMode.ThreadPolicy.Builder().permitAll().build(); /** - * Set the recommended policy for the app. + * Convenience method for disabling and enabling the thread policy death penalty using lambdas. + * + * <p>For example: + * + * <p><code> + * Value foo = StrictModeUtils.bypass(() -> doDiskAccessOnMainThreadReturningValue()); + * </code> * - * @param vmPenalties policy with preferred penalties. Detection bits should be unset. + * <p>The thread policy is only mutated if this is called from the main thread. */ - static void setRecommendedVMPolicy(StrictMode.VmPolicy vmPenalties) { - setRecommendedVMPolicy(vmPenalties, StrictModeVmConfig.empty()); + @AnyThread + public static <T> T bypass(Supplier<T> supplier) { + if (isStrictModeAllowed() && onMainThread()) { + ThreadPolicy originalPolicy = StrictMode.getThreadPolicy(); + StrictMode.setThreadPolicy(THREAD_NO_PENALTY); + try { + return supplier.get(); + } finally { + StrictMode.setThreadPolicy(originalPolicy); + } + } + return supplier.get(); } /** - * Set the recommended policy for the app. + * Convenience method for disabling and enabling the thread policy death penalty using lambdas. + * + * <p>For example: * - * @param vmPenalties policy with preferred penalties. Detection bits should be unset. + * <p><code> + * StrictModeUtils.bypass(() -> doDiskAccessOnMainThread()); + * </code> + * + * <p>The thread policy is only mutated if this is called from the main thread. */ - private static void setRecommendedVMPolicy( - StrictMode.VmPolicy vmPenalties, StrictModeVmConfig config) { - Assert.isNotNull(config); - StrictMode.VmPolicy.Builder vmPolicyBuilder = - new StrictMode.VmPolicy.Builder(vmPenalties) - .detectLeakedClosableObjects() - .detectLeakedSqlLiteObjects(); - if (Build.VERSION.SDK_INT >= 16) { - vmPolicyBuilder.detectLeakedRegistrationObjects(); - } - if (Build.VERSION.SDK_INT >= 18) { - vmPolicyBuilder.detectFileUriExposure(); - } - if (Build.VERSION.SDK_INT >= 21) { - // Even though this API is available earlier, it did not properly run finalizers. - // This avoids lots of false positives. - - // TODO(zachh): Use LeakCanary and remove this line. - vmPolicyBuilder.detectActivityLeaks(); - - if (config.maxInstanceLimits() != null) { - for (Entry<Class<?>, Integer> entry : config.maxInstanceLimits().entrySet()) { - vmPolicyBuilder.setClassInstanceLimit(entry.getKey(), entry.getValue()); - } + @AnyThread + public static void bypass(Runnable runnable) { + if (isStrictModeAllowed() && onMainThread()) { + ThreadPolicy originalPolicy = StrictMode.getThreadPolicy(); + StrictMode.setThreadPolicy(THREAD_NO_PENALTY); + try { + runnable.run(); + } finally { + StrictMode.setThreadPolicy(originalPolicy); } + } else { + runnable.run(); } - if (Build.VERSION.SDK_INT >= 23) { - // TODO(azlatin): Enable clear-text check once b/36730713 is fixed. - // vmPolicyBuilder.detectCleartextNetwork(); - } - // Once OC Lands: - // .detectContentUriWithoutPermission() - // .detectUntaggedSockets() - StrictMode.setVmPolicy(vmPolicyBuilder.build()); } - /** VmPolicy configuration. */ - @AutoValue - abstract static class StrictModeVmConfig { - /** A map of a class to the maximum number of allowed instances of that class. */ - @Nullable - abstract Map<Class<?>, Integer> maxInstanceLimits(); - - public static StrictModeVmConfig empty() { - return builder().build(); - } - - public static Builder builder() { - return new AutoValue_StrictModeUtils_StrictModeVmConfig.Builder(); - } - - /** VmPolicy configuration builder. */ - @AutoValue.Builder - public abstract static class Builder { - public abstract Builder setMaxInstanceLimits(Map<Class<?>, Integer> limits); - - public abstract StrictModeVmConfig build(); - - Builder() {} - } + public static boolean isStrictModeAllowed() { + return BuildType.get() == BuildType.BUGFOOD; + } - StrictModeVmConfig() {} + private static boolean onMainThread() { + return Looper.getMainLooper().equals(Looper.myLooper()); } private StrictModeUtils() {} |