Offer listener to observe StrictMode violations.

Primarily used by tests to be more robust, since reading raw logcat
data recently became very flaky.

Bug: 37915178
Test: cts-tradefed run commandAndExit cts-dev -m CtsOsTestCases -t android.os.cts.StrictModeTest
Change-Id: I3f12508bb6206c53005356b5d8d9ba57aac2436e
This commit is contained in:
Jeff Sharkey
2017-05-24 12:45:40 -06:00
parent 834f257f22
commit c985ffd5e9

View File

@@ -342,6 +342,18 @@ public final class StrictMode {
private static volatile int sVmPolicyMask = 0;
private static volatile VmPolicy sVmPolicy = VmPolicy.LAX;
/** {@hide} */
public interface ViolationListener {
public void onViolation(String message);
}
private static volatile ViolationListener sListener;
/** {@hide} */
public static void setViolationListener(ViolationListener listener) {
sListener = listener;
}
/**
* The number of threads trying to do an async dropbox write.
* Just to limit ourselves out of paranoia.
@@ -1581,6 +1593,9 @@ public final class StrictMode {
long timeSinceLastViolationMillis = lastViolationTime == 0 ?
Long.MAX_VALUE : (now - lastViolationTime);
if ((info.policy & PENALTY_LOG) != 0 && sListener != null) {
sListener.onViolation(info.crashInfo.stackTrace);
}
if ((info.policy & PENALTY_LOG) != 0 &&
timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
if (info.durationMillis != -1) {
@@ -2024,6 +2039,9 @@ public final class StrictMode {
}
}
if (penaltyLog && sListener != null) {
sListener.onViolation(originStack.toString());
}
if (penaltyLog && timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
Log.e(TAG, message, originStack);
}