|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import pathlib |
| 4 | +import shutil |
| 5 | +import subprocess |
| 6 | +import tempfile |
| 7 | +import time |
| 8 | + |
| 9 | +parser = argparse.ArgumentParser() |
| 10 | +parser.add_argument('--flutter', default='flutter') |
| 11 | +parser.add_argument('--adb', default='adb') |
| 12 | +parser.add_argument('--device', required=True) |
| 13 | +parser.add_argument('--scope', choices=['host', 'none'], default='host') |
| 14 | +parser.add_argument('--work-dir') |
| 15 | +args = parser.parse_args() |
| 16 | +if not args.device.startswith('emulator-'): |
| 17 | + parser.error('Use a disposable emulator; the probe enables test accessibility services.') |
| 18 | + |
| 19 | +here = pathlib.Path(__file__).resolve().parent |
| 20 | +repo = here.parents[1] |
| 21 | +work = pathlib.Path(args.work_dir or tempfile.mkdtemp(prefix='stack-a11y-')).resolve() |
| 22 | +app = work / 'app' |
| 23 | +package = 'com.cypherstack.accessibility_probe' |
| 24 | + |
| 25 | +def run(command, **kwargs): |
| 26 | + return subprocess.run(command, check=True, text=True, **kwargs) |
| 27 | + |
| 28 | +def adb(*command, capture=False): |
| 29 | + return run([args.adb, '-s', args.device, *command], capture_output=capture) |
| 30 | + |
| 31 | +if not app.exists(): |
| 32 | + run([args.flutter, 'create', '--empty', '--platforms=android', '--org', |
| 33 | + 'com.cypherstack', '--project-name', 'accessibility_probe', str(app)]) |
| 34 | +shutil.copyfile(here / 'main.dart', app / 'lib/main.dart') |
| 35 | +activity = (repo / 'scripts/app_config/templates/android/app/src/main/kotlin/com/cypherstack/stackwallet/MainActivity.kt').read_text() |
| 36 | +activity = activity.replace('package com.place.holder', f'package {package}') |
| 37 | +if args.scope == 'none': |
| 38 | + start = activity.index(' override fun provideRootLayout') |
| 39 | + end = activity.index(' var openPath:', start) |
| 40 | + activity = activity[:start] + activity[end:] |
| 41 | +kotlin = app / 'android/app/src/main/kotlin/com/cypherstack/accessibility_probe' |
| 42 | +kotlin.mkdir(parents=True, exist_ok=True) |
| 43 | +(kotlin / 'MainActivity.kt').write_text(activity) |
| 44 | +shutil.copyfile(here / 'Probe.kt', kotlin / 'Probe.kt') |
| 45 | +xml = app / 'android/app/src/main/res/xml' |
| 46 | +xml.mkdir(exist_ok=True) |
| 47 | +for name, tool in [('tool', 'true'), ('non_tool', 'false')]: |
| 48 | + (xml / f'{name}.xml').write_text(f'''<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" |
| 49 | + android:accessibilityEventTypes="typeAllMask" |
| 50 | + android:accessibilityFeedbackType="feedbackGeneric" |
| 51 | + android:accessibilityFlags="flagReportViewIds|flagRetrieveInteractiveWindows" |
| 52 | + android:canRetrieveWindowContent="true" |
| 53 | + android:isAccessibilityTool="{tool}" |
| 54 | + android:packageNames="{package}" />''') |
| 55 | +manifest_path = app / 'android/app/src/main/AndroidManifest.xml' |
| 56 | +manifest = manifest_path.read_text().replace('${applicationName}', '.ProbeApplication') |
| 57 | +start_marker, end_marker = '<!-- PROBE START -->', '<!-- PROBE END -->' |
| 58 | +while start_marker in manifest: |
| 59 | + start = manifest.index(start_marker) |
| 60 | + end = manifest.index(end_marker, start) + len(end_marker) |
| 61 | + manifest = manifest[:start] + manifest[end:] |
| 62 | +services = '' |
| 63 | +for cls, xml_name in [('ToolProbeService', 'tool'), ('NonToolProbeService', 'non_tool')]: |
| 64 | + services += f'''<service android:name=".{cls}" android:exported="true" |
| 65 | + android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"> |
| 66 | + <intent-filter><action android:name="android.accessibilityservice.AccessibilityService" /></intent-filter> |
| 67 | + <meta-data android:name="android.accessibilityservice" android:resource="@xml/{xml_name}" /> |
| 68 | + </service>''' |
| 69 | +services += '<receiver android:name=".ProbeReceiver" android:exported="true" />' |
| 70 | +manifest = manifest.replace('</application>', start_marker + services + end_marker + '</application>') |
| 71 | +manifest_path.write_text(manifest) |
| 72 | +run([args.flutter, 'build', 'apk', '--debug', '--target-platform', 'android-x64'], cwd=app) |
| 73 | +adb('install', '-r', str(app / 'build/app/outputs/flutter-apk/app-debug.apk')) |
| 74 | +old_services = adb('shell', 'settings', 'get', 'secure', 'enabled_accessibility_services', capture=True).stdout.strip() |
| 75 | +old_enabled = adb('shell', 'settings', 'get', 'secure', 'accessibility_enabled', capture=True).stdout.strip() |
| 76 | +try: |
| 77 | + services = f'{package}/.ToolProbeService:{package}/.NonToolProbeService' |
| 78 | + if old_services and old_services != 'null': |
| 79 | + services = old_services + ':' + services |
| 80 | + adb('shell', 'settings', 'put', 'secure', 'enabled_accessibility_services', services) |
| 81 | + adb('shell', 'settings', 'put', 'secure', 'accessibility_enabled', '1') |
| 82 | + adb('shell', 'run-as', package, 'rm', '-f', 'files/accessibility-result.txt') |
| 83 | + adb('shell', 'am', 'start', '-n', f'{package}/.MainActivity') |
| 84 | + adb('shell', 'am', 'broadcast', '-n', f'{package}/.ProbeReceiver', '--es', 'scope', args.scope) |
| 85 | + deadline = time.monotonic() + 90 |
| 86 | + output = '' |
| 87 | + while time.monotonic() < deadline: |
| 88 | + result = subprocess.run([args.adb, '-s', args.device, 'shell', 'run-as', package, |
| 89 | + 'cat', 'files/accessibility-result.txt'], text=True, capture_output=True) |
| 90 | + if result.returncode == 0 and result.stdout: |
| 91 | + output = result.stdout |
| 92 | + break |
| 93 | + time.sleep(1) |
| 94 | + print(output) |
| 95 | + if not output.startswith('PASS:'): |
| 96 | + raise SystemExit('Accessibility integration check failed or timed out') |
| 97 | + |
| 98 | +finally: |
| 99 | + for key, value in [('enabled_accessibility_services', old_services), ('accessibility_enabled', old_enabled)]: |
| 100 | + if value == 'null': |
| 101 | + adb('shell', 'settings', 'delete', 'secure', key) |
| 102 | + else: |
| 103 | + adb('shell', 'settings', 'put', 'secure', key, value) |
0 commit comments