-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3755 from element-hq/feature/bma/rotateFirebaseToken
Rotate firebase token in case of error
- Loading branch information
Showing
17 changed files
with
289 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...c/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenDeleter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2023, 2024 New Vector Ltd. | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
* Please see LICENSE in the repository root for full details. | ||
*/ | ||
|
||
package io.element.android.libraries.pushproviders.firebase | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import io.element.android.libraries.di.AppScope | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
interface FirebaseTokenDeleter { | ||
/** | ||
* Deletes the current Firebase token. | ||
*/ | ||
suspend fun delete() | ||
} | ||
|
||
@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 -> | ||
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) | ||
} | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...rc/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenGetter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2023, 2024 New Vector Ltd. | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
* Please see LICENSE in the repository root for full details. | ||
*/ | ||
|
||
package io.element.android.libraries.pushproviders.firebase | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import io.element.android.libraries.di.AppScope | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
import kotlin.coroutines.resume | ||
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 | ||
} | ||
|
||
@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 -> | ||
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) | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...c/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebaseTokenRotator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2024 New Vector Ltd. | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
* Please see LICENSE in the repository root for full details. | ||
*/ | ||
|
||
package io.element.android.libraries.pushproviders.firebase | ||
|
||
import com.squareup.anvil.annotations.ContributesBinding | ||
import io.element.android.libraries.di.AppScope | ||
import javax.inject.Inject | ||
|
||
interface FirebaseTokenRotator { | ||
suspend fun rotate(): Result<Unit> | ||
} | ||
|
||
/** | ||
* This class delete the Firebase token and generate a new one. | ||
*/ | ||
@ContributesBinding(AppScope::class) | ||
class DefaultFirebaseTokenRotator @Inject constructor( | ||
private val firebaseTokenDeleter: FirebaseTokenDeleter, | ||
private val firebaseTokenGetter: FirebaseTokenGetter, | ||
) : FirebaseTokenRotator { | ||
override suspend fun rotate(): Result<Unit> { | ||
return runCatching { | ||
firebaseTokenDeleter.delete() | ||
firebaseTokenGetter.get() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.