2014-10-26 16:47:18 +01:00
|
|
|
/*
|
|
|
|
Copyright 2013-2014 appPlant UG
|
|
|
|
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
or more contributor license agreements. See the NOTICE file
|
|
|
|
distributed with this work for additional information
|
|
|
|
regarding copyright ownership. The ASF licenses this file
|
|
|
|
to you under the Apache License, Version 2.0 (the
|
|
|
|
"License"); you may not use this file except in compliance
|
|
|
|
with the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing,
|
|
|
|
software distributed under the License is distributed on an
|
|
|
|
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
KIND, either express or implied. See the License for the
|
|
|
|
specific language governing permissions and limitations
|
|
|
|
under the License.
|
2014-11-04 16:51:32 +00:00
|
|
|
*/
|
2014-10-26 16:47:18 +01:00
|
|
|
|
|
|
|
package de.appplant.cordova.plugin.background;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
2015-06-03 19:45:26 +01:00
|
|
|
import android.app.NotificationManager;
|
2014-10-26 16:47:18 +01:00
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.app.Service;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.res.Resources;
|
2015-06-03 19:45:26 +01:00
|
|
|
import android.os.Binder;
|
2014-10-26 16:47:18 +01:00
|
|
|
import android.os.IBinder;
|
2016-08-17 11:14:47 +02:00
|
|
|
import android.os.PowerManager;
|
2014-10-26 16:47:18 +01:00
|
|
|
|
2016-08-17 11:14:47 +02:00
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2014-10-26 16:47:18 +01:00
|
|
|
/**
|
|
|
|
* Puts the service in a foreground state, where the system considers it to be
|
|
|
|
* something the user is actively aware of and thus not a candidate for killing
|
|
|
|
* when low on memory.
|
|
|
|
*/
|
|
|
|
public class ForegroundService extends Service {
|
|
|
|
|
|
|
|
// Fixed ID for the 'foreground' notification
|
2016-08-17 11:52:31 +02:00
|
|
|
public static final int NOTIFICATION_ID = -574543954;
|
2014-10-26 16:47:18 +01:00
|
|
|
|
2015-06-03 19:45:26 +01:00
|
|
|
// Binder given to clients
|
|
|
|
private final IBinder mBinder = new ForegroundBinder();
|
|
|
|
|
2016-08-17 11:52:31 +02:00
|
|
|
// Partial wake lock to prevent the app from going to sleep when locked
|
2016-08-17 11:14:47 +02:00
|
|
|
private PowerManager.WakeLock wakeLock;
|
2014-10-26 16:47:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow clients to call on to the service.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public IBinder onBind (Intent intent) {
|
2015-06-03 19:45:26 +01:00
|
|
|
return mBinder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class used for the client Binder. Because we know this service always
|
|
|
|
* runs in the same process as its clients, we don't need to deal with IPC.
|
|
|
|
*/
|
|
|
|
public class ForegroundBinder extends Binder {
|
|
|
|
ForegroundService getService() {
|
2016-08-17 11:52:31 +02:00
|
|
|
// Return this instance of ForegroundService
|
|
|
|
// so clients can call public methods
|
2015-06-03 19:45:26 +01:00
|
|
|
return ForegroundService.this;
|
|
|
|
}
|
2014-10-26 16:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Put the service in a foreground state to prevent app from being killed
|
|
|
|
* by the OS.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onCreate () {
|
|
|
|
super.onCreate();
|
|
|
|
keepAwake();
|
|
|
|
}
|
|
|
|
|
2016-08-17 11:14:47 +02:00
|
|
|
/**
|
|
|
|
* No need to run headless on destroy.
|
|
|
|
*/
|
2014-10-26 16:47:18 +01:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
sleepWell();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Put the service in a foreground state to prevent app from being killed
|
|
|
|
* by the OS.
|
|
|
|
*/
|
|
|
|
public void keepAwake() {
|
2016-08-17 11:52:31 +02:00
|
|
|
JSONObject settings = BackgroundMode.getSettings();
|
|
|
|
boolean isSilent = settings.optBoolean("silent", false);
|
2014-10-26 16:47:18 +01:00
|
|
|
|
2016-08-17 11:52:31 +02:00
|
|
|
if (!isSilent) {
|
2015-01-01 17:24:37 +01:00
|
|
|
startForeground(NOTIFICATION_ID, makeNotification());
|
|
|
|
}
|
2014-10-26 16:47:18 +01:00
|
|
|
|
2016-08-17 11:14:47 +02:00
|
|
|
PowerManager powerMgr = (PowerManager) getSystemService(POWER_SERVICE);
|
|
|
|
|
|
|
|
wakeLock = powerMgr.newWakeLock(
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK, "BackgroundMode");
|
|
|
|
|
|
|
|
wakeLock.acquire();
|
2014-10-26 16:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop background mode.
|
|
|
|
*/
|
|
|
|
private void sleepWell() {
|
|
|
|
stopForeground(true);
|
2016-08-17 11:14:47 +02:00
|
|
|
|
|
|
|
if (wakeLock != null) {
|
|
|
|
wakeLock.release();
|
|
|
|
wakeLock = null;
|
|
|
|
}
|
2014-10-26 16:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a notification as the visible part to be able to put the service
|
2016-08-17 11:52:31 +02:00
|
|
|
* in a foreground state by using the default settings.
|
2014-10-26 16:47:18 +01:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* A local ongoing notification which pending intent is bound to the
|
|
|
|
* main activity.
|
|
|
|
*/
|
|
|
|
private Notification makeNotification() {
|
2016-08-17 11:52:31 +02:00
|
|
|
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()
|
2014-10-26 16:47:18 +01:00
|
|
|
.getLaunchIntentForPackage(pkgName);
|
|
|
|
|
2016-08-17 11:14:47 +02:00
|
|
|
Notification.Builder notification = new Notification.Builder(context)
|
|
|
|
.setContentTitle(settings.optString("title", ""))
|
|
|
|
.setContentText(settings.optString("text", ""))
|
|
|
|
.setOngoing(true)
|
|
|
|
.setSmallIcon(getIconResId());
|
2015-06-03 16:33:09 +01:00
|
|
|
|
2014-11-04 16:51:32 +00:00
|
|
|
if (intent != null && settings.optBoolean("resume")) {
|
|
|
|
PendingIntent contentIntent = PendingIntent.getActivity(
|
2016-08-17 11:14:47 +02:00
|
|
|
context, NOTIFICATION_ID, intent,
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
2014-11-04 14:48:14 +00:00
|
|
|
|
|
|
|
notification.setContentIntent(contentIntent);
|
|
|
|
}
|
2014-10-26 16:47:18 +01:00
|
|
|
|
2016-08-17 11:14:47 +02:00
|
|
|
return notification.build();
|
2014-10-26 16:47:18 +01:00
|
|
|
}
|
|
|
|
|
2016-08-17 11:14:47 +02:00
|
|
|
/**
|
|
|
|
* Update the notification.
|
2016-08-17 11:52:31 +02:00
|
|
|
*
|
|
|
|
* @param settings
|
|
|
|
* The config settings
|
2016-08-17 11:14:47 +02:00
|
|
|
*/
|
2016-08-17 11:52:31 +02:00
|
|
|
public void updateNotification (JSONObject settings) {
|
|
|
|
boolean isSilent = settings.optBoolean("silent", false);
|
|
|
|
|
|
|
|
if (isSilent) {
|
|
|
|
stopForeground(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Notification notification = makeNotification(settings);
|
2016-08-17 11:14:47 +02:00
|
|
|
NotificationManager service = (NotificationManager)
|
|
|
|
getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
|
|
|
service.notify(NOTIFICATION_ID, notification);
|
2015-06-03 19:45:26 +01:00
|
|
|
}
|
|
|
|
|
2014-10-26 16:47:18 +01:00
|
|
|
/**
|
|
|
|
* Retrieves the resource ID of the app icon.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The resource ID of the app icon
|
|
|
|
*/
|
2015-01-01 17:24:37 +01:00
|
|
|
private int getIconResId() {
|
2015-06-04 10:59:11 +01:00
|
|
|
JSONObject settings = BackgroundMode.getSettings();
|
2014-10-26 16:47:18 +01:00
|
|
|
Context context = getApplicationContext();
|
|
|
|
Resources res = context.getResources();
|
|
|
|
String pkgName = context.getPackageName();
|
2016-08-17 11:14:47 +02:00
|
|
|
String icon = settings.optString("icon", "icon");
|
2014-10-26 16:47:18 +01:00
|
|
|
|
2016-08-17 11:14:47 +02:00
|
|
|
return res.getIdentifier(icon, "drawable", pkgName);
|
2014-10-26 16:47:18 +01:00
|
|
|
}
|
|
|
|
}
|