Support for multi line text [Fixes #171]

This commit is contained in:
Sebastián Katzer 2017-01-01 22:35:32 +01:00
parent 8e3a3528ac
commit 681f2f9275
2 changed files with 32 additions and 33 deletions

View File

@ -119,6 +119,7 @@ public class ForegroundService extends Service {
*/ */
private void sleepWell() { private void sleepWell() {
stopForeground(true); stopForeground(true);
getNotificationManager().cancel(NOTIFICATION_ID);
if (wakeLock != null) { if (wakeLock != null) {
wakeLock.release(); wakeLock.release();
@ -129,10 +130,6 @@ 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 by using the default settings. * 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() { private Notification makeNotification() {
return makeNotification(BackgroundMode.getSettings()); return makeNotification(BackgroundMode.getSettings());
@ -142,14 +139,12 @@ 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.
* *
* @param settings * @param settings The config settings
* The config settings
*
* @return
* A local ongoing notification which pending intent is bound to the
* main activity.
*/ */
private Notification makeNotification(JSONObject settings) { private Notification makeNotification(JSONObject settings) {
String text = settings.optString("text", "");
boolean bigText = settings.optBoolean("bigText", false);
Context context = getApplicationContext(); Context context = getApplicationContext();
String pkgName = context.getPackageName(); String pkgName = context.getPackageName();
Intent intent = context.getPackageManager() Intent intent = context.getPackageManager()
@ -157,11 +152,16 @@ public class ForegroundService extends Service {
Notification.Builder notification = new Notification.Builder(context) Notification.Builder notification = new Notification.Builder(context)
.setContentTitle(settings.optString("title", "")) .setContentTitle(settings.optString("title", ""))
.setContentText(settings.optString("text", "")) .setContentText(text)
.setTicker(settings.optString("ticker", "")) .setTicker(settings.optString("ticker", ""))
.setOngoing(true) .setOngoing(true)
.setSmallIcon(getIconResId()); .setSmallIcon(getIconResId());
if (bigText || text.contains("\n")) {
notification.setStyle(
new Notification.BigTextStyle().bigText(text));
}
setColor(notification, settings); setColor(notification, settings);
if (intent != null && settings.optBoolean("resume")) { if (intent != null && settings.optBoolean("resume")) {
@ -178,8 +178,7 @@ public class ForegroundService extends Service {
/** /**
* Update the notification. * Update the notification.
* *
* @param settings * @param settings The config settings
* The config settings
*/ */
protected void updateNotification (JSONObject settings) { protected void updateNotification (JSONObject settings) {
boolean isSilent = settings.optBoolean("silent", false); boolean isSilent = settings.optBoolean("silent", false);
@ -190,17 +189,13 @@ public class ForegroundService extends Service {
} }
Notification notification = makeNotification(settings); Notification notification = makeNotification(settings);
NotificationManager service = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
service.notify(NOTIFICATION_ID, notification); getNotificationManager().notify(
NOTIFICATION_ID, notification);
} }
/** /**
* Retrieves the resource ID of the app icon. * Retrieves the resource ID of the app icon.
*
* @return
* The resource ID of the app icon
*/ */
private int getIconResId() { private int getIconResId() {
JSONObject settings = BackgroundMode.getSettings(); JSONObject settings = BackgroundMode.getSettings();
@ -222,14 +217,10 @@ public class ForegroundService extends Service {
/** /**
* Retrieve resource id of the specified icon. * Retrieve resource id of the specified icon.
* *
* @param res * @param res The app resource bundle.
* The app resource bundle. * @param icon The name of the icon.
* @param icon * @param type The resource type where to look for.
* The name of the icon. * @param pkgName The name of the package.
* @param type
* The resource type where to look for.
* @param pkgName
* The name of the package.
* *
* @return The resource id or 0 if not found. * @return The resource id or 0 if not found.
*/ */
@ -248,10 +239,8 @@ public class ForegroundService extends Service {
/** /**
* Set notification color if its supported by the SDK. * Set notification color if its supported by the SDK.
* *
* @param notification * @param notification A Notification.Builder instance
* A Notification.Builder instance * @param settings A JSON dict containing the color definition (red: FF0000)
* @param settings
* A JSON dict containing the color definition (red: FF0000)
*/ */
private void setColor(Notification.Builder notification, private void setColor(Notification.Builder notification,
JSONObject settings) { JSONObject settings) {
@ -276,4 +265,13 @@ public class ForegroundService extends Service {
e.printStackTrace(); e.printStackTrace();
} }
} }
/**
* Shared manager for the notification service.
*/
private NotificationManager getNotificationManager() {
return (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);
}
} }

View File

@ -274,8 +274,9 @@ exports._isActive = false;
*/ */
exports._defaults = { exports._defaults = {
title: 'App is running in background', title: 'App is running in background',
text: 'Doing heavy tasks.', text: "Doing heavy tasks.",
ticker: 'Running in background', ticker: 'Running in background',
bigText: true,
resume: true, resume: true,
silent: false, silent: false,
color: undefined, color: undefined,