Skip to content

Commit

Permalink
Implemement a fix with alternative device code resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
tsterbak committed Jan 17, 2023
1 parent b179bf9 commit f2ae4a6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
27 changes: 25 additions & 2 deletions openandroidinstaller/installer_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ def __init__(


class InstallerConfig:

# map some detected device codes to their real code.
device_code_mapping = {
# Sony issues
"C6603": "yuga",
# OnePlus issues
"OnePlus6": "enchilada",
"OnePlus6T": "fajita",
"OnePlus7": "guacamoleb",
"OnePlus7Pro": "guacamole",
"OnePlus7T": "hotdogb",
"OnePlus7TPro": "hotdog",
"Nord": "avicii",
"NordN200": "dre",
}

def __init__(
self,
unlock_bootloader: List[Step],
Expand All @@ -62,6 +78,10 @@ def __init__(
self.install_os = install_os
self.metadata = metadata
self.requirements = requirements
self.device_code = metadata.get("devicecode")
self.alternative_device_code = self.device_code_mapping.get(
self.device_code, self.device_code
)

@classmethod
def from_file(cls, path):
Expand Down Expand Up @@ -107,15 +127,18 @@ def _load_config(device_code: str, config_path: Path) -> Optional[InstallerConfi
Try to load local file in the same directory as the executable first, then load from assets.
"""
# try loading a custom local file first
custom_path = Path.cwd().joinpath(Path(f"{device_code}.yaml"))
mapped_device_code = InstallerConfig.device_code_mapping.get(
device_code, device_code
)
custom_path = Path.cwd().joinpath(Path(f"{mapped_device_code}.yaml"))
try:
config = InstallerConfig.from_file(custom_path)
logger.info(f"Loaded custom device config from {custom_path}.")
logger.info(f"Config metadata: {config.metadata}.")
return config
except FileNotFoundError:
# if no localfile, then try to load a config file from assets
path = config_path.joinpath(Path(f"{device_code}.yaml"))
path = config_path.joinpath(Path(f"{mapped_device_code}.yaml"))
try:
config = InstallerConfig.from_file(path)
logger.info(f"Loaded device config from {path}.")
Expand Down
8 changes: 6 additions & 2 deletions openandroidinstaller/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def get_download_link(devicecode: str) -> Optional[str]:
return


def image_works_with_device(device_code: str, image_path: str) -> bool:
def image_works_with_device(
device_code: str, alternative_device_code: str, image_path: str
) -> bool:
"""Determine if an image works for the given device."""
with zipfile.ZipFile(image_path) as image_zip:
with image_zip.open(
Expand All @@ -49,7 +51,9 @@ def image_works_with_device(device_code: str, image_path: str) -> bool:
supported_devices = str(metadata[-1]).split("=")[-1][:-3].split(",")
logger.info(f"Image works with device: {supported_devices}")

if device_code in supported_devices:
if (device_code in supported_devices) or (
alternative_device_code in supported_devices
):
logger.success("Device supported by the selected image.")
return True
else:
Expand Down
14 changes: 9 additions & 5 deletions openandroidinstaller/views/select_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,11 @@ def pick_image_result(self, e: FilePickerResultEvent):
logger.info("No image selected.")
# check if the image works with the device and show the filename in different colors accordingly
if e.files:
device_code = self.state.config.metadata.get("devicecode")
device_code = self.state.config.device_code
if image_works_with_device(
device_code=device_code, image_path=self.state.image_path
device_code=device_code,
alternative_device_code=self.state.config.alternative_device_code,
image_path=self.state.image_path,
):
self.selected_image.color = colors.GREEN
else:
Expand All @@ -261,7 +263,7 @@ def pick_recovery_result(self, e: FilePickerResultEvent):
logger.info("No image selected.")
# check if the recovery works with the device and show the filename in different colors accordingly
if e.files:
device_code = self.state.config.metadata.get("devicecode")
device_code = self.state.config.device_code
if recovery_works_with_device(
device_code=device_code, recovery_path=self.state.recovery_path
):
Expand All @@ -276,10 +278,12 @@ def enable_button_if_ready(self, e):
if (".zip" in self.selected_image.value) and (
".img" in self.selected_recovery.value
):
device_code = self.state.config.metadata.get("devicecode")
device_code = self.state.config.device_code
if not (
image_works_with_device(
device_code=device_code, image_path=self.state.image_path
device_code=device_code,
alternative_device_code=self.state.config.alternative_device_code,
image_path=self.state.image_path,
)
and recovery_works_with_device(
device_code=device_code, recovery_path=self.state.recovery_path
Expand Down

0 comments on commit f2ae4a6

Please sign in to comment.