Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c985ffd5 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

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
parent 834f257f
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -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);
        }