From 08456ddf1363f67b444753d72a0812218b538712 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Wed, 20 Nov 2024 13:30:20 +0800 Subject: [PATCH] change: GetCurrentWindow, add compatibility with mResumedActivity --- hrp/internal/version/VERSION | 2 +- hrp/pkg/uixt/android_device.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/hrp/internal/version/VERSION b/hrp/internal/version/VERSION index d89860ea..c2e7b65d 100644 --- a/hrp/internal/version/VERSION +++ b/hrp/internal/version/VERSION @@ -1 +1 @@ -v5.0.0+2411201323 +v5.0.0+2411201330 diff --git a/hrp/pkg/uixt/android_device.go b/hrp/pkg/uixt/android_device.go index b3554f27..255a53f0 100644 --- a/hrp/pkg/uixt/android_device.go +++ b/hrp/pkg/uixt/android_device.go @@ -466,6 +466,7 @@ func (dev *AndroidDevice) installCommon(apkPath string, args ...string) error { } func (dev *AndroidDevice) GetCurrentWindow() (windowInfo WindowInfo, err error) { + // adb shell dumpsys window | grep -E 'mCurrentFocus|mFocusedApp' output, err := dev.d.RunShellCommand("dumpsys", "window", "|", "grep", "-E", "'mCurrentFocus|mFocusedApp'") if err != nil { return WindowInfo{}, errors.Wrap(err, "get current window failed") @@ -490,6 +491,23 @@ func (dev *AndroidDevice) GetCurrentWindow() (windowInfo WindowInfo, err error) } return windowInfo, nil } + + // adb shell dumpsys activity activities | grep mResumedActivity + output, err = dev.d.RunShellCommand("dumpsys", "activity", "activities", "|", "grep", "mResumedActivity") + if err != nil { + return WindowInfo{}, errors.Wrap(err, "get current activity failed") + } + // mResumedActivity: ActivityRecord{2db504f u0 com.miui.home/.launcher.Launcher t2} + reActivity := regexp.MustCompile(`mResumedActivity: ActivityRecord{.*? (\S+)/(\S+?)\s`) + matches = reActivity.FindStringSubmatch(output) + if len(matches) == 3 { + windowInfo = WindowInfo{ + PackageName: matches[1], + Activity: matches[2], + } + return windowInfo, nil + } + return WindowInfo{}, errors.New("failed to extract current window") }