From 8fb6be54aca2051e9de2350e17ff26b34ce1c956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20F=2E=20Wittenberger?= Date: Mon, 19 Aug 2019 16:13:15 +0200 Subject: [PATCH 1/8] A ForegroundService for Lambdanative. Exports a single procedure: `foreground-service!` A NOOP on anything but Android. On Android: (foreground-service! #t) Start the service. (foreground-service! #f) Stop the service. The service will send EVENT_IDLE every 30 seconds to your application. The main use is to prevent Android from aggressivly stopping the application when it should keep running. --- modules/androidforeground/ANDROID_c_additions | 21 ++++++ modules/androidforeground/ANDROID_c_defines | 6 ++ .../ANDROID_java_activityadditions | 8 +++ ..._java_public_LambdaNativeForegroundService | 72 +++++++++++++++++++ .../androidforeground/ANDROID_xml_permissions | 1 + .../androidforeground/ANDROID_xml_services | 1 + .../androidforeground/androidforeground.scm | 18 +++++ 7 files changed, 127 insertions(+) create mode 100644 modules/androidforeground/ANDROID_c_additions create mode 100644 modules/androidforeground/ANDROID_c_defines create mode 100644 modules/androidforeground/ANDROID_java_activityadditions create mode 100644 modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService create mode 100644 modules/androidforeground/ANDROID_xml_permissions create mode 100644 modules/androidforeground/ANDROID_xml_services create mode 100644 modules/androidforeground/androidforeground.scm diff --git a/modules/androidforeground/ANDROID_c_additions b/modules/androidforeground/ANDROID_c_additions new file mode 100644 index 00000000..7bbc66ad --- /dev/null +++ b/modules/androidforeground/ANDROID_c_additions @@ -0,0 +1,21 @@ +/* -*-C-*- */ + +void android_start_ln_foreground_service() +{ + JNIEnv *env = GetJNIEnv(); + jclass main_class = (*env)->FindClass(env, "@SYS_PACKAGE_SLASH@/@SYS_APPNAME@"); + if (env&&globalObj){ + jmethodID method = (*env)->GetMethodID(env, main_class, "startLnForegroundService", "()V"); + (*env)->CallIntMethod(env, globalObj, method); + } +} + +void android_stop_ln_foreground_service() +{ + JNIEnv *env = GetJNIEnv(); + jclass main_class = (*env)->FindClass(env, "@SYS_PACKAGE_SLASH@/@SYS_APPNAME@"); + if (env&&globalObj){ + jmethodID method = (*env)->GetMethodID(env, main_class, "stopLnForegroundService", "()V"); + (*env)->CallVoidMethod(env, globalObj, method); + } +} diff --git a/modules/androidforeground/ANDROID_c_defines b/modules/androidforeground/ANDROID_c_defines new file mode 100644 index 00000000..a9b41c20 --- /dev/null +++ b/modules/androidforeground/ANDROID_c_defines @@ -0,0 +1,6 @@ +/* -*-C-*- */ + +void Java_@SYS_PACKAGE_UNDERSCORE@_LambdaNativeForegroundService_nativeEvent(JNIEnv* e, jobject o, jint t, jint x, jint y){ + if ((*e)->ExceptionCheck(e)) return; + ffi_event((int)t,(int)x,(int)y); +} diff --git a/modules/androidforeground/ANDROID_java_activityadditions b/modules/androidforeground/ANDROID_java_activityadditions new file mode 100644 index 00000000..75c77caa --- /dev/null +++ b/modules/androidforeground/ANDROID_java_activityadditions @@ -0,0 +1,8 @@ +/* -*-java-*- */ +void startLnForegroundService() { + startService(new Intent(this, LambdaNativeForegroundService.class)); +} + +void stopLnForegroundService() { + stopService(new Intent(this, LambdaNativeForegroundService.class)); +} diff --git a/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService b/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService new file mode 100644 index 00000000..fca47c08 --- /dev/null +++ b/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService @@ -0,0 +1,72 @@ +/* -*-java-*- */ + +import android.util.Log; +import android.app.Service; +import android.app.Notification; +import android.app.Notification.Builder; +//import android.support.v4.app.NotificationCompat; +import android.content.Intent; +import android.os.IBinder; +import android.os.SystemClock; + +public class LambdaNativeForegroundService extends Service { + final static int notificationIsRunningId = 1; + boolean running=true; + Thread backgroundThread; + public LambdaNativeForegroundService() { + } + @Override + public IBinder onBind(Intent intent) { + throw new UnsupportedOperationException("Not implemented"); + } + @Override + public void onCreate() { +// Log.d("","LambdaNativeForegroundService created"); + } + private void bgActivity() { +// Log.d("","LambdaNativeForegroundService started"); + backgroundThread = new Thread() { + public void run() { + // setPriority(Thread.MAX_PRIORITY); + setPriority(Thread.MIN_PRIORITY); + while (running) { +// Log.d("", "LambdaNativeForegroundService EVENT_IDLE"); +// nativeEvent(19,0,0); // EVENT_IDLE + + // SystemClock.sleep(30000); // should be 30 seconds i.e. 30000 ms + try { + this.sleep(30000); // should be 30 seconds i.e. 30000 ms + try { + nativeEvent(19,0,0); // EVENT_IDLE + } catch (Throwable e) { + Log.e("LambdaNativeForegroundService", "exception: " + e.toString()); + } + } catch (InterruptedException e) {} + } +// Log.d("","LambdaNativeForegroundService thread stopped"); + } + }; + backgroundThread.start(); + } + @Override + public void onStart(Intent intent, int startId) { +// Log.d("","LambdaNativeForegroundService starting"); + + Notification notification = new Notification.Builder(this) + .setContentTitle(getString(R.string.app_name)) + // .setContentText("TBD") + .setSmallIcon(R.drawable.icon) + // .setLargeIcon(aBitmap) + .build(); + + startForeground(notificationIsRunningId, notification); + bgActivity(); + } + @Override + public void onDestroy() { + // running=false; +// Log.d("","LambdaNativeForegroundService stopped"); + } + native void nativeEvent(int t, int x, int y); +} + diff --git a/modules/androidforeground/ANDROID_xml_permissions b/modules/androidforeground/ANDROID_xml_permissions new file mode 100644 index 00000000..506377be --- /dev/null +++ b/modules/androidforeground/ANDROID_xml_permissions @@ -0,0 +1 @@ + diff --git a/modules/androidforeground/ANDROID_xml_services b/modules/androidforeground/ANDROID_xml_services new file mode 100644 index 00000000..61a36949 --- /dev/null +++ b/modules/androidforeground/ANDROID_xml_services @@ -0,0 +1 @@ + diff --git a/modules/androidforeground/androidforeground.scm b/modules/androidforeground/androidforeground.scm new file mode 100644 index 00000000..90291a2b --- /dev/null +++ b/modules/androidforeground/androidforeground.scm @@ -0,0 +1,18 @@ +;; this module creates an android service to drive the native lambdanative payload in the background + +(define foreground-service! + (let ((running #f) + (start! (c-lambda () void " +#if defined(__ANDROID__) + android_start_ln_foreground_service(); +#endif +")) + (stop! (c-lambda () void " +#if defined(__ANDROID__) + android_stop_ln_foreground_service(); +#endif +"))) + (lambda (flag) + (cond + ((and flag (not running)) (set! running #t) (start!)) + ((and (not flag) running) (set! running #f) (stop!)))))) From e1c77584f5298fb33c6de54f7c68260e23684ccd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20F=2E=20Wittenberger?= Date: Wed, 28 Aug 2019 14:46:39 +0200 Subject: [PATCH 2/8] Improve handling of battery optimization. See comments in ANDROID_xml_permissions and ANDROID_java_activityadditions how to compile for older API versions and possible issues wrt. restrictions regarding google play store. --- .../ANDROID_java_activityadditions | 23 +++++++++++++++++++ .../androidforeground/ANDROID_java_imports | 2 ++ ..._java_public_LambdaNativeForegroundService | 6 ++++- .../androidforeground/ANDROID_xml_permissions | 14 +++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 modules/androidforeground/ANDROID_java_imports diff --git a/modules/androidforeground/ANDROID_java_activityadditions b/modules/androidforeground/ANDROID_java_activityadditions index 75c77caa..4f94b7e9 100644 --- a/modules/androidforeground/ANDROID_java_activityadditions +++ b/modules/androidforeground/ANDROID_java_activityadditions @@ -1,5 +1,28 @@ /* -*-java-*- */ void startLnForegroundService() { + /* API 26+: In order to compile for prior API versions comment out + * the following attempt to disable battery optimizations + */ + /* // BEGIN "conditionally compiled code" */ + if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1) { + String pkg=getPackageName(); + PowerManager pm=getSystemService(PowerManager.class); + if(!pm.isIgnoringBatteryOptimizations(pkg)) { + /* See also the comment in ANDROID_xml_permissions: Google may + * not like the required + * ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS permission + * being used. + * + * If it is not requested in the permissions file, + * uncomment the following startActivityForResult(...) and + * comment out startActivity(...) in the line after. + */ + // startActivityForResult(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS), 0); + startActivity(new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).setData(Uri.parse("package:"+pkg))) + } + } + /* // END "conditionally compiled code" */ + startService(new Intent(this, LambdaNativeForegroundService.class)); } diff --git a/modules/androidforeground/ANDROID_java_imports b/modules/androidforeground/ANDROID_java_imports new file mode 100644 index 00000000..96c2540e --- /dev/null +++ b/modules/androidforeground/ANDROID_java_imports @@ -0,0 +1,2 @@ +import android.provider.Settings; +import android.os.Build; diff --git a/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService b/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService index fca47c08..a038e99b 100644 --- a/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService +++ b/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService @@ -57,11 +57,16 @@ public class LambdaNativeForegroundService extends Service { // .setContentText("TBD") .setSmallIcon(R.drawable.icon) // .setLargeIcon(aBitmap) + .setOngoing(true) .build(); startForeground(notificationIsRunningId, notification); bgActivity(); } + @Override public int onStartCommand(Intent intent, int flags, int startId) { + super.onStartCommand(intent, flags, startId); + return START_STICKY; + } @Override public void onDestroy() { // running=false; @@ -69,4 +74,3 @@ public class LambdaNativeForegroundService extends Service { } native void nativeEvent(int t, int x, int y); } - diff --git a/modules/androidforeground/ANDROID_xml_permissions b/modules/androidforeground/ANDROID_xml_permissions index 506377be..5dd9b4d2 100644 --- a/modules/androidforeground/ANDROID_xml_permissions +++ b/modules/androidforeground/ANDROID_xml_permissions @@ -1 +1,15 @@ + + From 9334b106c97d8cd5b57d21b4527e7029f69e86dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20F=2E=20Wittenberger?= Date: Wed, 28 Aug 2019 14:46:39 +0200 Subject: [PATCH 3/8] Improve handling of battery optimization. See comments in ANDROID_xml_permissions and ANDROID_java_activityadditions how to compile for older API versions and possible issues wrt. restrictions regarding google play store. --- .../ANDROID_java_activityadditions | 23 +++++++++++++++++++ .../androidforeground/ANDROID_java_imports | 2 ++ ..._java_public_LambdaNativeForegroundService | 6 ++++- .../androidforeground/ANDROID_xml_permissions | 14 +++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 modules/androidforeground/ANDROID_java_imports diff --git a/modules/androidforeground/ANDROID_java_activityadditions b/modules/androidforeground/ANDROID_java_activityadditions index 75c77caa..87b73ef3 100644 --- a/modules/androidforeground/ANDROID_java_activityadditions +++ b/modules/androidforeground/ANDROID_java_activityadditions @@ -1,5 +1,28 @@ /* -*-java-*- */ void startLnForegroundService() { + /* API 26+: In order to compile for prior API versions comment out + * the following attempt to disable battery optimizations + */ + /* // BEGIN "conditionally compiled code" */ + if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1) { + String pkg=getPackageName(); + PowerManager pm=getSystemService(PowerManager.class); + if(!pm.isIgnoringBatteryOptimizations(pkg)) { + /* See also the comment in ANDROID_xml_permissions: Google may + * not like the required + * ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS permission + * being used. + * + * If it is not requested in the permissions file, + * uncomment the following startActivityForResult(...) and + * comment out startActivity(...) in the line after. + */ + // startActivityForResult(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS), 0); + startActivity(new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).setData(Uri.parse("package:"+pkg))); + } + } + /* // END "conditionally compiled code" */ + startService(new Intent(this, LambdaNativeForegroundService.class)); } diff --git a/modules/androidforeground/ANDROID_java_imports b/modules/androidforeground/ANDROID_java_imports new file mode 100644 index 00000000..96c2540e --- /dev/null +++ b/modules/androidforeground/ANDROID_java_imports @@ -0,0 +1,2 @@ +import android.provider.Settings; +import android.os.Build; diff --git a/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService b/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService index fca47c08..a038e99b 100644 --- a/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService +++ b/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService @@ -57,11 +57,16 @@ public class LambdaNativeForegroundService extends Service { // .setContentText("TBD") .setSmallIcon(R.drawable.icon) // .setLargeIcon(aBitmap) + .setOngoing(true) .build(); startForeground(notificationIsRunningId, notification); bgActivity(); } + @Override public int onStartCommand(Intent intent, int flags, int startId) { + super.onStartCommand(intent, flags, startId); + return START_STICKY; + } @Override public void onDestroy() { // running=false; @@ -69,4 +74,3 @@ public class LambdaNativeForegroundService extends Service { } native void nativeEvent(int t, int x, int y); } - diff --git a/modules/androidforeground/ANDROID_xml_permissions b/modules/androidforeground/ANDROID_xml_permissions index 506377be..5dd9b4d2 100644 --- a/modules/androidforeground/ANDROID_xml_permissions +++ b/modules/androidforeground/ANDROID_xml_permissions @@ -1 +1,15 @@ + + From 72ec4504dd9ee4ad920dfefc475b6ff370e1013e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20F=2E=20Wittenberger?= Date: Thu, 29 Aug 2019 17:33:44 +0200 Subject: [PATCH 4/8] Use trick to conditionally compile some code for ANDROIDAPI>22. --- .../ANDROID_java_activityadditions | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/modules/androidforeground/ANDROID_java_activityadditions b/modules/androidforeground/ANDROID_java_activityadditions index 87b73ef3..b7d5d601 100644 --- a/modules/androidforeground/ANDROID_java_activityadditions +++ b/modules/androidforeground/ANDROID_java_activityadditions @@ -3,25 +3,25 @@ void startLnForegroundService() { /* API 26+: In order to compile for prior API versions comment out * the following attempt to disable battery optimizations */ - /* // BEGIN "conditionally compiled code" */ + /* Included @IF_ANDROIDAPI_GT_22@ if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1) { String pkg=getPackageName(); PowerManager pm=getSystemService(PowerManager.class); if(!pm.isIgnoringBatteryOptimizations(pkg)) { - /* See also the comment in ANDROID_xml_permissions: Google may - * not like the required - * ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS permission - * being used. - * - * If it is not requested in the permissions file, - * uncomment the following startActivityForResult(...) and - * comment out startActivity(...) in the line after. - */ + // See also the comment in ANDROID_xml_permissions: Google may + // not like the required + // ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS permission + // being used. + // + // If it is not requested in the permissions file, + // uncomment the following startActivityForResult(...) and + // comment out startActivity(...) in the line after. + // startActivityForResult(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS), 0); startActivity(new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).setData(Uri.parse("package:"+pkg))); } } - /* // END "conditionally compiled code" */ + // end of IF_ANDROIDAPI_GT_22 */ startService(new Intent(this, LambdaNativeForegroundService.class)); } From bca5ff5c564b1ce717d2f25ad81ee357bef3ad2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20F=2E=20Wittenberger?= Date: Thu, 29 Aug 2019 17:02:18 +0200 Subject: [PATCH 5/8] Handle compilation and permissions for ANDROIDAPI>22. Since Marshmallow permissions are not automatically granted as specified in the Manifest. As a consequence LN apps die when accessing the SDCard, e.g. when unpacking embedded files. This patch adds a fragile hack to enable an ifdef-alike trick to exclude code portions when targeting older APIs and uses it to request the permission at startup. Plus create the system-directory if it does not already exists. --- loaders/android/bootstrap.java.in | 12 +++++++++++- make.sh | 1 + modules/ln_core/packtool.scm | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/loaders/android/bootstrap.java.in b/loaders/android/bootstrap.java.in index 7a27b643..73963b90 100644 --- a/loaders/android/bootstrap.java.in +++ b/loaders/android/bootstrap.java.in @@ -75,6 +75,7 @@ public class @SYS_APPNAME@ extends Activity implements @ANDROID_JAVA_IMPLEMENTS@ //Variable declarations needed for modules, e.g. gps @ANDROID_JAVA_VARIABLES@ + int idle_tmScheduleRate = 250; Timer idle_tm = new Timer(); TimerTask idle_task = new TimerTask() { public void run() { @@ -109,11 +110,20 @@ public class @SYS_APPNAME@ extends Activity implements @ANDROID_JAVA_IMPLEMENTS@ mGLView = new xGLSurfaceView(this); setContentView(mGLView); mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); + + /* Included @IF_ANDROIDAPI_GT_22@ + if (getApplicationContext().checkCallingOrSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) + != android.content.pm.PackageManager.PERMISSION_GRANTED) { + requestPermissions(new String[] { android.Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1); + // startActivity(new Intent(android.provider.Settings.ACTION_MEMORY_CARD_SETTINGS).setData(Uri.parse("package:"+"@SYS_PACKAGE_DOT@"))); + } + /* end of IF_ANDROIDAPI_GT_22 */ + // Additions needed by modules, e.g. gps @ANDROID_JAVA_ONCREATE@ // start EVENT_IDLE - idle_tm.scheduleAtFixedRate(idle_task, 0, 250); + if(idle_tmScheduleRate > 0) idle_tm.scheduleAtFixedRate(idle_task, 0, idle_tmScheduleRate); nativeInstanceInit(); } diff --git a/make.sh b/make.sh index 4226df4f..e3f31641 100755 --- a/make.sh +++ b/make.sh @@ -1032,6 +1032,7 @@ make_setup_target() ac_subst SYS_APPVERSION ac_subst SYS_APPVERSIONCODE ac_subst SYS_ANDROIDAPI + ac_subst IF_ANDROIDAPI_GT_22 "`if [ $SYS_ANDROIDAPI -lt 23 ]; then echo 'commented out:'; else echo 'active here:*/'; fi`" ac_subst SYS_ANDROIDSDK ac_subst SYS_ANDROIDNDK ac_subst SYS_ANDROIDARCH diff --git a/modules/ln_core/packtool.scm b/modules/ln_core/packtool.scm index 2952c3e4..8b6f8dff 100644 --- a/modules/ln_core/packtool.scm +++ b/modules/ln_core/packtool.scm @@ -56,6 +56,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ;; extract an embedded file (define (packtool-unpack file cdata overwrite) + (let ((rootpath (system-directory))) + (if (not (file-exists? rootpath)) (create-directory rootpath))) (let ((path (packtool:prep file))) (if (or overwrite (not (file-exists? path))) (let ((data (u8vector-decompress cdata))) From 0d3f3d06464bd886267b27c841afff5d49fa9d60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20F=2E=20Wittenberger?= Date: Thu, 29 Aug 2019 21:13:16 +0200 Subject: [PATCH 6/8] Do not break syntax highlighting by `ifdef` alike trick. --- loaders/android/bootstrap.java.in | 2 +- make.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/loaders/android/bootstrap.java.in b/loaders/android/bootstrap.java.in index 73963b90..fe55ef6e 100644 --- a/loaders/android/bootstrap.java.in +++ b/loaders/android/bootstrap.java.in @@ -111,7 +111,7 @@ public class @SYS_APPNAME@ extends Activity implements @ANDROID_JAVA_IMPLEMENTS@ setContentView(mGLView); mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); - /* Included @IF_ANDROIDAPI_GT_22@ + @IF_ANDROIDAPI_GT_22@ if (getApplicationContext().checkCallingOrSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != android.content.pm.PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[] { android.Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1); diff --git a/make.sh b/make.sh index e3f31641..f95b4386 100755 --- a/make.sh +++ b/make.sh @@ -1032,7 +1032,7 @@ make_setup_target() ac_subst SYS_APPVERSION ac_subst SYS_APPVERSIONCODE ac_subst SYS_ANDROIDAPI - ac_subst IF_ANDROIDAPI_GT_22 "`if [ $SYS_ANDROIDAPI -lt 23 ]; then echo 'commented out:'; else echo 'active here:*/'; fi`" + ac_subst IF_ANDROIDAPI_GT_22 "`if [ $SYS_ANDROIDAPI -lt 23 ]; then echo '/* commented out:'; else echo '/* active here:*/'; fi`" ac_subst SYS_ANDROIDSDK ac_subst SYS_ANDROIDNDK ac_subst SYS_ANDROIDARCH From 09167de7221c5585243262c80c638e1a7f7d629a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20F=2E=20Wittenberger?= Date: Thu, 29 Aug 2019 21:35:01 +0200 Subject: [PATCH 7/8] Slightly improve the generated comments. --- make.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/make.sh b/make.sh index f95b4386..27c3f7c7 100755 --- a/make.sh +++ b/make.sh @@ -1032,7 +1032,7 @@ make_setup_target() ac_subst SYS_APPVERSION ac_subst SYS_APPVERSIONCODE ac_subst SYS_ANDROIDAPI - ac_subst IF_ANDROIDAPI_GT_22 "`if [ $SYS_ANDROIDAPI -lt 23 ]; then echo '/* commented out:'; else echo '/* active here:*/'; fi`" + ac_subst IF_ANDROIDAPI_GT_22 "`if [ $SYS_ANDROIDAPI -lt 23 ]; then echo '/* IF_ANDROIDAPI_GT_22 commented out:'; else echo '/* IF_ANDROIDAPI_GT_22 active here:*/'; fi`" ac_subst SYS_ANDROIDSDK ac_subst SYS_ANDROIDNDK ac_subst SYS_ANDROIDARCH From 38875f8a48dbc7ac2bc0219662728290110b9e9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20F=2E=20Wittenberger?= Date: Tue, 13 Oct 2020 16:24:20 +0200 Subject: [PATCH 8/8] ANDROID: a foreground service Please don't remove support for older versions. I still need to compile for at least API 19. --- modules/androidforeground/ANDROID_c_additions | 2 +- .../ANDROID_java_activityadditions | 2 +- ...va_public_LambdaNativeForegroundService.in | 78 +++++++++++++++++++ .../androidforeground/ANDROID_xml_permissions | 2 + 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService.in diff --git a/modules/androidforeground/ANDROID_c_additions b/modules/androidforeground/ANDROID_c_additions index 7bbc66ad..d20a9312 100644 --- a/modules/androidforeground/ANDROID_c_additions +++ b/modules/androidforeground/ANDROID_c_additions @@ -6,7 +6,7 @@ void android_start_ln_foreground_service() jclass main_class = (*env)->FindClass(env, "@SYS_PACKAGE_SLASH@/@SYS_APPNAME@"); if (env&&globalObj){ jmethodID method = (*env)->GetMethodID(env, main_class, "startLnForegroundService", "()V"); - (*env)->CallIntMethod(env, globalObj, method); + (*env)->CallVoidMethod(env, globalObj, method); } } diff --git a/modules/androidforeground/ANDROID_java_activityadditions b/modules/androidforeground/ANDROID_java_activityadditions index b7d5d601..51243b7f 100644 --- a/modules/androidforeground/ANDROID_java_activityadditions +++ b/modules/androidforeground/ANDROID_java_activityadditions @@ -3,7 +3,7 @@ void startLnForegroundService() { /* API 26+: In order to compile for prior API versions comment out * the following attempt to disable battery optimizations */ - /* Included @IF_ANDROIDAPI_GT_22@ + @IF_ANDROIDAPI_GT_22@ if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1) { String pkg=getPackageName(); PowerManager pm=getSystemService(PowerManager.class); diff --git a/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService.in b/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService.in new file mode 100644 index 00000000..96cb494f --- /dev/null +++ b/modules/androidforeground/ANDROID_java_public_LambdaNativeForegroundService.in @@ -0,0 +1,78 @@ +/* -*-java-*- */ + +package @SYS_PACKAGE_DOT@; +@IF_ANDROIDAPI_GT_25@ +import android.app.NotificationChannel; +import android.app.NotificationManager; +/* end of IF_ANDROIDAPI_GT_25 */ + +import android.util.Log; +import android.app.Service; +import android.app.Notification; +import android.app.Notification.Builder; + +//import android.support.v4.app.NotificationCompat; + +import android.content.Intent; +import android.os.IBinder; +import android.os.SystemClock; + +public class LambdaNativeForegroundService extends Service { + final static int notificationIsRunningId = 1; + boolean running=true; + Thread backgroundThread; + public LambdaNativeForegroundService() { + } + private Notification.Builder make_notification_template() { + return new Notification.Builder(this) + .setContentTitle(getString(R.string.app_name)) + // .setContentText("TBD") + .setSmallIcon(R.drawable.icon) + // .setLargeIcon(aBitmap) + .setOngoing(true); + } + private void keepAwake_LT_API26() { + startForeground(notificationIsRunningId, make_notification_template().build()); + } + private void keepAwake() { + @IF_ANDROIDAPI_GT_25@ + if(true) { + NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + assert mgr != null; + NotificationChannel channel = + new NotificationChannel ("@SYS_PACKAGE_DOT@", ".working", NotificationManager.IMPORTANCE_NONE); + mgr.createNotificationChannel(channel); + Notification.Builder mknote = make_notification_template() + .setChannelId("@SYS_PACKAGE_DOT@") + .setCategory(Notification.CATEGORY_SERVICE); + startForeground(notificationIsRunningId, mknote.build()); + return; + } + /* end of IF_ANDROIDAPI_GT_25 */ + keepAwake_LT_API26(); + } + @Override + public IBinder onBind(Intent intent) { + throw new UnsupportedOperationException("Not implemented"); + } + @Override + public void onCreate() { +// Log.d("","LambdaNativeForegroundService created"); + super.onCreate(); + keepAwake(); + } + @Override + public void onStart(Intent intent, int startId) { +// Log.d("","LambdaNativeForegroundService starting"); + } + @Override public int onStartCommand(Intent intent, int flags, int startId) { + super.onStartCommand(intent, flags, startId); + return START_STICKY; + } + @Override + public void onDestroy() { + // running=false; +// Log.d("","LambdaNativeForegroundService stopped"); + } + // This is bound in the main class only!!! native void nativeEvent(int t, int x, int y); +} diff --git a/modules/androidforeground/ANDROID_xml_permissions b/modules/androidforeground/ANDROID_xml_permissions index 5dd9b4d2..60eb79fb 100644 --- a/modules/androidforeground/ANDROID_xml_permissions +++ b/modules/androidforeground/ANDROID_xml_permissions @@ -1,4 +1,5 @@ + +