diff --git a/libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenDeleter.kt b/libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenDeleter.kt index db7ac0bb7c..597a39493f 100644 --- a/libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenDeleter.kt +++ b/libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenDeleter.kt @@ -17,37 +17,32 @@ import kotlin.coroutines.resumeWithException import kotlin.coroutines.suspendCoroutine interface FirebaseTokenDeleter { + /** + * Deletes the current Firebase token. + */ suspend fun delete() } -/** - * This class deletes the current Firebase token. - */ @ContributesBinding(AppScope::class) class DefaultFirebaseTokenDeleter @Inject constructor( private val isPlayServiceAvailable: IsPlayServiceAvailable, ) : FirebaseTokenDeleter { override suspend fun delete() { + // 'app should always check the device for a compatible Google Play services APK before accessing Google Play services features' + isPlayServiceAvailable.checkAvailableOrThrow() suspendCoroutine { continuation -> - // 'app should always check the device for a compatible Google Play services APK before accessing Google Play services features' - if (isPlayServiceAvailable.isAvailable()) { - try { - FirebaseMessaging.getInstance().deleteToken() - .addOnSuccessListener { - continuation.resume(Unit) - } - .addOnFailureListener { e -> - Timber.e(e, "## deleteFirebaseToken() : failed") - continuation.resumeWithException(e) - } - } catch (e: Throwable) { - Timber.e(e, "## deleteFirebaseToken() : failed") - continuation.resumeWithException(e) - } - } else { - val e = Exception("No valid Google Play Services found. Cannot use FCM.") - Timber.e(e) - throw e + try { + FirebaseMessaging.getInstance().deleteToken() + .addOnSuccessListener { + continuation.resume(Unit) + } + .addOnFailureListener { e -> + Timber.e(e, "## deleteFirebaseToken() : failed") + continuation.resumeWithException(e) + } + } catch (e: Throwable) { + Timber.e(e, "## deleteFirebaseToken() : failed") + continuation.resumeWithException(e) } } } diff --git a/libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenGetter.kt b/libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenGetter.kt index b120687f9a..a15d5a250c 100644 --- a/libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenGetter.kt +++ b/libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenGetter.kt @@ -17,37 +17,32 @@ import kotlin.coroutines.resumeWithException import kotlin.coroutines.suspendCoroutine interface FirebaseTokenGetter { + /** + * Read the current Firebase token from FirebaseMessaging. + * If the token does not exist, it will be generated. + */ suspend fun get(): String } -/** - * This class read the current Firebase token. - * If the token does not exist, it will be generated. - */ @ContributesBinding(AppScope::class) class DefaultFirebaseTokenGetter @Inject constructor( private val isPlayServiceAvailable: IsPlayServiceAvailable, ) : FirebaseTokenGetter { override suspend fun get(): String { + // 'app should always check the device for a compatible Google Play services APK before accessing Google Play services features' + isPlayServiceAvailable.checkAvailableOrThrow() return suspendCoroutine { continuation -> - // 'app should always check the device for a compatible Google Play services APK before accessing Google Play services features' - if (isPlayServiceAvailable.isAvailable()) { - try { - FirebaseMessaging.getInstance().token - .addOnSuccessListener { token -> - continuation.resume(token) - } - .addOnFailureListener { e -> - Timber.e(e, "## retrievedFirebaseToken() : failed") - continuation.resumeWithException(e) - } - } catch (e: Throwable) { - Timber.e(e, "## retrievedFirebaseToken() : failed") - continuation.resumeWithException(e) - } - } else { - val e = Exception("No valid Google Play Services found. Cannot use FCM.") - Timber.e(e) + try { + FirebaseMessaging.getInstance().token + .addOnSuccessListener { token -> + continuation.resume(token) + } + .addOnFailureListener { e -> + Timber.e(e, "## retrievedFirebaseToken() : failed") + continuation.resumeWithException(e) + } + } catch (e: Throwable) { + Timber.e(e, "## retrievedFirebaseToken() : failed") continuation.resumeWithException(e) } } diff --git a/libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/IsPlayServiceAvailable.kt b/libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/IsPlayServiceAvailable.kt index 02cf66cb74..a0177ef757 100644 --- a/libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/IsPlayServiceAvailable.kt +++ b/libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/IsPlayServiceAvailable.kt @@ -20,6 +20,12 @@ interface IsPlayServiceAvailable { 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) class DefaultIsPlayServiceAvailable @Inject constructor( @ApplicationContext private val context: Context,