change: GetCurrentWindow, add compatibility with mResumedActivity

This commit is contained in:
lilong.129
2024-11-20 13:30:20 +08:00
parent 3e7f5d5408
commit 08456ddf13
2 changed files with 19 additions and 1 deletions

View File

@@ -1 +1 @@
v5.0.0+2411201323
v5.0.0+2411201330

View File

@@ -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")
}