2424)
2525import shlex
2626from time import sleep
27- from typing import List , Optional , Union , Generator , Callable
27+ from typing import Optional , Union , Generator , Callable
2828
2929from loguru import logger
3030
@@ -140,6 +140,13 @@ def activate_sideload(bin_path: Path) -> TerminalResponse:
140140 yield line
141141
142142
143+ @add_logging ("Wait for device" )
144+ def adb_wait_for_device (bin_path : Path ) -> TerminalResponse :
145+ """Use adb to wait for the device to become available."""
146+ for line in run_command ("adb wait-for-device" , bin_path ):
147+ yield line
148+
149+
143150@add_logging ("Wait for recovery" )
144151def adb_wait_for_recovery (bin_path : Path ) -> TerminalResponse :
145152 """Use adb to wait for the recovery to become available."""
@@ -229,7 +236,7 @@ def adb_twrp_wipe_and_install(
229236 for partition in ["dalvik" , "cache" ]:
230237 for line in run_command (f"adb shell twrp wipe { partition } " , bin_path ):
231238 yield line
232- sleep (1 )
239+ sleep (3 )
233240 if (type (line ) == bool ) and not line :
234241 logger .error (f"Wiping { partition } failed." )
235242 # TODO: if this fails, a fix can be to just sideload something and then adb reboot
@@ -238,17 +245,20 @@ def adb_twrp_wipe_and_install(
238245 bin_path = bin_path ,
239246 ):
240247 yield line
248+ sleep (1 )
241249 if (type (line ) == bool ) and not line :
242250 yield False
243251 break
244252 sleep (2 )
245253 # finally reboot into os or to fastboot for flashing addons
246- sleep (7 )
254+ for line in adb_wait_for_recovery (bin_path ):
255+ yield line
247256 if install_addons :
248257 if is_ab :
249258 # reboot into the bootloader again
250259 for line in adb_reboot_bootloader (bin_path ):
251260 yield line
261+ sleep (3 )
252262 # boot to TWRP again
253263 for line in fastboot_boot_recovery (
254264 bin_path = bin_path , recovery = recovery , is_ab = is_ab
@@ -262,30 +272,39 @@ def adb_twrp_wipe_and_install(
262272 yield line
263273
264274
265- def adb_twrp_install_addons (
266- bin_path : Path , addons : List [ str ] , is_ab : bool
275+ def adb_twrp_install_addon (
276+ bin_path : Path , addon_path : str , is_ab : bool
267277) -> TerminalResponse :
268- """Flash addons through adb and twrp.
278+ """Flash addon through adb and twrp.
269279
270280 Only works for twrp recovery.
271281 """
272- logger .info ("Install addons with twrp." )
282+ logger .info (f "Install addon { addon_path } with twrp." )
273283 sleep (0.5 )
274284 if is_ab :
275285 adb_wait_for_recovery (bin_path = bin_path )
276- logger .info ("Sideload and install addons." )
277- for addon in addons :
278- # activate sideload
279- logger .info ("Activate sideload." )
280- for line in activate_sideload (bin_path = bin_path ):
281- yield line
282- # now flash os image
283- for line in adb_sideload (bin_path = bin_path , target = addon ):
284- yield line
285- sleep (7 )
286+ # activate sideload
287+ logger .info ("Activate sideload." )
288+ for line in activate_sideload (bin_path = bin_path ):
289+ yield line
290+ logger .info ("Sideload and install addon." )
291+ # now flash the addon
292+ for line in adb_sideload (bin_path = bin_path , target = addon_path ):
293+ yield line
294+ logger .info ("done." )
295+
296+
297+ def adb_twrp_finish_install_addons (bin_path : Path , is_ab : bool ) -> TerminalResponse :
298+ """Finish the process of flashing addons with TWRP and reboot.
299+
300+ Only works for twrp recovery.
301+ """
286302 sleep (3 )
303+ for line in adb_wait_for_recovery (bin_path ):
304+ yield line
287305 # finally reboot into os
288306 if is_ab :
307+ logger .info ("Switch partitions on a/b-partitioned device." )
289308 # reboot into the bootloader again
290309 for line in adb_reboot_bootloader (bin_path = bin_path ):
291310 yield line
@@ -302,6 +321,7 @@ def adb_twrp_install_addons(
302321 yield line
303322 else :
304323 # reboot with adb
324+ logger .info ("Reboot into OS." )
305325 for line in adb_reboot (bin_path = bin_path ):
306326 yield line
307327
@@ -360,30 +380,17 @@ def fastboot_boot_recovery(
360380 bin_path : Path , recovery : str , is_ab : bool = True
361381) -> TerminalResponse :
362382 """Temporarily, boot custom recovery with fastboot."""
363- # TODO: this can be unified now
364- if is_ab :
365- logger .info ("Boot custom recovery with fastboot." )
366- for line in run_command (
367- "fastboot boot" , target = f"{ recovery } " , bin_path = bin_path
368- ):
369- yield line
370- logger .info ("Boot into TWRP with fastboot." )
371- for line in adb_wait_for_recovery (bin_path = bin_path ):
372- yield line
373- else :
374- logger .info ("Boot custom recovery with fastboot." )
375- for line in run_command (
376- "fastboot boot" , target = f"{ recovery } " , bin_path = bin_path
377- ):
378- yield line
383+ logger .info ("Boot custom recovery with fastboot." )
384+ for line in run_command ("fastboot boot" , target = f"{ recovery } " , bin_path = bin_path ):
385+ yield line
386+ if not is_ab :
379387 if (type (line ) == bool ) and not line :
380388 logger .error ("Booting recovery failed." )
381389 yield False
382390 else :
383391 yield True
384- logger .info ("Boot into TWRP with fastboot." )
385- for line in adb_wait_for_recovery (bin_path = bin_path ):
386- yield line
392+ for line in adb_wait_for_recovery (bin_path = bin_path ):
393+ yield line
387394
388395
389396def fastboot_flash_boot (bin_path : Path , recovery : str ) -> TerminalResponse :
0 commit comments