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 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part 3: Rust SyncManager integration
Co-authored-by: Arturo Mejia <arturomejiamarmol@gmail.com>
- Loading branch information
Showing
28 changed files
with
747 additions
and
175 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
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
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
62 changes: 62 additions & 0 deletions
62
...e/firefox-accounts/src/main/java/mozilla/components/service/fxa/FxaDeviceSettingsCache.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,62 @@ | ||
/* 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 | ||
import mozilla.appservices.syncmanager.DeviceSettings | ||
import mozilla.appservices.syncmanager.DeviceType | ||
import mozilla.components.support.base.log.logger.Logger | ||
import org.json.JSONObject | ||
import java.lang.IllegalArgumentException | ||
import java.lang.IllegalStateException | ||
|
||
private const val CACHE_NAME = "FxaDeviceSettingsCache" | ||
private const val CACHE_KEY = CACHE_NAME | ||
private const val KEY_FXA_DEVICE_ID = "kid" | ||
private const val KEY_DEVICE_NAME = "syncKey" | ||
private const val KEY_DEVICE_TYPE = "tokenServerUrl" | ||
|
||
/** | ||
* A thin wrapper around [SharedPreferences] which knows how to serialize/deserialize [DeviceSettings]. | ||
* | ||
* This class exists to provide background sync workers with access to [DeviceSettings]. | ||
*/ | ||
class FxaDeviceSettingsCache(context: Context) : SharedPreferencesCache<DeviceSettings>(context) { | ||
override val logger = Logger("SyncAuthInfoCache") | ||
override val cacheKey = CACHE_KEY | ||
override val cacheName = CACHE_NAME | ||
|
||
override fun DeviceSettings.toJSON(): JSONObject { | ||
return JSONObject().also { | ||
it.put(KEY_FXA_DEVICE_ID, this.fxaDeviceId) | ||
it.put(KEY_DEVICE_NAME, this.name) | ||
it.put(KEY_DEVICE_TYPE, this.type.name) | ||
} | ||
} | ||
|
||
override fun fromJSON(obj: JSONObject): DeviceSettings { | ||
return DeviceSettings( | ||
fxaDeviceId = obj.getString(KEY_FXA_DEVICE_ID), | ||
name = obj.getString(KEY_DEVICE_NAME), | ||
type = obj.getString(KEY_DEVICE_TYPE).toDeviceType() | ||
) | ||
} | ||
|
||
fun updateCachedName(name: String) { | ||
val cached = getCached() ?: throw IllegalStateException("Trying to update cached value in an empty cache") | ||
setToCache(cached.copy(name = name)) | ||
} | ||
|
||
private fun String.toDeviceType(): DeviceType { | ||
return when (this) { | ||
"DESKTOP" -> DeviceType.DESKTOP | ||
"MOBILE" -> DeviceType.MOBILE | ||
"TABLET" -> DeviceType.TABLET | ||
"VR" -> DeviceType.VR | ||
"TV" -> DeviceType.TV | ||
else -> throw IllegalArgumentException("Unknown device type in cached string: $this") | ||
} | ||
} | ||
} |
Oops, something went wrong.