Browse Source

Handle Jorge's remarks.

pull/3755/head
Benoit Marty 2 days ago
parent
commit
12f5839eef
  1. 15
      libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenDeleter.kt
  2. 17
      libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenGetter.kt
  3. 6
      libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/IsPlayServiceAvailable.kt

15
libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenDeleter.kt

@ -17,20 +17,20 @@ import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
interface FirebaseTokenDeleter { interface FirebaseTokenDeleter {
/**
* Deletes the current Firebase token.
*/
suspend fun delete() suspend fun delete()
} }
/**
* This class deletes the current Firebase token.
*/
@ContributesBinding(AppScope::class) @ContributesBinding(AppScope::class)
class DefaultFirebaseTokenDeleter @Inject constructor( class DefaultFirebaseTokenDeleter @Inject constructor(
private val isPlayServiceAvailable: IsPlayServiceAvailable, private val isPlayServiceAvailable: IsPlayServiceAvailable,
) : FirebaseTokenDeleter { ) : FirebaseTokenDeleter {
override suspend fun delete() { override suspend fun delete() {
suspendCoroutine { continuation ->
// 'app should always check the device for a compatible Google Play services APK before accessing Google Play services features' // 'app should always check the device for a compatible Google Play services APK before accessing Google Play services features'
if (isPlayServiceAvailable.isAvailable()) { isPlayServiceAvailable.checkAvailableOrThrow()
suspendCoroutine { continuation ->
try { try {
FirebaseMessaging.getInstance().deleteToken() FirebaseMessaging.getInstance().deleteToken()
.addOnSuccessListener { .addOnSuccessListener {
@ -44,11 +44,6 @@ class DefaultFirebaseTokenDeleter @Inject constructor(
Timber.e(e, "## deleteFirebaseToken() : failed") Timber.e(e, "## deleteFirebaseToken() : failed")
continuation.resumeWithException(e) continuation.resumeWithException(e)
} }
} else {
val e = Exception("No valid Google Play Services found. Cannot use FCM.")
Timber.e(e)
throw e
}
} }
} }
} }

17
libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenGetter.kt

@ -17,21 +17,21 @@ import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
interface FirebaseTokenGetter { interface FirebaseTokenGetter {
suspend fun get(): String
}
/** /**
* This class read the current Firebase token. * Read the current Firebase token from FirebaseMessaging.
* If the token does not exist, it will be generated. * If the token does not exist, it will be generated.
*/ */
suspend fun get(): String
}
@ContributesBinding(AppScope::class) @ContributesBinding(AppScope::class)
class DefaultFirebaseTokenGetter @Inject constructor( class DefaultFirebaseTokenGetter @Inject constructor(
private val isPlayServiceAvailable: IsPlayServiceAvailable, private val isPlayServiceAvailable: IsPlayServiceAvailable,
) : FirebaseTokenGetter { ) : FirebaseTokenGetter {
override suspend fun get(): String { override suspend fun get(): String {
return suspendCoroutine { continuation ->
// 'app should always check the device for a compatible Google Play services APK before accessing Google Play services features' // 'app should always check the device for a compatible Google Play services APK before accessing Google Play services features'
if (isPlayServiceAvailable.isAvailable()) { isPlayServiceAvailable.checkAvailableOrThrow()
return suspendCoroutine { continuation ->
try { try {
FirebaseMessaging.getInstance().token FirebaseMessaging.getInstance().token
.addOnSuccessListener { token -> .addOnSuccessListener { token ->
@ -45,11 +45,6 @@ class DefaultFirebaseTokenGetter @Inject constructor(
Timber.e(e, "## retrievedFirebaseToken() : failed") Timber.e(e, "## retrievedFirebaseToken() : failed")
continuation.resumeWithException(e) continuation.resumeWithException(e)
} }
} else {
val e = Exception("No valid Google Play Services found. Cannot use FCM.")
Timber.e(e)
continuation.resumeWithException(e)
}
} }
} }
} }

6
libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/IsPlayServiceAvailable.kt

@ -20,6 +20,12 @@ interface IsPlayServiceAvailable {
fun isAvailable(): Boolean fun isAvailable(): Boolean
} }
fun IsPlayServiceAvailable.checkAvailableOrThrow() {
if (!isAvailable()) {
throw Exception("No valid Google Play Services found. Cannot use FCM.").also(Timber::e)
}
}
@ContributesBinding(AppScope::class) @ContributesBinding(AppScope::class)
class DefaultIsPlayServiceAvailable @Inject constructor( class DefaultIsPlayServiceAvailable @Inject constructor(
@ApplicationContext private val context: Context, @ApplicationContext private val context: Context,

Loading…
Cancel
Save