Skip to content

Commit 5080221

Browse files
committed
Restructure select page a bit
1 parent a2871fe commit 5080221

2 files changed

Lines changed: 51 additions & 30 deletions

File tree

openandroidinstaller/utils.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,10 @@ def get_download_link(devicecode: str) -> Optional[str]:
3939
return
4040

4141

42-
def image_recovery_works_with_device(
43-
device_code: str, image_path: str, recovery_path: str
42+
def image_works_with_device(
43+
device_code: str, image_path: str
4444
) -> bool:
45-
"""Determine if an image and recovery works for the given device.
46-
47-
BEWARE: THE RECOVERY PART IS STILL VERY BASIC!
48-
"""
45+
"""Determine if an image works for the given device."""
4946
with zipfile.ZipFile(image_path) as image_zip:
5047
with image_zip.open(
5148
"META-INF/com/android/metadata", mode="r"
@@ -54,17 +51,25 @@ def image_recovery_works_with_device(
5451
supported_devices = str(metadata[-1]).split("=")[-1][:-3].split(",")
5552
logger.info(f"Image works with device: {supported_devices}")
5653

57-
recovery_file_name = recovery_path.split("/")[-1]
58-
if (
59-
(device_code in supported_devices)
60-
and (device_code in recovery_file_name)
61-
and ("twrp" in recovery_file_name)
62-
):
63-
logger.success("Device supported by the image and recovery.")
54+
if (device_code in supported_devices):
55+
logger.success("Device supported by the selected image.")
6456
return True
6557
else:
66-
logger.error(
67-
f"Recovery {recovery_file_name} and/or image {image_path.split('/')[-1]} are not supported or don't match."
68-
)
58+
logger.error(f"Image file {image_path.split('/')[-1]} is not supported.")
59+
return False
60+
6961

70-
return False
62+
def recovery_works_with_device(
63+
device_code: str, recovery_path: str
64+
) -> bool:
65+
"""Determine if a recovery works for the given device.
66+
67+
BEWARE: THE RECOVERY PART IS STILL VERY BASIC!
68+
"""
69+
recovery_file_name = recovery_path.split("/")[-1]
70+
if (device_code in recovery_file_name) and ("twrp" in recovery_file_name):
71+
logger.success("Device supported by the selected recovery.")
72+
return True
73+
else:
74+
logger.error(f"Recovery file {recovery_file_name} is not supported.")
75+
return False

openandroidinstaller/views/select_view.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
from widgets import get_title, confirm_button
4040
from utils import (
4141
get_download_link,
42-
image_recovery_works_with_device,
42+
image_works_with_device,
43+
recovery_works_with_device
4344
)
4445

4546

@@ -58,25 +59,25 @@ def build(self):
5859
modal=True,
5960
title=Text("What is an OS image and recovery and why do I need it?"),
6061
content=Markdown(
61-
"""
62-
## OS image or ROM
63-
An operating system (OS) is system software that manages computer hardware, software resources, and provides common services for computer programs.
62+
"""## OS image or ROM
63+
An operating system (OS) is system software that manages computer hardware,
64+
software resources, and provides common services for computer programs.
6465
Popular, custom operating systems for mobile devices based on Android are
6566
- [LineageOS](https://lineageos.org/)
6667
- [/e/OS](https://e.foundation/e-os/) or
6768
- [LineageOS for microG](https://lineage.microg.org/)
6869
- and many others.
6970
70-
Often, the related OS images are called 'ROM'. 'ROM' stands for *R*ead-*o*nly *m*emory, which is a type of non-volatile memory used in computers
71-
for storing software that is rarely changed during the life of the system, also known as firmware.
71+
Often, the related OS images are called 'ROM'. 'ROM' stands for *R*ead-*o*nly *m*emory,
72+
which is a type of non-volatile memory used in computers for storing software that is
73+
rarely changed during the life of the system, also known as firmware.
7274
7375
# Recovery Image
7476
A custom recovery is used for installing custom software on your device.
7577
This custom software can include smaller modifications like rooting your device or even
7678
replacing the firmware of the device with a completely custom ROM. .
7779
78-
OpenAndroidInstaller works with the [TWRP recovery project](https://twrp.me/about/).
79-
""",
80+
OpenAndroidInstaller works with the [TWRP recovery project](https://twrp.me/about/).""",
8081
on_tap_link=lambda e: self.page.launch_url(e.data),
8182
),
8283
actions=[
@@ -220,6 +221,7 @@ def close_developer_options_dlg(self, e):
220221

221222
def pick_image_result(self, e: FilePickerResultEvent):
222223
path = ", ".join(map(lambda f: f.name, e.files)) if e.files else "Cancelled!"
224+
# update the textfield with the name of the file
223225
self.selected_image.value = (
224226
self.selected_image.value.split(":")[0] + f": {path}"
225227
)
@@ -229,10 +231,18 @@ def pick_image_result(self, e: FilePickerResultEvent):
229231
logger.info(f"Selected image from {self.image_path}")
230232
else:
231233
logger.info("No image selected.")
234+
# check if the image works with the device and show the filename in different colors accordingly
235+
device_code = self.state.config.metadata.get("devicecode")
236+
if image_works_with_device(device_code=device_code, image_path=self.state.image_path):
237+
self.selected_image.color = colors.GREEN
238+
else:
239+
self.selected_image.color = colors.RED
240+
# update
232241
self.selected_image.update()
233242

234243
def pick_recovery_result(self, e: FilePickerResultEvent):
235244
path = ", ".join(map(lambda f: f.name, e.files)) if e.files else "Cancelled!"
245+
# update the textfield with the name of the file
236246
self.selected_recovery.value = (
237247
self.selected_recovery.value.split(":")[0] + f": {path}"
238248
)
@@ -242,17 +252,23 @@ def pick_recovery_result(self, e: FilePickerResultEvent):
242252
logger.info(f"Selected recovery from {self.recovery_path}")
243253
else:
244254
logger.info("No image selected.")
255+
# check if the recovery works with the device and show the filename in different colors accordingly
256+
device_code = self.state.config.metadata.get("devicecode")
257+
if recovery_works_with_device(device_code=device_code, recovery_path=self.state.recovery_path):
258+
self.selected_recovery.color = colors.GREEN
259+
else:
260+
self.selected_recovery.color = colors.RED
261+
# update
245262
self.selected_recovery.update()
246263

247264
def enable_button_if_ready(self, e):
248265
"""Enable the confirm button if both files have been selected."""
249266
if (".zip" in self.selected_image.value) and (
250267
".img" in self.selected_recovery.value
251268
):
252-
if not image_recovery_works_with_device(
253-
device_code=self.state.config.metadata.get("devicecode"),
254-
image_path=self.state.image_path,
255-
recovery_path=self.state.recovery_path,
269+
device_code = self.state.config.metadata.get("devicecode")
270+
if not (image_works_with_device(device_code=device_code, image_path=self.state.image_path)
271+
and recovery_works_with_device(device_code=device_code, recovery_path=self.state.recovery_path)
256272
):
257273
# if image and recovery work for device allow to move on, otherwise display message
258274
logger.error(
@@ -261,7 +277,7 @@ def enable_button_if_ready(self, e):
261277
self.info_field.controls = [
262278
Text(
263279
"Image and/or recovery don't work with the device. Make sure you use a TWRP-based recovery.",
264-
color=colors.RED,
280+
color=colors.RED, weight="bold",
265281
)
266282
]
267283
self.right_view.update()

0 commit comments

Comments
 (0)