Skip to content

Commit 58b1e8a

Browse files
committed
Move call to phone functipn to other modul
1 parent a55e23a commit 58b1e8a

File tree

5 files changed

+544
-112
lines changed

5 files changed

+544
-112
lines changed

openandroidinstaller/openandroidinstaller.py

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,24 @@
1515

1616
import sys
1717
import webbrowser
18-
from loguru import logger
18+
from pathlib import Path
1919
from subprocess import STDOUT, CalledProcessError, call, check_output
2020
from time import sleep
2121
from typing import Callable, Optional
22-
from pathlib import Path
2322

2423
import flet
25-
from flet import (
26-
AppBar,
27-
Banner,
28-
Checkbox,
29-
Column,
30-
Container,
31-
Divider,
32-
ElevatedButton,
33-
FilePicker,
34-
FilePickerResultEvent,
35-
Icon,
36-
Image,
37-
Page,
38-
ProgressBar,
39-
ProgressRing,
40-
Row,
41-
Text,
42-
TextButton,
43-
TextField,
44-
UserControl,
45-
VerticalDivider,
46-
colors,
47-
FilledButton,
48-
AlertDialog,
49-
icons,
50-
)
24+
from flet import (AlertDialog, AppBar, Banner, Checkbox, Column, Container,
25+
Divider, ElevatedButton, FilePicker, FilePickerResultEvent,
26+
FilledButton, Icon, Image, Page, ProgressBar, ProgressRing,
27+
Row, Text, TextButton, TextField, UserControl,
28+
VerticalDivider, colors, icons)
5129
from installer_config import InstallerConfig, Step
30+
from loguru import logger
31+
from tool_utils import call_tool_with_command, search_device
5232
from widgets import call_button, confirm_button, get_title
53-
from tool_utils import search_device, call_tool_with_command
5433

5534
# Toggle to True for development purposes
56-
DEVELOPMENT = False
35+
DEVELOPMENT = False
5736
DEVELOPMENT_CONFIG = "a3y17lte" # "sargo"
5837

5938

@@ -183,17 +162,21 @@ def search_devices(self, e):
183162
if DEVELOPMENT:
184163
# this only happens for testing
185164
device_code = DEVELOPMENT_CONFIG
186-
logger.info(f"Running search in development mode and loading config {device_code}.yaml.")
165+
logger.info(
166+
f"Running search in development mode and loading config {device_code}.yaml."
167+
)
187168
else:
188169
device_code = search_device(platform=PLATFORM, bin_path=BIN_PATH)
189170
if device_code:
190171
self.device_name.value = device_code
191172
else:
192-
self.device_name.value = "No device detected! Connect to USB and try again."
173+
self.device_name.value = (
174+
"No device detected! Connect to USB and try again."
175+
)
193176

194177
# load the config, if a device is detected
195178
if device_code:
196-
self.device_name.value = device_code
179+
self.device_name.value = device_code
197180
# load config from file
198181
path = CONFIG_PATH.joinpath(Path(f"{device_code}.yaml"))
199182
device_name = self.load_config(path)
@@ -401,7 +384,7 @@ def load_config(self, path: str) -> Optional[str]:
401384
return self.config.metadata.get("devicename", "No device name in config.")
402385
except FileNotFoundError:
403386
logger.info(f"No device config found for {path}.")
404-
return None
387+
return None
405388

406389
def pick_image_result(self, e: FilePickerResultEvent):
407390
self.selected_image.value = (
@@ -476,36 +459,32 @@ def build(self):
476459
def call_to_phone(self, e, command: str):
477460
"""
478461
Run the command given on the phone.
479-
462+
480463
Some parts of the command are changed by placeholders.
481464
"""
482-
command = command.replace("adb", str(BIN_PATH.joinpath(Path("adb"))))
483-
command = command.replace("fastboot", str(BIN_PATH.joinpath(Path("fastboot"))))
484-
command = command.replace("heimdall", str(BIN_PATH.joinpath(Path("heimdall"))))
485-
486465
command = command.replace("<recovery>", self.recovery_path)
487466
command = command.replace("<image>", self.image_path)
488467
command = command.replace("<inputtext>", self.inputtext.value)
468+
489469
self.right_view.controls.append(
490470
Row(
491-
[ProgressRing(color="#00d886")], # , Text("Wait for completion...")],
471+
[ProgressRing(color="#00d886")],
492472
alignment="center",
493473
)
494474
)
495475
self.right_view.update()
496-
logger.info(f"Run command: {command}")
497-
res = call(f"{command}", shell=True)
498-
if res != 0:
499-
logger.info(f"Command {command} failed.")
476+
# run the command
477+
success = call_tool_with_command(command=command, bin_path=BIN_PATH)
478+
# update the view accordingly
479+
if success:
500480
self.right_view.controls.pop()
501-
self.right_view.controls.append(Text("Command {command} failed!"))
481+
self.right_view.controls.append(Text(f"Command {command} failed!"))
502482
else:
503-
sleep(5)
483+
sleep(5) # wait to make sure everything is fine
504484
self.right_view.controls.pop() # pop the progress ring
505485
self.right_view.controls.append(
506486
ElevatedButton("Confirm and continue", on_click=self.on_confirm)
507487
)
508-
logger.info("Success.")
509488
self.view.update()
510489

511490

openandroidinstaller/tool_utils.py

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

16-
from typing import Optional
17-
from loguru import logger
1816
from pathlib import Path
1917
from subprocess import STDOUT, CalledProcessError, call, check_output
18+
from typing import Optional
2019

20+
import regex as re
21+
from loguru import logger
2122

22-
def call_tool_with_command(command: str, bin_path: Path):
23+
24+
def call_tool_with_command(command: str, bin_path: Path) -> bool:
2325
"""Call an executable with a specific command."""
24-
26+
command = re.sub(r"^adb", str(bin_path.joinpath(Path("adb"))), command)
27+
command = re.sub(r"^fastboot", str(bin_path.joinpath(Path("fastboot"))), command)
28+
command = re.sub(r"^heimdall", str(bin_path.joinpath(Path("heimdall"))), command)
29+
30+
logger.info(f"Run command: {command}")
31+
res = call(f"{command}", shell=True)
32+
if res != 0:
33+
logger.info("Success.")
34+
return True
35+
logger.info(f"Command {command} failed.")
36+
return False
37+
2538

2639
def search_device(platform: str, bin_path: Path) -> Optional[str]:
2740
"""Search for a connected device."""
@@ -37,7 +50,7 @@ def search_device(platform: str, bin_path: Path) -> Optional[str]:
3750
"getprop",
3851
"|",
3952
"grep",
40-
"ro.product.device"
53+
"ro.product.device",
4154
],
4255
stderr=STDOUT,
4356
).decode()
@@ -49,7 +62,7 @@ def search_device(platform: str, bin_path: Path) -> Optional[str]:
4962
"getprop",
5063
"|",
5164
"findstr",
52-
"ro.product.device"
65+
"ro.product.device",
5366
],
5467
stderr=STDOUT,
5568
).decode()

0 commit comments

Comments
 (0)