Skip to content

Commit 0b53acc

Browse files
committed
ADd workflows to not flash the recovery and still work
1 parent ff5c2a1 commit 0b53acc

File tree

5 files changed

+131
-57
lines changed

5 files changed

+131
-57
lines changed

openandroidinstaller/app_state.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
import copy
1717
from pathlib import Path
1818
from typing import List, Optional
19+
from loguru import logger
1920

20-
from installer_config import _load_config
21+
from installer_config import _load_config, Step
2122

2223

2324
class AppState:
@@ -37,6 +38,10 @@ def __init__(
3738
self.test = test
3839
self.test_config = test_config
3940

41+
# store state
42+
self.unlock_bootloader = True
43+
self.flash_recovery = True
44+
4045
# placeholders
4146
self.advanced = False
4247
self.install_addons = False
@@ -72,3 +77,48 @@ def load_config(self, device_code: str):
7277
self.steps = copy.deepcopy(self.config.unlock_bootloader) + copy.deepcopy(
7378
self.config.boot_recovery
7479
)
80+
81+
def toggle_flash_unlock_bootloader(self):
82+
"""Toggle flashing of unlock bootloader."""
83+
self.unlock_bootloader = not self.unlock_bootloader
84+
if self.unlock_bootloader:
85+
logger.info("Enabled unlocking the bootloader again.")
86+
self.steps = copy.deepcopy(self.config.unlock_bootloader)
87+
else:
88+
logger.info("Skipping bootloader unlocking.")
89+
self.steps = []
90+
# if the recovery is already flashed, skip flashing it again
91+
if self.flash_recovery:
92+
self.steps += copy.deepcopy(self.config.boot_recovery)
93+
else:
94+
self.steps = [
95+
Step(
96+
title="Boot custom recovery",
97+
type="confirm_button",
98+
content="If you already flashed TWRP, boot into it by pressing 'Confirm and run'. Otherwise restart the process. Once your phone screen looks like the picture on the left, continue.",
99+
command="adb_reboot_recovery",
100+
img="twrp-start.jpeg",
101+
)
102+
]
103+
104+
def toggle_flash_recovery(self):
105+
"""Toggle flashing of recovery."""
106+
self.flash_recovery = not self.flash_recovery
107+
if self.unlock_bootloader:
108+
self.steps = copy.deepcopy(self.config.unlock_bootloader)
109+
else:
110+
self.steps = []
111+
if self.flash_recovery:
112+
logger.info("Enabled flashing recovery again.")
113+
self.steps += copy.deepcopy(self.config.boot_recovery)
114+
else:
115+
logger.info("Skipping flashing recovery.")
116+
self.steps = [
117+
Step(
118+
title="Boot custom recovery",
119+
type="confirm_button",
120+
content="If you already flashed TWRP, boot into it by pressing 'Confirm and run'. Otherwise restart the process. Once your phone screen looks like the picture on the left, continue.",
121+
command="adb_reboot_recovery",
122+
img="twrp-start.jpeg",
123+
)
124+
]

openandroidinstaller/tooling.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ def adb_wait_for_sideload(bin_path: Path) -> TerminalResponse:
161161
yield line
162162

163163

164+
@add_logging("Reboot to recovery with adb")
165+
def adb_reboot_recovery(bin_path: Path) -> TerminalResponse:
166+
"""Reboot to recovery with adb."""
167+
for line in run_command("adb reboot recovery", bin_path):
168+
yield line
169+
for line in adb_wait_for_recovery(bin_path=bin_path):
170+
yield line
171+
172+
164173
def adb_twrp_copy_partitions(bin_path: Path, config_path: Path) -> TerminalResponse:
165174
# some devices like one plus 6t or motorola moto g7 power need the partitions copied to prevent a hard brick
166175
logger.info("Sideload copy_partitions script with adb.")

openandroidinstaller/views/select_view.py

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -251,33 +251,38 @@ def build(self):
251251
),
252252
self.selected_image,
253253
Divider(),
254-
Text("Select a TWRP recovery image:", style="titleSmall"),
255-
Markdown(
256-
f"""
254+
]
255+
)
256+
if self.state.flash_recovery:
257+
self.right_view.controls.extend(
258+
[
259+
Text("Select a TWRP recovery image:", style="titleSmall"),
260+
Markdown(
261+
f"""
257262
The recovery image should look something like `twrp-3.7.0_12-0-{self.state.config.device_code}.img`.
258263
259264
**Note:** This tool **only supports TWRP recoveries**.""",
260-
extension_set="gitHubFlavored",
261-
),
262-
Row(
263-
[
264-
FilledButton(
265-
"Pick TWRP recovery file",
266-
icon=icons.UPLOAD_FILE,
267-
on_click=lambda _: self.pick_recovery_dialog.pick_files(
268-
allow_multiple=False,
269-
file_type="custom",
270-
allowed_extensions=["img"],
265+
extension_set="gitHubFlavored",
266+
),
267+
Row(
268+
[
269+
FilledButton(
270+
"Pick TWRP recovery file",
271+
icon=icons.UPLOAD_FILE,
272+
on_click=lambda _: self.pick_recovery_dialog.pick_files(
273+
allow_multiple=False,
274+
file_type="custom",
275+
allowed_extensions=["img"],
276+
),
277+
expand=True,
271278
),
272-
expand=True,
273-
),
274-
]
275-
),
276-
self.selected_recovery,
277-
Divider(),
278-
self.additional_image_selection,
279-
]
280-
)
279+
]
280+
),
281+
self.selected_recovery,
282+
Divider(),
283+
self.additional_image_selection,
284+
]
285+
)
281286

282287
# attach the bottom buttons
283288
self.right_view.controls.extend(
@@ -296,11 +301,11 @@ def get_notes(self) -> str:
296301
notes = []
297302

298303
brand = self.state.config.metadata.get("brand", "")
299-
if brand in "xiaomi":
304+
if brand == "xiaomi":
300305
notes.append(
301306
"- If something goes wrong, you can reinstall MiUI here:\n<https://xiaomifirmwareupdater.com/>\n"
302307
)
303-
elif brand in "poco":
308+
elif brand == "poco":
304309
notes.append(
305310
f"- If something goes wrong, you can reinstall MiUI here:\n<https://xiaomifirmwareupdater.com/miui/{self.state.config.device_code}/>\n"
306311
)
@@ -498,14 +503,15 @@ def pick_image_result(self, e: FilePickerResultEvent):
498503
else:
499504
self.selected_image.color = colors.RED
500505
# if the image works and the sdk level is 33 or higher, show the additional image selection
501-
if (
502-
self.selected_image.color == colors.GREEN
503-
and image_sdk_level(self.state.image_path) >= 33
504-
):
505-
self.toggle_additional_image_selection()
506-
else:
507-
self.additional_image_selection.controls = []
508-
self.additional_image_selection.update()
506+
if self.state.flash_recovery:
507+
if (
508+
self.selected_image.color == colors.GREEN
509+
and image_sdk_level(self.state.image_path) >= 33
510+
):
511+
self.toggle_additional_image_selection()
512+
else:
513+
self.additional_image_selection.controls = []
514+
self.additional_image_selection.update()
509515
# update
510516
self.selected_image.update()
511517

@@ -523,9 +529,9 @@ def pick_recovery_result(self, e: FilePickerResultEvent):
523529
logger.info("No image selected.")
524530
# check if the recovery works with the device and show the filename in different colors accordingly
525531
if e.files:
526-
device_code = self.state.config.device_code
527532
if recovery_works_with_device(
528-
device_code=device_code, recovery_path=self.state.recovery_path
533+
supported_device_codes=self.state.config.supported_device_codes,
534+
recovery_path=self.state.recovery_path,
529535
):
530536
self.selected_recovery.color = colors.GREEN
531537
else:
@@ -642,5 +648,31 @@ def enable_button_if_ready(self, e):
642648
self.info_field.controls = []
643649
self.confirm_button.disabled = False
644650
self.right_view.update()
651+
elif (".zip" in self.selected_image.value) and (not self.state.flash_recovery):
652+
if not (
653+
image_works_with_device(
654+
supported_device_codes=self.state.config.supported_device_codes,
655+
image_path=self.state.image_path,
656+
)
657+
):
658+
# if image works for device allow to move on, otherwise display message
659+
logger.error(
660+
"Image doesn't work with the device. Please select a different one."
661+
)
662+
self.info_field.controls = [
663+
Text(
664+
"Image doesn't work with the device.",
665+
color=colors.RED,
666+
weight="bold",
667+
)
668+
]
669+
self.confirm_button.disabled = True
670+
self.right_view.update()
671+
return
672+
673+
logger.info("Image works with the device. You can continue.")
674+
self.info_field.controls = []
675+
self.confirm_button.disabled = False
676+
self.right_view.update()
645677
else:
646678
self.confirm_button.disabled = True

openandroidinstaller/views/start_view.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# If not, see <https://www.gnu.org/licenses/>."""
1414
# Author: Tobias Sterbak
1515

16-
import copy
1716
import webbrowser
1817
from loguru import logger
1918
from typing import Callable
@@ -99,15 +98,7 @@ def init_visuals(
9998
# toggleswitch to allow skipping unlocking the bootloader
10099
def check_bootloader_unlocked(e):
101100
"""Enable skipping unlocking the bootloader if selected."""
102-
if self.bootloader_switch.value:
103-
logger.info("Skipping bootloader unlocking.")
104-
self.state.steps = []
105-
else:
106-
logger.info("Enabled unlocking the bootloader again.")
107-
self.state.steps = copy.deepcopy(self.state.config.unlock_bootloader)
108-
# if the recovery is already flashed, skip flashing it again
109-
if self.recovery_switch.value == False:
110-
self.state.steps += copy.deepcopy(self.state.config.boot_recovery)
101+
self.state.toggle_flash_unlock_bootloader()
111102

112103
self.bootloader_switch = Switch(
113104
label="Bootloader is already unlocked.",
@@ -120,17 +111,7 @@ def check_bootloader_unlocked(e):
120111
# toggleswitch to allow skipping flashing recovery
121112
def check_recovery_already_flashed(e):
122113
"""Enable skipping flashing recovery if selected."""
123-
# manage the bootloader unlocking switch
124-
if self.bootloader_switch.value == False:
125-
self.state.steps = copy.deepcopy(self.state.config.unlock_bootloader)
126-
else:
127-
self.state.steps = []
128-
129-
if self.recovery_switch.value:
130-
logger.info("Skipping flashing recovery.")
131-
else:
132-
logger.info("Enabled flashing recovery again.")
133-
self.state.steps += copy.deepcopy(self.state.config.boot_recovery)
114+
self.state.toggle_flash_recovery()
134115

135116
self.recovery_switch = Switch(
136117
label="Custom recovery is already flashed.",

openandroidinstaller/views/step_view.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
adb_reboot,
4242
adb_reboot_bootloader,
4343
adb_reboot_download,
44+
adb_reboot_recovery,
4445
adb_sideload,
4546
adb_twrp_copy_partitions,
4647
fastboot_boot_recovery,
@@ -216,6 +217,7 @@ def call_to_phone(self, e, command: str):
216217
"adb_reboot": adb_reboot,
217218
"adb_reboot_bootloader": adb_reboot_bootloader,
218219
"adb_reboot_download": adb_reboot_download,
220+
"adb_reboot_recovery": adb_reboot_recovery,
219221
"adb_sideload": partial(adb_sideload, target=self.state.image_path),
220222
"adb_twrp_copy_partitions": partial(
221223
adb_twrp_copy_partitions, config_path=self.state.config_path

0 commit comments

Comments
 (0)