From 3cf8b62e84473559ed9a144cf2deaa47aa72cc22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Tue, 4 Nov 2014 16:59:33 +0000 Subject: [PATCH] Update notification when calling configure in background --- src/android/BackgroundMode.java | 38 +++++++++++++++++++++--------- src/android/ForegroundService.java | 10 ++++---- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index 6d612d4..339910f 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -17,10 +17,16 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/ + */ package de.appplant.cordova.plugin.background; +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaPlugin; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + import android.app.Activity; import android.content.ComponentName; import android.content.Context; @@ -29,12 +35,6 @@ import android.content.ServiceConnection; import android.os.IBinder; import android.util.Log; -import org.apache.cordova.CallbackContext; -import org.apache.cordova.CordovaPlugin; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - public class BackgroundMode extends CordovaPlugin { // Flag indicates if the app is in background or foreground @@ -47,7 +47,7 @@ public class BackgroundMode extends CordovaPlugin { static JSONObject settings = new JSONObject(); // Used to (un)bind the service to with the activity - private ServiceConnection connection = new ServiceConnection() { + private final ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder binder) { @@ -77,7 +77,7 @@ public class BackgroundMode extends CordovaPlugin { */ @Override public boolean execute (String action, JSONArray args, - CallbackContext callback) throws JSONException { + CallbackContext callback) throws JSONException { if (action.equalsIgnoreCase("observeLifeCycle")) { // Nothing to do here @@ -85,7 +85,8 @@ public class BackgroundMode extends CordovaPlugin { } if (action.equalsIgnoreCase("configure")) { - settings = args.getJSONObject(0); + setSettings(args.getJSONObject(0)); + return true; } @@ -154,6 +155,20 @@ public class BackgroundMode extends CordovaPlugin { isDisabled = true; } + /** + * Update the settings and maybe the notification. + * + * @param settings + */ + private void setSettings(JSONObject settings) { + this.settings = settings; + + if (inBackground) { + stopService(); + startService(); + } + } + /** * Bind the activity to a background service and put them into foreground * state. @@ -164,8 +179,9 @@ public class BackgroundMode extends CordovaPlugin { Intent intent = new Intent( context, ForegroundService.class); - if (isDisabled) + if (isDisabled) { return; + } context.bindService( intent, connection, Context.BIND_AUTO_CREATE); diff --git a/src/android/ForegroundService.java b/src/android/ForegroundService.java index f851d9c..e293171 100644 --- a/src/android/ForegroundService.java +++ b/src/android/ForegroundService.java @@ -128,11 +128,11 @@ public class ForegroundService extends Service { .getLaunchIntentForPackage(pkgName); Notification.Builder notification = new Notification.Builder(context) - .setContentTitle(settings.optString("title", "")) - .setContentText(settings.optString("text", "")) - .setTicker(settings.optString("ticker", "")) - .setOngoing(true) - .setSmallIcon(getIconResId()); + .setContentTitle(settings.optString("title", "")) + .setContentText(settings.optString("text", "")) + .setTicker(settings.optString("ticker", "")) + .setOngoing(true) + .setSmallIcon(getIconResId()); if (intent != null && settings.optBoolean("resume")) {