From 65089bf2e334bc1ed953aa4c1c3cb9da66520558 Mon Sep 17 00:00:00 2001 From: Philipp Auersperg Date: Thu, 30 May 2019 15:23:53 +0200 Subject: [PATCH 1/4] cleanup PR --- .../java/org/kivy/android/PythonService.java | 25 ++++++++++++++++--- .../build/templates/AndroidManifest.tmpl.xml | 6 +++-- .../build/templates/Service.tmpl.java | 2 +- pythonforandroid/recipes/android/__init__.py | 5 ++-- testapps/setup_testapp_service.py | 3 ++- 5 files changed, 32 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 4f20fb7808..495059620a 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 @@ -18,6 +18,12 @@ import org.renpy.android.Hardware; +//imports for channel definition +import android.app.NotificationManager; +import android.app.NotificationChannel; +import android.content.Context; +import android.graphics.Color; +import android.os.Build; public class PythonService extends Service implements Runnable { @@ -90,13 +96,13 @@ public int onStartCommand(Intent intent, int flags, int startId) { protected void doStartForeground(Bundle extras) { String serviceTitle = extras.getString("serviceTitle"); String serviceDescription = extras.getString("serviceDescription"); - Notification notification; Context context = getApplicationContext(); Intent contextIntent = new Intent(context, PythonActivity.class); PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent, PendingIntent.FLAG_UPDATE_CURRENT); - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { notification = new Notification( context.getApplicationInfo().icon, serviceTitle, System.currentTimeMillis()); try { @@ -109,7 +115,20 @@ protected void doStartForeground(Bundle extras) { IllegalArgumentException | InvocationTargetException e) { } } else { - Notification.Builder builder = new Notification.Builder(context); + // 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 = "PythonSerice"; //TODO: make this configurable + NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, + NotificationManager.IMPORTANCE_NONE); + + chan.setLightColor(Color.BLUE); + chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); + NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + assert manager != null; + manager.createNotificationChannel(chan); + + Notification.Builder builder = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID); builder.setContentTitle(serviceTitle); builder.setContentText(serviceDescription); builder.setContentIntent(pIntent); diff --git a/pythonforandroid/bootstraps/service_only/build/templates/AndroidManifest.tmpl.xml b/pythonforandroid/bootstraps/service_only/build/templates/AndroidManifest.tmpl.xml index 5903517f22..81eb7e92ee 100644 --- a/pythonforandroid/bootstraps/service_only/build/templates/AndroidManifest.tmpl.xml +++ b/pythonforandroid/bootstraps/service_only/build/templates/AndroidManifest.tmpl.xml @@ -71,11 +71,13 @@ {% if service %} + android:process=":pythonservice" + android:exported="true"/> {% endif %} {% for name in service_names %} + android:process=":service_{{ name }}" + android:exported="true" /> {% endfor %} {% if args.billing_pubkey %} diff --git a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java index 622fbffa02..ecbf3fe961 100644 --- a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java @@ -17,7 +17,7 @@ public class Service{{ name|capitalize }} extends PythonService { * {@inheritDoc} */ @Override - public int getStartType() { + public int startType() { return START_STICKY; } {% endif %} diff --git a/pythonforandroid/recipes/android/__init__.py b/pythonforandroid/recipes/android/__init__.py index 2fb3e8d682..3658c772ba 100644 --- a/pythonforandroid/recipes/android/__init__.py +++ b/pythonforandroid/recipes/android/__init__.py @@ -38,9 +38,10 @@ def prebuild_arch(self, arch): bootstrap = bootstrap_name = ctx_bootstrap is_sdl2 = bootstrap_name in ('sdl2', 'sdl2python3', 'sdl2_gradle') - is_webview = bootstrap_name in ('webview',) + is_webview = bootstrap_name == 'webview' + is_service_only = bootstrap_name == 'service_only' - if is_sdl2 or is_webview: + if is_sdl2 or is_webview or is_service_only: if is_sdl2: bootstrap = 'sdl2' java_ns = u'org.kivy.android' diff --git a/testapps/setup_testapp_service.py b/testapps/setup_testapp_service.py index b246f108d9..a3342d43a6 100644 --- a/testapps/setup_testapp_service.py +++ b/testapps/setup_testapp_service.py @@ -7,7 +7,8 @@ 'blacklist-requirements': 'openssl,sqlite3', 'android-api': 27, 'ndk-api': 21, - 'ndk-dir': '/home/asandy/android/crystax-ndk-10.3.2', + 'sdk-dir':'/opt/android/android-sdk/', + 'ndk-dir':'/opt/android/android-ndk-r17c/', 'dist-name': 'testapp_service', 'ndk-version': '10.3.2', 'bootstrap': 'service_only', From 571bac73587a1e6e248de9296d5b0f0690a4aa86 Mon Sep 17 00:00:00 2001 From: Philipp Auersperg Date: Thu, 30 May 2019 15:44:17 +0200 Subject: [PATCH 2/4] cleaned up PR #1725 --- .../java/org/kivy/android/PythonService.java | 25 ++++++++++++++++--- .../build/templates/AndroidManifest.tmpl.xml | 6 +++-- .../build/templates/Service.tmpl.java | 2 +- pythonforandroid/recipes/android/__init__.py | 5 ++-- testapps/setup_testapp_service.py | 3 ++- 5 files changed, 32 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 4f20fb7808..495059620a 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 @@ -18,6 +18,12 @@ import org.renpy.android.Hardware; +//imports for channel definition +import android.app.NotificationManager; +import android.app.NotificationChannel; +import android.content.Context; +import android.graphics.Color; +import android.os.Build; public class PythonService extends Service implements Runnable { @@ -90,13 +96,13 @@ public int onStartCommand(Intent intent, int flags, int startId) { protected void doStartForeground(Bundle extras) { String serviceTitle = extras.getString("serviceTitle"); String serviceDescription = extras.getString("serviceDescription"); - Notification notification; Context context = getApplicationContext(); Intent contextIntent = new Intent(context, PythonActivity.class); PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent, PendingIntent.FLAG_UPDATE_CURRENT); - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { notification = new Notification( context.getApplicationInfo().icon, serviceTitle, System.currentTimeMillis()); try { @@ -109,7 +115,20 @@ protected void doStartForeground(Bundle extras) { IllegalArgumentException | InvocationTargetException e) { } } else { - Notification.Builder builder = new Notification.Builder(context); + // 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 = "PythonSerice"; //TODO: make this configurable + NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, + NotificationManager.IMPORTANCE_NONE); + + chan.setLightColor(Color.BLUE); + chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); + NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + assert manager != null; + manager.createNotificationChannel(chan); + + Notification.Builder builder = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID); builder.setContentTitle(serviceTitle); builder.setContentText(serviceDescription); builder.setContentIntent(pIntent); diff --git a/pythonforandroid/bootstraps/service_only/build/templates/AndroidManifest.tmpl.xml b/pythonforandroid/bootstraps/service_only/build/templates/AndroidManifest.tmpl.xml index 5903517f22..81eb7e92ee 100644 --- a/pythonforandroid/bootstraps/service_only/build/templates/AndroidManifest.tmpl.xml +++ b/pythonforandroid/bootstraps/service_only/build/templates/AndroidManifest.tmpl.xml @@ -71,11 +71,13 @@ {% if service %} + android:process=":pythonservice" + android:exported="true"/> {% endif %} {% for name in service_names %} + android:process=":service_{{ name }}" + android:exported="true" /> {% endfor %} {% if args.billing_pubkey %} diff --git a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java index 622fbffa02..ecbf3fe961 100644 --- a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java @@ -17,7 +17,7 @@ public class Service{{ name|capitalize }} extends PythonService { * {@inheritDoc} */ @Override - public int getStartType() { + public int startType() { return START_STICKY; } {% endif %} diff --git a/pythonforandroid/recipes/android/__init__.py b/pythonforandroid/recipes/android/__init__.py index 2fb3e8d682..3658c772ba 100644 --- a/pythonforandroid/recipes/android/__init__.py +++ b/pythonforandroid/recipes/android/__init__.py @@ -38,9 +38,10 @@ def prebuild_arch(self, arch): bootstrap = bootstrap_name = ctx_bootstrap is_sdl2 = bootstrap_name in ('sdl2', 'sdl2python3', 'sdl2_gradle') - is_webview = bootstrap_name in ('webview',) + is_webview = bootstrap_name == 'webview' + is_service_only = bootstrap_name == 'service_only' - if is_sdl2 or is_webview: + if is_sdl2 or is_webview or is_service_only: if is_sdl2: bootstrap = 'sdl2' java_ns = u'org.kivy.android' diff --git a/testapps/setup_testapp_service.py b/testapps/setup_testapp_service.py index b246f108d9..a3342d43a6 100644 --- a/testapps/setup_testapp_service.py +++ b/testapps/setup_testapp_service.py @@ -7,7 +7,8 @@ 'blacklist-requirements': 'openssl,sqlite3', 'android-api': 27, 'ndk-api': 21, - 'ndk-dir': '/home/asandy/android/crystax-ndk-10.3.2', + 'sdk-dir':'/opt/android/android-sdk/', + 'ndk-dir':'/opt/android/android-ndk-r17c/', 'dist-name': 'testapp_service', 'ndk-version': '10.3.2', 'bootstrap': 'service_only', From 5466fac6e7798c28e0124bd922699d37f20e8e1e Mon Sep 17 00:00:00 2001 From: Philipp Auersperg Date: Thu, 30 May 2019 17:21:33 +0200 Subject: [PATCH 3/4] remove redundant imports --- .../build/src/main/java/org/kivy/android/PythonService.java | 2 -- 1 file changed, 2 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 495059620a..019789a028 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 @@ -21,9 +21,7 @@ //imports for channel definition import android.app.NotificationManager; import android.app.NotificationChannel; -import android.content.Context; import android.graphics.Color; -import android.os.Build; public class PythonService extends Service implements Runnable { From c9f0310e65077a17f627231a83ae2a182348db39 Mon Sep 17 00:00:00 2001 From: Philipp Auersperg Date: Fri, 31 May 2019 10:18:41 +0200 Subject: [PATCH 4/4] removed assert --- .../build/src/main/java/org/kivy/android/PythonService.java | 1 - 1 file changed, 1 deletion(-) 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 019789a028..7456c617b3 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 @@ -123,7 +123,6 @@ protected void doStartForeground(Bundle extras) { chan.setLightColor(Color.BLUE); chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - assert manager != null; manager.createNotificationChannel(chan); Notification.Builder builder = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID);