Skip to content

feat(usb): add optional accessibility service to auto-confirm USB accessory permissions - #38

Closed
yanwuu wants to merge 1 commit into
shilapi:masterfrom
yanwuu:feat/usb-auto-confirm-service
Closed

yanwuu wants to merge 1 commit into
shilapi:masterfrom
yanwuu:feat/usb-auto-confirm-service

Conversation

@yanwuu

@yanwuu yanwuu commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Add an optional UsbAutoConfirmService (Accessibility Service) to automatically grant the Android USB accessory permission prompt for CarPlay on head units that forget USB permissions across reboots.

Note on Origin: This feature was originally developed and authored by me for shihabal3amri/DiPlay. I am now extracting and contributing this clean, vendor-neutral implementation directly to upstream shilapi/xcertplay.


Motivation & Problem

On many automotive head units (especially customized aftermarket or OEM Android systems), the system forgets USB device/accessory permission grants every time the device cold boots. Consequently:

  • Whenever the driver connects an iPhone or adapter via USB after starting the car, Android pops up a modal dialog asking: "Allow [App] to access this USB device?"
  • Drivers must manually tap the screen to check "Always allow" and press "OK", which is inconvenient, repetitive, and introduces distracted-driving risks.

Key Changes

  • Targeted Accessibility Service (UsbAutoConfirmService.kt):
    • Monitors window state and content change events.
    • Strict Security Scoping:
      • Only listens to system packages (com.android.systemui and android).
      • Only responds to system USB permission dialogs (UsbPermissionActivity and UsbConfirmActivity).
      • Uses regex matching against applicationInfo.loadLabel(packageManager) and "USB" to guarantee it never clicks permission dialogs intended for third-party apps or non-USB permissions.
    • Automatically checks android:id/alwaysUse if unchecked.
    • Clicks the confirmation button (android:id/button1 or labels matching "确定", "允许", "OK", "Allow", "Confirm").
    • Employs an 800ms debounce interval and properly recycles all AccessibilityNodeInfo references.
  • Service Configuration & Registration:
    • Created usb_auto_confirm_service_config.xml declaring event types and view ID reporting flags.
    • Registered the service in AndroidManifest.xml with android.permission.BIND_ACCESSIBILITY_SERVICE.
  • UI & Settings Integration:
    • Added an "Auto-confirm USB permission" toggle/indicator row under the "Startup" settings section in CarPlayHostActivity.
    • Added helper openAccessibilitySettings() to allow one-tap navigation directly into Android's Accessibility Settings page.
  • Localization: Added English and Chinese (values-zh-rCN) string resources.
  • Unit Tests (UsbAutoConfirmServiceTest.kt): Added comprehensive test coverage for package verification, activity class white-listing, and prompt text matching.

…essory permissions

- Introduce UsbAutoConfirmService, an optional AccessibilityService to
  automatically check 'Always allow / Use by default' and confirm the
  system USB permission dialog on head units that forget permissions on boot.
- Restrict event processing strictly to system USB activities
  (UsbPermissionActivity, UsbConfirmActivity under com.android.systemui and
  android) and verify that the dialog prompt explicitly names the app
  and USB access before acting.
- Add an 800ms debounce window and ensure all AccessibilityNodeInfo instances
  are recycled cleanly.
- Add an 'Auto-confirm USB permission' setting item and helper to open system
  Accessibility settings from CarPlayHostActivity.
- Add unit tests covering system activity filters and prompt target verification.
@shilapi

shilapi commented Oct 10, 2026

Copy link
Copy Markdown
Owner

这也太fifa了,并不优雅也不安全

@shilapi shilapi closed this Oct 10, 2026
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.

2 participants