@@ -17,6 +17,7 @@ def good_stack_size():
1717 return stacksize
1818
1919# Run the script in the current thread:
20+ # Returns True if successful
2021def execute_script (script_source , is_file , cwd = None , classname = None ):
2122 import utime # for timing read and compile
2223 thread_id = _thread .get_ident ()
@@ -55,26 +56,32 @@ def execute_script(script_source, is_file, cwd=None, classname=None):
5556 #print("Classes:", classes.keys())
5657 #print("Functions:", functions.keys())
5758 #print("Variables:", variables.keys())
58- if classname :
59- main_activity = script_globals .get (classname )
60- if main_activity :
61- start_time = utime .ticks_ms ()
62- Activity .startActivity (None , Intent (activity_class = main_activity ))
63- end_time = utime .ticks_diff (utime .ticks_ms (), start_time )
64- print (f"execute_script: Activity.startActivity took { end_time } ms" )
65- else :
66- print ("Warning: could not find main_activity" )
59+ if not classname :
60+ print ("Running without a classname isn't supported right now." )
61+ return False
62+ main_activity = script_globals .get (classname )
63+ if main_activity :
64+ start_time = utime .ticks_ms ()
65+ Activity .startActivity (None , Intent (activity_class = main_activity ))
66+ end_time = utime .ticks_diff (utime .ticks_ms (), start_time )
67+ print (f"execute_script: Activity.startActivity took { end_time } ms" )
68+ else :
69+ print (f"Warning: could not find app's main_activity { main_activity } " )
70+ return False
6771 except Exception as e :
6872 print (f"Thread { thread_id } : exception during execution:" )
6973 # Print stack trace with exception type, value, and traceback
7074 tb = getattr (e , '__traceback__' , None )
7175 traceback .print_exception (type (e ), e , tb )
72- print (f"Thread { thread_id } : script { compile_name } finished" )
76+ return False
77+ print (f"Thread { thread_id } : script { compile_name } finished, restoring sys.path to { sys .path } " )
7378 sys .path = path_before
79+ return True
7480 except Exception as e :
7581 print (f"Thread { thread_id } : error:" )
7682 tb = getattr (e , '__traceback__' , None )
7783 traceback .print_exception (type (e ), e , tb )
84+ return False
7885
7986""" Unused:
8087# Run the script in a new thread:
@@ -104,6 +111,7 @@ def execute_script_new_thread(scriptname, is_file):
104111 print("main.py: execute_script_new_thread(): error starting new thread thread: ", e)
105112"""
106113
114+ # Returns True if successful
107115def start_app (fullname ):
108116 mpos .ui .set_foreground_app (fullname )
109117 import utime
@@ -119,24 +127,22 @@ def start_app(fullname):
119127 print (f"WARNING: start_app can't start { fullname } because it doesn't have a main_launcher_activity" )
120128 return
121129 start_script_fullpath = f"{ app .installed_path } /{ app .main_launcher_activity .get ('entrypoint' )} "
122- execute_script (start_script_fullpath , True , app .installed_path + "/assets/" , app .main_launcher_activity .get ("classname" ))
130+ result = execute_script (start_script_fullpath , True , app .installed_path + "/assets/" , app .main_launcher_activity .get ("classname" ))
123131 # Launchers have the bar, other apps don't have it
124132 if app .is_valid_launcher ():
125133 mpos .ui .topmenu .open_bar ()
126134 else :
127135 mpos .ui .topmenu .close_bar ()
128136 end_time = utime .ticks_diff (utime .ticks_ms (), start_time )
129137 print (f"start_app() took { end_time } ms" )
138+ return result
139+
130140
131141# Starts the first launcher that's found
132142def restart_launcher ():
133143 print ("restart_launcher" )
134144 # Stop all apps
135145 mpos .ui .remove_and_stop_all_activities ()
136146 # No need to stop the other launcher first, because it exits after building the screen
137- for app in PackageManager .get_app_list ():
138- if app .is_valid_launcher ():
139- print (f"Found launcher, starting { app .fullname } " )
140- start_app (app .fullname )
141- break
147+ return start_app (PackageManager .get_launcher ().fullname )
142148
0 commit comments