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