From 3d5425cce02a65d5f792eea5b8ad1ad9221e9f8f Mon Sep 17 00:00:00 2001 From: RobertF <34464649+RobertFlatt@users.noreply.github.com> Date: Sat, 8 Oct 2022 15:24:03 -1000 Subject: [PATCH 01/16] add_resources --- pythonforandroid/toolchain.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pythonforandroid/toolchain.py b/pythonforandroid/toolchain.py index 1b81aa923c..feadb89e59 100644 --- a/pythonforandroid/toolchain.py +++ b/pythonforandroid/toolchain.py @@ -512,6 +512,10 @@ def add_parser(subparsers, *args, **kwargs): '--add-asset', dest='assets', action="append", default=[], help='Put this in the assets folder in the apk.') + parser_packaging.add_argument( + '--add-resource', dest='resources', + action="append", default=[], + help='Put this in the res folder in the apk.') parser_packaging.add_argument( '--private', dest='private', help='the directory with the app source code files' + @@ -1000,6 +1004,13 @@ def _fix_args(args): asset_src = asset_dest = asset # take abspath now, because build.py will be run in bootstrap dir unknown_args += ["--asset", os.path.abspath(asset_src)+":"+asset_dest] + for resource in args.resources: + if ":" in resource: + resource_src, resource_dest = resource.split(":") + else: + resource_src = resource_dest = resource + # take abspath now, because build.py will be run in bootstrap dir + unknown_args += ["--resource", os.path.abspath(resource_src)+":"+resource_dest] for i, arg in enumerate(unknown_args): argx = arg.split('=') if argx[0] in fix_args: From 9cb1ab693ef033317194af29ef3a3b64eccdb1d4 Mon Sep 17 00:00:00 2001 From: RobertF <34464649+RobertFlatt@users.noreply.github.com> Date: Sat, 8 Oct 2022 15:45:20 -1000 Subject: [PATCH 02/16] add_resource --- pythonforandroid/bootstraps/common/build/build.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pythonforandroid/bootstraps/common/build/build.py b/pythonforandroid/bootstraps/common/build/build.py index 6885a333df..6b38ca4b3b 100644 --- a/pythonforandroid/bootstraps/common/build/build.py +++ b/pythonforandroid/bootstraps/common/build/build.py @@ -340,6 +340,17 @@ def make_package(args): # Prepare some variables for templating process res_dir = "src/main/res" + # Add user resouces + shutil.rmtree(res_dir, ignore_errors=True) + ensure_dir(res_dir) + for resource in args.resources: + resource_src, resource_dest = resource.split(":") + if isfile(realpath(resource_src)): + ensure_dir(dirname(join(res_dir, resource_dest))) + shutil.copy(realpath(resource_src), join(res_dir, resource_dest)) + else: + shutil.copytree(realpath(resource_src), join(res_dir, resource_dest)) + default_icon = 'templates/kivy-icon.png' default_presplash = 'templates/kivy-presplash.jpg' shutil.copy( @@ -698,6 +709,10 @@ def parse_args_and_make_package(args=None): action="append", default=[], metavar="/path/to/source:dest", help='Put this in the assets folder at assets/dest') + ap.add_argument('--resource', dest='resources', + action="append", default=[], + metavar="/path/to/source:kind/asset", + help='Put this in the res folder at res/kind') ap.add_argument('--icon', dest='icon', help=('A png file to use as the icon for ' 'the application.')) From ed8b865511495ca3e74e4cfa2c5693f8b12d48bd Mon Sep 17 00:00:00 2001 From: RobertF <34464649+RobertFlatt@users.noreply.github.com> Date: Sat, 8 Oct 2022 16:31:57 -1000 Subject: [PATCH 03/16] Update build.py --- pythonforandroid/bootstraps/common/build/build.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pythonforandroid/bootstraps/common/build/build.py b/pythonforandroid/bootstraps/common/build/build.py index 6b38ca4b3b..7d1984b642 100644 --- a/pythonforandroid/bootstraps/common/build/build.py +++ b/pythonforandroid/bootstraps/common/build/build.py @@ -341,8 +341,6 @@ def make_package(args): # Prepare some variables for templating process res_dir = "src/main/res" # Add user resouces - shutil.rmtree(res_dir, ignore_errors=True) - ensure_dir(res_dir) for resource in args.resources: resource_src, resource_dest = resource.split(":") if isfile(realpath(resource_src)): From 20ccb7c04975e7e7393f7cb51100c29d16c38924 Mon Sep 17 00:00:00 2001 From: RobertFlatt <34464649+RobertFlatt@users.noreply.github.com> Date: Wed, 12 Oct 2022 17:10:33 -1000 Subject: [PATCH 04/16] stateless --- pythonforandroid/bootstraps/common/build/build.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pythonforandroid/bootstraps/common/build/build.py b/pythonforandroid/bootstraps/common/build/build.py index 7d1984b642..c9a992fc00 100644 --- a/pythonforandroid/bootstraps/common/build/build.py +++ b/pythonforandroid/bootstraps/common/build/build.py @@ -340,6 +340,14 @@ def make_package(args): # Prepare some variables for templating process res_dir = "src/main/res" + res_dir_initial = "src/res_initial" + # make res_dir stateless + if exists(res_dir_initial): + shutil.rmtree(res_dir, ignore_errors=True) + shutil.copytree(res_dir_initial, res_dir) + else: + shutil.copytree(res_dir, res_dir_initial) + # Add user resouces for resource in args.resources: resource_src, resource_dest = resource.split(":") @@ -347,7 +355,8 @@ def make_package(args): ensure_dir(dirname(join(res_dir, resource_dest))) shutil.copy(realpath(resource_src), join(res_dir, resource_dest)) else: - shutil.copytree(realpath(resource_src), join(res_dir, resource_dest)) + shutil.copytree(realpath(resource_src), + join(res_dir, resource_dest), dirs_exist_ok=True) default_icon = 'templates/kivy-icon.png' default_presplash = 'templates/kivy-presplash.jpg' From 9a32a9c0012c6540d4480140a038ec1f8d0edb29 Mon Sep 17 00:00:00 2001 From: RobertFlatt <34464649+RobertFlatt@users.noreply.github.com> Date: Wed, 12 Oct 2022 17:11:39 -1000 Subject: [PATCH 05/16] multiple kinds --- pythonforandroid/toolchain.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pythonforandroid/toolchain.py b/pythonforandroid/toolchain.py index feadb89e59..daf895e897 100644 --- a/pythonforandroid/toolchain.py +++ b/pythonforandroid/toolchain.py @@ -1008,7 +1008,8 @@ def _fix_args(args): if ":" in resource: resource_src, resource_dest = resource.split(":") else: - resource_src = resource_dest = resource + resource_src = resource + resource_dest = "" # take abspath now, because build.py will be run in bootstrap dir unknown_args += ["--resource", os.path.abspath(resource_src)+":"+resource_dest] for i, arg in enumerate(unknown_args): From 76e52043274ee1b146e838b2e36e0f7ed4d61493 Mon Sep 17 00:00:00 2001 From: RobertFlatt <34464649+RobertFlatt@users.noreply.github.com> Date: Wed, 12 Oct 2022 17:16:01 -1000 Subject: [PATCH 06/16] pep8 --- pythonforandroid/bootstraps/common/build/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonforandroid/bootstraps/common/build/build.py b/pythonforandroid/bootstraps/common/build/build.py index c9a992fc00..a9f4dde42e 100644 --- a/pythonforandroid/bootstraps/common/build/build.py +++ b/pythonforandroid/bootstraps/common/build/build.py @@ -347,7 +347,7 @@ def make_package(args): shutil.copytree(res_dir_initial, res_dir) else: shutil.copytree(res_dir, res_dir_initial) - + # Add user resouces for resource in args.resources: resource_src, resource_dest = resource.split(":") From fa2cb13f8fbb2b57faee225248844cdf34a1ef58 Mon Sep 17 00:00:00 2001 From: RobertF <34464649+RobertFlatt@users.noreply.github.com> Date: Sun, 16 Oct 2022 14:58:38 -1000 Subject: [PATCH 07/16] --add_resource --- doc/source/buildoptions.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/buildoptions.rst b/doc/source/buildoptions.rst index d9f97175a2..cfe8bc1a04 100644 --- a/doc/source/buildoptions.rst +++ b/doc/source/buildoptions.rst @@ -91,6 +91,7 @@ options (this list may not be exhaustive): - ``--add-source``: Add a source directory to the app's Java code. - ``--no-compile-pyo``: Do not optimise .py files to .pyo. - ``--enable-androidx``: Enable AndroidX support library. +- ``--add-resource``: Put this file or directory in the apk res directory. webview From 1e159c5badd2fbaf6f753b73a4705e951388dac8 Mon Sep 17 00:00:00 2001 From: RobertF <34464649+RobertFlatt@users.noreply.github.com> Date: Thu, 27 Oct 2022 11:36:48 -1000 Subject: [PATCH 08/16] Custom notification --- .../common/build/templates/Service.tmpl.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java index de84ac42bf..b2a4c14c5d 100644 --- a/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java @@ -19,23 +19,37 @@ protected int getServiceId() { } static public void start(Context ctx, String pythonServiceArgument) { - Intent intent = getDefaultIntent(ctx, pythonServiceArgument); + Intent intent = getDefaultIntent(ctx, "", "{{ args.name }}", + "{{ name|capitalize }}", + pythonServiceArgument); ctx.startService(intent); } - static public Intent getDefaultIntent(Context ctx, String pythonServiceArgument) { + static public void start(Context ctx, String smallIconName, + String contentTitle, String contentText, + String pythonServiceArgument) { + Intent intent = getDefaultIntent(ctx, smallIconName, contentTitle, + contentText, pythonServiceArgument); + ctx.startService(intent); + } + + 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("serviceDescription", "{{ name|capitalize }}"); 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; } From 34420b5bc4ecd6d7f2eed6859b6b74093583af76 Mon Sep 17 00:00:00 2001 From: RobertF <34464649+RobertFlatt@users.noreply.github.com> Date: Thu, 27 Oct 2022 11:41:57 -1000 Subject: [PATCH 09/16] Update Service.tmpl.java --- .../bootstraps/common/build/templates/Service.tmpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java index b2a4c14c5d..95517f1b20 100644 --- a/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java @@ -55,7 +55,8 @@ static public Intent getDefaultIntent(Context ctx, String smallIconName, @Override protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument) { - return Service{{ name|capitalize }}.getDefaultIntent(ctx, pythonServiceArgument); + return Service{{ name|capitalize }}.getDefaultIntent(ctx, "", "", "", + pythonServiceArgument); } static public void stop(Context ctx) { From 4afc58d5dca3ce1c6b9d55a5db50d187902efc3c Mon Sep 17 00:00:00 2001 From: RobertF <34464649+RobertFlatt@users.noreply.github.com> Date: Thu, 27 Oct 2022 13:31:34 -1000 Subject: [PATCH 10/16] Custom Notification --- .../java/org/kivy/android/PythonService.java | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonService.java b/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonService.java index dd6f307ec7..cb89e3bdcd 100644 --- a/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonService.java +++ b/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonService.java @@ -102,30 +102,47 @@ protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument) protected void doStartForeground(Bundle extras) { String serviceTitle = extras.getString("serviceTitle"); - String serviceDescription = extras.getString("serviceDescription"); + String smallIconName = extras.getString("smallIconName"); + String contentTitle = extras.getString("contentTitle"); + String contentText = extras.getString("contentText"); Notification notification; Context context = getApplicationContext(); Intent contextIntent = new Intent(context, PythonActivity.class); PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + // Unspecified icon uses default. + int smallIconId = context.getApplicationInfo().icon; + if (!smallIconName.equals("")){ + int resId = getResources().getIdentifier(smallIconName, "mipmap", + getPackageName()); + if (resId ==0) { + resId = getResources().getIdentifier(smallIconName, "drawable", + getPackageName()); + } + if (resId !=0) { + smallIconId = resId; + } + } + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + // This constructor is deprecated notification = new Notification( - context.getApplicationInfo().icon, serviceTitle, System.currentTimeMillis()); + smallIconId, serviceTitle, System.currentTimeMillis()); try { // prevent using NotificationCompat, this saves 100kb on apk Method func = notification.getClass().getMethod( "setLatestEventInfo", Context.class, CharSequence.class, CharSequence.class, PendingIntent.class); - func.invoke(notification, context, serviceTitle, serviceDescription, pIntent); + func.invoke(notification, context, contentTitle, contentText, pIntent); } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { } } else { // for android 8+ we need to create our own channel // https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1 - String NOTIFICATION_CHANNEL_ID = "org.kivy.p4a"; //TODO: make this configurable - String channelName = "Background Service"; //TODO: make this configurable + String NOTIFICATION_CHANNEL_ID = "org.kivy.p4a" + getServiceId(); + String channelName = "Background Service" + getServiceId(); NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE); @@ -135,10 +152,10 @@ protected void doStartForeground(Bundle extras) { manager.createNotificationChannel(chan); Notification.Builder builder = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID); - builder.setContentTitle(serviceTitle); - builder.setContentText(serviceDescription); + builder.setContentTitle(contentTitle); + builder.setContentText(contentText); builder.setContentIntent(pIntent); - builder.setSmallIcon(context.getApplicationInfo().icon); + builder.setSmallIcon(smallIconId); notification = builder.build(); } startForeground(getServiceId(), notification); From 38f733fa024ce6e574d8b7d5a0b0929fb139a6ad Mon Sep 17 00:00:00 2001 From: RobertF <34464649+RobertFlatt@users.noreply.github.com> Date: Thu, 27 Oct 2022 13:40:31 -1000 Subject: [PATCH 11/16] Custom Notification --- .../build/templates/Service.tmpl.java | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java index f1eaf0702d..7ca6077ca9 100644 --- a/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java @@ -36,8 +36,27 @@ public static void prepare(Context ctx) { } public static void start(Context ctx, String pythonServiceArgument) { - Intent intent = getDefaultIntent(ctx, pythonServiceArgument); - + Intent intent = getDefaultIntent(ctx, "", "{{ args.name }}", + "{{ name|capitalize }}", + pythonServiceArgument); + //foreground: {{foreground}} + {% if foreground %} + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + ctx.startForegroundService(intent); + } else { + ctx.startService(intent); + } + {% else %} + ctx.startService(intent); + {% endif %} + } + + static public void start(Context ctx, String smallIconName, + String contentTitle, + String contentText, + String pythonServiceArgument) { + Intent intent = getDefaultIntent(ctx, smallIconName, contentTitle, + contentText, pythonServiceArgument); //foreground: {{foreground}} {% if foreground %} if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -50,26 +69,32 @@ public static void start(Context ctx, String pythonServiceArgument) { {% endif %} } - static public Intent getDefaultIntent(Context ctx, String pythonServiceArgument) { + static public Intent getDefaultIntent(Context ctx, String smallIconName, + String contentTitle, + 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("serviceDescription", ""); 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; } @Override protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument) { - return Service{{ name|capitalize }}.getDefaultIntent(ctx, pythonServiceArgument); + return Service{{ name|capitalize }}.getDefaultIntent(ctx, "", "", "", + pythonServiceArgument); } From 874df1c4d8b39c6bab799541a32f50146bfe8fc8 Mon Sep 17 00:00:00 2001 From: RobertF <34464649+RobertFlatt@users.noreply.github.com> Date: Fri, 4 Nov 2022 15:31:29 -1000 Subject: [PATCH 12/16] service notification --- .../build/templates/Service.tmpl.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java index 598549d345..eeda810bef 100644 --- a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java @@ -34,16 +34,40 @@ public static void start(Context ctx, String pythonServiceArgument) { intent.putExtra("androidArgument", argument); intent.putExtra("serviceEntrypoint", "{{ entrypoint }}"); intent.putExtra("serviceTitle", "{{ name|capitalize }}"); - intent.putExtra("serviceDescription", ""); 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", ""); ctx.startService(intent); } - + + 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); + ctx.startService(intent); + } + public static void stop(Context ctx) { Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class); ctx.stopService(intent); From f08b08fead0ccff1bafc1d148e915e91c3cfe959 Mon Sep 17 00:00:00 2001 From: RobertF <34464649+RobertFlatt@users.noreply.github.com> Date: Sat, 5 Nov 2022 11:09:54 -1000 Subject: [PATCH 13/16] customize notification --- doc/source/services.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/doc/source/services.rst b/doc/source/services.rst index b5519a5cb7..8b5d9a7851 100644 --- a/doc/source/services.rst +++ b/doc/source/services.rst @@ -107,6 +107,14 @@ the json module to encode and decode more complex data. from os import environ argument = environ.get('PYTHON_SERVICE_ARGUMENT', '') + +To customize the notification icon, title, and text use three optional +arguments to service.start():: + + service.start(mActivity, 'small_icon', 'title', 'content' , argument) + +Where 'small_icon' is the name of an Android drawable or mipmap resource, +and 'title' and 'content' are strings in the notification. Services support a range of options and interactions not yet documented here but all accessible via calling other methods of the From e92c2794418d33c3f2832507388a018005f01605 Mon Sep 17 00:00:00 2001 From: RobertF <34464649+RobertFlatt@users.noreply.github.com> Date: Thu, 8 Dec 2022 10:46:00 -1000 Subject: [PATCH 14/16] share code --- .../common/build/templates/Service.tmpl.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java index 95517f1b20..fd40aae666 100644 --- a/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java @@ -17,20 +17,23 @@ public int startType() { protected int getServiceId() { return {{ service_id }}; } + + static private void _start(Context ctx, String smallIconName, + String contentTitle, String contentText, + String pythonServiceArgument) { + Intent intent = getDefaultIntent(ctx, smallIconName, contentTitle, + contentText, pythonServiceArgument); + ctx.startService(intent); + } static public void start(Context ctx, String pythonServiceArgument) { - Intent intent = getDefaultIntent(ctx, "", "{{ args.name }}", - "{{ name|capitalize }}", - pythonServiceArgument); - ctx.startService(intent); + _start(ctx, "", "{{ args.name }}", "{{ name|capitalize }}", pythonServiceArgument); } static public void start(Context ctx, String smallIconName, String contentTitle, String contentText, String pythonServiceArgument) { - Intent intent = getDefaultIntent(ctx, smallIconName, contentTitle, - contentText, pythonServiceArgument); - ctx.startService(intent); + _start(ctx, smallIconName, contentTitle, ontentText, pythonServiceArgument); } static public Intent getDefaultIntent(Context ctx, String smallIconName, From 9d26fa5ac163ed20ca237cce37342e99805cd299 Mon Sep 17 00:00:00 2001 From: RobertF <34464649+RobertFlatt@users.noreply.github.com> Date: Thu, 8 Dec 2022 10:46:16 -1000 Subject: [PATCH 15/16] share code --- .../build/templates/Service.tmpl.java | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java index 7ca6077ca9..ff889b462c 100644 --- a/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java @@ -34,11 +34,13 @@ public static void prepare(Context ctx) { PythonUtil.unpackAsset(ctx, "private", app_root_file, true); PythonUtil.unpackPyBundle(ctx, ctx.getApplicationInfo().nativeLibraryDir + "/" + "libpybundle", app_root_file, false); } - - public static void start(Context ctx, String pythonServiceArgument) { - Intent intent = getDefaultIntent(ctx, "", "{{ args.name }}", - "{{ name|capitalize }}", - pythonServiceArgument); + + static private void _start(Context ctx, String smallIconName, + String contentTitle, + String contentText, + String pythonServiceArgument) { + Intent intent = getDefaultIntent(ctx, smallIconName, contentTitle, + contentText, pythonServiceArgument); //foreground: {{foreground}} {% if foreground %} if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -50,23 +52,16 @@ public static void start(Context ctx, String pythonServiceArgument) { ctx.startService(intent); {% endif %} } + + public static void start(Context ctx, String pythonServiceArgument) { + _start(ctx, "", "{{ args.name }}", "{{ name|capitalize }}", pythonServiceArgument); + } static public void start(Context ctx, String smallIconName, String contentTitle, String contentText, String pythonServiceArgument) { - Intent intent = getDefaultIntent(ctx, smallIconName, contentTitle, - contentText, pythonServiceArgument); - //foreground: {{foreground}} - {% if foreground %} - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - ctx.startForegroundService(intent); - } else { - ctx.startService(intent); - } - {% else %} - ctx.startService(intent); - {% endif %} + _start(ctx, smallIconName, contentTitle, contentText, pythonServiceArgument); } static public Intent getDefaultIntent(Context ctx, String smallIconName, From c2e405b10655202f685f33d1399f9d95cd34e305 Mon Sep 17 00:00:00 2001 From: RobertF <34464649+RobertFlatt@users.noreply.github.com> Date: Thu, 8 Dec 2022 11:41:07 -1000 Subject: [PATCH 16/16] Update Service.tmpl.java --- .../bootstraps/common/build/templates/Service.tmpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java index fd40aae666..9406f91d89 100644 --- a/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java @@ -33,7 +33,7 @@ static public void start(Context ctx, String pythonServiceArgument) { static public void start(Context ctx, String smallIconName, String contentTitle, String contentText, String pythonServiceArgument) { - _start(ctx, smallIconName, contentTitle, ontentText, pythonServiceArgument); + _start(ctx, smallIconName, contentTitle, contentText, pythonServiceArgument); } static public Intent getDefaultIntent(Context ctx, String smallIconName,