From 8cfadda33f565d68694a85d665d5bbf19d11ce61 Mon Sep 17 00:00:00 2001 From: Alessandro Mautone Date: Wed, 3 Jan 2024 19:02:39 +0100 Subject: [PATCH] Add automatic Android SDK detection --- sdk_setter/sdk_screen.py | 8 ++++++-- sdk_setter/utils.py | 44 +++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/sdk_setter/sdk_screen.py b/sdk_setter/sdk_screen.py index f367570..0e2e582 100644 --- a/sdk_setter/sdk_screen.py +++ b/sdk_setter/sdk_screen.py @@ -6,7 +6,7 @@ from screen_manager.screen_constants import HOME_SCREEN from screen_manager.utils import navigate_to_screen -from sdk_setter.utils import store_sdk_directory as store_sdk +from sdk_setter.utils import trigger_store_sdk_directory, check_default_android_sdk_location from sdk_setter.utils import get_stored_sdk_directory @@ -46,7 +46,11 @@ def __init__(self, **kwargs): self.add_widget(layout) + def on_pre_enter(self, *args): + if check_default_android_sdk_location(): + navigate_to_screen(self.manager, HOME_SCREEN) + def store_sdk_directory(self, *args): - stored_successfully = store_sdk(self.sdk_location_text_input.text) + stored_successfully = trigger_store_sdk_directory(self.sdk_location_text_input.text) if stored_successfully: navigate_to_screen(self.manager, HOME_SCREEN) diff --git a/sdk_setter/utils.py b/sdk_setter/utils.py index 4f51917..b3c3d8f 100644 --- a/sdk_setter/utils.py +++ b/sdk_setter/utils.py @@ -1,3 +1,5 @@ +import os +import platform import shelve from os import path @@ -7,13 +9,31 @@ def android_sdk_location_is_valid(directory_path): directory_path = optionally_expand_user_directory(directory_path) + return is_valid_adb_file(directory_path) - if path.exists(directory_path): - platform_tools_directory = path.join(directory_path, "platform-tools") + +def check_default_android_sdk_location(): + # Define default paths for different OS + os_default_paths = { + 'Windows': 'C:\\Users\\%USERNAME%\\AppData\\Local\\Android\\Sdk\\', + 'Darwin': '/Users/$USER/Library/Android/sdk/', # macOS + 'Linux': '/home/$USER/Android/Sdk/' + } + + current_os = platform.system() + default_android_sdk_path = os_default_paths.get(current_os) + if default_android_sdk_path: + # Replace placeholders with actual environment variables + default_android_sdk_path = os.path.expandvars(default_android_sdk_path) + if is_valid_adb_file(default_android_sdk_path): + return store_sdk_directory(default_android_sdk_path) + + +def is_valid_adb_file(android_sdk_directory): + if path.exists(android_sdk_directory): + platform_tools_directory = path.join(android_sdk_directory, "platform-tools") adb_file = path.join(platform_tools_directory, "adb") return path.isfile(adb_file) - else: - return False def optionally_expand_user_directory(directory_path): @@ -35,12 +55,10 @@ def get_adb(): return adb_file -def store_sdk_directory(sdk_location): - location_to_check = optionally_expand_user_directory(sdk_location) - if android_sdk_location_is_valid(location_to_check): - with shelve.open("settings") as storage: - storage["sdk_directory"] = location_to_check - return True +def trigger_store_sdk_directory(sdk_location): + sdk_location = optionally_expand_user_directory(sdk_location) + if android_sdk_location_is_valid(sdk_location): + return store_sdk_directory(sdk_location) else: Popup( title='Error', @@ -57,3 +75,9 @@ def store_sdk_directory(sdk_location): title_size="20sp" ).open() return False + + +def store_sdk_directory(sdk_location): + with shelve.open("settings") as storage: + storage["sdk_directory"] = sdk_location + return True