From 6c14d7739ac2b177035aa084060aaccdaa7a8ba7 Mon Sep 17 00:00:00 2001 From: zachh Date: Wed, 9 Aug 2017 17:00:19 -0700 Subject: 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 --- .../dialer/strictmode/DialerStrictMode.java | 27 +++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'java/com/android/dialer') 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. - * - *

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 { * *

You should typically do this only temporarily and restore the death penalty in a finally * block using {@link #enableDeathPenalty()}. + * + *

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()}. + * + *

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 get(); @@ -81,8 +90,10 @@ public final class DialerStrictMode { *

For example: * *

- * DialerStrictMode.bypass(() -> doDiskAccessOnMainThread()); + * Value foo = DialerStrictMode.bypass(() -> doDiskAccessOnMainThreadReturningValue()); * + * + *

The thread policy is only mutated if this is called from the main thread. */ public static T bypass(Provider provider) { disableDeathPenalty(); -- cgit v1.2.3