Fix silent mode
This commit is contained in:
parent
830c4a97ad
commit
ef779d8f32
@ -42,7 +42,8 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
}
|
||||
|
||||
// Plugin namespace
|
||||
private static final String JS_NAMESPACE = "cordova.plugins.backgroundMode";
|
||||
private static final String JS_NAMESPACE =
|
||||
"cordova.plugins.backgroundMode";
|
||||
|
||||
// Flag indicates if the app is in background or foreground
|
||||
private boolean inBackground = false;
|
||||
@ -56,9 +57,6 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
// Default settings for the notification
|
||||
private static JSONObject defaultSettings = new JSONObject();
|
||||
|
||||
// Tmp config settings for the notification
|
||||
private static JSONObject updateSettings;
|
||||
|
||||
ForegroundService mService;
|
||||
|
||||
// Used to (un)bind the service to with the activity
|
||||
@ -66,7 +64,9 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||
ForegroundService.ForegroundBinder binder = (ForegroundService.ForegroundBinder) service;
|
||||
ForegroundService.ForegroundBinder binder =
|
||||
(ForegroundService.ForegroundBinder) service;
|
||||
|
||||
mService = binder.getService();
|
||||
}
|
||||
|
||||
@ -98,8 +98,7 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
boolean update = args.getBoolean(1);
|
||||
|
||||
if (update) {
|
||||
setUpdateSettings(settings);
|
||||
updateNotifcation();
|
||||
updateNotification(settings);
|
||||
} else {
|
||||
setDefaultSettings(settings);
|
||||
}
|
||||
@ -184,16 +183,6 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
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.
|
||||
*
|
||||
@ -201,25 +190,18 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the notification.
|
||||
*
|
||||
* @param settings
|
||||
* The config settings
|
||||
*/
|
||||
private void updateNotifcation() {
|
||||
private void updateNotification(JSONObject settings) {
|
||||
if (isBind) {
|
||||
mService.updateNotification();
|
||||
mService.updateNotification(settings);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,6 @@ import android.content.res.Resources;
|
||||
import android.os.Binder;
|
||||
import android.os.IBinder;
|
||||
import android.os.PowerManager;
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
@ -43,11 +42,12 @@ import org.json.JSONObject;
|
||||
public class ForegroundService extends Service {
|
||||
|
||||
// Fixed ID for the 'foreground' notification
|
||||
private static final int NOTIFICATION_ID = -574543954;
|
||||
public static final int NOTIFICATION_ID = -574543954;
|
||||
|
||||
// Binder given to clients
|
||||
private final IBinder mBinder = new ForegroundBinder();
|
||||
|
||||
// Partial wake lock to prevent the app from going to sleep when locked
|
||||
private PowerManager.WakeLock wakeLock;
|
||||
|
||||
/**
|
||||
@ -64,7 +64,8 @@ public class ForegroundService extends Service {
|
||||
*/
|
||||
public class ForegroundBinder extends Binder {
|
||||
ForegroundService getService() {
|
||||
// Return this instance of ForegroundService so clients can call public methods
|
||||
// Return this instance of ForegroundService
|
||||
// so clients can call public methods
|
||||
return ForegroundService.this;
|
||||
}
|
||||
}
|
||||
@ -93,15 +94,13 @@ public class ForegroundService extends Service {
|
||||
* by the OS.
|
||||
*/
|
||||
public void keepAwake() {
|
||||
JSONObject settings = BackgroundMode.getSettings();
|
||||
boolean isSilent = settings.optBoolean("silent", false);
|
||||
|
||||
if (!this.inSilentMode()) {
|
||||
if (!isSilent) {
|
||||
startForeground(NOTIFICATION_ID, makeNotification());
|
||||
} else {
|
||||
Log.w("BackgroundMode", "In silent mode app may be paused by OS!");
|
||||
}
|
||||
|
||||
BackgroundMode.deleteUpdateSettings();
|
||||
|
||||
PowerManager powerMgr = (PowerManager) getSystemService(POWER_SERVICE);
|
||||
|
||||
wakeLock = powerMgr.newWakeLock(
|
||||
@ -124,17 +123,31 @@ public class ForegroundService extends Service {
|
||||
|
||||
/**
|
||||
* Create a notification as the visible part to be able to put the service
|
||||
* in a foreground state.
|
||||
* in a foreground state by using the default settings.
|
||||
*
|
||||
* @return
|
||||
* A local ongoing notification which pending intent is bound to the
|
||||
* main activity.
|
||||
*/
|
||||
private Notification makeNotification() {
|
||||
JSONObject settings = BackgroundMode.getSettings();
|
||||
Context context = getApplicationContext();
|
||||
String pkgName = context.getPackageName();
|
||||
Intent intent = context.getPackageManager()
|
||||
return makeNotification(BackgroundMode.getSettings());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a notification as the visible part to be able to put the service
|
||||
* in a foreground state.
|
||||
*
|
||||
* @param settings
|
||||
* The config settings
|
||||
*
|
||||
* @return
|
||||
* A local ongoing notification which pending intent is bound to the
|
||||
* main activity.
|
||||
*/
|
||||
private Notification makeNotification(JSONObject settings) {
|
||||
Context context = getApplicationContext();
|
||||
String pkgName = context.getPackageName();
|
||||
Intent intent = context.getPackageManager()
|
||||
.getLaunchIntentForPackage(pkgName);
|
||||
|
||||
Notification.Builder notification = new Notification.Builder(context)
|
||||
@ -156,9 +169,19 @@ public class ForegroundService extends Service {
|
||||
|
||||
/**
|
||||
* Update the notification.
|
||||
*
|
||||
* @param settings
|
||||
* The config settings
|
||||
*/
|
||||
public void updateNotification() {
|
||||
Notification notification = makeNotification();
|
||||
public void updateNotification (JSONObject settings) {
|
||||
boolean isSilent = settings.optBoolean("silent", false);
|
||||
|
||||
if (isSilent) {
|
||||
stopForeground(true);
|
||||
return;
|
||||
}
|
||||
|
||||
Notification notification = makeNotification(settings);
|
||||
NotificationManager service = (NotificationManager)
|
||||
getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
|
||||
@ -180,16 +203,4 @@ public class ForegroundService extends Service {
|
||||
|
||||
return res.getIdentifier(icon, "drawable", pkgName);
|
||||
}
|
||||
|
||||
/**
|
||||
* In silent mode no notification has to be added.
|
||||
*
|
||||
* @return
|
||||
* True if silent: was set to true
|
||||
*/
|
||||
private boolean inSilentMode() {
|
||||
JSONObject settings = BackgroundMode.getSettings();
|
||||
|
||||
return settings.optBoolean("silent", false);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user