diff options
author | zachh <zachh@google.com> | 2017-08-09 17:00:19 -0700 |
---|---|---|
committer | Eric Erfanian <erfanian@google.com> | 2017-08-09 17:37:14 -0700 |
commit | 6c14d7739ac2b177035aa084060aaccdaa7a8ba7 (patch) | |
tree | 326e73199fe32125a7bcbbcdda4a64dc2a7c45a9 /java/com/android | |
parent | 4e0d3bcc3b9e962ee5b8cb9614a9a545d50b8882 (diff) |
Some improvements to DialerStrictMode.
Only mutate the thread policy when on the main thread to prevent accidentally changing the policy for a non-main thread. The alternative here is Assert.isMainThread(), but that would make DialerStrictMode more difficult to use.
Also switched one use of #disableDeathPenalty to the more concise #bypass.
Bug: 64118795
Test: verified bugfood build does not crash on launch
PiperOrigin-RevId: 164794473
Change-Id: I58db414cbb003241a1afcddabfd6557612351598
Diffstat (limited to 'java/com/android')
-rw-r--r-- | java/com/android/dialer/strictmode/DialerStrictMode.java | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/java/com/android/dialer/strictmode/DialerStrictMode.java b/java/com/android/dialer/strictmode/DialerStrictMode.java index c9bbeafbf..3c31aa954 100644 --- a/java/com/android/dialer/strictmode/DialerStrictMode.java +++ b/java/com/android/dialer/strictmode/DialerStrictMode.java @@ -16,16 +16,13 @@ package com.android.dialer.strictmode; +import android.os.Looper; import android.os.StrictMode; import android.os.StrictMode.ThreadPolicy; import android.os.StrictMode.VmPolicy; import com.android.dialer.buildtype.BuildType; -/** - * Enables strict mode for the application, and provides means of temporarily disabling it. - * - * <p>NOTE: All methods in this class are stripped by proguard in release builds. - */ +/** Enables strict mode for the application, and provides means of temporarily disabling it. */ public final class DialerStrictMode { /** Initializes strict mode on application start. */ @@ -39,10 +36,14 @@ public final class DialerStrictMode { * * <p>You should typically do this only temporarily and restore the death penalty in a finally * block using {@link #enableDeathPenalty()}. + * + * <p>The thread policy is only mutated if this is called from the main thread. */ public static void disableDeathPenalty() { if (isStrictModeAllowed()) { - StrictMode.setThreadPolicy(threadPolicyTemplate().build()); + if (onMainThread()) { + StrictMode.setThreadPolicy(threadPolicyTemplate().build()); + } StrictMode.setVmPolicy(vmPolicyTemplate().build()); } } @@ -50,10 +51,14 @@ public final class DialerStrictMode { /** * Restore the death penalty. This should typically be called in a finally block after calling * {@link #disableDeathPenalty()}. + * + * <p>The thread policy is only mutated if this is called from the main thread. */ public static void enableDeathPenalty() { if (isStrictModeAllowed()) { - StrictMode.setThreadPolicy(threadPolicyTemplate().penaltyDeath().build()); + if (onMainThread()) { + StrictMode.setThreadPolicy(threadPolicyTemplate().penaltyDeath().build()); + } StrictMode.setVmPolicy(vmPolicyTemplate().penaltyDeath().build()); } } @@ -70,6 +75,10 @@ public final class DialerStrictMode { return BuildType.get() == BuildType.BUGFOOD; } + private static boolean onMainThread() { + return Looper.getMainLooper().equals(Looper.myLooper()); + } + /** Functional interface intended to be used with {@link #bypass(Provider)}. */ public interface Provider<T> { T get(); @@ -81,8 +90,10 @@ public final class DialerStrictMode { * <p>For example: * * <p><code> - * DialerStrictMode.bypass(() -> doDiskAccessOnMainThread()); + * Value foo = DialerStrictMode.bypass(() -> doDiskAccessOnMainThreadReturningValue()); * </code> + * + * <p>The thread policy is only mutated if this is called from the main thread. */ public static <T> T bypass(Provider<T> provider) { disableDeathPenalty(); |