diff --git a/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonServiceIntent.java b/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonServiceIntent.java new file mode 100644 index 0000000000..4f68d6343b --- /dev/null +++ b/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonServiceIntent.java @@ -0,0 +1,173 @@ +package org.kivy.android; + +import android.content.Context; +import android.content.Intent; + +public class PythonServiceIntent { + + private PythonServiceIntent() {} + + public static Intent build( + Context ctx, + Class serviceClass, + String serviceEntrypoint, + String serviceTitle, + String pythonName, + String serviceStartAsForeground, + String pythonServiceArgument, + String smallIconName, + String contentTitle, + String contentText) { + String appRoot = PythonUtil.getAppRoot(ctx); + return buildWithPaths( + ctx, + serviceClass, + ctx.getFilesDir().getAbsolutePath(), + appRoot, + null, + serviceEntrypoint, + serviceTitle, + null, + pythonName, + serviceStartAsForeground, + pythonServiceArgument, + smallIconName, + contentTitle, + contentText); + } + + public static Intent build( + Context ctx, + Class serviceClass, + String serviceEntrypoint, + String serviceTitle, + String pythonName, + boolean serviceStartAsForeground, + String pythonServiceArgument, + String smallIconName, + String contentTitle, + String contentText) { + return build( + ctx, + serviceClass, + serviceEntrypoint, + serviceTitle, + pythonName, + booleanToString(serviceStartAsForeground), + pythonServiceArgument, + smallIconName, + contentTitle, + contentText); + } + + public static Intent buildActivityService( + Context ctx, + Class serviceClass, + String serviceEntrypoint, + String serviceTitle, + String serviceDescription, + boolean serviceStartAsForeground, + String pythonServiceArgument) { + String appRoot = PythonUtil.getAppRoot(ctx); + return buildWithPaths( + ctx, + serviceClass, + ctx.getFilesDir().getAbsolutePath(), + appRoot, + null, + serviceEntrypoint, + serviceTitle, + serviceDescription, + "python", + booleanToString(serviceStartAsForeground), + pythonServiceArgument, + null, + null, + null); + } + + public static Intent buildWithPaths( + Context ctx, + Class serviceClass, + String androidPrivate, + String androidArgument, + String androidUnpack, + String serviceEntrypoint, + String serviceTitle, + String serviceDescription, + String pythonName, + String serviceStartAsForeground, + String pythonServiceArgument, + String smallIconName, + String contentTitle, + String contentText) { + Intent intent = new Intent(ctx, serviceClass); + intent.putExtra("androidPrivate", androidPrivate); + intent.putExtra("androidArgument", androidArgument); + intent.putExtra("serviceEntrypoint", serviceEntrypoint); + intent.putExtra("pythonName", pythonName); + intent.putExtra("serviceStartAsForeground", serviceStartAsForeground); + intent.putExtra("pythonHome", androidArgument); + intent.putExtra("pythonPath", androidArgument + ":" + androidArgument + "/lib"); + intent.putExtra("pythonServiceArgument", pythonServiceArgument); + intent.putExtra("serviceTitle", serviceTitle); + + if (androidUnpack != null) { + intent.putExtra("androidUnpack", androidUnpack); + } + if (serviceDescription != null) { + intent.putExtra("serviceDescription", serviceDescription); + } + if (smallIconName != null) { + intent.putExtra("smallIconName", smallIconName); + } + if (contentTitle != null) { + intent.putExtra("contentTitle", contentTitle); + } + if (contentText != null) { + intent.putExtra("contentText", contentText); + } + + return intent; + } + + public static Intent buildWithPaths( + Context ctx, + Class serviceClass, + String androidPrivate, + String androidArgument, + String androidUnpack, + String serviceEntrypoint, + String serviceTitle, + String serviceDescription, + String pythonName, + boolean serviceStartAsForeground, + String pythonServiceArgument, + String smallIconName, + String contentTitle, + String contentText) { + return buildWithPaths( + ctx, + serviceClass, + androidPrivate, + androidArgument, + androidUnpack, + serviceEntrypoint, + serviceTitle, + serviceDescription, + pythonName, + booleanToString(serviceStartAsForeground), + pythonServiceArgument, + smallIconName, + contentTitle, + contentText); + } + + public static void stop(Context ctx, Class serviceClass) { + ctx.stopService(new Intent(ctx, serviceClass)); + } + + private static String booleanToString(boolean value) { + return value ? "true" : "false"; + } +} diff --git a/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java index bbce7bed4f..fdab9d963f 100644 --- a/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java @@ -3,6 +3,7 @@ import android.content.Intent; import android.content.Context; import {{ args.service_class_name }}; +import org.kivy.android.PythonServiceIntent; public class Service{{ name|capitalize }} extends {{ base_service_class }} { @@ -39,21 +40,17 @@ static public void start(Context ctx, String smallIconName, static public Intent getDefaultIntent(Context ctx, String smallIconName, String contentTitle, String contentText, String pythonServiceArgument) { - Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class); - String argument = ctx.getFilesDir().getAbsolutePath() + "/app"; - intent.putExtra("androidPrivate", ctx.getFilesDir().getAbsolutePath()); - intent.putExtra("androidArgument", argument); - intent.putExtra("serviceTitle", "{{ args.name }}"); - intent.putExtra("serviceEntrypoint", "{{ entrypoint }}"); - intent.putExtra("pythonName", "{{ name }}"); - intent.putExtra("serviceStartAsForeground", "{{ foreground|lower }}"); - intent.putExtra("pythonHome", argument); - intent.putExtra("pythonPath", argument + ":" + argument + "/lib"); - intent.putExtra("pythonServiceArgument", pythonServiceArgument); - intent.putExtra("smallIconName", smallIconName); - intent.putExtra("contentTitle", contentTitle); - intent.putExtra("contentText", contentText); - return intent; + return PythonServiceIntent.build( + ctx, + Service{{ name|capitalize }}.class, + "{{ entrypoint }}", + "{{ args.name }}", + "{{ name }}", + "{{ foreground|lower }}", + pythonServiceArgument, + smallIconName, + contentTitle, + contentText); } @Override @@ -63,7 +60,6 @@ protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument) } static public void stop(Context ctx) { - Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class); - ctx.stopService(intent); + PythonServiceIntent.stop(ctx, Service{{ name|capitalize }}.class); } } diff --git a/pythonforandroid/bootstraps/qt/build/src/main/java/org/kivy/android/PythonActivity.java b/pythonforandroid/bootstraps/qt/build/src/main/java/org/kivy/android/PythonActivity.java index 01bdd96805..9c1415db0c 100644 --- a/pythonforandroid/bootstraps/qt/build/src/main/java/org/kivy/android/PythonActivity.java +++ b/pythonforandroid/bootstraps/qt/build/src/main/java/org/kivy/android/PythonActivity.java @@ -216,26 +216,21 @@ public static void _do_start_service( String serviceDescription, String pythonServiceArgument, boolean showForegroundNotification) { - Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class); - String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath(); String app_root_dir = PythonActivity.mActivity.getAppRoot(); String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service"); - serviceIntent.putExtra("androidPrivate", argument); - serviceIntent.putExtra("androidArgument", app_root_dir); - serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point); - serviceIntent.putExtra("pythonName", "python"); - serviceIntent.putExtra("pythonHome", app_root_dir); - serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib"); - serviceIntent.putExtra( - "serviceStartAsForeground", (showForegroundNotification ? "true" : "false")); - serviceIntent.putExtra("serviceTitle", serviceTitle); - serviceIntent.putExtra("serviceDescription", serviceDescription); - serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument); + Intent serviceIntent = + PythonServiceIntent.buildActivityService( + PythonActivity.mActivity, + PythonService.class, + "service/" + entry_point, + serviceTitle, + serviceDescription, + showForegroundNotification, + pythonServiceArgument); PythonActivity.mActivity.startService(serviceIntent); } public static void stop_service() { - Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class); - PythonActivity.mActivity.stopService(serviceIntent); + PythonServiceIntent.stop(PythonActivity.mActivity, PythonService.class); } } diff --git a/pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonActivity.java b/pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonActivity.java index 04d3eee30a..a5d494213e 100644 --- a/pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonActivity.java +++ b/pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonActivity.java @@ -302,27 +302,22 @@ public static void _do_start_service( String serviceDescription, String pythonServiceArgument, boolean showForegroundNotification) { - Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class); - String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath(); String app_root_dir = PythonActivity.mActivity.getAppRoot(); String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service"); - serviceIntent.putExtra("androidPrivate", argument); - serviceIntent.putExtra("androidArgument", app_root_dir); - serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point); - serviceIntent.putExtra("pythonName", "python"); - serviceIntent.putExtra("pythonHome", app_root_dir); - serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib"); - serviceIntent.putExtra( - "serviceStartAsForeground", (showForegroundNotification ? "true" : "false")); - serviceIntent.putExtra("serviceTitle", serviceTitle); - serviceIntent.putExtra("serviceDescription", serviceDescription); - serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument); + Intent serviceIntent = + PythonServiceIntent.buildActivityService( + PythonActivity.mActivity, + PythonService.class, + "service/" + entry_point, + serviceTitle, + serviceDescription, + showForegroundNotification, + pythonServiceArgument); PythonActivity.mActivity.startService(serviceIntent); } public static void stop_service() { - Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class); - PythonActivity.mActivity.stopService(serviceIntent); + PythonServiceIntent.stop(PythonActivity.mActivity, PythonService.class); } /** Loading screen view * */ diff --git a/pythonforandroid/bootstraps/sdl3/build/src/main/java/org/kivy/android/PythonActivity.java b/pythonforandroid/bootstraps/sdl3/build/src/main/java/org/kivy/android/PythonActivity.java index 8feed58ee4..abc4afbf15 100644 --- a/pythonforandroid/bootstraps/sdl3/build/src/main/java/org/kivy/android/PythonActivity.java +++ b/pythonforandroid/bootstraps/sdl3/build/src/main/java/org/kivy/android/PythonActivity.java @@ -301,27 +301,22 @@ public static void _do_start_service( String serviceDescription, String pythonServiceArgument, boolean showForegroundNotification) { - Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class); - String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath(); String app_root_dir = PythonActivity.mActivity.getAppRoot(); String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service"); - serviceIntent.putExtra("androidPrivate", argument); - serviceIntent.putExtra("androidArgument", app_root_dir); - serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point); - serviceIntent.putExtra("pythonName", "python"); - serviceIntent.putExtra("pythonHome", app_root_dir); - serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib"); - serviceIntent.putExtra( - "serviceStartAsForeground", (showForegroundNotification ? "true" : "false")); - serviceIntent.putExtra("serviceTitle", serviceTitle); - serviceIntent.putExtra("serviceDescription", serviceDescription); - serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument); + Intent serviceIntent = + PythonServiceIntent.buildActivityService( + PythonActivity.mActivity, + PythonService.class, + "service/" + entry_point, + serviceTitle, + serviceDescription, + showForegroundNotification, + pythonServiceArgument); PythonActivity.mActivity.startService(serviceIntent); } public static void stop_service() { - Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class); - PythonActivity.mActivity.stopService(serviceIntent); + PythonServiceIntent.stop(PythonActivity.mActivity, PythonService.class); } /** Loading screen view * */ diff --git a/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java index ff889b462c..ea00bc8d7e 100644 --- a/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java @@ -9,6 +9,7 @@ import android.util.Log; import org.kivy.android.PythonService; +import org.kivy.android.PythonServiceIntent; import org.kivy.android.PythonUtil; public class Service{{ name|capitalize }} extends PythonService { @@ -69,21 +70,21 @@ static public Intent getDefaultIntent(Context ctx, String smallIconName, String contentText, String pythonServiceArgument) { String appRoot = PythonUtil.getAppRoot(ctx); - Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class); - intent.putExtra("androidPrivate", appRoot); - intent.putExtra("androidArgument", appRoot); - intent.putExtra("serviceEntrypoint", "{{ entrypoint }}"); - intent.putExtra("serviceTitle", "{{ name|capitalize }}"); - intent.putExtra("pythonName", "{{ name }}"); - intent.putExtra("serviceStartAsForeground", "{{ foreground|lower }}"); - intent.putExtra("pythonHome", appRoot); - intent.putExtra("androidUnpack", appRoot); - intent.putExtra("pythonPath", appRoot + ":" + appRoot + "/lib"); - intent.putExtra("pythonServiceArgument", pythonServiceArgument); - intent.putExtra("smallIconName", smallIconName); - intent.putExtra("contentTitle", contentTitle); - intent.putExtra("contentText", contentText); - return intent; + return PythonServiceIntent.buildWithPaths( + ctx, + Service{{ name|capitalize }}.class, + appRoot, + appRoot, + appRoot, + "{{ entrypoint }}", + "{{ name|capitalize }}", + null, + "{{ name }}", + "{{ foreground|lower }}", + pythonServiceArgument, + smallIconName, + contentTitle, + contentText); } @Override @@ -95,8 +96,7 @@ protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument) static public void stop(Context ctx) { - Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class); - ctx.stopService(intent); + PythonServiceIntent.stop(ctx, Service{{ name|capitalize }}.class); } } diff --git a/pythonforandroid/bootstraps/service_only/build/src/main/java/org/kivy/android/PythonActivity.java b/pythonforandroid/bootstraps/service_only/build/src/main/java/org/kivy/android/PythonActivity.java index 5d532de29a..9bffcc9b28 100644 --- a/pythonforandroid/bootstraps/service_only/build/src/main/java/org/kivy/android/PythonActivity.java +++ b/pythonforandroid/bootstraps/service_only/build/src/main/java/org/kivy/android/PythonActivity.java @@ -286,27 +286,22 @@ public static void _do_start_service( String serviceDescription, String pythonServiceArgument, boolean showForegroundNotification) { - Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class); - String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath(); String app_root_dir = PythonActivity.mActivity.getAppRoot(); String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service"); - serviceIntent.putExtra("androidPrivate", argument); - serviceIntent.putExtra("androidArgument", app_root_dir); - serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point); - serviceIntent.putExtra("pythonName", "python"); - serviceIntent.putExtra("pythonHome", app_root_dir); - serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib"); - serviceIntent.putExtra( - "serviceStartAsForeground", (showForegroundNotification ? "true" : "false")); - serviceIntent.putExtra("serviceTitle", serviceTitle); - serviceIntent.putExtra("serviceDescription", serviceDescription); - serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument); + Intent serviceIntent = + PythonServiceIntent.buildActivityService( + PythonActivity.mActivity, + PythonService.class, + "service/" + entry_point, + serviceTitle, + serviceDescription, + showForegroundNotification, + pythonServiceArgument); PythonActivity.mActivity.startService(serviceIntent); } public static void stop_service() { - Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class); - PythonActivity.mActivity.stopService(serviceIntent); + PythonServiceIntent.stop(PythonActivity.mActivity, PythonService.class); } public static native void nativeSetenv(String name, String value); diff --git a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java index eeda810bef..853262a1c6 100644 --- a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java @@ -5,6 +5,8 @@ import android.content.Intent; import android.content.Context; import org.kivy.android.PythonService; +import org.kivy.android.PythonServiceIntent; +import org.kivy.android.PythonUtil; public class Service{{ name|capitalize }} extends PythonService { /** @@ -28,21 +30,8 @@ protected int getServiceId() { } public static void start(Context ctx, String pythonServiceArgument) { - String argument = ctx.getFilesDir().getAbsolutePath() + "/app"; - Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class); - intent.putExtra("androidPrivate", argument); - intent.putExtra("androidArgument", argument); - intent.putExtra("serviceEntrypoint", "{{ entrypoint }}"); - intent.putExtra("serviceTitle", "{{ name|capitalize }}"); - intent.putExtra("pythonName", "{{ name }}"); - intent.putExtra("serviceStartAsForeground", "{{ foreground|lower }}"); - intent.putExtra("pythonHome", argument); - intent.putExtra("androidUnpack", argument); - intent.putExtra("pythonPath", argument + ":" + argument + "/lib"); - intent.putExtra("pythonServiceArgument", pythonServiceArgument); - intent.putExtra("smallIconName", ""); - intent.putExtra("contentTitle", "{{ name|capitalize }}"); - intent.putExtra("contentText", ""); + Intent intent = getDefaultIntent(ctx, "", "{{ name|capitalize }}", "", + pythonServiceArgument); ctx.startService(intent); } @@ -50,27 +39,34 @@ public static void start(Context ctx, String smallIconName, String contentTitle, String contentText, String pythonServiceArgument) { - String argument = ctx.getFilesDir().getAbsolutePath() + "/app"; - Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class); - intent.putExtra("androidPrivate", argument); - intent.putExtra("androidArgument", argument); - intent.putExtra("serviceEntrypoint", "{{ entrypoint }}"); - intent.putExtra("serviceTitle", "{{ name|capitalize }}"); - intent.putExtra("pythonName", "{{ name }}"); - intent.putExtra("serviceStartAsForeground", "{{ foreground|lower }}"); - intent.putExtra("pythonHome", argument); - intent.putExtra("androidUnpack", argument); - intent.putExtra("pythonPath", argument + ":" + argument + "/lib"); - intent.putExtra("pythonServiceArgument", pythonServiceArgument); - intent.putExtra("smallIconName", smallIconName); - intent.putExtra("contentTitle", contentTitle); - intent.putExtra("contentText", contentText); + Intent intent = getDefaultIntent(ctx, smallIconName, contentTitle, + contentText, pythonServiceArgument); ctx.startService(intent); } + + public static Intent getDefaultIntent(Context ctx, String smallIconName, + String contentTitle, String contentText, + String pythonServiceArgument) { + String appRoot = PythonUtil.getAppRoot(ctx); + return PythonServiceIntent.buildWithPaths( + ctx, + Service{{ name|capitalize }}.class, + appRoot, + appRoot, + appRoot, + "{{ entrypoint }}", + "{{ name|capitalize }}", + null, + "{{ name }}", + "{{ foreground|lower }}", + pythonServiceArgument, + smallIconName, + contentTitle, + contentText); + } public static void stop(Context ctx) { - Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class); - ctx.stopService(intent); + PythonServiceIntent.stop(ctx, Service{{ name|capitalize }}.class); } /** diff --git a/pythonforandroid/bootstraps/webview/build/src/main/java/org/kivy/android/PythonActivity.java b/pythonforandroid/bootstraps/webview/build/src/main/java/org/kivy/android/PythonActivity.java index 9ad9503a6f..f0e384adaa 100644 --- a/pythonforandroid/bootstraps/webview/build/src/main/java/org/kivy/android/PythonActivity.java +++ b/pythonforandroid/bootstraps/webview/build/src/main/java/org/kivy/android/PythonActivity.java @@ -463,27 +463,22 @@ public static void _do_start_service( String serviceDescription, String pythonServiceArgument, boolean showForegroundNotification) { - Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class); - String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath(); String app_root_dir = PythonActivity.mActivity.getAppRoot(); String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service"); - serviceIntent.putExtra("androidPrivate", argument); - serviceIntent.putExtra("androidArgument", app_root_dir); - serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point); - serviceIntent.putExtra("pythonName", "python"); - serviceIntent.putExtra("pythonHome", app_root_dir); - serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib"); - serviceIntent.putExtra( - "serviceStartAsForeground", (showForegroundNotification ? "true" : "false")); - serviceIntent.putExtra("serviceTitle", serviceTitle); - serviceIntent.putExtra("serviceDescription", serviceDescription); - serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument); + Intent serviceIntent = + PythonServiceIntent.buildActivityService( + PythonActivity.mActivity, + PythonService.class, + "service/" + entry_point, + serviceTitle, + serviceDescription, + showForegroundNotification, + pythonServiceArgument); PythonActivity.mActivity.startService(serviceIntent); } public static void stop_service() { - Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class); - PythonActivity.mActivity.stopService(serviceIntent); + PythonServiceIntent.stop(PythonActivity.mActivity, PythonService.class); } public static native void nativeSetenv(String name, String value);