From 830c4a97ad3125066bd6a7ec03a50b18bb76eb6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Wed, 17 Aug 2016 11:14:47 +0200 Subject: [PATCH] Fix app going to sleep when device locked --- plugin.xml | 5 +- src/android/BackgroundMode.java | 25 +++---- src/android/ForegroundService.java | 104 ++++++++++------------------- www/background-mode.js | 8 --- 4 files changed, 50 insertions(+), 92 deletions(-) diff --git a/plugin.xml b/plugin.xml index cc7b924..f117362 100644 --- a/plugin.xml +++ b/plugin.xml @@ -3,7 +3,7 @@ + version="0.6.6-dev"> BackgroundMode @@ -72,7 +72,10 @@ * candidate for killing when low on memory. --> + + + = Build.VERSION_CODES.LOLLIPOP) { - if(settings.optBoolean("isPublic") == true) { - notification.setVisibility(Notification.VISIBILITY_PUBLIC); - } - - if(!settings.optString("color").equals("")) { - try { - notification.setColor(Color.parseColor(settings.optString("color"))); - } catch (Exception e) { - Log.e("BackgroundMode", settings.optString("color") + " is not a valid color"); - } - } - } + Notification.Builder notification = new Notification.Builder(context) + .setContentTitle(settings.optString("title", "")) + .setContentText(settings.optString("text", "")) + .setOngoing(true) + .setSmallIcon(getIconResId()); if (intent != null && settings.optBoolean("resume")) { - PendingIntent contentIntent = PendingIntent.getActivity( - context, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); + context, NOTIFICATION_ID, intent, + PendingIntent.FLAG_UPDATE_CURRENT); notification.setContentIntent(contentIntent); } - - if (Build.VERSION.SDK_INT < 16) { - // Build notification for HoneyComb to ICS - return notification.getNotification(); - } else { - // Notification for Jellybean and above - return notification.build(); - } + return notification.build(); } + /** + * Update the notification. + */ public void updateNotification() { - Notification n = makeNotification(); - NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - mNotificationManager.notify(NOTIFICATION_ID, n); + Notification notification = makeNotification(); + NotificationManager service = (NotificationManager) + getSystemService(Context.NOTIFICATION_SERVICE); + + service.notify(NOTIFICATION_ID, notification); } /** @@ -209,10 +176,9 @@ public class ForegroundService extends Service { Context context = getApplicationContext(); Resources res = context.getResources(); String pkgName = context.getPackageName(); + String icon = settings.optString("icon", "icon"); - int resId = res.getIdentifier(settings.optString("icon", "icon"), "drawable", pkgName); - - return resId; + return res.getIdentifier(icon, "drawable", pkgName); } /** diff --git a/www/background-mode.js b/www/background-mode.js index 962da52..6205e4c 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -30,11 +30,6 @@ channel.onCordovaReady.subscribe(function () { // Set the defaults exports.setDefaults({}); }); - - // Only enable WP8 by default - if (['WinCE', 'Win32NT'].indexOf(device.platform) > -1) { - exports.enable(); - } }); @@ -60,11 +55,8 @@ exports._isActive = false; exports._defaults = { title: 'App is running in background', text: 'Doing heavy tasks.', - ticker: 'App is running in background', resume: true, silent: false, - isPublic: false, - color: "", icon: "icon" };