1 package host.exp.exponent.notifications
2 
3 import android.content.Context
4 import android.util.Log
5 import expo.modules.notifications.notifications.channels.managers.NotificationsChannelManager
6 import expo.modules.notifications.notifications.interfaces.NotificationBuilder
7 import expo.modules.notifications.notifications.model.Notification
8 import expo.modules.notifications.notifications.presentation.builders.CategoryAwareNotificationBuilder
9 import expo.modules.notifications.service.delegates.SharedPreferencesNotificationCategoriesStore
10 import expo.modules.manifests.core.Manifest
11 import host.exp.exponent.Constants
12 import host.exp.exponent.ExponentManifest
13 import host.exp.exponent.di.NativeModuleDepsProvider
14 import host.exp.exponent.kernel.ExperienceKey
15 import host.exp.exponent.notifications.model.ScopedNotificationRequest
16 import host.exp.exponent.storage.ExponentDB
17 import host.exp.expoview.R
18 import org.json.JSONException
19 import versioned.host.exp.exponent.modules.api.notifications.channels.ScopedNotificationsChannelManager
20 import versioned.host.exp.exponent.modules.api.notifications.channels.ScopedNotificationsGroupManager
21 import javax.inject.Inject
22 
23 open class ScopedExpoNotificationBuilder(
24   context: Context,
25   store: SharedPreferencesNotificationCategoriesStore?
26 ) : CategoryAwareNotificationBuilder(context, store!!) {
27   @Inject
28   lateinit var exponentManifest: ExponentManifest
29 
30   var manifest: Manifest? = null
31   var experienceKey: ExperienceKey? = null
32 
33   override fun setNotification(notification: Notification): NotificationBuilder {
34     super.setNotification(notification)
35 
36     // We parse manifest here to have easy access to it from other methods.
37     val requester = getNotification().notificationRequest
38     if (requester is ScopedNotificationRequest) {
39       val experienceScopeKey = requester.experienceScopeKeyString
40       try {
41         val exponentDBObject = ExponentDB.experienceScopeKeyToExperienceSync(experienceScopeKey!!)
42         manifest = exponentDBObject!!.manifest
43         experienceKey = ExperienceKey.fromManifest(manifest!!)
44       } catch (e: JSONException) {
45         Log.e("notifications", "Couldn't parse manifest.", e)
46         e.printStackTrace()
47       }
48     }
49     return this
50   }
51 
52   override fun getNotificationsChannelManager(): NotificationsChannelManager {
53     return if (experienceKey == null) {
54       super.getNotificationsChannelManager()
55     } else ScopedNotificationsChannelManager(
56       context,
57       experienceKey,
58       ScopedNotificationsGroupManager(
59         context,
60         experienceKey
61       )
62     )
63   }
64 
65   override fun getIcon(): Int {
66     return if (Constants.isStandaloneApp()) R.drawable.shell_notification_icon else R.drawable.notification_icon
67   }
68 
69   override fun getColor(): Number? {
70     // Try to use color defined in notification content
71     if (notificationContent.color != null) {
72       return notificationContent.color
73     }
74     return if (manifest == null) {
75       super.getColor()
76     } else NotificationHelper.getColor(null, manifest!!, exponentManifest)
77   }
78 
79   init {
80     NativeModuleDepsProvider.instance.inject(ScopedExpoNotificationBuilder::class.java, this)
81   }
82 }
83