This repository was archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New AccountManager component, update samples apps to use it
This introduces an account manager component, and updates two sample apps to use it - logins and history sync. AccountManager encapsulates state transitions exposed by Rust liblogins, and provides a simple observable API and a few public methods to interact with a FirefoxAccount. Internally it's implemented as a state machine. Consumers are expected to handle interactions with a web view that are necessary to complete an OAUTH flow.
- Loading branch information
Showing
8 changed files
with
1,123 additions
and
117 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
49 changes: 49 additions & 0 deletions
49
...s/service/firefox-accounts/src/main/java/mozilla/components/service/fxa/AccountStorage.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 @@ | ||
/* 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.service.fxa | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
|
||
const val FXA_STATE_PREFS_KEY = "fxaAppState" | ||
const val FXA_STATE_KEY = "fxaState" | ||
|
||
interface AccountStorage { | ||
@Throws(Exception::class) | ||
fun read(): FirefoxAccountShaped? | ||
fun write(account: FirefoxAccountShaped) | ||
fun clear() | ||
} | ||
|
||
class SharedPrefAccountStorage(val context: Context) : AccountStorage { | ||
/** | ||
* @throws FxaException if JSON failed to parse into a [FirefoxAccount]. | ||
*/ | ||
override fun read(): FirefoxAccountShaped? { | ||
val savedJSON = accountPreferences().getString(FXA_STATE_KEY, null) | ||
?: return null | ||
|
||
// May throw a generic FxaException if it fails to process saved JSON. | ||
return FirefoxAccount.fromJSONString(savedJSON) | ||
} | ||
|
||
override fun write(account: FirefoxAccountShaped) { | ||
accountPreferences() | ||
.edit() | ||
.putString(FXA_STATE_KEY, account.toJSONString()) | ||
.apply() | ||
} | ||
|
||
override fun clear() { | ||
accountPreferences() | ||
.edit() | ||
.remove(FXA_STATE_KEY) | ||
.apply() | ||
} | ||
|
||
private fun accountPreferences(): SharedPreferences { | ||
return context.getSharedPreferences(FXA_STATE_PREFS_KEY, Context.MODE_PRIVATE) | ||
} | ||
} |
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.