Update notification when calling configure in background

This commit is contained in:
Sebastián Katzer 2014-11-04 16:59:33 +00:00
parent 8b7ef48cff
commit 3cf8b62e84
2 changed files with 32 additions and 16 deletions

View File

@ -17,10 +17,16 @@
KIND, either express or implied. See the License for the KIND, either express or implied. See the License for the
specific language governing permissions and limitations specific language governing permissions and limitations
under the License. under the License.
*/ */
package de.appplant.cordova.plugin.background; 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.app.Activity;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
@ -29,12 +35,6 @@ import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; 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 { public class BackgroundMode extends CordovaPlugin {
// Flag indicates if the app is in background or foreground // Flag indicates if the app is in background or foreground
@ -47,7 +47,7 @@ public class BackgroundMode extends CordovaPlugin {
static JSONObject settings = new JSONObject(); static JSONObject settings = new JSONObject();
// Used to (un)bind the service to with the activity // Used to (un)bind the service to with the activity
private ServiceConnection connection = new ServiceConnection() { private final ServiceConnection connection = new ServiceConnection() {
@Override @Override
public void onServiceConnected(ComponentName name, IBinder binder) { public void onServiceConnected(ComponentName name, IBinder binder) {
@ -77,7 +77,7 @@ public class BackgroundMode extends CordovaPlugin {
*/ */
@Override @Override
public boolean execute (String action, JSONArray args, public boolean execute (String action, JSONArray args,
CallbackContext callback) throws JSONException { CallbackContext callback) throws JSONException {
if (action.equalsIgnoreCase("observeLifeCycle")) { if (action.equalsIgnoreCase("observeLifeCycle")) {
// Nothing to do here // Nothing to do here
@ -85,7 +85,8 @@ public class BackgroundMode extends CordovaPlugin {
} }
if (action.equalsIgnoreCase("configure")) { if (action.equalsIgnoreCase("configure")) {
settings = args.getJSONObject(0); setSettings(args.getJSONObject(0));
return true; return true;
} }
@ -154,6 +155,20 @@ public class BackgroundMode extends CordovaPlugin {
isDisabled = true; 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 * Bind the activity to a background service and put them into foreground
* state. * state.
@ -164,8 +179,9 @@ public class BackgroundMode extends CordovaPlugin {
Intent intent = new Intent( Intent intent = new Intent(
context, ForegroundService.class); context, ForegroundService.class);
if (isDisabled) if (isDisabled) {
return; return;
}
context.bindService( context.bindService(
intent, connection, Context.BIND_AUTO_CREATE); intent, connection, Context.BIND_AUTO_CREATE);

View File

@ -128,11 +128,11 @@ public class ForegroundService extends Service {
.getLaunchIntentForPackage(pkgName); .getLaunchIntentForPackage(pkgName);
Notification.Builder notification = new Notification.Builder(context) Notification.Builder notification = new Notification.Builder(context)
.setContentTitle(settings.optString("title", "")) .setContentTitle(settings.optString("title", ""))
.setContentText(settings.optString("text", "")) .setContentText(settings.optString("text", ""))
.setTicker(settings.optString("ticker", "")) .setTicker(settings.optString("ticker", ""))
.setOngoing(true) .setOngoing(true)
.setSmallIcon(getIconResId()); .setSmallIcon(getIconResId());
if (intent != null && settings.optBoolean("resume")) { if (intent != null && settings.optBoolean("resume")) {