Skip to content

Commit 213d4ba

Browse files
committed
♻️ Deduplicate Java service intent setup
Add a shared PythonServiceIntent helper for building Python service intents and stopping services. Migrate activity service-start methods and generated service templates to delegate intent construction while preserving public APIs, service-only Binder behavior, and service-library foreground startup behavior.
1 parent 98a1063 commit 213d4ba

9 files changed

Lines changed: 281 additions & 141 deletions

File tree

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package org.kivy.android;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
6+
public class PythonServiceIntent {
7+
8+
private PythonServiceIntent() {}
9+
10+
public static Intent build(
11+
Context ctx,
12+
Class<?> serviceClass,
13+
String serviceEntrypoint,
14+
String serviceTitle,
15+
String pythonName,
16+
String serviceStartAsForeground,
17+
String pythonServiceArgument,
18+
String smallIconName,
19+
String contentTitle,
20+
String contentText) {
21+
String appRoot = PythonUtil.getAppRoot(ctx);
22+
return buildWithPaths(
23+
ctx,
24+
serviceClass,
25+
ctx.getFilesDir().getAbsolutePath(),
26+
appRoot,
27+
null,
28+
serviceEntrypoint,
29+
serviceTitle,
30+
null,
31+
pythonName,
32+
serviceStartAsForeground,
33+
pythonServiceArgument,
34+
smallIconName,
35+
contentTitle,
36+
contentText);
37+
}
38+
39+
public static Intent build(
40+
Context ctx,
41+
Class<?> serviceClass,
42+
String serviceEntrypoint,
43+
String serviceTitle,
44+
String pythonName,
45+
boolean serviceStartAsForeground,
46+
String pythonServiceArgument,
47+
String smallIconName,
48+
String contentTitle,
49+
String contentText) {
50+
return build(
51+
ctx,
52+
serviceClass,
53+
serviceEntrypoint,
54+
serviceTitle,
55+
pythonName,
56+
booleanToString(serviceStartAsForeground),
57+
pythonServiceArgument,
58+
smallIconName,
59+
contentTitle,
60+
contentText);
61+
}
62+
63+
public static Intent buildActivityService(
64+
Context ctx,
65+
Class<?> serviceClass,
66+
String serviceEntrypoint,
67+
String serviceTitle,
68+
String serviceDescription,
69+
boolean serviceStartAsForeground,
70+
String pythonServiceArgument) {
71+
String appRoot = PythonUtil.getAppRoot(ctx);
72+
return buildWithPaths(
73+
ctx,
74+
serviceClass,
75+
ctx.getFilesDir().getAbsolutePath(),
76+
appRoot,
77+
null,
78+
serviceEntrypoint,
79+
serviceTitle,
80+
serviceDescription,
81+
"python",
82+
booleanToString(serviceStartAsForeground),
83+
pythonServiceArgument,
84+
null,
85+
null,
86+
null);
87+
}
88+
89+
public static Intent buildWithPaths(
90+
Context ctx,
91+
Class<?> serviceClass,
92+
String androidPrivate,
93+
String androidArgument,
94+
String androidUnpack,
95+
String serviceEntrypoint,
96+
String serviceTitle,
97+
String serviceDescription,
98+
String pythonName,
99+
String serviceStartAsForeground,
100+
String pythonServiceArgument,
101+
String smallIconName,
102+
String contentTitle,
103+
String contentText) {
104+
Intent intent = new Intent(ctx, serviceClass);
105+
intent.putExtra("androidPrivate", androidPrivate);
106+
intent.putExtra("androidArgument", androidArgument);
107+
intent.putExtra("serviceEntrypoint", serviceEntrypoint);
108+
intent.putExtra("pythonName", pythonName);
109+
intent.putExtra("serviceStartAsForeground", serviceStartAsForeground);
110+
intent.putExtra("pythonHome", androidArgument);
111+
intent.putExtra("pythonPath", androidArgument + ":" + androidArgument + "/lib");
112+
intent.putExtra("pythonServiceArgument", pythonServiceArgument);
113+
intent.putExtra("serviceTitle", serviceTitle);
114+
115+
if (androidUnpack != null) {
116+
intent.putExtra("androidUnpack", androidUnpack);
117+
}
118+
if (serviceDescription != null) {
119+
intent.putExtra("serviceDescription", serviceDescription);
120+
}
121+
if (smallIconName != null) {
122+
intent.putExtra("smallIconName", smallIconName);
123+
}
124+
if (contentTitle != null) {
125+
intent.putExtra("contentTitle", contentTitle);
126+
}
127+
if (contentText != null) {
128+
intent.putExtra("contentText", contentText);
129+
}
130+
131+
return intent;
132+
}
133+
134+
public static Intent buildWithPaths(
135+
Context ctx,
136+
Class<?> serviceClass,
137+
String androidPrivate,
138+
String androidArgument,
139+
String androidUnpack,
140+
String serviceEntrypoint,
141+
String serviceTitle,
142+
String serviceDescription,
143+
String pythonName,
144+
boolean serviceStartAsForeground,
145+
String pythonServiceArgument,
146+
String smallIconName,
147+
String contentTitle,
148+
String contentText) {
149+
return buildWithPaths(
150+
ctx,
151+
serviceClass,
152+
androidPrivate,
153+
androidArgument,
154+
androidUnpack,
155+
serviceEntrypoint,
156+
serviceTitle,
157+
serviceDescription,
158+
pythonName,
159+
booleanToString(serviceStartAsForeground),
160+
pythonServiceArgument,
161+
smallIconName,
162+
contentTitle,
163+
contentText);
164+
}
165+
166+
public static void stop(Context ctx, Class<?> serviceClass) {
167+
ctx.stopService(new Intent(ctx, serviceClass));
168+
}
169+
170+
private static String booleanToString(boolean value) {
171+
return value ? "true" : "false";
172+
}
173+
}

pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Intent;
44
import android.content.Context;
55
import {{ args.service_class_name }};
6+
import org.kivy.android.PythonServiceIntent;
67

78

89
public class Service{{ name|capitalize }} extends {{ base_service_class }} {
@@ -39,21 +40,17 @@ static public void start(Context ctx, String smallIconName,
3940
static public Intent getDefaultIntent(Context ctx, String smallIconName,
4041
String contentTitle, String contentText,
4142
String pythonServiceArgument) {
42-
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
43-
String argument = ctx.getFilesDir().getAbsolutePath() + "/app";
44-
intent.putExtra("androidPrivate", ctx.getFilesDir().getAbsolutePath());
45-
intent.putExtra("androidArgument", argument);
46-
intent.putExtra("serviceTitle", "{{ args.name }}");
47-
intent.putExtra("serviceEntrypoint", "{{ entrypoint }}");
48-
intent.putExtra("pythonName", "{{ name }}");
49-
intent.putExtra("serviceStartAsForeground", "{{ foreground|lower }}");
50-
intent.putExtra("pythonHome", argument);
51-
intent.putExtra("pythonPath", argument + ":" + argument + "/lib");
52-
intent.putExtra("pythonServiceArgument", pythonServiceArgument);
53-
intent.putExtra("smallIconName", smallIconName);
54-
intent.putExtra("contentTitle", contentTitle);
55-
intent.putExtra("contentText", contentText);
56-
return intent;
43+
return PythonServiceIntent.build(
44+
ctx,
45+
Service{{ name|capitalize }}.class,
46+
"{{ entrypoint }}",
47+
"{{ args.name }}",
48+
"{{ name }}",
49+
"{{ foreground|lower }}",
50+
pythonServiceArgument,
51+
smallIconName,
52+
contentTitle,
53+
contentText);
5754
}
5855

5956
@Override
@@ -63,7 +60,6 @@ protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument)
6360
}
6461

6562
static public void stop(Context ctx) {
66-
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
67-
ctx.stopService(intent);
63+
PythonServiceIntent.stop(ctx, Service{{ name|capitalize }}.class);
6864
}
6965
}

pythonforandroid/bootstraps/qt/build/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,26 +216,21 @@ public static void _do_start_service(
216216
String serviceDescription,
217217
String pythonServiceArgument,
218218
boolean showForegroundNotification) {
219-
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
220-
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
221219
String app_root_dir = PythonActivity.mActivity.getAppRoot();
222220
String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service");
223-
serviceIntent.putExtra("androidPrivate", argument);
224-
serviceIntent.putExtra("androidArgument", app_root_dir);
225-
serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point);
226-
serviceIntent.putExtra("pythonName", "python");
227-
serviceIntent.putExtra("pythonHome", app_root_dir);
228-
serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib");
229-
serviceIntent.putExtra(
230-
"serviceStartAsForeground", (showForegroundNotification ? "true" : "false"));
231-
serviceIntent.putExtra("serviceTitle", serviceTitle);
232-
serviceIntent.putExtra("serviceDescription", serviceDescription);
233-
serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument);
221+
Intent serviceIntent =
222+
PythonServiceIntent.buildActivityService(
223+
PythonActivity.mActivity,
224+
PythonService.class,
225+
"service/" + entry_point,
226+
serviceTitle,
227+
serviceDescription,
228+
showForegroundNotification,
229+
pythonServiceArgument);
234230
PythonActivity.mActivity.startService(serviceIntent);
235231
}
236232

237233
public static void stop_service() {
238-
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
239-
PythonActivity.mActivity.stopService(serviceIntent);
234+
PythonServiceIntent.stop(PythonActivity.mActivity, PythonService.class);
240235
}
241236
}

pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -302,27 +302,22 @@ public static void _do_start_service(
302302
String serviceDescription,
303303
String pythonServiceArgument,
304304
boolean showForegroundNotification) {
305-
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
306-
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
307305
String app_root_dir = PythonActivity.mActivity.getAppRoot();
308306
String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service");
309-
serviceIntent.putExtra("androidPrivate", argument);
310-
serviceIntent.putExtra("androidArgument", app_root_dir);
311-
serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point);
312-
serviceIntent.putExtra("pythonName", "python");
313-
serviceIntent.putExtra("pythonHome", app_root_dir);
314-
serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib");
315-
serviceIntent.putExtra(
316-
"serviceStartAsForeground", (showForegroundNotification ? "true" : "false"));
317-
serviceIntent.putExtra("serviceTitle", serviceTitle);
318-
serviceIntent.putExtra("serviceDescription", serviceDescription);
319-
serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument);
307+
Intent serviceIntent =
308+
PythonServiceIntent.buildActivityService(
309+
PythonActivity.mActivity,
310+
PythonService.class,
311+
"service/" + entry_point,
312+
serviceTitle,
313+
serviceDescription,
314+
showForegroundNotification,
315+
pythonServiceArgument);
320316
PythonActivity.mActivity.startService(serviceIntent);
321317
}
322318

323319
public static void stop_service() {
324-
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
325-
PythonActivity.mActivity.stopService(serviceIntent);
320+
PythonServiceIntent.stop(PythonActivity.mActivity, PythonService.class);
326321
}
327322

328323
/** Loading screen view * */

pythonforandroid/bootstraps/sdl3/build/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -301,27 +301,22 @@ public static void _do_start_service(
301301
String serviceDescription,
302302
String pythonServiceArgument,
303303
boolean showForegroundNotification) {
304-
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
305-
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
306304
String app_root_dir = PythonActivity.mActivity.getAppRoot();
307305
String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service");
308-
serviceIntent.putExtra("androidPrivate", argument);
309-
serviceIntent.putExtra("androidArgument", app_root_dir);
310-
serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point);
311-
serviceIntent.putExtra("pythonName", "python");
312-
serviceIntent.putExtra("pythonHome", app_root_dir);
313-
serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib");
314-
serviceIntent.putExtra(
315-
"serviceStartAsForeground", (showForegroundNotification ? "true" : "false"));
316-
serviceIntent.putExtra("serviceTitle", serviceTitle);
317-
serviceIntent.putExtra("serviceDescription", serviceDescription);
318-
serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument);
306+
Intent serviceIntent =
307+
PythonServiceIntent.buildActivityService(
308+
PythonActivity.mActivity,
309+
PythonService.class,
310+
"service/" + entry_point,
311+
serviceTitle,
312+
serviceDescription,
313+
showForegroundNotification,
314+
pythonServiceArgument);
319315
PythonActivity.mActivity.startService(serviceIntent);
320316
}
321317

322318
public static void stop_service() {
323-
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
324-
PythonActivity.mActivity.stopService(serviceIntent);
319+
PythonServiceIntent.stop(PythonActivity.mActivity, PythonService.class);
325320
}
326321

327322
/** Loading screen view * */

pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.util.Log;
1010

1111
import org.kivy.android.PythonService;
12+
import org.kivy.android.PythonServiceIntent;
1213
import org.kivy.android.PythonUtil;
1314

1415
public class Service{{ name|capitalize }} extends PythonService {
@@ -69,21 +70,21 @@ static public Intent getDefaultIntent(Context ctx, String smallIconName,
6970
String contentText,
7071
String pythonServiceArgument) {
7172
String appRoot = PythonUtil.getAppRoot(ctx);
72-
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
73-
intent.putExtra("androidPrivate", appRoot);
74-
intent.putExtra("androidArgument", appRoot);
75-
intent.putExtra("serviceEntrypoint", "{{ entrypoint }}");
76-
intent.putExtra("serviceTitle", "{{ name|capitalize }}");
77-
intent.putExtra("pythonName", "{{ name }}");
78-
intent.putExtra("serviceStartAsForeground", "{{ foreground|lower }}");
79-
intent.putExtra("pythonHome", appRoot);
80-
intent.putExtra("androidUnpack", appRoot);
81-
intent.putExtra("pythonPath", appRoot + ":" + appRoot + "/lib");
82-
intent.putExtra("pythonServiceArgument", pythonServiceArgument);
83-
intent.putExtra("smallIconName", smallIconName);
84-
intent.putExtra("contentTitle", contentTitle);
85-
intent.putExtra("contentText", contentText);
86-
return intent;
73+
return PythonServiceIntent.buildWithPaths(
74+
ctx,
75+
Service{{ name|capitalize }}.class,
76+
appRoot,
77+
appRoot,
78+
appRoot,
79+
"{{ entrypoint }}",
80+
"{{ name|capitalize }}",
81+
null,
82+
"{{ name }}",
83+
"{{ foreground|lower }}",
84+
pythonServiceArgument,
85+
smallIconName,
86+
contentTitle,
87+
contentText);
8788
}
8889

8990
@Override
@@ -95,8 +96,7 @@ protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument)
9596

9697

9798
static public void stop(Context ctx) {
98-
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
99-
ctx.stopService(intent);
99+
PythonServiceIntent.stop(ctx, Service{{ name|capitalize }}.class);
100100
}
101101

102102
}

0 commit comments

Comments
 (0)