Skip to content

Instantly share code, notes, and snippets.

@hvisser
Created January 23, 2018 11:44
Show Gist options
  • Select an option

  • Save hvisser/e716105f4e3cf2908ea463dbdb50679c to your computer and use it in GitHub Desktop.

Select an option

Save hvisser/e716105f4e3cf2908ea463dbdb50679c to your computer and use it in GitHub Desktop.

Revisions

  1. hvisser created this gist Jan 23, 2018.
    35 changes: 35 additions & 0 deletions DemoModeEnabler.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    class DemoModeEnabler {

    fun enable() {
    executeShellCommand("settings put global sysui_demo_allowed 1")
    sendCommand("exit")
    sendCommand("enter")
    sendCommand("notifications", "visible" to "false")
    sendCommand("network", "wifi" to "hide")
    sendCommand("battery", "level" to "100", "plugged" to "false")
    sendCommand("clock", "hhmm" to "1000")
    }

    fun disable() {
    sendCommand("exit")
    }

    private fun sendCommand(command: String, vararg extras: Pair<String, Any>) {
    val exec = StringBuilder("am broadcast -a com.android.systemui.demo -e command $command")
    for ((key, value) in extras) {
    exec.append(" -e $key $value")
    }
    executeShellCommand(exec.toString())
    }

    private fun executeShellCommand(command: String) {
    waitForCompletion(InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand(command))
    }

    private fun waitForCompletion(descriptor: ParcelFileDescriptor) {
    val reader = BufferedReader(InputStreamReader(ParcelFileDescriptor.AutoCloseInputStream(descriptor)))
    reader.use {
    it.readText()
    }
    }
    }
    16 changes: 16 additions & 0 deletions DemoModeRule.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    class DemoModeRule: TestRule {

    private val helper = DemoModeEnabler()

    override fun apply(base: Statement, description: Description): Statement {
    return DemoModeStatement(base)
    }

    private inner class DemoModeStatement(private val base: Statement) : Statement() {
    override fun evaluate() {
    helper.enable()
    base.evaluate()
    helper.disable()
    }
    }
    }