|
| 1 | +"""Contains the install addons view.""" |
| 2 | + |
| 3 | +# This file is part of OpenAndroidInstaller. |
| 4 | +# OpenAndroidInstaller is free software: you can redistribute it and/or modify it under the terms of |
| 5 | +# the GNU General Public License as published by the Free Software Foundation, |
| 6 | +# either version 3 of the License, or (at your option) any later version. |
| 7 | + |
| 8 | +# OpenAndroidInstaller is distributed in the hope that it will be useful, but WITHOUT ANY |
| 9 | +# WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 11 | + |
| 12 | +# You should have received a copy of the GNU General Public License along with OpenAndroidInstaller. |
| 13 | +# If not, see <https://www.gnu.org/licenses/>.""" |
| 14 | +# Author: Tobias Sterbak |
| 15 | + |
| 16 | +from loguru import logger |
| 17 | +from time import sleep |
| 18 | +from typing import Callable |
| 19 | + |
| 20 | +from flet import ( |
| 21 | + Column, |
| 22 | + ElevatedButton, |
| 23 | + Row, |
| 24 | + Text, |
| 25 | + icons, |
| 26 | + Switch, |
| 27 | + colors, |
| 28 | + Markdown, |
| 29 | +) |
| 30 | + |
| 31 | +from views import BaseView |
| 32 | +from app_state import AppState |
| 33 | +from tooling import adb_twrp_install_addons |
| 34 | +from widgets import ( |
| 35 | + confirm_button, |
| 36 | + get_title, |
| 37 | +) |
| 38 | +from views.step_view import TerminalBox, ProgressIndicator |
| 39 | + |
| 40 | + |
| 41 | +class InstallAddonsView(BaseView): |
| 42 | + def __init__( |
| 43 | + self, |
| 44 | + state: AppState, |
| 45 | + on_confirm: Callable, |
| 46 | + ): |
| 47 | + super().__init__(state=state) |
| 48 | + self.on_confirm = on_confirm |
| 49 | + |
| 50 | + def build(self): |
| 51 | + """Create the content of the view.""" |
| 52 | + # error text |
| 53 | + self.error_text = Text("", color=colors.RED) |
| 54 | + |
| 55 | + # switch to enable advanced output - here it means show terminal input/output in tool |
| 56 | + def check_advanced_switch(e): |
| 57 | + """Check the box to enable advanced output.""" |
| 58 | + if self.advanced_switch.value: |
| 59 | + logger.info("Enable advanced output.") |
| 60 | + self.state.advanced = True |
| 61 | + self.terminal_box.toggle_visibility() |
| 62 | + else: |
| 63 | + logger.info("Disable advanced output.") |
| 64 | + self.state.advanced = False |
| 65 | + self.terminal_box.toggle_visibility() |
| 66 | + |
| 67 | + self.advanced_switch = Switch( |
| 68 | + label="Advanced output", |
| 69 | + on_change=check_advanced_switch, |
| 70 | + disabled=False, |
| 71 | + ) |
| 72 | + |
| 73 | + # text box for terminal output |
| 74 | + self.terminal_box = TerminalBox(expand=True) |
| 75 | + |
| 76 | + # container for progress indicators |
| 77 | + self.progress_indicator = ProgressIndicator(expand=True) |
| 78 | + |
| 79 | + # main controls |
| 80 | + self.right_view_header.controls = [ |
| 81 | + get_title( |
| 82 | + "Install Addons", |
| 83 | + step_indicator_img="steps-header-install.png", |
| 84 | + ) |
| 85 | + ] |
| 86 | + self.right_view.controls = [ |
| 87 | + Markdown( |
| 88 | + """In the next steps, you finally flash the selected Addons. |
| 89 | +
|
| 90 | +Confirm to install. |
| 91 | +
|
| 92 | +This might take a while. At the end your phone will boot into the new OS. |
| 93 | +""" |
| 94 | + ) |
| 95 | + ] |
| 96 | + # basic view |
| 97 | + logger.info("Starting addon installation.") |
| 98 | + self.confirm_button = confirm_button(self.on_confirm) |
| 99 | + self.confirm_button.disabled = True |
| 100 | + # button to run the installation process |
| 101 | + self.install_button = ElevatedButton( |
| 102 | + "Confirm and install addons", |
| 103 | + on_click=self.run_install_addons, |
| 104 | + expand=True, |
| 105 | + icon=icons.DIRECTIONS_RUN_OUTLINED, |
| 106 | + ) |
| 107 | + # build the view |
| 108 | + self.right_view.controls.extend( |
| 109 | + [ |
| 110 | + Row([self.error_text]), |
| 111 | + Row([self.progress_indicator]), |
| 112 | + Column( |
| 113 | + [ |
| 114 | + self.advanced_switch, |
| 115 | + Row([self.install_button, self.confirm_button]), |
| 116 | + ] |
| 117 | + ), |
| 118 | + Row([self.terminal_box]), |
| 119 | + ] |
| 120 | + ) |
| 121 | + |
| 122 | + # if skipping is allowed add a button to the view |
| 123 | + if self.state.test: |
| 124 | + self.right_view.controls.append( |
| 125 | + Row( |
| 126 | + [ |
| 127 | + Text("Do you want to skip?"), |
| 128 | + ElevatedButton( |
| 129 | + "Skip", |
| 130 | + on_click=self.on_confirm, |
| 131 | + icon=icons.NEXT_PLAN_OUTLINED, |
| 132 | + expand=True, |
| 133 | + ), |
| 134 | + ] |
| 135 | + ) |
| 136 | + ) |
| 137 | + return self.view |
| 138 | + |
| 139 | + def run_install_addons(self, e): |
| 140 | + """ |
| 141 | + Run the addon installation process trough twrp. |
| 142 | +
|
| 143 | + Some parts of the command are changed by placeholders. |
| 144 | + """ |
| 145 | + # disable the call button while the command is running |
| 146 | + self.install_button.disabled = True |
| 147 | + self.install_addons_switch.disabled = True |
| 148 | + # reset the progress indicators |
| 149 | + self.progress_indicator.clear() |
| 150 | + # reset terminal output |
| 151 | + if self.state.advanced: |
| 152 | + self.terminal_box.clear() |
| 153 | + self.right_view.update() |
| 154 | + |
| 155 | + # run the install script |
| 156 | + for line in adb_twrp_install_addons( |
| 157 | + addons=self.state.addon_paths, |
| 158 | + bin_path=self.state.bin_path, |
| 159 | + ): |
| 160 | + # write the line to advanced output terminal |
| 161 | + self.terminal_box.write_line(line) |
| 162 | + # in case the install command is run, we want to update progress ring for now |
| 163 | + self.progress_indicator.display_progress_ring() |
| 164 | + success = line # the last element of the iterable is a boolean encoding success/failure |
| 165 | + |
| 166 | + # update the view accordingly |
| 167 | + if not success: |
| 168 | + # enable call button to retry |
| 169 | + self.install_button.disabled = False |
| 170 | + # also remove the last error text if it happened |
| 171 | + self.error_text.value = "Installation failed! Try again or make sure everything is setup correctly." |
| 172 | + else: |
| 173 | + sleep(5) # wait to make sure everything is fine |
| 174 | + logger.success("Installation process was successful. Allow to continue.") |
| 175 | + # enable the confirm button and disable the call button |
| 176 | + self.confirm_button.disabled = False |
| 177 | + self.install_button.disabled = True |
| 178 | + # reset the progress indicator |
| 179 | + self.progress_indicator.clear() |
| 180 | + self.view.update() |
0 commit comments