|
| 1 | +const { device } = require('detox'); |
| 2 | + |
| 3 | +const initCache = require('./DeviceInitCache'); |
| 4 | +const sleep = require('../../utils/sleep'); |
| 5 | +const log = { |
| 6 | + info: (...args) => console.log('[AndroidUIInit]', ...args), |
| 7 | +} |
| 8 | +const adbWrapper = () => { |
| 9 | + const adbName = device.id; |
| 10 | + const { adb } = device.deviceDriver; |
| 11 | + |
| 12 | + const shell = async (cmd) => { |
| 13 | + await adb.shell(adbName, cmd); |
| 14 | + await sleep(200); |
| 15 | + }; |
| 16 | + |
| 17 | + return { |
| 18 | + name: adbName, |
| 19 | + shell, |
| 20 | + }; |
| 21 | +}; |
| 22 | + |
| 23 | +class AndroidUIInitHelper { |
| 24 | + subscribe({ testEvents }) { |
| 25 | + testEvents.on('setup', this._handleSetupEvent.bind(this)); |
| 26 | + } |
| 27 | + |
| 28 | + async _handleSetupEvent() { |
| 29 | + if (device.getPlatform() !== 'android') { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + const adb = adbWrapper(); |
| 35 | + |
| 36 | + if (initCache.isInitialized(adb.name)) { |
| 37 | + log.info(`Skipping setup for ${adb.name} (already initialized by this worker)`); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + log.info(`Running init for ${adb.name}`); |
| 42 | + |
| 43 | + await this._setupKeyboardBehavior(adb); |
| 44 | + await this._setupPointerIndicators(adb); |
| 45 | + await this._setupNavigationMode(adb); |
| 46 | + await this._setupStatusBar(adb); |
| 47 | + |
| 48 | + initCache.markInitialized(adb.name); |
| 49 | + log.info(`Finished init for ${adb.name}`); |
| 50 | + } |
| 51 | + |
| 52 | + async _setupKeyboardBehavior(adb) { |
| 53 | + // Force-hide the on-screen keyboard |
| 54 | + await adb.shell('settings put Secure show_ime_with_hard_keyboard 0'); |
| 55 | + } |
| 56 | + |
| 57 | + async _setupPointerIndicators(adb) { |
| 58 | + await adb.shell('settings put system show_touches 1'); |
| 59 | + await adb.shell('settings put system pointer_location 1'); |
| 60 | + } |
| 61 | + |
| 62 | + async _setupNavigationMode(adb) { |
| 63 | + await adb.shell('cmd overlay enable com.android.internal.systemui.navbar.threebutton'); |
| 64 | + } |
| 65 | + |
| 66 | + // Ref: https://android.googlesource.com/platform/frameworks/base/+/master/packages/SystemUI/docs/demo_mode.md |
| 67 | + async _setupStatusBar(adb) { |
| 68 | + // Enable, then get out (= reset status-bar) and back into demo mode |
| 69 | + await adb.shell('settings put global sysui_demo_allowed 1'); |
| 70 | + await adb.shell('am broadcast -a com.android.systemui.demo -e command exit'); |
| 71 | + await adb.shell('am broadcast -a com.android.systemui.demo -e command enter'); |
| 72 | + |
| 73 | + // Force status bar content |
| 74 | + await adb.shell('am broadcast -a com.android.systemui.demo -e command notifications -e visible false'); |
| 75 | + await adb.shell('am broadcast -a com.android.systemui.demo -e command network -e wifi hide'); |
| 76 | + await adb.shell('am broadcast -a com.android.systemui.demo -e command network -e wifi show -e level 4 -e fully true'); |
| 77 | + await adb.shell('am broadcast -a com.android.systemui.demo -e command network -e mobile hide'); |
| 78 | + // Best to keep this last due to a "charging" indicator animation which can |
| 79 | + // break UI changes made by consequent commands |
| 80 | + await adb.shell('am broadcast -a com.android.systemui.demo -e command battery -e level 100 -e plugged true'); |
| 81 | + await sleep(1500); // Wait for the animation to finish |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +module.exports = new AndroidUIInitHelper(); |
0 commit comments