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
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);

View File

@ -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")) {