|
1 | | -async def check_status(): |
2 | | - print("[adb] Checking status...") |
| 1 | +import subprocess |
| 2 | +import asyncio |
| 3 | +import platform |
| 4 | +import os |
| 5 | +from Framework.install_handler.utils import send_response |
| 6 | + |
| 7 | + |
| 8 | +async def check_status() -> bool: |
| 9 | + """Check if ADB (Android Debug Bridge) is installed.""" |
| 10 | + print("[installer][android-adb] Checking status...") |
| 11 | + |
| 12 | + # Dynamically refresh ANDROID_HOME and PATH from registry on Windows |
| 13 | + system = platform.system() |
| 14 | + if system == "Windows": |
| 15 | + try: |
| 16 | + import winreg |
| 17 | + with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Environment", 0, winreg.KEY_READ) as key: |
| 18 | + try: |
| 19 | + android_home_reg, _ = winreg.QueryValueEx(key, "ANDROID_HOME") |
| 20 | + if android_home_reg: |
| 21 | + # Expand environment variables before checking if path exists |
| 22 | + android_home_expanded = os.path.expandvars(android_home_reg) |
| 23 | + if os.path.exists(android_home_expanded): |
| 24 | + os.environ['ANDROID_HOME'] = android_home_expanded |
| 25 | + # Update PATH with platform-tools (where ADB is located) |
| 26 | + platform_tools = os.path.join(android_home_expanded, "platform-tools") |
| 27 | + current_path = os.environ.get('PATH', '') |
| 28 | + if platform_tools not in current_path: |
| 29 | + os.environ['PATH'] = f"{platform_tools};{current_path}" |
| 30 | + print(f"[installer][android-adb] Refreshed ANDROID_HOME from registry: {android_home_expanded}") |
| 31 | + except FileNotFoundError: |
| 32 | + pass |
| 33 | + |
| 34 | + # Also check ANDROID_SDK_ROOT if ANDROID_HOME not found |
| 35 | + if 'ANDROID_HOME' not in os.environ or not os.path.exists(os.environ.get('ANDROID_HOME', '')): |
| 36 | + try: |
| 37 | + android_sdk_root_reg, _ = winreg.QueryValueEx(key, "ANDROID_SDK_ROOT") |
| 38 | + if android_sdk_root_reg: |
| 39 | + # Expand environment variables before checking if path exists |
| 40 | + android_sdk_root_expanded = os.path.expandvars(android_sdk_root_reg) |
| 41 | + if os.path.exists(android_sdk_root_expanded): |
| 42 | + os.environ['ANDROID_SDK_ROOT'] = android_sdk_root_expanded |
| 43 | + # Update PATH with platform-tools (where ADB is located) |
| 44 | + platform_tools = os.path.join(android_sdk_root_expanded, "platform-tools") |
| 45 | + current_path = os.environ.get('PATH', '') |
| 46 | + if platform_tools not in current_path: |
| 47 | + os.environ['PATH'] = f"{platform_tools};{current_path}" |
| 48 | + print(f"[installer][android-adb] Refreshed ANDROID_SDK_ROOT from registry: {android_sdk_root_expanded}") |
| 49 | + except FileNotFoundError: |
| 50 | + pass |
| 51 | + except Exception as e: |
| 52 | + print(f"[installer][android-adb] Failed to refresh from registry: {e}") |
| 53 | + |
| 54 | + try: |
| 55 | + loop = asyncio.get_event_loop() |
| 56 | + result = await loop.run_in_executor( |
| 57 | + None, |
| 58 | + lambda: subprocess.run( |
| 59 | + ["adb", "version"], |
| 60 | + capture_output=True, |
| 61 | + text=True, |
| 62 | + check=False |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + # If command succeeds (returncode = 0), ADB is installed |
| 67 | + if result.returncode == 0: |
| 68 | + version_output = (result.stdout or result.stderr).strip() |
| 69 | + print(f"[installer][android-adb] Already installed") |
| 70 | + await send_response({ |
| 71 | + "action": "status", |
| 72 | + "data": { |
| 73 | + "category": "Android", |
| 74 | + "name": "ADB", |
| 75 | + "status": "installed", |
| 76 | + "comment": f"ADB is installed (version: {version_output.split()[0] if version_output else 'unknown'})", |
| 77 | + } |
| 78 | + }) |
| 79 | + return True |
| 80 | + else: |
| 81 | + print("[installer][android-adb] Not installed") |
| 82 | + await send_response({ |
| 83 | + "action": "status", |
| 84 | + "data": { |
| 85 | + "category": "Android", |
| 86 | + "name": "ADB", |
| 87 | + "status": "not installed", |
| 88 | + "comment": "Install ADB to use it.", |
| 89 | + } |
| 90 | + }) |
| 91 | + return False |
| 92 | + except Exception as e: |
| 93 | + print(f"[installer][android-adb] Error checking status: {e}") |
| 94 | + await send_response({ |
| 95 | + "action": "status", |
| 96 | + "data": { |
| 97 | + "category": "Android", |
| 98 | + "name": "ADB", |
| 99 | + "status": "not installed", |
| 100 | + "comment": "Unable to check ADB status.", |
| 101 | + } |
| 102 | + }) |
| 103 | + return False |
| 104 | + |
| 105 | + |
3 | 106 |
|
4 | 107 |
|
5 | 108 | async def install(): |
6 | | - print("[adb] Installing...") |
| 109 | + """Install ADB - checks if already installed, otherwise prompts to install Android SDK.""" |
| 110 | + print("[installer][android-adb] Installing...") |
| 111 | + |
| 112 | + # Check if ADB is already installed |
| 113 | + if await check_status(): |
| 114 | + print("[installer][android-adb] ADB is already installed") |
| 115 | + return |
| 116 | + |
| 117 | + # ADB is not installed, send response to install Android SDK |
| 118 | + print("[installer][android-adb] ADB is not installed. Install Android SDK to get ADB.") |
| 119 | + await send_response({ |
| 120 | + "action": "status", |
| 121 | + "data": { |
| 122 | + "category": "Android", |
| 123 | + "name": "ADB", |
| 124 | + "status": "not installed", |
| 125 | + "comment": "Install the Android SDK, it will automatically install ADB.", |
| 126 | + } |
| 127 | + }) |
| 128 | + |
| 129 | + |
| 130 | + |
7 | 131 |
|
0 commit comments