11package com.mparticle.kits
22
33import android.content.Context
4- import androidx.core.content.edit
5- import androidx.core.graphics.toColorInt
4+ import android.graphics.Color
65import com.mparticle.MParticle
76import com.mparticle.internal.Logger
87import com.mparticle.kits.UrbanAirshipKit.ChannelIdListener
9- import com.urbanairship.Airship
108import com.urbanairship.AirshipConfigOptions
119import com.urbanairship.Autopilot
10+ import com.urbanairship.UAirship
11+ import com.urbanairship.util.UAStringUtil
1212
1313/* *
1414 * Autopilot for UrbanAirshipKit integration.
@@ -35,28 +35,29 @@ class MParticleAutopilot : Autopilot() {
3535 .setInProduction(true )
3636 }
3737 if (" EU" .equals(preferences.getString(DOMAIN , null ), true )) {
38- optionsBuilder.setSite(AirshipConfigOptions .Site . SITE_EU )
38+ optionsBuilder.setSite(AirshipConfigOptions .SITE_EU )
3939 }
4040 val customDomain = preferences.getString(CUSTOM_DOMAIN_PROXY_URL , null )
41- if (! customDomain.isNullOrEmpty( )) {
41+ if (! UAStringUtil .isEmpty(customDomain )) {
4242 optionsBuilder.setInitialConfigUrl(customDomain).setUrlAllowList(arrayOf(customDomain))
4343 }
4444 return optionsBuilder.build()
4545 }
4646
47- override fun onAirshipReady (context : Context ) {
47+ override fun onAirshipReady (airship : UAirship ) {
4848 val preferences =
49- Airship .application.applicationContext
49+ UAirship
50+ .getApplicationContext()
5051 .getSharedPreferences(PREFERENCE_NAME , Context .MODE_PRIVATE )
5152 if (preferences.getBoolean(FIRST_RUN_KEY , true )) {
52- preferences.edit { putBoolean(FIRST_RUN_KEY , false ) }
53- Airship .push .userNotificationsEnabled = true
53+ preferences.edit(). putBoolean(FIRST_RUN_KEY , false ). apply ()
54+ airship.pushManager .userNotificationsEnabled = true
5455 }
5556
5657 // Restore the last registration token
57- val token = Airship .push .pushToken
58+ val token = airship.pushManager .pushToken
5859 MParticlePushProvider .instance.setRegistrationToken(token)
59- Airship .channel.addChannelListener { callChannelIdListener() }
60+ airship .channel.addChannelListener { callChannelIdListener() }
6061
6162 callChannelIdListener()
6263 }
@@ -95,43 +96,41 @@ class MParticleAutopilot : Autopilot() {
9596 context : Context ,
9697 configuration : UrbanAirshipConfiguration ,
9798 ) {
98- context
99- .getSharedPreferences(PREFERENCE_NAME , Context .MODE_PRIVATE )
100- .edit {
101- putString(APP_KEY , configuration.applicationKey)
102- .putString(APP_SECRET , configuration.applicationSecret)
103- .putString(DOMAIN , configuration.domain)
104- .putString(CUSTOM_DOMAIN_PROXY_URL , configuration.customDomainProxyUrl)
99+ val editor =
100+ context
101+ .getSharedPreferences(PREFERENCE_NAME , Context .MODE_PRIVATE )
102+ .edit()
103+ .putString(APP_KEY , configuration.applicationKey)
104+ .putString(APP_SECRET , configuration.applicationSecret)
105+ .putString(DOMAIN , configuration.domain)
106+ .putString(CUSTOM_DOMAIN_PROXY_URL , configuration.customDomainProxyUrl)
105107
106- // Convert accent color hex string to an int
107- val accentColor = configuration.notificationColor
108- if (! accentColor.isNullOrEmpty()) {
109- try {
110- putInt(NOTIFICATION_COLOR , accentColor.toColorInt())
111- } catch (e: IllegalArgumentException ) {
112- Logger .warning(
113- e,
114- " Unable to parse notification accent color: $accentColor " ,
115- )
116- }
117- }
108+ // Convert accent color hex string to an int
109+ val accentColor = configuration.notificationColor
110+ if (! UAStringUtil .isEmpty(accentColor)) {
111+ try {
112+ editor.putInt(NOTIFICATION_COLOR , Color .parseColor(accentColor))
113+ } catch (e: IllegalArgumentException ) {
114+ Logger .warning(e, " Unable to parse notification accent color: $accentColor " )
115+ }
116+ }
118117
119- // Convert notification name to a drawable resource ID
120- val notificationIconName = configuration.notificationIconName
121- if (! notificationIconName.isNullOrEmpty()) {
122- val id =
123- context.resources.getIdentifier(
124- notificationIconName,
125- " drawable" ,
126- context.packageName,
127- )
128- if (id != 0 ) {
129- putInt(NOTIFICATION_ICON_NAME , id)
130- } else {
131- Logger .error(" Unable to find notification icon with name: $notificationIconName " )
132- }
133- }
118+ // Convert notification name to a drawable resource ID
119+ val notificationIconName = configuration.notificationIconName
120+ if (! UAStringUtil .isEmpty(notificationIconName)) {
121+ val id =
122+ context.resources.getIdentifier(
123+ notificationIconName,
124+ " drawable" ,
125+ context.packageName,
126+ )
127+ if (id != 0 ) {
128+ editor.putInt(NOTIFICATION_ICON_NAME , id)
129+ } else {
130+ Logger .error(" Unable to find notification icon with name: $notificationIconName " )
134131 }
132+ }
133+ editor.apply ()
135134 }
136135 }
137136}
0 commit comments