checking for firebase inclusion or play services availability and removing FCM option if it's missing

This commit is contained in:
Adam Brown 2022-08-31 17:52:39 +01:00
parent d01451e3e9
commit 477ac52d16
7 changed files with 33 additions and 17 deletions

View File

@ -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

View File

@ -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,

View File

@ -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<Registrar> {
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()
}
}

View File

@ -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()
}

View File

@ -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

View File

@ -2,6 +2,8 @@ package app.dapk.st.firebase.messaging
class Messaging {
fun isAvailable() = false
fun enable() {
// do nothing
}

View File

@ -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),