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

Commit 6f2720a8 authored by Jeff DeCew's avatar Jeff DeCew
Browse files

Allow engineers to prevent refactor flags from crashing systemui

Bug: 308041333
Test: trigger flag misuse with and without 'adb shell setprop log.tag.RefactorFlagAssert silent'
Flag: NA
Change-Id: Ib613bc1ebf6bbbae47b653cd8043b2c6d5316d88
parent 95535358
Loading
Loading
Loading
Loading
+35 −2
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package com.android.systemui.flags
package com.android.systemui.flags


import android.os.Build
import android.util.Log
import android.util.Log


/**
/**
@@ -32,6 +33,11 @@ import android.util.Log
 *         RefactorFlagUtils.assertInLegacyMode(isEnabled, FLAG_NAME)
 *         RefactorFlagUtils.assertInLegacyMode(isEnabled, FLAG_NAME)
 * }
 * }
 * ```
 * ```
 *
 * Legacy mode crashes can be disabled with the command:
 * ```
 * adb shell setprop log.tag.RefactorFlagAssert silent
 * ```
 */
 */
@Suppress("NOTHING_TO_INLINE")
@Suppress("NOTHING_TO_INLINE")
object RefactorFlagUtils {
object RefactorFlagUtils {
@@ -51,8 +57,7 @@ object RefactorFlagUtils {
    inline fun isUnexpectedlyInLegacyMode(isEnabled: Boolean, flagName: Any): Boolean {
    inline fun isUnexpectedlyInLegacyMode(isEnabled: Boolean, flagName: Any): Boolean {
        val inLegacyMode = !isEnabled
        val inLegacyMode = !isEnabled
        if (inLegacyMode) {
        if (inLegacyMode) {
            val message = "New code path expects $flagName to be enabled."
            assertOnEngBuild("New code path expects $flagName to be enabled.")
            Log.wtf("RefactorFlag", message, IllegalStateException(message))
        }
        }
        return inLegacyMode
        return inLegacyMode
    }
    }
@@ -71,4 +76,32 @@ object RefactorFlagUtils {
     */
     */
    inline fun assertInLegacyMode(isEnabled: Boolean, flagName: Any) =
    inline fun assertInLegacyMode(isEnabled: Boolean, flagName: Any) =
        check(!isEnabled) { "Legacy code path not supported when $flagName is enabled." }
        check(!isEnabled) { "Legacy code path not supported when $flagName is enabled." }

    /**
     * This will [Log.wtf] with the given message, assuming [ASSERT_TAG] is loggable at that level.
     * This means an engineer can prevent this from crashing by running the command:
     * ```
     * adb shell setprop log.tag.RefactorFlagAssert silent
     * ```
     */
    fun assertOnEngBuild(message: String) {
        if (Log.isLoggable(ASSERT_TAG, Log.ASSERT)) {
            val exception = if (Build.isDebuggable()) IllegalStateException(message) else null
            Log.wtf(ASSERT_TAG, message, exception)
        } else if (Log.isLoggable(STANDARD_TAG, Log.WARN)) {
            Log.w(STANDARD_TAG, message)
        }
    }

    /**
     * Tag used to determine if an incorrect flag guard should crash System UI running an eng build.
     * This is enabled by default. To disable, run:
     * ```
     * adb shell setprop log.tag.RefactorFlagAssert silent
     * ```
     */
    private const val ASSERT_TAG = "RefactorFlagAssert"

    /** Tag used for non-crashing logs or when the [ASSERT_TAG] has been silenced. */
    private const val STANDARD_TAG = "RefactorFlag"
}
}