1111class OSUpdate (Activity ):
1212
1313keep_running = True
14+ download_update_url = None
1415
1516# Widgets:
17+ status_label = None
1618install_button = None
19+ force_update = None
1720main_screen = None
1821progress_label = None
1922progress_bar = None
2023
2124def onCreate (self ):
2225self .main_screen = lv .obj ()
2326self .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 ))
2434self .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 )
2636self .install_button .add_state (lv .STATE .DISABLED )# button will be enabled if there is an update available
2737self .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 )
2839install_label = lv .label (self .install_button )
2940install_label .set_text ("Update OS" )
3041install_label .center ()
3142self .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 ) )
3344self .setContentView (self .main_screen )
3445
3546def onStart (self ,screen ):
@@ -85,20 +96,25 @@ def show_update_info(self):
8596print ("Error:" ,str (e ))
8697
8798def 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
89100if compare_versions (version ,mpos .info .CURRENT_OS_VERSION ):
90101#if True: # for testing
91- label + ="Available new "
102+ label = "New "
92103self .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 )
94104else :
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 )
96108label += f" version:{ version } \n \n Details:\n \n { changelog } "
97109self .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 } " )
102118self .install_button .add_state (lv .STATE .DISABLED )# button will be enabled if there is an update available
103119self .status_label .set_text ("Update in progress.\n Navigate away to cancel." )
104120self .progress_label = lv .label (self .main_screen )
@@ -111,10 +127,16 @@ def install_button_click(self, download_url):
111127self .progress_bar .set_value (0 ,False )
112128try :
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 ,))
115131except Exception as e :
116132print ("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+
118140def progress_callback (self ,percent ):
119141print (f"OTA Update:{ percent :.1f} %" )
120142lv .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):
168190machine .reset ()
169191# In case it didn't reset:
170192lv .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