Created
January 23, 2018 11:44
-
-
Save hvisser/e716105f4e3cf2908ea463dbdb50679c to your computer and use it in GitHub Desktop.
Revisions
-
hvisser created this gist
Jan 23, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() } } }