diff options
| author | 2010-12-10 14:19:58 -0800 | |
|---|---|---|
| committer | 2010-12-10 14:19:58 -0800 | |
| commit | 4decffa46d3ca17df2c5bbb8ba729c2dba4f6a79 (patch) | |
| tree | 328a14d4cbe286a2bc96c69cf661aee1d67e39ef | |
| parent | 89d7787454c181d6c501a38fee694f32c4a74c31 (diff) | |
| parent | e36f9bf123c7cd07ce1007a16de564b2840ea1fe (diff) | |
Merge "StrictMode "custom" trigger support."
| -rw-r--r-- | api/current.xml | 35 | ||||
| -rw-r--r-- | core/java/android/os/StrictMode.java | 82 |
2 files changed, 111 insertions, 6 deletions
diff --git a/api/current.xml b/api/current.xml index c6fac03ee5d2..fb6b950517cf 100644 --- a/api/current.xml +++ b/api/current.xml @@ -144604,6 +144604,19 @@ visibility="public" > </method> +<method name="noteSlowCall" + return="void" + abstract="false" + native="false" + synchronized="false" + static="true" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="name" type="java.lang.String"> +</parameter> +</method> <method name="setThreadPolicy" return="void" abstract="false" @@ -144698,6 +144711,17 @@ visibility="public" > </method> +<method name="detectCustomSlowCalls" + return="android.os.StrictMode.ThreadPolicy.Builder" + abstract="false" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +</method> <method name="detectDiskReads" return="android.os.StrictMode.ThreadPolicy.Builder" abstract="false" @@ -144808,6 +144832,17 @@ visibility="public" > </method> +<method name="permitCustomSlowCalls" + return="android.os.StrictMode.ThreadPolicy.Builder" + abstract="false" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +</method> <method name="permitDiskReads" return="android.os.StrictMode.ThreadPolicy.Builder" abstract="false" diff --git a/core/java/android/os/StrictMode.java b/core/java/android/os/StrictMode.java index 641493634755..ba97dcf2e9f6 100644 --- a/core/java/android/os/StrictMode.java +++ b/core/java/android/os/StrictMode.java @@ -150,6 +150,16 @@ public final class StrictMode { */ public static final int DETECT_NETWORK = 0x04; // for ThreadPolicy + /** + * For StrictMode.noteSlowCall() + * + * @hide + */ + public static final int DETECT_CUSTOM = 0x08; // for ThreadPolicy + + private static final int ALL_THREAD_DETECT_BITS = + DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK | DETECT_CUSTOM; + // Process-policy: /** @@ -309,14 +319,14 @@ public final class StrictMode { * disk operations but will likely expand in future releases. */ public Builder detectAll() { - return enable(DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK); + return enable(ALL_THREAD_DETECT_BITS); } /** * Disable the detection of everything. */ public Builder permitAll() { - return disable(DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK); + return disable(ALL_THREAD_DETECT_BITS); } /** @@ -348,6 +358,20 @@ public final class StrictMode { } /** + * Enable detection of disk reads. + */ + public Builder detectCustomSlowCalls() { + return enable(DETECT_CUSTOM); + } + + /** + * Enable detection of disk reads. + */ + public Builder permitCustomSlowCalls() { + return enable(DETECT_CUSTOM); + } + + /** * Enable detection of disk writes. */ public Builder detectDiskWrites() { @@ -663,6 +687,12 @@ public final class StrictMode { } } + private static class StrictModeCustomViolation extends BlockGuard.BlockGuardPolicyException { + public StrictModeCustomViolation(int policyMask, String name) { + super(policyMask, DETECT_CUSTOM, name); + } + } + /** * Returns the bitmask of the current thread's policy. * @@ -678,6 +708,10 @@ public final class StrictMode { * Returns the current thread's policy. */ public static ThreadPolicy getThreadPolicy() { + // TODO: this was a last minute Gingerbread API change (to + // introduce VmPolicy cleanly) but this isn't particularly + // optimal for users who might call this method often. This + // should be in a thread-local and not allocate on each call. return new ThreadPolicy(getThreadPolicyMask()); } @@ -799,7 +833,9 @@ public final class StrictMode { * getMessage() String value. Kinda gross, but least * invasive. :/ * - * Input is of form "policy=137 violation=64" + * Input is of the following forms: + * "policy=137 violation=64" + * "policy=137 violation=64 msg=Arbitrary text" * * Returns 0 on failure, which is a valid policy, but not a * valid policy during a violation (else there must've been @@ -832,7 +868,12 @@ public final class StrictMode { if (violationIndex == -1) { return 0; } - String violationString = message.substring(violationIndex + 10); + int numberStartIndex = violationIndex + "violation=".length(); + int numberEndIndex = message.indexOf(' ', numberStartIndex); + if (numberEndIndex == -1) { + numberEndIndex = message.length(); + } + String violationString = message.substring(numberStartIndex, numberEndIndex); try { return Integer.valueOf(violationString).intValue(); } catch (NumberFormatException e) { @@ -893,6 +934,19 @@ public final class StrictMode { startHandlingViolationException(e); } + // Not part of BlockGuard.Policy; just part of StrictMode: + void onCustomSlowCall(String name) { + if ((mPolicyMask & DETECT_CUSTOM) == 0) { + return; + } + if (tooManyViolationsThisLoop()) { + return; + } + BlockGuard.BlockGuardPolicyException e = new StrictModeCustomViolation(mPolicyMask, name); + e.fillInStackTrace(); + startHandlingViolationException(e); + } + // Part of BlockGuard.Policy interface: public void onReadFromDisk() { if ((mPolicyMask & DETECT_DISK_READ) == 0) { @@ -1229,8 +1283,7 @@ public final class StrictMode { .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() - .detectLeakedSqlLiteObjects() - .detectLeakedClosableObjects() + .detectAll() .penaltyLog() .build()); } @@ -1525,6 +1578,23 @@ public final class StrictMode { return span; } + /** + * For code to note that it's slow. This is a no-op unless the + * current thread's {@link android.os.StrictMode.ThreadPolicy} has + * {@link android.os.StrictMode.ThreadPolicy.Builder#detectCustomSlowCalls} + * enabled. + * + * @param name a short string for the exception stack trace that's + * built if when this fires. + */ + public static void noteSlowCall(String name) { + BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); + if (!(policy instanceof AndroidBlockGuardPolicy)) { + // StrictMode not enabled. + return; + } + ((AndroidBlockGuardPolicy) policy).onCustomSlowCall(name); + } /** * Parcelable that gets sent in Binder call headers back to callers |