Skip to content

Commit e93fc5a

Browse files
committed
Simple check if the device code is in the recovery name
1 parent 5221f90 commit e93fc5a

File tree

5 files changed

+113
-45
lines changed

5 files changed

+113
-45
lines changed

openandroidinstaller/installer_config.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def __init__(
4343

4444

4545
class InstallerConfig:
46-
4746
def __init__(
4847
self,
4948
unlock_bootloader: List[Step],
@@ -78,7 +77,9 @@ def from_file(cls, path):
7877
]
7978
else:
8079
unlock_bootloader = []
81-
flash_recovery = [Step(**raw_step) for raw_step in raw_steps.get("flash_recovery", [])]
80+
flash_recovery = [
81+
Step(**raw_step) for raw_step in raw_steps.get("flash_recovery", [])
82+
]
8283
install_os = [Step(**raw_step) for raw_step in raw_steps.get("install_os", [])]
8384
return cls(unlock_bootloader, flash_recovery, install_os, metadata)
8485

@@ -111,34 +112,38 @@ def _load_config(device_code: str, config_path: Path) -> Optional[InstallerConfi
111112

112113

113114
def validate_config(config: str) -> bool:
114-
"""Validate the schema of the config."""
115+
"""Validate the schema of the config."""
115116

116117
step_schema = {
117118
"title": str,
118-
"type": Regex(r"text|confirm_button|call_button|call_button_with_input|link_button_with_confirm"),
119+
"type": Regex(
120+
r"text|confirm_button|call_button|call_button_with_input|link_button_with_confirm"
121+
),
119122
"content": str,
120-
schema.Optional("command"): Regex(r"^adb\s|^fastboot\s|^heimdall\s"),
123+
schema.Optional("command"): Regex(r"^adb\s|^fastboot\s|^heimdall\s"),
121124
schema.Optional("allow_skip"): bool,
122125
schema.Optional("img"): str,
123126
schema.Optional("link"): str,
124127
}
125128

126-
config_schema = Schema({
127-
"metadata": {
128-
"maintainer": str,
129-
"devicename": str,
130-
"devicecode": str,
131-
},
132-
"steps": {
133-
"unlock_bootloader": schema.Or(None, [step_schema]),
134-
"flash_recovery": [step_schema],
135-
"install_os": [step_schema],
129+
config_schema = Schema(
130+
{
131+
"metadata": {
132+
"maintainer": str,
133+
"devicename": str,
134+
"devicecode": str,
135+
},
136+
"steps": {
137+
"unlock_bootloader": schema.Or(None, [step_schema]),
138+
"flash_recovery": [step_schema],
139+
"install_os": [step_schema],
140+
},
136141
}
137-
})
142+
)
138143
try:
139144
config_schema.validate(config)
140145
logger.info("Config is valid.")
141146
return True
142147
except SchemaError as se:
143148
logger.info(f"Config is invalid. Error {se}")
144-
return False
149+
return False

openandroidinstaller/openandroidinstaller.py

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,52 @@
2222
from typing import Callable, Optional
2323

2424
import flet
25-
from flet import (AlertDialog, AppBar, Banner, Checkbox, Column, Container,
26-
Divider, ElevatedButton, FilePicker, FilePickerResultEvent,
27-
FilledButton, Icon, Image, Page, ProgressBar, ProgressRing,
28-
Row, Text, TextButton, TextField, UserControl, FloatingActionButton,
29-
VerticalDivider, colors, icons)
25+
from flet import (
26+
AlertDialog,
27+
AppBar,
28+
Banner,
29+
Checkbox,
30+
Column,
31+
Container,
32+
Divider,
33+
ElevatedButton,
34+
FilePicker,
35+
FilePickerResultEvent,
36+
FilledButton,
37+
Icon,
38+
Image,
39+
Page,
40+
ProgressBar,
41+
ProgressRing,
42+
Row,
43+
Text,
44+
TextButton,
45+
TextField,
46+
UserControl,
47+
FloatingActionButton,
48+
VerticalDivider,
49+
colors,
50+
icons,
51+
)
3052
from installer_config import InstallerConfig, Step, _load_config
3153
from loguru import logger
3254
from tool_utils import call_tool_with_command, search_device
3355
from utils import AppState, get_download_link, image_recovery_works_with_device
3456
from widgets import call_button, confirm_button, get_title, link_button
3557

3658
# Toggle to True for development purposes
37-
DEVELOPMENT = True
59+
DEVELOPMENT = False
3860
DEVELOPMENT_CONFIG = "yuga" # "a3y17lte" # "sargo"
3961

4062

4163
PLATFORM = sys.platform
4264
# Define asset paths
43-
CONFIG_PATH = Path(__file__).parent.joinpath(Path(os.sep.join(["assets", "configs"]))).resolve()
44-
IMAGE_PATH = Path(__file__).parent.joinpath(Path(os.sep.join(["assets", "imgs"]))).resolve()
65+
CONFIG_PATH = (
66+
Path(__file__).parent.joinpath(Path(os.sep.join(["assets", "configs"]))).resolve()
67+
)
68+
IMAGE_PATH = (
69+
Path(__file__).parent.joinpath(Path(os.sep.join(["assets", "imgs"]))).resolve()
70+
)
4571
BIN_PATH = Path(__file__).parent.joinpath(Path("bin")).resolve()
4672

4773

@@ -117,7 +143,9 @@ def check_bootloader_unlocked(e):
117143
self.state.num_total_steps = len(self.state.steps)
118144

119145
self.bootloader_checkbox = Checkbox(
120-
label="Bootloader is already unlocked.", on_change=check_bootloader_unlocked, disabled=True
146+
label="Bootloader is already unlocked.",
147+
on_change=check_bootloader_unlocked,
148+
disabled=True,
121149
)
122150

123151
# build up the main view
@@ -325,7 +353,9 @@ def build(self):
325353
),
326354
self.selected_recovery,
327355
Divider(),
328-
Text("If you selected both files and they work for your device you can continue."),
356+
Text(
357+
"If you selected both files and they work for your device you can continue."
358+
),
329359
self.info_field,
330360
Row([self.confirm_button]),
331361
]
@@ -335,12 +365,20 @@ def build(self):
335365
def enable_button_if_ready(self, e):
336366
"""Enable the confirm button if both files have been selected."""
337367

338-
if (".zip" in self.selected_image.value) and (".img" in self.selected_recovery.value):
368+
if (".zip" in self.selected_image.value) and (
369+
".img" in self.selected_recovery.value
370+
):
339371
if not image_recovery_works_with_device(
340-
device_code=self.state.config.metadata.get("devicecode"), image_path=self.state.image_path
341-
):
372+
device_code=self.state.config.metadata.get("devicecode"),
373+
image_path=self.state.image_path,
374+
recovery_path=self.state.recovery_path,
375+
):
342376
# 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."))
377+
self.info_field.controls.append(
378+
Text(
379+
"Image and recovery don't work with the device. Please select different ones."
380+
)
381+
)
344382
self.right_view.update()
345383
return
346384
self.info_field.controls = []
@@ -612,15 +650,17 @@ def main(page: Page):
612650
logger.info(f"Running OpenAndroidInstaller on {PLATFORM}")
613651
# Configure the application base page
614652
page.title = "OpenAndroidInstaller"
615-
page.window_height = 780
653+
page.window_height = 780
616654
page.window_width = int(1.77 * page.window_height)
617655
page.window_top = 100
618656
page.window_left = 120
619657
page.scroll = "adaptive"
620658
page.horizontal_alignment = "center"
621659

