From ba6d8ea3214cee0134797f11c26922f970b80807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BA=91=E9=BE=99?= <76432572+nankingjing@users.noreply.github.com> Date: Sat, 11 Jul 2026 18:39:54 +0800 Subject: [PATCH] fix: use regex exact match in get_current_app to prevent package substring misidentification --- phone_agent/adb/device.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/phone_agent/adb/device.py b/phone_agent/adb/device.py index 995336a16..8a078f0fb 100644 --- a/phone_agent/adb/device.py +++ b/phone_agent/adb/device.py @@ -1,6 +1,7 @@ """Device control utilities for Android automation.""" import os +import re import subprocess import time from typing import List, Optional, Tuple @@ -31,9 +32,22 @@ def get_current_app(device_id: str | None = None) -> str: # Parse window focus info for line in output.split("\n"): if "mCurrentFocus" in line or "mFocusedApp" in line: - for app_name, package in APP_PACKAGES.items(): - if package in line: - return app_name + # Extract the package name from the window focus line. + # Format: Window{... u0 com.example.app/com.example.MainActivity} + # Using regex avoids false substring matches (e.g., + # "com.google.android.apps.docs" incorrectly matching + # "com.google.android.apps.docs.editors.docs"). + match = re.search(r"\s(\w+)\s+([\w.]+)/", line) + if match: + pkg = match.group(2) + for app_name, package in APP_PACKAGES.items(): + if package == pkg: + return app_name + else: + # Fallback for unusual dumpsys formats + for app_name, package in APP_PACKAGES.items(): + if package in line: + return app_name return "System Home"