Skip to content

Commit

Permalink
Use the Verify Flash Page with Odin4, improve wording of the Verify F…
Browse files Browse the repository at this point in the history
…lash Page for Thor. Allow switching between flash-tools (e.g. Thor to Odin4) without re-starting the app. Selecting "No" from the Verify Flash Page now brings you to the Start Page, instead of the Successful Flash Page.
  • Loading branch information
ethical-haquer committed Nov 16, 2024
1 parent bbb762e commit 36a2433
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 28 deletions.
23 changes: 20 additions & 3 deletions source/flash_tool_plugins/odin4.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def __init__(self, main):
}

def test(self):
print(f"This is a test of the {self.name} plugin.")
logger.debug("test is running")
logger.debug(f"test: This is a test of the {self.name} plugin.")

def setup_flash_tool(self, main):
logger.debug("setup_flash_tool is running")
Expand Down Expand Up @@ -93,7 +94,7 @@ def connect(self, main):
main.on_no_devices_found()
else:
# Have the user select a device.
main.display_devices(devices)
main.display_select_device_page(devices)

else:
output = main.remove_ansi_escape_sequences(
Expand Down Expand Up @@ -162,7 +163,8 @@ def selected_device(self, main, device, num_devices):
self.device = None
else:
self.device = device
self.flash(main)
# self.flash(main)
self.verify_flash(main)

def flash(self, main):
logger.debug("flash is running")
Expand Down Expand Up @@ -192,3 +194,18 @@ def flash(self, main):

def select_partitions(self, main, files, base_dir, auto):
print("Odin4 has no working select_partitions function.")

def verify_flash(self, main):
logger.debug("verify_flash is running")
num_files = len(self.selected_files)
files_noun = "files" if num_files > 1 else "file"
pronoun = "them" if num_files > 1 else "it"
text = f"You selected {num_files} {files_noun}. Are you absolutely sure you want to flash {pronoun}?"
main.display_verify_flash(text, self.on_verified_flash)

def on_verified_flash(self, main, continue_flashing):
if continue_flashing:
self.flash(main)
else:
logger.debug(f"on_verified_flash: Canceling the flash.")
main.cancel_flash("start")
27 changes: 16 additions & 11 deletions source/flash_tool_plugins/thor.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def connect(self, main):
self.selected_device(main, device_number, num_devices)
else:
# Have the user select a device.
main.display_devices(devices)
main.display_select_device_page(devices)

# If no devices were found.
elif result == 1:
Expand Down Expand Up @@ -324,7 +324,7 @@ def cycle(self, main):
self.send_selected_partitions(main, selected_partitions)
else:
# Have the user select the partitions to flash.
main.display_partitions(
main.display_select_partitions_page(
file, partitions, self.send_selected_partitions
)
else:
Expand Down Expand Up @@ -425,9 +425,15 @@ def verify_flash(self, main):
logger.debug("verify_flash is running")
output = main.child.before.decode("utf-8")
cleaned_output = main.remove_ansi_escape_sequences(output)
num_files = len(self.selected_files)
num_partitions = self.get_num_partitions(cleaned_output)
if not num_partitions == None:
main.display_verify_flash(self.auto, num_partitions, self.on_verified_flash)
noun = "the computer" if self.auto else "you"
files_noun = "files" if num_files > 1 else "file"
partitions_noun = "partitions" if num_partitions > 1 else "partition"
pronoun = "them" if num_partitions > 1 else "it"
text = f"You selected {num_files} {files_noun}, and {noun} selected {num_partitions} {partitions_noun} to flash. Are you absolutely sure you want to flash {pronoun}?"
main.display_verify_flash(text, self.on_verified_flash)
else:
logger.error(
f"verify_flash: {num_partitions=}, {repr(cleaned_output)}\n\n\n"
Expand Down Expand Up @@ -470,7 +476,7 @@ def check_for_output(main):
self.glib.idle_add(
main.update_flash_progress, "Ending Odin Session..."
)
ended = self.end_odin_session(main, go_to_done_page=True)
ended = self.end_odin_session(main)
if ended:
self.glib.idle_add(
main.update_flash_progress, "Disconnecting the device..."
Expand All @@ -497,7 +503,7 @@ def check_for_output(main):
check_for_output(main)

# Returns True if able to end, False otherwise.
def end_odin_session(self, main, go_to_done_page=False):
def end_odin_session(self, main):
logger.debug("end_odin_session is running")
logger.debug('end_odin_session: Running "end".')
main.child.sendline("end")
Expand Down Expand Up @@ -531,17 +537,16 @@ def on_verified_flash(self, main, continue_flashing):
)
thread.start()
else:
logger.debug(f"verified_flash: Canceling the flash.")
logger.debug('verified_flash: Sending "n".')
logger.debug(f"on_verified_flash: Canceling the flash.")
logger.debug('on_verified_flash: Sending "n".')
main.child.send("n")
logger.debug('verified_flash: Sending "Enter".')
logger.debug('on_verified_flash: Sending "Enter".')
main.child.send("\n")
# TODO: Display a "Cancel Successful" page.
ended = self.end_odin_session(main, go_to_done_page=True)
ended = self.end_odin_session(main)
if ended:
disconnected = self.disconnect(main)
if disconnected:
main.display_done_flashing()
main.cancel_flash("start")
else:
logger.error("on_verified_flash: Failed to disconnect the device!")
else:
Expand Down
78 changes: 64 additions & 14 deletions source/galaxy-flasher-gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def __init__(self, *args, **kwargs):
self.strings = shared_utils.load_strings(locale_file)
# Load settings
self.settings = shared_utils.load_settings(settings_file)
self.flashtool = self.settings.get("flash_tool", "thor")
# Load resources.gresource file.
resource = Gio.resource_load(
os.path.join(f"{swd}/share/resources/", "resources.gresource")
Expand All @@ -87,7 +86,10 @@ def __init__(self, *args, **kwargs):
)
print(icon_theme.get_resource_path())
# Load plug-ins
self.load_plugins()
self.available_plugins = self.get_available_plugins()
#self.load_plugins()
self.flashtool = self.settings.get("flash_tool", "thor")
self.ft_plugin = self.load_plugin(self.flashtool)
self.ft_plugin.test()
self.window_title = Adw.WindowTitle.new("Galaxy Flasher", f"{version}")

Expand Down Expand Up @@ -225,6 +227,7 @@ def __init__(self, *args, **kwargs):
"""
)

"""
def load_plugins(self):
logger.debug("load_plugins is running")
try:
Expand All @@ -249,6 +252,44 @@ def load_plugins(self):
except Exception as e:
logger.error(f"load_plugins: Error: {e}")
"""

def get_available_plugins(self):
logger.debug("get_available_plugins is running")
try:
plugins = load_plugins(self) # Assuming load_plugins is defined elsewhere
if not plugins:
logger.error("get_available_plugins: No flash-tool plugins found.")
return []

# Log available plugins and append them to flash_tool_options
for plugin in plugins:
logger.info(f"get_available_plugins: Available flash-tool plugin: {plugin.name}")
self.flash_tool_options.append(
{"name": plugin.displayed_name, "value": plugin.name}
)

return plugins
except Exception as e:
logger.error(f"get_available_plugins: Error: {e}")
return []

def load_plugin(self, plugin_name):
logger.debug(f"load_plugin is running")
logger.debug(f"load_plugin: {plugin_name=}")

plugins = self.available_plugins
if not plugins:
return None

for plugin in plugins:
if plugin.__class__.__name__.lower() == plugin_name.lower():
self.ft_plugin = plugin
logger.info(f"load_plugin: Loaded plug-in {plugin_name}")
return plugin
else:
logger.error(f"load_plugin: Plug-in {plugin_name} not found")
return None

def verify_supported_os(self):
logger.debug("verify_supported_os is running")
Expand Down Expand Up @@ -351,7 +392,7 @@ def display_start_page(self):
nav_buttons = [
{
"title": "Select Files",
"command": lambda _: self.display_files(),
"command": lambda _: self.display_select_files_page(),
"add_css_classes": ["suggested-action"],
},
]
Expand All @@ -366,8 +407,8 @@ def display_start_page(self):
self.set_widget_state(self.start_page.button0, state=False)
self.stack.set_visible_child_name("start")

def display_files(self):
logger.debug("display_files is running")
def display_select_files_page(self):
logger.debug("display_select_files_page is running")
if not self.stack.get_child_by_name("files"):
grid = Gtk.Grid.new()
grid.set_column_spacing(10)
Expand Down Expand Up @@ -445,8 +486,8 @@ def check_files(self):
self.ft_plugin.on_selected_files(self, files, paths)
"""

def display_devices(self, devices):
logger.debug("display_devices is running")
def display_select_device_page(self, devices):
logger.debug("display_select_device_page is running")

def set_selected_device(btn, device):
if btn.get_active:
Expand Down Expand Up @@ -500,8 +541,8 @@ def set_selected_device(btn, device):
)
self.stack.set_visible_child_name("devices")

def display_partitions(self, file, partitions, function):
logger.debug("display_partitions is running")
def display_select_partitions_page(self, file, partitions, function):
logger.debug("display_select_partitions_page is running")
selected_partitions = []

def partition_toggled(button, row):
Expand Down Expand Up @@ -559,7 +600,7 @@ def partition_toggled(button, row):
)
self.stack.set_visible_child_name("partitions")

def display_verify_flash(self, auto, num_partitions, function):
def display_verify_flash(self, text, function):
logger.debug("display_verify_flash is running")

verify_flash_page = self.stack.get_child_by_name("verify")
Expand All @@ -580,10 +621,7 @@ def display_verify_flash(self, auto, num_partitions, function):

secondary_label = Gtk.Label.new()
secondary_label.set_wrap(True)
noun = "The computer" if auto else "You"
secondary_label.set_label(
f"{noun} selected {num_partitions} partitions in total. Are you absolutely sure you want to flash them?"
)
secondary_label.set_label(text)

label_box.append(main_label)
label_box.append(secondary_label)
Expand Down Expand Up @@ -766,9 +804,21 @@ def set_setting(self, setting, value):
logger.debug("set_setting is running")
if setting == "theme":
self.set_theme(value)
elif setting == "flash_tool":
current_flashtool = self.flashtool
self.settings[setting] = value
logger.info(f"set_setting: {setting} set to: '{value}'")
shared_utils.save_settings(self.settings, settings_file)
if setting == "flash_tool":
selected_flashtool = self.settings.get("flash_tool")
if current_flashtool != selected_flashtool:
self.flashtool = self.settings.get("flash_tool")
if self.child:
self.set_widget_state(self.start_page.button0, state=False)
self.child.terminate()
self.ft_plugin = self.load_plugin(selected_flashtool)
self.ft_plugin.test()
self.setup_flash_tool()

def set_theme(self, theme):
logger.debug("set_theme is running")
Expand Down

0 comments on commit 36a2433

Please sign in to comment.