This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #9829: Move IO work in FxaPushSupportFeature to a coroutine
The `FxaPushSupportFeature` needs to generate, read and write a push scope (identifier) to disk. This work was meant to be done lazily and not during app startup. However, if a Sync account is setup then the IO work is done immediately during the initial account manager initialization at startup. In this patch, we delegate this IO work to a new `PushScopeProperty` that retrieves the value from disk using a provided `CoroutineScope`.
- Loading branch information
1 parent
86824c3
commit 16767af
Showing
7 changed files
with
113 additions
and
38 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
43 changes: 43 additions & 0 deletions
43
...ts-push/src/main/java/mozilla/components/feature/accounts/push/cache/PushScopeProperty.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,43 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.feature.accounts.push.cache | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.withContext | ||
import mozilla.components.feature.accounts.push.FxaPushSupportFeature | ||
import mozilla.components.feature.accounts.push.PREF_FXA_SCOPE | ||
import mozilla.components.feature.accounts.push.preference | ||
import mozilla.components.feature.push.PushScope | ||
import java.util.UUID | ||
|
||
/** | ||
* An implementation of a [ScopeProperty] that generates and stores a scope in [SharedPreferences]. | ||
*/ | ||
internal class PushScopeProperty( | ||
private val context: Context, | ||
private val coroutineScope: CoroutineScope, | ||
) : ScopeProperty { | ||
|
||
override suspend fun value(): PushScope = withContext(coroutineScope.coroutineContext) { | ||
val prefs = preference(context) | ||
|
||
// Generate a unique scope if one doesn't exist. | ||
val randomUuid = UUID.randomUUID().toString().replace("-", "") | ||
|
||
// Return a scope in the format example: "fxa_push_scope_a62d5f27c9d74af4996d057f0e0e9c38" | ||
val scope = FxaPushSupportFeature.PUSH_SCOPE_PREFIX + randomUuid | ||
|
||
if (!prefs.contains(PREF_FXA_SCOPE)) { | ||
prefs.edit().putString(PREF_FXA_SCOPE, scope).apply() | ||
|
||
return@withContext scope | ||
} | ||
|
||
// The default string is non-null, so we can safely cast. | ||
prefs.getString(PREF_FXA_SCOPE, scope) as String | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...counts-push/src/main/java/mozilla/components/feature/accounts/push/cache/ScopeProperty.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,18 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.feature.accounts.push.cache | ||
|
||
import mozilla.components.feature.push.PushScope | ||
|
||
/** | ||
* A [ScopeProperty] implementation generates and holds the [PushScope]. | ||
*/ | ||
interface ScopeProperty { | ||
|
||
/** | ||
* Returns the [PushScope] value. | ||
*/ | ||
suspend fun value(): PushScope | ||
} |
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