From 75f3853b791acec3b75b682928dec3cc26924be7 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Thu, 19 Dec 2024 12:12:22 +0800 Subject: [PATCH] fix: unload android device screen --- hrp/internal/version/VERSION | 2 +- hrp/pkg/uixt/android_adb_driver.go | 32 ++++++++++++++---- hrp/pkg/uixt/android_key.go | 52 +++++++++++++++--------------- 3 files changed, 53 insertions(+), 33 deletions(-) diff --git a/hrp/internal/version/VERSION b/hrp/internal/version/VERSION index 3eb51a5b..f83ba795 100644 --- a/hrp/internal/version/VERSION +++ b/hrp/internal/version/VERSION @@ -1 +1 @@ -v5.0.0+2412182148 +v5.0.0+2412191212 diff --git a/hrp/pkg/uixt/android_adb_driver.go b/hrp/pkg/uixt/android_adb_driver.go index 45939eee..aa71d5fd 100644 --- a/hrp/pkg/uixt/android_adb_driver.go +++ b/hrp/pkg/uixt/android_adb_driver.go @@ -279,7 +279,18 @@ func (ad *adbDriver) Homescreen() (err error) { } func (ad *adbDriver) Unlock() (err error) { - return ad.PressKeyCodes(KCMenu, KMEmpty) + // Notice: brighten should be executed before unlock + // brighten android device screen + if err := ad.PressKeyCodes(KCWakeup, KMEmpty); err != nil { + log.Error().Err(err).Msg("brighten android device screen failed") + } + // unlock android device screen + if err := ad.PressKeyCodes(KCMenu, KMEmpty); err != nil { + log.Error().Err(err).Msg("press menu key to unlock screen failed") + } + + // swipe up to unlock + return ad.Swipe(500, 1500, 500, 500) } func (ad *adbDriver) Backspace(count int, options ...ActionOption) (err error) { @@ -287,12 +298,12 @@ func (ad *adbDriver) Backspace(count int, options ...ActionOption) (err error) { return nil } if count == 1 { - return ad.PressKeyCode(67) + return ad.PressKeyCode(KCDel) } keyArray := make([]KeyCode, count) for i := range keyArray { - keyArray[i] = KeyCode(67) + keyArray[i] = KCDel } return ad.combinationKey(keyArray) } @@ -315,9 +326,18 @@ func (ad *adbDriver) PressKeyCode(keyCode KeyCode) (err error) { } func (ad *adbDriver) PressKeyCodes(keyCode KeyCode, metaState KeyMeta) (err error) { - // adb shell input keyevent - _, err = ad.runShellCommand( - "input", "keyevent", fmt.Sprintf("%d", keyCode)) + // adb shell input keyevent [--longpress] KEYCODE [METASTATE] + if metaState != KMEmpty { + // press key with metastate, e.g. KMShiftOn/KMCtrlOn + _, err = ad.runShellCommand( + "input", "keyevent", "--longpress", + fmt.Sprintf("%d", keyCode), + fmt.Sprintf("%d", metaState)) + } else { + _, err = ad.runShellCommand( + "input", "keyevent", + fmt.Sprintf("%d", keyCode)) + } return } diff --git a/hrp/pkg/uixt/android_key.go b/hrp/pkg/uixt/android_key.go index 6304de9d..5761652a 100644 --- a/hrp/pkg/uixt/android_key.go +++ b/hrp/pkg/uixt/android_key.go @@ -5,32 +5,32 @@ package uixt type KeyMeta int const ( - KMEmpty KeyMeta = 0 // As a `null` - KMCapLocked KeyMeta = 0x100 // SHIFT key locked in CAPS mode. - KMAltLocked KeyMeta = 0x200 // ALT key locked. - KMSymLocked KeyMeta = 0x400 // SYM key locked. - KMSelecting KeyMeta = 0x800 // Text is in selection mode. - // KMAltOn KeyMeta = 0x02 // This mask is used to check whether one of the ALT meta keys is pressed. - // KMAltLeftOn KeyMeta = 0x10 // This mask is used to check whether the left ALT meta key is pressed. - // KMAltRightOn KeyMeta = 0x20 // This mask is used to check whether the right the ALT meta key is pressed. - // KMShiftOn KeyMeta = 0x1 // This mask is used to check whether one of the SHIFT meta keys is pressed. - // KMShiftLeftOn KeyMeta = 0x40 // This mask is used to check whether the left SHIFT meta key is pressed. - // KMShiftRightOn KeyMeta = 0x80 // This mask is used to check whether the right SHIFT meta key is pressed. - // KMSymOn KeyMeta = 0x4 // This mask is used to check whether the SYM meta key is pressed. - // KMFunctionOn KeyMeta = 0x8 // This mask is used to check whether the FUNCTION meta key is pressed. - // KMCtrlOn KeyMeta = 0x1000 // This mask is used to check whether one of the CTRL meta keys is pressed. - // KMCtrlLeftOn KeyMeta = 0x2000 // This mask is used to check whether the left CTRL meta key is pressed. - // KMCtrlRightOn KeyMeta = 0x4000 // This mask is used to check whether the right CTRL meta key is pressed. - // KMMetaOn KeyMeta = 0x10000 // This mask is used to check whether one of the META meta keys is pressed. - // KMMetaLeftOn KeyMeta = 0x20000 // This mask is used to check whether the left META meta key is pressed. - // KMMetaRightOn KeyMeta = 0x40000 // This mask is used to check whether the right META meta key is pressed. - // KMCapsLockOn KeyMeta = 0x100000 // This mask is used to check whether the CAPS LOCK meta key is on. - // KMNumLockOn KeyMeta = 0x200000 // This mask is used to check whether the NUM LOCK meta key is on. - // KMScrollLockOn KeyMeta = 0x400000 // This mask is used to check whether the SCROLL LOCK meta key is on. - // KMShiftMask = KMShiftOn | KMShiftLeftOn | KMShiftRightOn - // KMAltMask = KMAltOn | KMAltLeftOn | KMAltRightOn - // KMCtrlMask = KMCtrlOn | KMCtrlLeftOn | KMCtrlRightOn - // KMMetaMask = KMMetaOn | KMMetaLeftOn | KMMetaRightOn + KMEmpty KeyMeta = 0 // As a `null` + KMCapLocked KeyMeta = 0x100 // SHIFT key locked in CAPS mode. + KMAltLocked KeyMeta = 0x200 // ALT key locked. + KMSymLocked KeyMeta = 0x400 // SYM key locked. + KMSelecting KeyMeta = 0x800 // Text is in selection mode. + KMAltOn KeyMeta = 0x02 // This mask is used to check whether one of the ALT meta keys is pressed. + KMAltLeftOn KeyMeta = 0x10 // This mask is used to check whether the left ALT meta key is pressed. + KMAltRightOn KeyMeta = 0x20 // This mask is used to check whether the right the ALT meta key is pressed. + KMShiftOn KeyMeta = 0x1 // This mask is used to check whether one of the SHIFT meta keys is pressed. + KMShiftLeftOn KeyMeta = 0x40 // This mask is used to check whether the left SHIFT meta key is pressed. + KMShiftRightOn KeyMeta = 0x80 // This mask is used to check whether the right SHIFT meta key is pressed. + KMSymOn KeyMeta = 0x4 // This mask is used to check whether the SYM meta key is pressed. + KMFunctionOn KeyMeta = 0x8 // This mask is used to check whether the FUNCTION meta key is pressed. + KMCtrlOn KeyMeta = 0x1000 // This mask is used to check whether one of the CTRL meta keys is pressed. + KMCtrlLeftOn KeyMeta = 0x2000 // This mask is used to check whether the left CTRL meta key is pressed. + KMCtrlRightOn KeyMeta = 0x4000 // This mask is used to check whether the right CTRL meta key is pressed. + KMMetaOn KeyMeta = 0x10000 // This mask is used to check whether one of the META meta keys is pressed. + KMMetaLeftOn KeyMeta = 0x20000 // This mask is used to check whether the left META meta key is pressed. + KMMetaRightOn KeyMeta = 0x40000 // This mask is used to check whether the right META meta key is pressed. + KMCapsLockOn KeyMeta = 0x100000 // This mask is used to check whether the CAPS LOCK meta key is on. + KMNumLockOn KeyMeta = 0x200000 // This mask is used to check whether the NUM LOCK meta key is on. + KMScrollLockOn KeyMeta = 0x400000 // This mask is used to check whether the SCROLL LOCK meta key is on. + KMShiftMask = KMShiftOn | KMShiftLeftOn | KMShiftRightOn + KMAltMask = KMAltOn | KMAltLeftOn | KMAltRightOn + KMCtrlMask = KMCtrlOn | KMCtrlLeftOn | KMCtrlRightOn + KMMetaMask = KMMetaOn | KMMetaLeftOn | KMMetaRightOn ) type KeyFlag int