Skip to content

Commit

Permalink
Add automatic Android SDK detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Mautone committed Jan 3, 2024
1 parent 09851b9 commit 8cfadda
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
8 changes: 6 additions & 2 deletions sdk_setter/sdk_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
44 changes: 34 additions & 10 deletions sdk_setter/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import platform
import shelve
from os import path

Expand All @@ -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):
Expand All @@ -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',
Expand All @@ -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

0 comments on commit 8cfadda

Please sign in to comment.