Skip to content

Commit 5221f90

Browse files
committed
Function to check if the device is supported by the image
1 parent 7f38246 commit 5221f90

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

openandroidinstaller/openandroidinstaller.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
from installer_config import InstallerConfig, Step, _load_config
3131
from loguru import logger
3232
from tool_utils import call_tool_with_command, search_device
33-
from utils import AppState, get_download_link
33+
from utils import AppState, get_download_link, image_recovery_works_with_device
3434
from widgets import call_button, confirm_button, get_title, link_button
3535

3636
# Toggle to True for development purposes
37-
DEVELOPMENT = False
37+
DEVELOPMENT = True
3838
DEVELOPMENT_CONFIG = "yuga" # "a3y17lte" # "sargo"
3939

4040

@@ -250,7 +250,7 @@ def __init__(
250250

251251
def build(self):
252252
self.download_link = get_download_link(
253-
self.state.config.metadata.get("devicecode", "test")
253+
self.state.config.metadata.get("devicecode", "ERROR")
254254
)
255255
self.confirm_button = confirm_button(self.on_confirm)
256256
self.confirm_button.disabled = True
@@ -264,6 +264,8 @@ def build(self):
264264
# add title and progressbar
265265
self.right_view.controls.append(get_title("Pick image and recovery files:"))
266266
self.right_view.controls.append(self.progressbar)
267+
# text row to show infos during the process
268+
self.info_field = Row()
267269
# if there is an available download, show the button to the page
268270
if self.download_link:
269271
self.right_view.controls.append(
@@ -323,15 +325,25 @@ def build(self):
323325
),
324326
self.selected_recovery,
325327
Divider(),
326-
Text("If you selected both files you can continue."),
328+
Text("If you selected both files and they work for your device you can continue."),
329+
self.info_field,
327330
Row([self.confirm_button]),
328331
]
329332
)
330333
return self.view
331334

332335
def enable_button_if_ready(self, e):
333336
"""Enable the confirm button if both files have been selected."""
337+
334338
if (".zip" in self.selected_image.value) and (".img" in self.selected_recovery.value):
339+
if not image_recovery_works_with_device(
340+
device_code=self.state.config.metadata.get("devicecode"), image_path=self.state.image_path
341+
):
342+
# if image and recovery work for device allow to move on, otherwise display message
343+
self.info_field.controls.append(Text("Image and recovery don't work with the device. Please select different ones."))
344+
self.right_view.update()
345+
return
346+
self.info_field.controls = []
335347
self.confirm_button.disabled = False
336348
self.right_view.update()
337349
else:
@@ -465,6 +477,7 @@ def pick_image_result(self, e: FilePickerResultEvent):
465477
)
466478
if e.files:
467479
self.image_path = e.files[0].path
480+
self.state.image_path = e.files[0].path
468481
logger.info(f"Selected image from {self.image_path}")
469482
else:
470483
logger.info("No image selected.")
@@ -476,6 +489,7 @@ def pick_recovery_result(self, e: FilePickerResultEvent):
476489
)
477490
if e.files:
478491
self.recovery_path = e.files[0].path
492+
self.state.recovery_path = e.files[0].path
479493
logger.info(f"Selected recovery from {self.recovery_path}")
480494
else:
481495
logger.info("No image selected.")

openandroidinstaller/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from typing import Optional
1717

18+
import zipfile
1819
import requests
1920
from loguru import logger
2021

@@ -38,6 +39,20 @@ def get_download_link(devicecode: str) -> Optional[str]:
3839
return
3940

4041

42+
def image_recovery_works_with_device(device_code: str, image_path: str) -> bool:
43+
"""Determine if an image and recovery works for the given device."""
44+
with zipfile.ZipFile(image_path) as image_zip:
45+
with image_zip.open("META-INF/com/android/metadata", mode="r") as image_metadata:
46+
metadata = image_metadata.readlines()
47+
supported_devices = str(metadata[-1]).split("=")[-1][:-3].split(",")
48+
logger.info(f"Image works with device: {supported_devices}")
49+
50+
if device_code in supported_devices:
51+
logger.info("Device supported by the image.")
52+
return True
53+
return False
54+
55+
4156
class AppState:
4257
"""Container class to store all kinds of state."""
4358

0 commit comments

Comments
 (0)