Ensure that a PushProvider is available on a device before using it.

It help to fallback to UnifiedPush (if available) if the PlayServices are not installed on the device.
This commit is contained in:
Benoit Marty 2024-01-17 16:07:26 +01:00 committed by Benoit Marty
parent e2c9389d36
commit 7498c148f1
5 changed files with 45 additions and 1 deletions

View file

@ -16,9 +16,13 @@
package io.element.android.libraries.pushproviders.firebase
import android.content.Context
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.squareup.anvil.annotations.ContributesMultibinding
import io.element.android.libraries.core.log.logger.LoggerTag
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.pushproviders.api.Distributor
import io.element.android.libraries.pushproviders.api.PushProvider
@ -30,6 +34,7 @@ private val loggerTag = LoggerTag("FirebasePushProvider", LoggerTag.PushLoggerTa
@ContributesMultibinding(AppScope::class)
class FirebasePushProvider @Inject constructor(
@ApplicationContext private val context: Context,
private val firebaseStore: FirebaseStore,
private val firebaseTroubleshooter: FirebaseTroubleshooter,
private val pusherSubscriber: PusherSubscriber,
@ -37,6 +42,19 @@ class FirebasePushProvider @Inject constructor(
override val index = FirebaseConfig.INDEX
override val name = FirebaseConfig.NAME
override fun isAvailable(): Boolean {
// The PlayServices has to be available
val apiAvailability = GoogleApiAvailability.getInstance()
val resultCode = apiAvailability.isGooglePlayServicesAvailable(context)
return if (resultCode == ConnectionResult.SUCCESS) {
Timber.tag(loggerTag.value).d("Google Play Services is available")
true
} else {
Timber.tag(loggerTag.value).w("Google Play Services is not available")
false
}
}
override fun getDistributors(): List<Distributor> {
return listOf(Distributor("Firebase", "Firebase"))
}