diff --git a/backend/android_tools.py b/backend/android_tools.py index e3199b9..c0a53bd 100644 --- a/backend/android_tools.py +++ b/backend/android_tools.py @@ -9,7 +9,86 @@ PROJECTS_DIR = DATA_DIR / 'projects' def list_devices() -> dict: - return run_command('adb devices -l', PROJECTS_DIR.as_posix(), timeout=60) + result = run_command('adb devices -l', PROJECTS_DIR.as_posix(), timeout=60) + result['devices'] = parse_devices(result.get('stdout', '')) + return result + + +def parse_devices(stdout: str) -> list[dict]: + devices: list[dict] = [] + + for line in stdout.splitlines(): + line = line.strip() + if not line or line.startswith('List of devices'): + continue + + parts = line.split() + if len(parts) < 2: + continue + + serial = parts[0] + state = parts[1] + + if state != 'device': + devices.append({ + 'serial': serial, + 'state': state, + 'title': f'{serial} ({state})', + 'subtitle': 'Устройство не готово', + }) + continue + + props = get_device_props(serial) + brand = props.get('brand') or '' + model = props.get('model') or '' + device = props.get('device') or '' + android = props.get('android') or '' + sdk = props.get('sdk') or '' + + title = ' '.join(part for part in [brand, model] if part).strip() or serial + subtitle_parts = [] + if android: + subtitle_parts.append(f'Android {android}') + if sdk: + subtitle_parts.append(f'SDK {sdk}') + if device: + subtitle_parts.append(device) + subtitle_parts.append(serial) + + devices.append({ + 'serial': serial, + 'state': state, + 'brand': brand, + 'model': model, + 'device': device, + 'android': android, + 'sdk': sdk, + 'title': title, + 'subtitle': ' · '.join(subtitle_parts), + }) + + return devices + + +def get_device_props(serial: str) -> dict: + commands = { + 'brand': f'adb -s {serial} shell getprop ro.product.brand', + 'model': f'adb -s {serial} shell getprop ro.product.model', + 'device': f'adb -s {serial} shell getprop ro.product.device', + 'android': f'adb -s {serial} shell getprop ro.build.version.release', + 'sdk': f'adb -s {serial} shell getprop ro.build.version.sdk', + } + + props: dict[str, str] = {} + + for key, command in commands.items(): + try: + result = run_command(command, PROJECTS_DIR.as_posix(), timeout=20) + props[key] = (result.get('stdout') or '').strip() + except Exception: + props[key] = '' + + return props def find_apks(workspace: str) -> list[str]: