Skip to content

Commit 368462f

Browse files
Fix and simplify Activity.appFullName propagation
1 parent 3df3278 commit 368462f

6 files changed

Lines changed: 68 additions & 15 deletions

File tree

internal_filesystem/lib/mpos/activity_navigator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def get_foreground_app():
1111
if mpos.ui.screen_stack:
1212
current_activity, _, _, _ = mpos.ui.screen_stack[-1]
1313
if current_activity:
14-
return getattr(current_activity, "appFullName", None)
14+
return current_activity.appFullName
1515
return None
1616

1717

internal_filesystem/lib/mpos/app/activity.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,12 @@ def setContentView(self, screen):
3232

3333
def startActivity(self, intent):
3434
from mpos.activity_navigator import ActivityNavigator
35-
app_fullname = getattr(self, "appFullName", None)
36-
if app_fullname is not None:
37-
intent.app_fullname = app_fullname
35+
intent.app_fullname = self.appFullName
3836
ActivityNavigator.startActivity(intent)
3937

4038
def startActivityForResult(self, intent, result_callback):
4139
from mpos.activity_navigator import ActivityNavigator
42-
app_fullname = getattr(self, "appFullName", None)
43-
if app_fullname is not None:
44-
intent.app_fullname = app_fullname
40+
intent.app_fullname = self.appFullName
4541
ActivityNavigator.startActivityForResult(intent, result_callback)
4642

4743
def initError(self, e):

internal_filesystem/lib/mpos/content/app_manager.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def is_installed_by_name(app_fullname):
252252
return AppManager.is_installed_by_path(f"apps/{app_fullname}") or AppManager.is_installed_by_path(f"builtin/apps/{app_fullname}")
253253

254254
@staticmethod
255-
def execute_script(script_source, is_file, classname, cwd=None):
255+
def execute_script(script_source, is_file, classname, cwd=None, app_fullname=None):
256256
"""Run the script in the current thread. Returns True if successful."""
257257
import utime # for timing read and compile
258258
import lvgl as lv
@@ -297,13 +297,14 @@ def execute_script(script_source, is_file, classname, cwd=None):
297297
print("Variables:", variables.keys())
298298
main_activity = script_globals.get(classname)
299299
if main_activity:
300-
from ..app.activity import Activity
300+
from mpos.activity_navigator import ActivityNavigator
301301
from .intent import Intent
302-
from mpos.activity_navigator import get_foreground_app
303302
start_time = utime.ticks_ms()
304-
Activity.startActivity(None, Intent(activity_class=main_activity, app_fullname=get_foreground_app()))
303+
ActivityNavigator.startActivity(
304+
Intent(activity_class=main_activity, app_fullname=app_fullname)
305+
)
305306
end_time = utime.ticks_diff(utime.ticks_ms(), start_time)
306-
print(f"execute_script: Activity.startActivity took {end_time}ms")
307+
print(f"execute_script: ActivityNavigator.startActivity took {end_time}ms")
307308
else:
308309
print(f"Warning: could not find app's main_activity {classname}")
309310
return False
@@ -341,7 +342,13 @@ def start_app(fullname):
341342
else:
342343
entrypoint = app.main_launcher_activity.get('entrypoint')
343344
classname = app.main_launcher_activity.get("classname")
344-
result = AppManager.execute_script(app.installed_path + "/" + entrypoint, True, classname, app.installed_path + "/assets/")
345+
result = AppManager.execute_script(
346+
app.installed_path + "/" + entrypoint,
347+
True,
348+
classname,
349+
app.installed_path + "/assets/",
350+
app_fullname=fullname,
351+
)
345352
# Launchers have the bar, other apps don't have it
346353
import mpos.ui
347354
if app.is_valid_launcher():

internal_filesystem/lib/mpos/testing/mocks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ def restart_launcher():
886886
return True
887887

888888
@staticmethod
889-
def execute_script(script_source, is_file, classname, cwd=None):
889+
def execute_script(script_source, is_file, classname, cwd=None, app_fullname=None):
890890
"""Mock execute_script function."""
891891
return True
892892

@@ -910,7 +910,7 @@ def restart_launcher():
910910
return True
911911

912912
@staticmethod
913-
def execute_script(script_source, is_file, classname, cwd=None):
913+
def execute_script(script_source, is_file, classname, cwd=None, app_fullname=None):
914914
"""Mock execute_script function."""
915915
return True
916916

internal_filesystem/lib/mpos/ui/topmenu.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def close_drawer(to_launcher=False):
4848
from mpos.activity_navigator import get_foreground_app
4949
drawer_open=False
5050
fg = get_foreground_app()
51+
print(f"topmenu.py foreground app: {fg}")
5152
if not to_launcher and fg is not None and not "launcher" in fg:
5253
print(f"close_drawer: also closing bar because to_launcher is {to_launcher} and foreground_app_name is {get_foreground_app()}")
5354
close_bar(False)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Graphical test to verify get_foreground_app returns the active app.
3+
"""
4+
5+
import time
6+
import unittest
7+
8+
from mpos import AppManager
9+
from mpos.ui import get_foreground_app
10+
from mpos.ui.testing import wait_for_render
11+
12+
13+
class TestForegroundApp(unittest.TestCase):
14+
"""Ensure get_foreground_app tracks the top activity in the UI stack."""
15+
16+
def _wait_for_foreground(self, expected, timeout_ms=2500):
17+
start = time.ticks_ms()
18+
while time.ticks_diff(time.ticks_ms(), start) < timeout_ms:
19+
fg = get_foreground_app()
20+
if fg == expected:
21+
return True
22+
wait_for_render(iterations=10)
23+
return False
24+
25+
def test_get_foreground_app_after_starting_apps(self):
26+
launcher_fullname = "com.micropythonos.launcher"
27+
about_fullname = "com.micropythonos.about"
28+
29+
AppManager.start_app(launcher_fullname)
30+
wait_for_render(iterations=30)
31+
self.assertTrue(
32+
self._wait_for_foreground(launcher_fullname),
33+
f"Expected foreground app to be {launcher_fullname}, got {get_foreground_app()}",
34+
)
35+
36+
AppManager.start_app(about_fullname)
37+
wait_for_render(iterations=30)
38+
self.assertTrue(
39+
self._wait_for_foreground(about_fullname),
40+
f"Expected foreground app to be {about_fullname}, got {get_foreground_app()}",
41+
)
42+
43+
fg = get_foreground_app()
44+
self.assertIsNotNone(fg, "Foreground app should not be None after start_app")
45+
self.assertEqual(
46+
fg,
47+
about_fullname,
48+
f"Foreground app mismatch: expected {about_fullname}, got {fg}",
49+
)

0 commit comments

Comments
 (0)