Set default settings through setDefaults

This commit is contained in:
Sebastián Katzer
2014-12-14 13:38:36 +01:00
parent 0994147d13
commit cbed556154
5 changed files with 143 additions and 48 deletions

View File

@@ -33,7 +33,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
public class BackgroundMode extends CordovaPlugin {
@@ -54,8 +53,11 @@ public class BackgroundMode extends CordovaPlugin {
// Flag indicates if the service is bind
private boolean isBind = false;
// Settings for the notification
protected static JSONObject settings = new JSONObject();
// Default settings for the notification
private static JSONObject defaultSettings = new JSONObject();
// Tmp config settings for the notification
private static JSONObject updateSettings;
// Used to (un)bind the service to with the activity
private final ServiceConnection connection = new ServiceConnection() {
@@ -89,8 +91,16 @@ public class BackgroundMode extends CordovaPlugin {
CallbackContext callback) throws JSONException {
if (action.equalsIgnoreCase("configure")) {
setSettings(args.getJSONObject(0));
updateNotifcation();
JSONObject settings = args.getJSONObject(0);
boolean update = args.getBoolean(1);
if (update) {
setUpdateSettings(settings);
updateNotifcation();
} else {
setDefaultSettings(settings);
}
return true;
}
@@ -162,13 +172,43 @@ public class BackgroundMode extends CordovaPlugin {
}
/**
* Update the settings for the notification.
* Update the default settings for the notification.
*
* @param newSettings
* The new settings
* @param settings
* The new default settings
*/
private void setSettings(JSONObject newSettings) {
settings = newSettings;
private void setDefaultSettings(JSONObject settings) {
defaultSettings = settings;
}
/**
* Update the config settings for the notification.
*
* @param settings
* The tmp config settings
*/
private void setUpdateSettings(JSONObject settings) {
updateSettings = settings;
}
/**
* The settings for the new/updated notification.
*
* @return
* updateSettings if set or default settings
*/
protected static JSONObject getSettings() {
if (updateSettings != null)
return updateSettings;
return defaultSettings;
}
/**
* Called by ForegroundService to delete the update settings.
*/
protected static void deleteUpdateSettings() {
updateSettings = null;
}
/**
@@ -191,17 +231,16 @@ public class BackgroundMode extends CordovaPlugin {
Intent intent = new Intent(
context, ForegroundService.class);
if (isDisabled || isBind) {
if (isDisabled || isBind)
return;
}
try {
context.bindService(
intent, connection, Context.BIND_AUTO_CREATE);
context.startService(intent);
fireEvent(Event.ACTIVATE, null);
context.startService(intent);
} catch (Exception e) {
fireEvent(Event.FAILURE, e.getMessage());
}
@@ -219,11 +258,12 @@ public class BackgroundMode extends CordovaPlugin {
Intent intent = new Intent(
context, ForegroundService.class);
if (isBind) {
fireEvent(Event.DEACTIVATE, null);
context.unbindService(connection);
}
if (!isBind)
return;
fireEvent(Event.DEACTIVATE, null);
context.unbindService(connection);
context.stopService(intent);
isBind = false;
@@ -240,6 +280,9 @@ public class BackgroundMode extends CordovaPlugin {
private void fireEvent (Event event, String params) {
String eventName;
if (updateSettings != null && event != Event.FAILURE)
return;
switch (event) {
case ACTIVATE:
eventName = "activate"; break;

View File

@@ -86,6 +86,8 @@ public class ForegroundService extends Service {
startForeground(NOTIFICATION_ID, makeNotification());
BackgroundMode.deleteUpdateSettings();
keepAliveTask = new TimerTask() {
@Override
public void run() {
@@ -121,7 +123,7 @@ public class ForegroundService extends Service {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private Notification makeNotification() {
JSONObject settings = BackgroundMode.settings;
JSONObject settings = BackgroundMode.getSettings();
Context context = getApplicationContext();
String pkgName = context.getPackageName();
Intent intent = context.getPackageManager()
@@ -142,6 +144,7 @@ public class ForegroundService extends Service {
notification.setContentIntent(contentIntent);
}
if (Build.VERSION.SDK_INT < 16) {
// Build notification for HoneyComb to ICS
return notification.getNotification();