cordova-plugin-run-in-backg.../src/android/ForegroundService.java

281 lines
8.2 KiB
Java
Raw Normal View History

2014-10-26 16:47:18 +01:00
/*
2017-01-01 22:40:41 +01:00
Copyright 2013-2017 appPlant GmbH
2014-10-26 16:47:18 +01:00
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-10-26 16:47:18 +01:00
package de.appplant.cordova.plugin.background;
2017-02-06 13:34:27 +01:00
import android.annotation.TargetApi;
2014-10-26 16:47:18 +01:00
import android.app.Notification;
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;
import android.os.Binder;
2016-08-17 13:55:51 +02:00
import android.os.Build;
2014-10-26 16:47:18 +01:00
import android.os.IBinder;
import android.os.PowerManager;
2014-10-26 16:47:18 +01:00
import org.json.JSONObject;
2017-02-06 13:34:27 +01:00
import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
2016-08-17 13:55:51 +02:00
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
2017-01-23 10:12:42 +01:00
// Default title of the background notification
private static final String NOTIFICATION_TITLE =
"App is running in background";
// Default text of the background notification
private static final String NOTIFICATION_TEXT =
"Doing heavy tasks.";
// Default icon of the background notification
private static final String NOTIFICATION_ICON = "icon";
// 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
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) {
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
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();
}
/**
* 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.
*/
2016-08-17 13:26:47 +02:00
private 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) {
startForeground(NOTIFICATION_ID, makeNotification());
}
2014-10-26 16:47:18 +01:00
2016-08-17 13:26:47 +02:00
PowerManager powerMgr = (PowerManager)
getSystemService(POWER_SERVICE);
wakeLock = powerMgr.newWakeLock(
2017-02-06 13:34:27 +01:00
PARTIAL_WAKE_LOCK, "BackgroundMode");
wakeLock.acquire();
2014-10-26 16:47:18 +01:00
}
/**
* Stop background mode.
*/
private void sleepWell() {
stopForeground(true);
getNotificationManager().cancel(NOTIFICATION_ID);
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
*/
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
2016-08-17 11:52:31 +02:00
*/
private Notification makeNotification(JSONObject settings) {
2017-01-23 10:12:42 +01:00
String title = settings.optString("title", NOTIFICATION_TITLE);
String text = settings.optString("text", NOTIFICATION_TEXT);
boolean bigText = settings.optBoolean("bigText", false);
2016-08-17 11:52:31 +02:00
Context context = getApplicationContext();
String pkgName = context.getPackageName();
Intent intent = context.getPackageManager()
2014-10-26 16:47:18 +01:00
.getLaunchIntentForPackage(pkgName);
Notification.Builder notification = new Notification.Builder(context)
2017-01-23 10:12:42 +01:00
.setContentTitle(title)
.setContentText(text)
.setOngoing(true)
2017-01-16 15:18:19 +01:00
.setSmallIcon(getIconResId(settings));
2017-01-26 14:45:01 +01:00
if (settings.optBoolean("hidden", true)) {
notification.setPriority(Notification.PRIORITY_MIN);
}
if (bigText || text.contains("\n")) {
notification.setStyle(
new Notification.BigTextStyle().bigText(text));
}
2016-08-17 13:55:51 +02:00
setColor(notification, settings);
if (intent != null && settings.optBoolean("resume")) {
PendingIntent contentIntent = PendingIntent.getActivity(
context, NOTIFICATION_ID, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
2017-02-06 13:34:27 +01:00
notification.setContentIntent(contentIntent);
}
2014-10-26 16:47:18 +01:00
return notification.build();
2014-10-26 16:47:18 +01:00
}
/**
* Update the notification.
2016-08-17 11:52:31 +02:00
*
* @param settings The config settings
*/
2016-08-17 13:26:47 +02:00
protected void updateNotification (JSONObject settings) {
2016-08-17 11:52:31 +02:00
boolean isSilent = settings.optBoolean("silent", false);
if (isSilent) {
stopForeground(true);
return;
}
Notification notification = makeNotification(settings);
2017-02-06 13:34:27 +01:00
getNotificationManager().notify(NOTIFICATION_ID, notification);
}
2014-10-26 16:47:18 +01:00
/**
* Retrieves the resource ID of the app icon.
2017-01-16 15:18:19 +01:00
*
* @param settings A JSON dict containing the icon name.
2014-10-26 16:47:18 +01:00
*/
2017-01-16 15:18:19 +01:00
private int getIconResId(JSONObject settings) {
2017-02-07 09:12:04 +01:00
String icon = settings.optString("icon", NOTIFICATION_ICON);
2014-10-26 16:47:18 +01:00
// cordova-android 6 uses mipmaps
2017-02-07 09:12:04 +01:00
int resId = getIconResId(icon, "mipmap");
2016-08-17 13:26:47 +02:00
if (resId == 0) {
2017-02-07 09:12:04 +01:00
resId = getIconResId(icon, "drawable");
}
return resId;
}
/**
* Retrieve resource id of the specified icon.
*
* @param icon The name of the icon.
* @param type The resource type where to look for.
*
* @return The resource id or 0 if not found.
*/
2017-02-07 09:12:04 +01:00
private int getIconResId(String icon, String type) {
Resources res = getResources();
String pkgName = getPackageName();
int resId = res.getIdentifier(icon, type, pkgName);
if (resId == 0) {
resId = res.getIdentifier("icon", type, pkgName);
2016-08-17 13:26:47 +02:00
}
return resId;
2014-10-26 16:47:18 +01:00
}
2016-08-17 13:55:51 +02:00
/**
* Set notification color if its supported by the SDK.
*
* @param notification A Notification.Builder instance
* @param settings A JSON dict containing the color definition (red: FF0000)
2016-08-17 13:55:51 +02:00
*/
2017-02-06 13:34:27 +01:00
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
2016-08-17 13:55:51 +02:00
private void setColor(Notification.Builder notification,
JSONObject settings) {
String hex = settings.optString("color", null);
if (Build.VERSION.SDK_INT < 21 || hex == null)
return;
try {
2017-01-16 10:17:25 +01:00
int aRGB = Integer.parseInt(hex, 16) + 0xFF000000;
2017-02-06 13:34:27 +01:00
notification.setColor(aRGB);
2017-01-16 10:17:25 +01:00
} catch (Exception e) {
2016-08-17 13:55:51 +02:00
e.printStackTrace();
}
}
/**
* Shared manager for the notification service.
*/
private NotificationManager getNotificationManager() {
2017-02-06 13:34:27 +01:00
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
2014-10-26 16:47:18 +01:00
}