From 477ac52d16e97004681a86d2e77afe827ff6d9eb Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 31 Aug 2022 17:52:39 +0100 Subject: [PATCH] checking for firebase inclusion or play services availability and removing FCM option if it's missing --- .../kotlin/app/dapk/st/graph/AppModule.kt | 2 +- .../kotlin/app/dapk/st/push/PushModule.kt | 5 ++-- .../app/dapk/st/push/PushTokenRegistrars.kt | 29 ++++++++++++------- .../MessagingPushTokenRegistrar.kt} | 6 ++-- .../MessagingServiceAdapter.kt | 2 +- .../dapk/st/firebase/messaging/Messaging.kt | 2 ++ .../dapk/st/firebase/messaging/Messaging.kt | 4 +++ 7 files changed, 33 insertions(+), 17 deletions(-) rename domains/android/push/src/main/kotlin/app/dapk/st/push/{firebase/FirebasePushTokenRegistrar.kt => messaging/MessagingPushTokenRegistrar.kt} (91%) rename domains/android/push/src/main/kotlin/app/dapk/st/push/{firebase => messaging}/MessagingServiceAdapter.kt (96%) diff --git a/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt b/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt index b5f35a3..8f20819 100644 --- a/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt +++ b/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt @@ -56,7 +56,7 @@ import app.dapk.st.olm.OlmPersistenceWrapper import app.dapk.st.olm.OlmWrapper import app.dapk.st.profile.ProfileModule import app.dapk.st.push.PushModule -import app.dapk.st.push.firebase.MessagingServiceAdapter +import app.dapk.st.push.messaging.MessagingServiceAdapter import app.dapk.st.settings.SettingsModule import app.dapk.st.share.ShareEntryModule import app.dapk.st.tracking.TrackingModule diff --git a/domains/android/push/src/main/kotlin/app/dapk/st/push/PushModule.kt b/domains/android/push/src/main/kotlin/app/dapk/st/push/PushModule.kt index 1333aff..dc8c77e 100644 --- a/domains/android/push/src/main/kotlin/app/dapk/st/push/PushModule.kt +++ b/domains/android/push/src/main/kotlin/app/dapk/st/push/PushModule.kt @@ -8,8 +8,7 @@ import app.dapk.st.core.extensions.unsafeLazy import app.dapk.st.domain.Preferences import app.dapk.st.domain.push.PushTokenRegistrarPreferences import app.dapk.st.firebase.messaging.Messaging -import app.dapk.st.firebase.messaging.MessagingModule -import app.dapk.st.push.firebase.FirebasePushTokenRegistrar +import app.dapk.st.push.messaging.MessagingPushTokenRegistrar import app.dapk.st.push.unifiedpush.UnifiedPushRegistrar class PushModule( @@ -24,7 +23,7 @@ class PushModule( private val registrars by unsafeLazy { PushTokenRegistrars( context, - FirebasePushTokenRegistrar( + MessagingPushTokenRegistrar( errorTracker, pushHandler, messaging, diff --git a/domains/android/push/src/main/kotlin/app/dapk/st/push/PushTokenRegistrars.kt b/domains/android/push/src/main/kotlin/app/dapk/st/push/PushTokenRegistrars.kt index 7ce0658..44a9679 100644 --- a/domains/android/push/src/main/kotlin/app/dapk/st/push/PushTokenRegistrars.kt +++ b/domains/android/push/src/main/kotlin/app/dapk/st/push/PushTokenRegistrars.kt @@ -2,7 +2,7 @@ package app.dapk.st.push import android.content.Context import app.dapk.st.domain.push.PushTokenRegistrarPreferences -import app.dapk.st.push.firebase.FirebasePushTokenRegistrar +import app.dapk.st.push.messaging.MessagingPushTokenRegistrar import app.dapk.st.push.unifiedpush.UnifiedPushRegistrar import org.unifiedpush.android.connector.UnifiedPush @@ -11,7 +11,7 @@ private val NONE = Registrar("None") class PushTokenRegistrars( private val context: Context, - private val firebasePushTokenRegistrar: FirebasePushTokenRegistrar, + private val messagingPushTokenRegistrar: MessagingPushTokenRegistrar, private val unifiedPushRegistrar: UnifiedPushRegistrar, private val pushTokenStore: PushTokenRegistrarPreferences, ) : PushTokenRegistrar { @@ -19,27 +19,36 @@ class PushTokenRegistrars( private var selection: Registrar? = null fun options(): List { - return listOf(NONE, FIREBASE_OPTION) + UnifiedPush.getDistributors(context).map { Registrar(it) } + val messagingOption = when (messagingPushTokenRegistrar.isAvailable()) { + true -> FIREBASE_OPTION + else -> null + } + return listOfNotNull(NONE, messagingOption) + UnifiedPush.getDistributors(context).map { Registrar(it) } } - suspend fun currentSelection() = selection ?: (pushTokenStore.currentSelection()?.let { Registrar(it) } ?: FIREBASE_OPTION).also { selection = it } + suspend fun currentSelection() = selection ?: (pushTokenStore.currentSelection()?.let { Registrar(it) } ?: defaultSelection()).also { selection = it } + + private fun defaultSelection() = when (messagingPushTokenRegistrar.isAvailable()) { + true -> FIREBASE_OPTION + else -> NONE + } suspend fun makeSelection(option: Registrar) { selection = option pushTokenStore.store(option.id) when (option) { NONE -> { - firebasePushTokenRegistrar.unregister() + messagingPushTokenRegistrar.unregister() unifiedPushRegistrar.unregister() } FIREBASE_OPTION -> { unifiedPushRegistrar.unregister() - firebasePushTokenRegistrar.registerCurrentToken() + messagingPushTokenRegistrar.registerCurrentToken() } else -> { - firebasePushTokenRegistrar.unregister() + messagingPushTokenRegistrar.unregister() unifiedPushRegistrar.registerSelection(option) } } @@ -47,7 +56,7 @@ class PushTokenRegistrars( override suspend fun registerCurrentToken() { when (selection) { - FIREBASE_OPTION -> firebasePushTokenRegistrar.registerCurrentToken() + FIREBASE_OPTION -> messagingPushTokenRegistrar.registerCurrentToken() NONE -> { // do nothing } @@ -58,10 +67,10 @@ class PushTokenRegistrars( override fun unregister() { when (selection) { - FIREBASE_OPTION -> firebasePushTokenRegistrar.unregister() + FIREBASE_OPTION -> messagingPushTokenRegistrar.unregister() NONE -> { runCatching { - firebasePushTokenRegistrar.unregister() + messagingPushTokenRegistrar.unregister() unifiedPushRegistrar.unregister() } } diff --git a/domains/android/push/src/main/kotlin/app/dapk/st/push/firebase/FirebasePushTokenRegistrar.kt b/domains/android/push/src/main/kotlin/app/dapk/st/push/messaging/MessagingPushTokenRegistrar.kt similarity index 91% rename from domains/android/push/src/main/kotlin/app/dapk/st/push/firebase/FirebasePushTokenRegistrar.kt rename to domains/android/push/src/main/kotlin/app/dapk/st/push/messaging/MessagingPushTokenRegistrar.kt index bd5a05b..760c313 100644 --- a/domains/android/push/src/main/kotlin/app/dapk/st/push/firebase/FirebasePushTokenRegistrar.kt +++ b/domains/android/push/src/main/kotlin/app/dapk/st/push/messaging/MessagingPushTokenRegistrar.kt @@ -1,4 +1,4 @@ -package app.dapk.st.push.firebase +package app.dapk.st.push.messaging import app.dapk.st.core.AppLogTag import app.dapk.st.core.extensions.CrashScope @@ -11,7 +11,7 @@ import app.dapk.st.push.PushTokenRegistrar private const val SYGNAL_GATEWAY = "https://sygnal.dapk.app/_matrix/push/v1/notify" -class FirebasePushTokenRegistrar( +class MessagingPushTokenRegistrar( override val errorTracker: ErrorTracker, private val pushHandler: PushHandler, private val messaging: Messaging, @@ -43,4 +43,6 @@ class FirebasePushTokenRegistrar( messaging.disable() } + fun isAvailable() = messaging.isAvailable() + } \ No newline at end of file diff --git a/domains/android/push/src/main/kotlin/app/dapk/st/push/firebase/MessagingServiceAdapter.kt b/domains/android/push/src/main/kotlin/app/dapk/st/push/messaging/MessagingServiceAdapter.kt similarity index 96% rename from domains/android/push/src/main/kotlin/app/dapk/st/push/firebase/MessagingServiceAdapter.kt rename to domains/android/push/src/main/kotlin/app/dapk/st/push/messaging/MessagingServiceAdapter.kt index 0bd2f0e..cceed3d 100644 --- a/domains/android/push/src/main/kotlin/app/dapk/st/push/firebase/MessagingServiceAdapter.kt +++ b/domains/android/push/src/main/kotlin/app/dapk/st/push/messaging/MessagingServiceAdapter.kt @@ -1,4 +1,4 @@ -package app.dapk.st.push.firebase +package app.dapk.st.push.messaging import app.dapk.st.core.AppLogTag import app.dapk.st.core.log diff --git a/domains/firebase/messaging-noop/src/main/kotlin/app/dapk/st/firebase/messaging/Messaging.kt b/domains/firebase/messaging-noop/src/main/kotlin/app/dapk/st/firebase/messaging/Messaging.kt index 160ae63..aea7843 100644 --- a/domains/firebase/messaging-noop/src/main/kotlin/app/dapk/st/firebase/messaging/Messaging.kt +++ b/domains/firebase/messaging-noop/src/main/kotlin/app/dapk/st/firebase/messaging/Messaging.kt @@ -2,6 +2,8 @@ package app.dapk.st.firebase.messaging class Messaging { + fun isAvailable() = false + fun enable() { // do nothing } diff --git a/domains/firebase/messaging/src/main/kotlin/app/dapk/st/firebase/messaging/Messaging.kt b/domains/firebase/messaging/src/main/kotlin/app/dapk/st/firebase/messaging/Messaging.kt index dc8dadd..5852c4d 100644 --- a/domains/firebase/messaging/src/main/kotlin/app/dapk/st/firebase/messaging/Messaging.kt +++ b/domains/firebase/messaging/src/main/kotlin/app/dapk/st/firebase/messaging/Messaging.kt @@ -4,6 +4,8 @@ import android.content.ComponentName import android.content.Context import android.content.Intent import android.content.pm.PackageManager +import com.google.android.gms.common.ConnectionResult +import com.google.android.gms.common.GoogleApiAvailabilityLight import com.google.firebase.messaging.FirebaseMessaging import kotlin.coroutines.resume import kotlin.coroutines.suspendCoroutine @@ -13,6 +15,8 @@ class Messaging( private val context: Context, ) { + fun isAvailable() = GoogleApiAvailabilityLight.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS + fun enable() { context.packageManager.setComponentEnabledSetting( ComponentName(context, FirebasePushServiceDelegate::class.java),