Skip to content

Commit 4141b22

Browse files
authored
Implemement a fix with alternative device code resolution (#57)
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
2 parents b179bf9 + 0462a30 commit 4141b22

5 files changed

Lines changed: 47 additions & 28 deletions

File tree

openandroidinstaller/installer_config.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ def __init__(
4949

5050

5151
class InstallerConfig:
52+
53+
# map some detected device codes to their real code.
54+
device_code_mapping = {
55+
# Sony issues
56+
"C6603": "yuga",
57+
# OnePlus issues
58+
"OnePlus6": "enchilada",
59+
"OnePlus6T": "fajita",
60+
"OnePlus7": "guacamoleb",
61+
"OnePlus7Pro": "guacamole",
62+
"OnePlus7T": "hotdogb",
63+
"OnePlus7TPro": "hotdog",
64+
"Nord": "avicii",
65+
"NordN200": "dre",
66+
}
67+
5268
def __init__(
5369
self,
5470
unlock_bootloader: List[Step],
@@ -62,6 +78,11 @@ def __init__(
6278
self.install_os = install_os
6379
self.metadata = metadata
6480
self.requirements = requirements
81+
self.device_code = metadata.get("devicecode")
82+
inverted_mapping = dict(map(reversed, self.device_code_mapping.items()))
83+
self.alternative_device_code = inverted_mapping.get(
84+
self.device_code, self.device_code
85+
)
6586

6687
@classmethod
6788
def from_file(cls, path):
@@ -107,15 +128,18 @@ def _load_config(device_code: str, config_path: Path) -> Optional[InstallerConfi
107128
Try to load local file in the same directory as the executable first, then load from assets.
108129
"""
109130
# try loading a custom local file first
110-
custom_path = Path.cwd().joinpath(Path(f"{device_code}.yaml"))
131+
mapped_device_code = InstallerConfig.device_code_mapping.get(
132+
device_code, device_code
133+
)
134+
custom_path = Path.cwd().joinpath(Path(f"{mapped_device_code}.yaml"))
111135
try:
112136
config = InstallerConfig.from_file(custom_path)
113137
logger.info(f"Loaded custom device config from {custom_path}.")
114138
logger.info(f"Config metadata: {config.metadata}.")
115139
return config
116140
except FileNotFoundError:
117141
# if no localfile, then try to load a config file from assets
118-
path = config_path.joinpath(Path(f"{device_code}.yaml"))
142+
path = config_path.joinpath(Path(f"{mapped_device_code}.yaml"))
119143
try:
120144
config = InstallerConfig.from_file(path)
121145
logger.info(f"Loaded device config from {path}.")

openandroidinstaller/tooling.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -303,20 +303,6 @@ def heimdall_flash_recovery(bin_path: Path, recovery: str) -> bool:
303303
def search_device(platform: str, bin_path: Path) -> Optional[str]:
304304
"""Search for a connected device."""
305305
logger.info(f"Search devices on {platform} with {bin_path}...")
306-
# map some detected device codes to their real code.
307-
device_code_mapping = {
308-
# Sony issues
309-
"C6603": "yuga",
310-
# OnePlus issues
311-
"OnePlus6": "enchilada",
312-
"OnePlus6T": "fajita",
313-
"OnePlus7": "guacamoleb",
314-
"OnePlus7Pro": "guacamole",
315-
"OnePlus7T": "hotdogb",
316-
"OnePlus7TPro": "hotdog",
317-
"Nord": "avicii",
318-
"NordN200": "dre",
319-
}
320306
try:
321307
# read device properties
322308
if platform in ("linux", "darwin"):
@@ -348,7 +334,7 @@ def search_device(platform: str, bin_path: Path) -> Optional[str]:
348334
raise Exception(f"Unknown platform {platform}.")
349335
device_code = output.split("[")[-1].strip()[:-1].strip()
350336
logger.info(device_code)
351-
return device_code_mapping.get(device_code, device_code)
337+
return device_code
352338
except CalledProcessError:
353339
logger.error("Failed to detect a device.")
354340
return None

openandroidinstaller/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def get_download_link(devicecode: str) -> Optional[str]:
3939
return
4040

4141

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

52-
if device_code in supported_devices:
54+
if (device_code in supported_devices) or (
55+
alternative_device_code in supported_devices
56+
):
5357
logger.success("Device supported by the selected image.")
5458
return True
5559
else:

openandroidinstaller/views/select_view.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def build(self):
148148
"Download TWRP recovery",
149149
icon=icons.DOWNLOAD_OUTLINED,
150150
on_click=lambda _: webbrowser.open(
151-
f"https://dl.twrp.me/{self.state.config.metadata.get('devicecode')}"
151+
f"https://dl.twrp.me/{self.state.config.device_code}"
152152
),
153153
expand=True,
154154
),
@@ -164,7 +164,7 @@ def build(self):
164164
Text("Select an OS image:", style="titleSmall"),
165165
Markdown(
166166
f"""
167-
The image file should look something like `lineage-19.1-20221101-nightly-{self.state.config.metadata.get('devicecode')}-signed.zip`."""
167+
The image file should look something like `lineage-19.1-20221101-nightly-{self.state.config.device_code}-signed.zip`."""
168168
),
169169
Row(
170170
[
@@ -185,7 +185,7 @@ def build(self):
185185
Text("Select a TWRP recovery image:", style="titleSmall"),
186186
Markdown(
187187
f"""
188-
The recovery image should look something like `twrp-3.6.2_9-0-{self.state.config.metadata.get('devicecode')}.img`.
188+
The recovery image should look something like `twrp-3.7.0_12-0-{self.state.config.device_code}.img`.
189189
190190
**Note:** This tool **only supports TWRP recoveries**.""",
191191
extension_set="gitHubFlavored",
@@ -237,9 +237,11 @@ def pick_image_result(self, e: FilePickerResultEvent):
237237
logger.info("No image selected.")
238238
# check if the image works with the device and show the filename in different colors accordingly
239239
if e.files:
240-
device_code = self.state.config.metadata.get("devicecode")
240+
device_code = self.state.config.device_code
241241
if image_works_with_device(
242-
device_code=device_code, image_path=self.state.image_path
242+
device_code=device_code,
243+
alternative_device_code=self.state.config.alternative_device_code,
244+
image_path=self.state.image_path,
243245
):
244246
self.selected_image.color = colors.GREEN
245247
else:
@@ -261,7 +263,7 @@ def pick_recovery_result(self, e: FilePickerResultEvent):
261263
logger.info("No image selected.")
262264
# check if the recovery works with the device and show the filename in different colors accordingly
263265
if e.files:
264-
device_code = self.state.config.metadata.get("devicecode")
266+
device_code = self.state.config.device_code
265267
if recovery_works_with_device(
266268
device_code=device_code, recovery_path=self.state.recovery_path
267269
):
@@ -276,10 +278,12 @@ def enable_button_if_ready(self, e):
276278
if (".zip" in self.selected_image.value) and (
277279
".img" in self.selected_recovery.value
278280
):
279-
device_code = self.state.config.metadata.get("devicecode")
281+
device_code = self.state.config.device_code
280282
if not (
281283
image_works_with_device(
282-
device_code=device_code, image_path=self.state.image_path
284+
device_code=device_code,
285+
alternative_device_code=self.state.config.alternative_device_code,
286+
image_path=self.state.image_path,
283287
)
284288
and recovery_works_with_device(
285289
device_code=device_code, recovery_path=self.state.recovery_path

openandroidinstaller/views/start_view.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from app_state import AppState
3838
from widgets import get_title
3939
from tooling import search_device
40+
from installer_config import InstallerConfig
4041

4142

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

0 commit comments

Comments
 (0)