Skip to content

Commit dc8dda1

Browse files
OSUpdate: add force mode and improve UI
1 parent 79b28bd commit dc8dda1

File tree

1 file changed

+32
-9
lines changed
  • internal_filesystem/builtin/apps/com.micropythonos.osupdate/assets

1 file changed

+32
-9
lines changed

internal_filesystem/builtin/apps/com.micropythonos.osupdate/assets/osupdate.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,36 @@
1111
class OSUpdate(Activity):
1212

1313
keep_running = True
14+
download_update_url = None
1415

1516
# Widgets:
17+
status_label = None
1618
install_button = None
19+
force_update = None
1720
main_screen = None
1821
progress_label = None
1922
progress_bar = None
2023

2124
def onCreate(self):
2225
self.main_screen = lv.obj()
2326
self.main_screen.set_style_pad_all(mpos.ui.pct_of_display_width(2), 0)
27+
self.current_version_label = lv.label(self.main_screen)
28+
self.current_version_label.align(lv.ALIGN.TOP_LEFT,0,0)
29+
self.current_version_label.set_text(f"Installed OS version: {mpos.info.CURRENT_OS_VERSION}")
30+
self.force_update = lv.checkbox(self.main_screen)
31+
self.force_update.set_text("Force Update")
32+
self.force_update.add_event_cb(lambda *args: self.force_update_clicked(), lv.EVENT.CLICKED, None)
33+
self.force_update.align_to(self.current_version_label, lv.ALIGN.OUT_BOTTOM_LEFT, 0, mpos.ui.pct_of_display_height(5))
2434
self.install_button = lv.button(self.main_screen)
25-
self.install_button.align(lv.ALIGN.TOP_RIGHT, 0, mpos.ui.topmenu.NOTIFICATION_BAR_HEIGHT)
35+
self.install_button.align(lv.ALIGN.TOP_RIGHT, 0, 0)
2636
self.install_button.add_state(lv.STATE.DISABLED) # button will be enabled if there is an update available
2737
self.install_button.set_size(lv.SIZE_CONTENT, lv.pct(25))
38+
self.install_button.add_event_cb(lambda e: self.install_button_click(), lv.EVENT.CLICKED, None)
2839
install_label = lv.label(self.install_button)
2940
install_label.set_text("Update OS")
3041
install_label.center()
3142
self.status_label = lv.label(self.main_screen)
32-
self.status_label.align(lv.ALIGN.TOP_LEFT,0,mpos.ui.topmenu.NOTIFICATION_BAR_HEIGHT)
43+
self.status_label.align_to(self.force_update, lv.ALIGN.OUT_BOTTOM_LEFT, 0, mpos.ui.pct_of_display_height(5))
3344
self.setContentView(self.main_screen)
3445

3546
def onStart(self, screen):
@@ -85,20 +96,25 @@ def show_update_info(self):
8596
print("Error:", str(e))
8697

8798
def handle_update_info(self, version, download_url, changelog):
88-
label = f"Installed OS version: {mpos.info.CURRENT_OS_VERSION}\n"
99+
self.download_update_url = download_url
89100
if compare_versions(version, mpos.info.CURRENT_OS_VERSION):
90101
#if True: # for testing
91-
label += "Available new"
102+
label = "New "
92103
self.install_button.remove_state(lv.STATE.DISABLED)
93-
self.install_button.add_event_cb(lambda e, u=download_url: self.install_button_click(u), lv.EVENT.CLICKED, None)
94104
else:
95-
label += "isn't older than latest"
105+
label = "Same "
106+
if (self.force_update.get_state() & lv.STATE.CHECKED):
107+
self.install_button.remove_state(lv.STATE.DISABLED)
96108
label += f" version: {version}\n\nDetails:\n\n{changelog}"
97109
self.status_label.set_text(label)
98110

99111

100-
def install_button_click(self, download_url):
101-
print(f"install_button_click for url {download_url}")
112+
def install_button_click(self):
113+
if not self.download_update_url:
114+
print("Install button clicked but download_update_url is unknown, returning...")
115+
return
116+
else:
117+
print(f"install_button_click for url {self.download_update_url}")
102118
self.install_button.add_state(lv.STATE.DISABLED) # button will be enabled if there is an update available
103119
self.status_label.set_text("Update in progress.\nNavigate away to cancel.")
104120
self.progress_label = lv.label(self.main_screen)
@@ -111,10 +127,16 @@ def install_button_click(self, download_url):
111127
self.progress_bar.set_value(0, False)
112128
try:
113129
_thread.stack_size(mpos.apps.good_stack_size())
114-
_thread.start_new_thread(self.update_with_lvgl, (download_url,))
130+
_thread.start_new_thread(self.update_with_lvgl, (self.download_update_url,))
115131
except Exception as e:
116132
print("Could not start update_with_lvgl thread: ", e)
117133

134+
def force_update_clicked(self):
135+
if self.download_update_url and (self.force_update.get_state() & lv.STATE.CHECKED):
136+
self.install_button.remove_state(lv.STATE.DISABLED)
137+
else:
138+
self.install_button.add_state(lv.STATE.DISABLED)
139+
118140
def progress_callback(self, percent):
119141
print(f"OTA Update: {percent:.1f}%")
120142
lv.async_call(lambda l: self.progress_label.set_text(f"OTA Update: {percent:.2f}%"), None)
@@ -168,6 +190,7 @@ def update_with_lvgl(self, url):
168190
machine.reset()
169191
# In case it didn't reset:
170192
lv.async_call(lambda l: self.status_label.set_text("Update finished! Please restart."), None)
193+
# self.install_button stays disabled to prevent the user from downloading an update twice
171194

172195
# Non-class functions:
173196

0 commit comments

Comments
 (0)