Skip to content

fix: prevent delayed accessibility events from overwriting foreground cache - #571

Closed
DeanTMaxim wants to merge 1366 commits into
SuperMonster003:masterfrom
DeanTMaxim:fix/current-package-cache
Closed

DeanTMaxim wants to merge 1366 commits into
SuperMonster003:masterfrom
DeanTMaxim:fix/current-package-cache

Conversation

@DeanTMaxim

Copy link
Copy Markdown
Contributor

Summary / 变更概述

Prevent delayed accessibility window-state events from overwriting the cached foreground package/activity when their original window no longer exists.

避免原窗口已经消失的延迟无障碍窗口状态事件覆盖当前前台包名/Activity 缓存。

Closes #570. Related symptom report: #529.

Root cause / 根因

Both ActivityInfoProvider implementations used:

if (window?.isFocused != false)

If getWindow(event.windowId) returns null, Kotlin evaluates null != false as true. A delayed event from a disappeared SystemUI/launcher/app window could therefore be accepted as if it were focused and replace mLatestPackage/mLatestActivity.

两个 ActivityInfoProvider 实现都使用了上述判断。当 getWindow(event.windowId) 返回 null 时,Kotlin 中 null != false 的结果为 true,所以来自已消失窗口的延迟事件可能被当作聚焦事件接纳,并覆盖 mLatestPackage/mLatestActivity。

Fix / 修复方式

Apply the following policy consistently to both maintained namespaces:

  • Existing focused event window: accept.
  • Existing unfocused event window: reject.
  • Missing event window: accept only if both the event window ID and package match rootInActiveWindow.
  • Otherwise, preserve the previous cache.

在两个维护中的命名空间内统一应用以下策略:

  • 事件窗口存在且聚焦:接纳。
  • 事件窗口存在但未聚焦:拒绝。
  • 事件窗口不存在:仅当事件窗口 ID 和包名都与 rootInActiveWindow 一致时接纳。
  • 其他情况保留原缓存。

Changed namespaces / 修改的命名空间:

  • org.autojs.autojs.core.activity
  • com.stardust.autojs.core.activity

Tests / 测试

Added six policy cases for each namespace (12 tests total):

  • focused event window;
  • unfocused event window;
  • missing window with mismatched active-root window ID;
  • missing window with mismatched active-root package;
  • missing window confirmed by the active root;
  • missing package evidence.

两个命名空间各增加 6 个策略用例,共 12 个测试,覆盖聚焦、未聚焦、活动根窗口 ID 不匹配、活动根包名不匹配、活动根确认以及缺少包名证据。

TDD evidence / TDD 证据:

  • With the old eventWindowFocused != false policy, all six missing-window regression cases failed across the two namespaces.
  • With this fix, both test classes report tests="6", failures="0", errors="0" (12/12 passed).

Full verification command / 完整验证命令:

gradle :app:testAppDebugUnitTest --rerun-tasks --no-daemon --stacktrace

Result / 结果:

BUILD SUCCESSFUL in 1m 24s
217 actionable tasks: 217 executed

Verification note / 验证说明

Current master has unrelated compilation errors introduced with the bottom log sheet (console_debug/console_verbose resource references and obsolete AutoJs.getInstance() calls). They are already covered by #556. To run the full unit-test task, equivalent corrections were applied locally only, the full task was rerun, and those corrections were then reverted. This PR does not contain the unrelated bottom-sheet changes.

当前 master 的底部日志面板存在与本修复无关的编译错误(console_debug/console_verbose 资源引用以及过时的 AutoJs.getInstance() 调用),#556 已处理这些问题。为执行完整单元测试,本地仅临时应用了等效修正;测试完成后已恢复。本 PR 不包含这些无关的底部日志面板改动。

The original symptom and event/window mismatch were also reproduced on an OPPO Android test device using ADB and accessibility-window inspection. Device identifiers, accounts, and application data are omitted.

