11
11
12
12
from flask import (
13
13
Flask ,
14
+ current_app ,
14
15
render_template ,
15
16
request ,
16
17
Markup
26
27
get_android_python_activity ,
27
28
set_device_orientation ,
28
29
setup_lifecycle_callbacks ,
30
+ skip_if_not_running_from_android_device ,
29
31
)
30
32
31
33
32
- app = Flask (__name__ )
33
- setup_lifecycle_callbacks ()
34
- service_running = False
34
+ class App (Flask ):
35
+ def __init__ (self , * args , ** kwargs ):
36
+ super ().__init__ (* args , ** kwargs )
37
+ setup_lifecycle_callbacks ()
38
+ self .service_running = False
39
+
40
+ @property
41
+ @skip_if_not_running_from_android_device
42
+ def service (self ):
43
+ from jnius import autoclass
44
+
45
+ return autoclass ('org.test.unit_tests_app.ServiceP4a_test_service' )
46
+
47
+ @skip_if_not_running_from_android_device
48
+ def service_start (self ):
49
+ activity = get_android_python_activity ()
50
+ self .service .start (activity , 'Some argument' )
51
+ self .service_running = True
52
+
53
+ @skip_if_not_running_from_android_device
54
+ def service_stop (self ):
55
+ activity = get_android_python_activity ()
56
+ self .service .stop (activity )
57
+ self .service_running = False
58
+
59
+
60
+ app = App (__name__ )
35
61
TESTS_TO_PERFORM = dict ()
36
62
NON_ANDROID_DEVICE_MSG = 'Not running from Android device'
37
63
@@ -53,34 +79,12 @@ def get_html_for_tested_modules(tested_modules, failed_tests):
53
79
return Markup (modules_text )
54
80
55
81
56
- def get_test_service ():
57
- from jnius import autoclass
58
-
59
- return autoclass ('org.test.unit_tests_app.ServiceP4a_test_service' )
60
-
61
-
62
- def start_service ():
63
- global service_running
64
- activity = get_android_python_activity ()
65
- test_service = get_test_service ()
66
- test_service .start (activity , 'Some argument' )
67
- service_running = True
68
-
69
-
70
- def stop_service ():
71
- global service_running
72
- activity = get_android_python_activity ()
73
- test_service = get_test_service ()
74
- test_service .stop (activity )
75
- service_running = False
76
-
77
-
78
82
@app .route ('/' )
79
83
def index ():
80
84
return render_template (
81
85
'index.html' ,
82
86
platform = 'Android' if RUNNING_ON_ANDROID else 'Desktop' ,
83
- service_running = service_running ,
87
+ service_running = current_app . service_running ,
84
88
)
85
89
86
90
@@ -179,7 +183,7 @@ def service():
179
183
180
184
action = args ['action' ]
181
185
if action == 'start' :
182
- start_service ()
186
+ current_app . service_start ()
183
187
else :
184
- stop_service ()
188
+ current_app . service_stop ()
185
189
return ('' , 204 )
0 commit comments