Skip to content

fix: resolve app_current() from visible tasks instead of window focus - #1207

Open
codeskyblue-agent wants to merge 1 commit into
masterfrom
agent/issue-1085-jdnfdd
Open

codeskyblue-agent wants to merge 1 commit into
masterfrom
agent/issue-1085-jdnfdd

Conversation

@codeskyblue-agent

Copy link
Copy Markdown
Collaborator

This is by an automated agent, not a human.

变更

app_current() previously delegated to adbutils, which reads window focus first and, on Android 13+, falls back to the last ACTIVITY in dumpsys activity top; floating windows, secondary displays and still-alive background apps could therefore be reported as current. It now parses dumpsys activity activities, prefers the primary display, skips tasks marked visible=false, reads topResumedActivity/mResumedActivity from the first visible task and extracts the matching app ProcessRecord pid. The adbutils path remains as a fallback for keyguard, screen-off or unusual vendor output, so DeviceError semantics are unchanged. app_wait(front=True) and wait_activity() benefit without API changes.

改动

  • uiautomator2/init.py: added a dumpsys activity activities parser for visible tasks (display, visibility, resumed activity and pid) and used it in _AppMixIn.app_current() before the existing adbutils fallback
  • tests/test_app_current.py: added unit tests with real dumpsys shapes for Android 10 and 14, floating/background tasks, secondary displays, hidden-only output, unparseable output and the DeviceError path

Closes #1085

提交

560fce8 fix: resolve app_current from visible tasks instead of window focus

检查

✅ 检查通过(2 项)

  • ✅ app_current tests
  • ✅ python tests

功能验证

  • python3.9 -m pytest tests/test_app_current.py -q — 10 passed
  • python3.9 -m pytest tests/ -q — 226 passed

UI 验证

未涉及 UI 改动,无截图。

tests/test_app_current.py | 174 ++++++++++++++++++++++++++++++++++++++++++++++
 uiautomator2/__init__.py  |  92 ++++++++++++++++++++++++
 2 files changed, 266 insertions(+)

app_current() delegated to adbutils, which reads mCurrentFocus from
dumpsys window windows first. Floating windows, dialogs and secondary
displays can hold that focus; on Android 13+ the fallback to
dumpsys activity top returns the last ACTIVITY, which is often a
background app (WhatsApp/Telegram) that is still alive.

Parse dumpsys activity activities instead: take the primary display,
skip tasks that are explicitly not visible, read topResumedActivity/
mResumedActivity from the first visible task and its app ProcessRecord
for the pid. Keep the adbutils call as a fallback for keyguard, screen
off or unusual vendor output so DeviceError semantics do not change.
@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.47826% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 45.88%. Comparing base (876219c) to head (560fce8).

Files with missing lines Patch % Lines
uiautomator2/__init__.py 93.47% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1207      +/-   ##
==========================================
+ Coverage   45.26%   45.88%   +0.62%     
==========================================
  Files          29       29              
  Lines        4158     4204      +46     
  Branches      812      828      +16     
==========================================
+ Hits         1882     1929      +47     
+ Misses       2164     2160       -4     
- Partials      112      115       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

app_current() often returning wrong results

1 participant