Re-add color

This commit is contained in:
Sebastián Katzer 2016-08-17 13:55:51 +02:00
parent aed89d8503
commit f7e5684a66
2 changed files with 39 additions and 0 deletions

View File

@ -29,11 +29,15 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Binder; import android.os.Binder;
import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.os.PowerManager; import android.os.PowerManager;
import org.json.JSONObject; import org.json.JSONObject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/** /**
* Puts the service in a foreground state, where the system considers it to be * 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 * something the user is actively aware of and thus not a candidate for killing
@ -158,6 +162,8 @@ public class ForegroundService extends Service {
.setOngoing(true) .setOngoing(true)
.setSmallIcon(getIconResId()); .setSmallIcon(getIconResId());
setColor(notification, settings);
if (intent != null && settings.optBoolean("resume")) { if (intent != null && settings.optBoolean("resume")) {
PendingIntent contentIntent = PendingIntent.getActivity( PendingIntent contentIntent = PendingIntent.getActivity(
context, NOTIFICATION_ID, intent, context, NOTIFICATION_ID, intent,
@ -211,4 +217,36 @@ public class ForegroundService extends Service {
return resId; return resId;
} }
/**
* 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)
*/
private void setColor(Notification.Builder notification,
JSONObject settings) {
String hex = settings.optString("color", null);
if (Build.VERSION.SDK_INT < 21 || hex == null)
return;
int aRGB = Integer.parseInt(hex, 16) + 0xFF000000;
try {
Method setColorMethod = notification.getClass().getMethod(
"setColor", int.class);
setColorMethod.invoke(notification, aRGB);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
} }

View File

@ -58,6 +58,7 @@ exports._defaults = {
ticker: 'Running in background', ticker: 'Running in background',
resume: true, resume: true,
silent: false, silent: false,
color: undefined,
icon: 'icon' icon: 'icon'
}; };