From c09eebe6e684bcdb6e4c6507c15e1b570766103f Mon Sep 17 00:00:00 2001 From: KennyHuRadar <139801512+KennyHuRadar@users.noreply.github.com> Date: Fri, 10 Nov 2023 11:35:53 -0500 Subject: [PATCH] Add colour field to setForegroundOptions (#320) * Change notification icon from an integer to a string in RadarForegroundService and RadarTrackingOptions * Change notification icon type from integer to string in RadarForegroundService and RadarLocationManager * fix for test * bump version and also add toJson field * added back icon as default * change val to var * make calls with unexplicit parameter naming non-breaking * cahnge back icon int * added color field and changed tests --- sdk/build.gradle | 2 +- .../java/io/radar/sdk/RadarForegroundService.kt | 5 +++++ .../main/java/io/radar/sdk/RadarLocationManager.kt | 1 + .../main/java/io/radar/sdk/RadarTrackingOptions.kt | 13 ++++++++++--- sdk/src/test/java/io/radar/sdk/RadarTest.kt | 10 ++++++---- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/sdk/build.gradle b/sdk/build.gradle index 8564dd5b..0abb5a46 100644 --- a/sdk/build.gradle +++ b/sdk/build.gradle @@ -10,7 +10,7 @@ apply plugin: "org.jetbrains.dokka" apply plugin: 'io.radar.mvnpublish' ext { - radarVersion = '3.8.14' + radarVersion = '3.8.15' } String buildNumber = ".${System.currentTimeMillis()}" diff --git a/sdk/src/main/java/io/radar/sdk/RadarForegroundService.kt b/sdk/src/main/java/io/radar/sdk/RadarForegroundService.kt index 6144b280..28303984 100644 --- a/sdk/src/main/java/io/radar/sdk/RadarForegroundService.kt +++ b/sdk/src/main/java/io/radar/sdk/RadarForegroundService.kt @@ -2,6 +2,7 @@ package io.radar.sdk import android.app.* import android.content.Intent +import android.graphics.Color import android.os.Build import android.os.Bundle import android.os.IBinder @@ -57,6 +58,7 @@ class RadarForegroundService : Service() { val text = extras?.getString("text") ?: "Location tracking started" var icon = extras?.getInt("icon") ?: 0 var iconString = extras?.getString("iconString") ?: this.applicationInfo.icon.toString() + var iconColor = extras?.getString("iconColor") ?: "" var smallIcon = resources.getIdentifier(iconString, "drawable", applicationContext.packageName) if (icon != 0){ smallIcon = resources.getIdentifier(icon.toString(), "drawable", applicationContext.packageName) @@ -72,6 +74,9 @@ class RadarForegroundService : Service() { if (title != null && title.isNotEmpty()) { builder = builder.setContentTitle(title as CharSequence?) } + if (iconColor.isNotEmpty()) { + builder.setColor(Color.parseColor(iconColor)) + } try { extras?.getString("activity")?.let { val activityClass = Class.forName(it) diff --git a/sdk/src/main/java/io/radar/sdk/RadarLocationManager.kt b/sdk/src/main/java/io/radar/sdk/RadarLocationManager.kt index a0e9d73a..a9ffef0e 100644 --- a/sdk/src/main/java/io/radar/sdk/RadarLocationManager.kt +++ b/sdk/src/main/java/io/radar/sdk/RadarLocationManager.kt @@ -655,6 +655,7 @@ internal class RadarLocationManager( .putExtra("text", foregroundService.text) .putExtra("icon", foregroundService.icon) .putExtra("iconString", foregroundService.iconString) + .putExtra("iconColor", foregroundService.iconColor) .putExtra("activity", foregroundService.activity) logger.d("Starting foreground service with intent | intent = $intent") context.applicationContext.startForegroundService(intent) diff --git a/sdk/src/main/java/io/radar/sdk/RadarTrackingOptions.kt b/sdk/src/main/java/io/radar/sdk/RadarTrackingOptions.kt index fbcafaaf..a8f66cdb 100644 --- a/sdk/src/main/java/io/radar/sdk/RadarTrackingOptions.kt +++ b/sdk/src/main/java/io/radar/sdk/RadarTrackingOptions.kt @@ -294,6 +294,11 @@ data class RadarTrackingOptions( * Determines the notification icon, like `R.drawable.ic_your_icon`. Optional, defaults to `applicationContext.applicationInfo.icon`. */ val iconString: String? = null, + + /** + * Determines the color notification icon. Optional. + */ + val iconColor: String? = null, ) { companion object { @@ -301,6 +306,7 @@ data class RadarTrackingOptions( internal const val KEY_FOREGROUND_SERVICE_TITLE = "title" internal const val KEY_FOREGROUND_SERVICE_ICON = "icon" internal const val KEY_FOREGROUND_SERVICE_ICON_STRING = "iconString" + internal const val KEY_FOREGROUND_SERVICE_ICON_COLOR = "iconColor" internal const val KEY_FOREGROUND_SERVICE_UPDATES_ONLY = "updatesOnly" internal const val KEY_FOREGROUND_SERVICE_ACTIVITY = "activity" internal const val KEY_FOREGROUND_SERVICE_IMPORTANCE = "importance" @@ -317,13 +323,13 @@ data class RadarTrackingOptions( val title = if (obj.isNull(KEY_FOREGROUND_SERVICE_TITLE)) null else obj.optString(KEY_FOREGROUND_SERVICE_TITLE) val icon = if (obj.isNull(KEY_FOREGROUND_SERVICE_ICON)) null else obj.optInt(KEY_FOREGROUND_SERVICE_ICON) val iconString = if (obj.isNull(KEY_FOREGROUND_SERVICE_ICON_STRING)) null else obj.optString(KEY_FOREGROUND_SERVICE_ICON_STRING) + val iconColor = if (obj.isNull(KEY_FOREGROUND_SERVICE_ICON_COLOR)) null else obj.optString(KEY_FOREGROUND_SERVICE_ICON_COLOR) val updatesOnly: Boolean = obj.optBoolean(KEY_FOREGROUND_SERVICE_UPDATES_ONLY) val activity = if (obj.isNull(KEY_FOREGROUND_SERVICE_ACTIVITY)) null else obj.optString(KEY_FOREGROUND_SERVICE_ACTIVITY) val importance = if (obj.isNull(KEY_FOREGROUND_SERVICE_IMPORTANCE)) null else obj.optInt(KEY_FOREGROUND_SERVICE_IMPORTANCE) val id = if (obj.isNull(KEY_FOREGROUND_SERVICE_ID)) null else obj.optInt(KEY_FOREGROUND_SERVICE_ID) val channelName = if (obj.isNull(KEY_FOREGROUND_SERVICE_CHANNEL_NAME)) null else obj.optString(KEY_FOREGROUND_SERVICE_CHANNEL_NAME) - - return RadarTrackingOptionsForegroundService(text, title, icon, updatesOnly, activity, importance, id, channelName, iconString) + return RadarTrackingOptionsForegroundService(text, title, icon, updatesOnly, activity, importance, id, channelName, iconString, iconColor) } } @@ -333,7 +339,8 @@ data class RadarTrackingOptions( obj.put(KEY_FOREGROUND_SERVICE_TEXT, text) obj.put(KEY_FOREGROUND_SERVICE_TITLE, title) obj.put(KEY_FOREGROUND_SERVICE_ICON, icon) - obj.put(KEY_FOREGROUND_SERVICE_ICON_STRING, iconString) + obj.put(KEY_FOREGROUND_SERVICE_ICON_STRING, iconString) + obj.put(KEY_FOREGROUND_SERVICE_ICON_COLOR, iconColor) obj.put(KEY_FOREGROUND_SERVICE_ACTIVITY, activity) obj.put(KEY_FOREGROUND_SERVICE_UPDATES_ONLY, updatesOnly) obj.put(KEY_FOREGROUND_SERVICE_IMPORTANCE, importance) diff --git a/sdk/src/test/java/io/radar/sdk/RadarTest.kt b/sdk/src/test/java/io/radar/sdk/RadarTest.kt index 4105d104..12648911 100644 --- a/sdk/src/test/java/io/radar/sdk/RadarTest.kt +++ b/sdk/src/test/java/io/radar/sdk/RadarTest.kt @@ -533,10 +533,12 @@ class RadarTest { Radar.stopTracking() Radar.setForegroundServiceOptions(RadarTrackingOptions.RadarTrackingOptionsForegroundService( - "Text", - "Title", - 1337, - true + text="Text", + title = "Title", + icon = 1337, + iconString = "test", + updatesOnly = true, + iconColor = "#FF0000" )) val options = RadarTrackingOptions.EFFICIENT