原始现象及事件/窗口不匹配也已在一台 OPPO Android 测试机上通过 ADB 和无障碍窗口检查复现;设备标识、账号和应用数据均已省略。

…03#416); 移除 UiObject#hashCode/equals 覆写; 新增 UiObject#isShifted 方法
SuperMonster003 and others added 23 commits March 3, 2026 23:41
…yout, Expandable RecyclerView, Recyclerview Flexible Divider
Fix ConcatReader.close() to close all readers on failure
- 新增 LogBottomSheet 底部日志面板
- ConsoleView 支持堆栈帧点击跳转
- EditorView 添加 LogPanelCallback 接口
- EditActivity 集成日志面板与代码跳转
- 添加日志菜单项和图标资源
feat: 添加底部日志面板功能
Prevent delayed window-state events from replacing the foreground package cache unless the active root confirms both the event window and package. Add policy tests for both maintained namespaces.
SuperMonster003 pushed a commit that referenced this pull request Nov 3, 2026
…e foreground cache

When getWindow(event.windowId) returned null, `window?.isFocused != false`
treated a delayed TYPE_WINDOW_STATE_CHANGED event of a window that had
already disappeared as focused, so currentPackage()/currentActivity() could
report SystemUI, the launcher or a closed app. A missing window now updates
the cached package and activity only when rootInActiveWindow has the same
window ID and package; focused windows are accepted and unfocused ones
rejected as before. The Activity identity used by flow keeps its own
windowKnown handling.

Ported from #571 (reported in #570);
the com.stardust copy it also patched no longer exists.
SuperMonster003 added a commit that referenced this pull request Nov 3, 2026
…nd the contributor table

Add the fix entries of the ported PRs #571 (DeanTMaxim, reported in #570), #558 and #557 (djbclark) to the 6.8.0 notes in all ten languages, and add both contributors to the README contributor table by hand, sorted by the date of their latest pull request (2026/08/21 and 2026/07/14), with the counts of all their pull requests, including #556 and #573 whose fixes were already in place. Regenerate the changelog and README files, record the translation baseline, and set VERSION_BUILD to 4982.
@SuperMonster003

Copy link
Copy Markdown
Owner

感谢修复! 今天本仓库改写了历史 (清理作者邮箱) 并强制推送, GitHub 因此自动关闭了所有未合并的 PR, 本 PR 已无法在 GitHub 上重新打开或合并.

问题在当前代码中仍然存在, 已按你的策略移植为 b589e7b (提交作者为你), 并带上 org 命名空间的 6 个用例; com.stardust 副本已在 6.8.0 中移除, flow 使用的 Activity 身份逻辑保持其原有的 windowKnown 处理. 6.8.0 发行记录与 README 贡献者列表中均已署名, #570 将在 6.8.0 正式发布后关闭.

Thanks for the fix! Today's history rewrite (force push) made GitHub close all open pull requests, so this one can no longer be merged here. The bug is still present, so your policy has been ported as b589e7b with you as the commit author, together with the 6 test cases of the org namespace (the com.stardust copy was removed in 6.8.0). It is credited in the 6.8.0 release notes and the README contributor table; #570 will be closed once 6.8.0 is released.

SuperMonster003 added a commit that referenced this pull request Nov 3, 2026
The contributor table scraper rebuilt the table from merged pull requests only, so it would have dropped the rows added by hand for the ported pull requests. Pull requests #556, #557, #558, #571 and #573 are now recorded with the author's commit count and latest commit time as they were in each pull request (their GitHub commit lists span the whole old history after the 2026-11-03 rewrite) and are counted like merged ones; authors with such pull requests link to all their pull requests instead of merged ones only. Running the scraper corrects the commit count of djbclark from 3 to 5 (#556 and #558 have two commits each).
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.

Bug: delayed accessibility events can overwrite the foreground cache / 延迟无障碍事件会覆盖前台缓存

6 participants