Skip to content

Commit

Permalink
Implemement a fix with alternative device code resolution (#57)
Browse files Browse the repository at this point in the history
This PR fixes the issue with devices where vendor device code is not the
official device code.
Mostly present in One Plus devices so far. #56
  • Loading branch information
tsterbak authored Jan 17, 2023
2 parents b179bf9 + 0462a30 commit 4141b22
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 28 deletions.
28 changes: 26 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,11 @@ def __init__(
self.install_os = install_os
self.metadata = metadata
self.requirements = requirements
self.device_code = metadata.get("devicecode")
inverted_mapping = dict(map(reversed, self.device_code_mapping.items()))
self.alternative_device_code = inverted_mapping.get(
self.device_code, self.device_code
)

@classmethod
def from_file(cls, path):
Expand Down Expand Up @@ -107,15 +128,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
16 changes: 1 addition & 15 deletions openandroidinstaller/tooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,20 +303,6 @@ def heimdall_flash_recovery(bin_path: Path, recovery: str) -> bool:
def search_device(platform: str, bin_path: Path) -> Optional[str]:
"""Search for a connected device."""
logger.info(f"Search devices on {platform} with {bin_path}...")
# 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",
}
try:
# read device properties
if platform in ("linux", "darwin"):
Expand Down Expand Up @@ -348,7 +334,7 @@ def search_device(platform: str, bin_path: Path) -> Optional[str]:
raise Exception(f"Unknown platform {platform}.")
device_code = output.split("[")[-1].strip()[:-1].strip()
logger.info(device_code)
return device_code_mapping.get(device_code, device_code)
return device_code
except CalledProcessError:
logger.error("Failed to detect a device.")
return None
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
20 changes: 12 additions & 8 deletions openandroidinstaller/views/select_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def build(self):
"Download TWRP recovery",
icon=icons.DOWNLOAD_OUTLINED,
on_click=lambda _: webbrowser.open(
f"https://dl.twrp.me/{self.state.config.metadata.get('devicecode')}"
f"https://dl.twrp.me/{self.state.config.device_code}"
),
expand=True,
),
Expand All @@ -164,7 +164,7 @@ def build(self):
Text("Select an OS image:", style="titleSmall"),
Markdown(
f"""
The image file should look something like `lineage-19.1-20221101-nightly-{self.state.config.metadata.get('devicecode')}-signed.zip`."""
The image file should look something like `lineage-19.1-20221101-nightly-{self.state.config.device_code}-signed.zip`."""
),
Row(
[
Expand All @@ -185,7 +185,7 @@ def build(self):
Text("Select a TWRP recovery image:", style="titleSmall"),
Markdown(
f"""
The recovery image should look something like `twrp-3.6.2_9-0-{self.state.config.metadata.get('devicecode')}.img`.
The recovery image should look something like `twrp-3.7.0_12-0-{self.state.config.device_code}.img`.
**Note:** This tool **only supports TWRP recoveries**.""",
extension_set="gitHubFlavored",
Expand Down 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
3 changes: 2 additions & 1 deletion openandroidinstaller/views/start_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from app_state import AppState
from widgets import get_title
from tooling import search_device
from installer_config import InstallerConfig


class StartView(BaseView):
Expand Down Expand Up @@ -224,7 +225,7 @@ def search_devices(self, e):
self.continue_button.disabled = False
self.bootloader_switch.disabled = False
# overwrite the text field with the real name from the config
self.device_name.value = f"{device_name} (code: {device_code})"
self.device_name.value = f"{device_name} (code: {InstallerConfig.device_code_mapping.get(device_code, device_code)})"
self.device_name.color = colors.GREEN
else:
# failed to load config
Expand Down

0 comments on commit 4141b22

Please sign in to comment.