change: GetCurrentWindow, add compatibility with mFocusedApp

This commit is contained in:
lilong.129
2024-11-20 13:23:12 +08:00
parent 7d6b5d2405
commit 3e7f5d5408
2 changed files with 20 additions and 10 deletions

View File

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

View File

@@ -466,21 +466,31 @@ func (dev *AndroidDevice) installCommon(apkPath string, args ...string) error {
}
func (dev *AndroidDevice) GetCurrentWindow() (windowInfo WindowInfo, err error) {
output, err := dev.d.RunShellCommand("dumpsys", "window", "|", "grep", "mCurrentFocus")
output, err := dev.d.RunShellCommand("dumpsys", "window", "|", "grep", "-E", "'mCurrentFocus|mFocusedApp'")
if err != nil {
return WindowInfo{}, errors.Wrap(err, "get current window failed")
}
// mCurrentFocus=Window{a33bc55 u0 com.miui.home/com.miui.home.launcher.Launcher}
re := regexp.MustCompile(`mCurrentFocus=Window{.*? (\S+)/(\S+)}`)
matches := re.FindStringSubmatch(output)
if len(matches) != 3 {
return WindowInfo{}, errors.New("failed to extract current window")
reFocus := regexp.MustCompile(`mCurrentFocus=Window{.*? (\S+)/(\S+)}`)
matches := reFocus.FindStringSubmatch(output)
if len(matches) == 3 {
windowInfo = WindowInfo{
PackageName: matches[1],
Activity: matches[2],
}
return windowInfo, nil
}
windowInfo = WindowInfo{
PackageName: matches[1],
Activity: matches[2],
// mFocusedApp=ActivityRecord{2db504f u0 com.miui.home/.launcher.Launcher t2}
reApp := regexp.MustCompile(`mFocusedApp=ActivityRecord{.*? (\S+)/(\S+?)\s`)
matches = reApp.FindStringSubmatch(output)
if len(matches) == 3 {
windowInfo = WindowInfo{
PackageName: matches[1],
Activity: matches[2],
}
return windowInfo, nil
}
return windowInfo, nil
return WindowInfo{}, errors.New("failed to extract current window")
}
func (dev *AndroidDevice) GetPackageInfo(packageName string) (AppInfo, error) {