622660
# header
623-
image_path = Path(__file__).parent.joinpath(Path(os.sep.join(["assets", "logo-192x192.png"])))
661+
image_path = Path(__file__).parent.joinpath(
662+
Path(os.sep.join(["assets", "logo-192x192.png"]))
663+
)
624664
page.appbar = AppBar(
625665
leading=Image(src=image_path, height=40, width=40, border_radius=40),
626666
leading_width=56,

openandroidinstaller/utils.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,25 @@ def get_download_link(devicecode: str) -> Optional[str]:
3939
return
4040

4141

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."""
42+
def image_recovery_works_with_device(
43+
device_code: str, image_path: str, recovery_path: str
44+
) -> bool:
45+
"""Determine if an image and recovery works for the given device.
46+
47+
BEWARE: THE RECOVERY PART IS STILL VERY BASIC!
48+
"""
4449
with zipfile.ZipFile(image_path) as image_zip:
45-
with image_zip.open("META-INF/com/android/metadata", mode="r") as image_metadata:
50+
with image_zip.open(
51+
"META-INF/com/android/metadata", mode="r"
52+
) as image_metadata:
4653
metadata = image_metadata.readlines()
4754
supported_devices = str(metadata[-1]).split("=")[-1][:-3].split(",")
4855
logger.info(f"Image works with device: {supported_devices}")
4956

50-
if device_code in supported_devices:
51-
logger.info("Device supported by the image.")
57+
if (device_code in supported_devices) and (
58+
device_code in recovery_path.split("/")[-1]
59+
):
60+
logger.info("Device supported by the image and recovery.")
5261
return True
5362
return False
5463

openandroidinstaller/widgets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ def link_button(link: str, text: str) -> ElevatedButton:
6666
f"{text}",
6767
on_click=lambda _: webbrowser.open(link),
6868
expand=True,
69-
)
69+
)

scripts/download-tools.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,16 @@ def move_files_to_lib(platform: str):
6767
target_path.mkdir(exist_ok=True)
6868
# move adb
6969
adb_path = (
70-
Path(__file__).parent.joinpath(Path(os.sep.join(["..", "tools", "platform-tools", "adb"]))).resolve()
70+
Path(__file__)
71+
.parent.joinpath(Path(os.sep.join(["..", "tools", "platform-tools", "adb"])))
72+
.resolve()
7173
)
7274
logger.info(adb_path)
7375
adb_target_path = (
7476
Path(__file__)
75-
.parent.joinpath(Path(os.sep.join(["..", "openandroidinstaller", "bin", "adb"])))
77+
.parent.joinpath(
78+
Path(os.sep.join(["..", "openandroidinstaller", "bin", "adb"]))
79+
)
7680
.resolve()
7781
)
7882
if platform == "win32":
@@ -83,23 +87,33 @@ def move_files_to_lib(platform: str):
8387
# move fastboot
8488
fb_path = (
8589
Path(__file__)
86-
.parent.joinpath(Path(os.sep.join(["..", "tools", "platform-tools", "fastboot"])))
90+
.parent.joinpath(
91+
Path(os.sep.join(["..", "tools", "platform-tools", "fastboot"]))
92+
)
8793
.resolve()
8894
)
8995
fb_target_path = (
9096
Path(__file__)
91-
.parent.joinpath(Path(os.sep.join(["..", "openandroidinstaller", "bin", "fastboot"])))
97+
.parent.joinpath(
98+
Path(os.sep.join(["..", "openandroidinstaller", "bin", "fastboot"]))
99+
)
92100
.resolve()
93101
)
94102
if platform == "win32":
95103
fb_path = fb_path.parents[0] / "fastboot.exe"
96104
fb_target_path = fb_target_path.parents[0] / "fastboot.exe"
97105
fb_path.rename(fb_target_path)
98106
# move heimdall
99-
hd_path = Path(__file__).parent.joinpath(Path(os.sep.join(["..", "heimdall", "heimdall"]))).resolve()
107+
hd_path = (
108+
Path(__file__)
109+
.parent.joinpath(Path(os.sep.join(["..", "heimdall", "heimdall"])))
110+
.resolve()
111+
)
100112
hd_target_path = (
101113
Path(__file__)
102-
.parent.joinpath(Path(os.sep.join(["..", "openandroidinstaller", "bin", "heimdall"])))
114+
.parent.joinpath(
115+
Path(os.sep.join(["..", "openandroidinstaller", "bin", "heimdall"]))
116+
)
103117
.resolve()
104118
)
105119
if platform == "win32":

0 commit comments

Comments
 (0)