Skip to content

Commit

Permalink
patch: rename and move the storage to idenity manager
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen committed Mar 12, 2022
1 parent 0ad1317 commit 0b240ef
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 71 deletions.
7 changes: 3 additions & 4 deletions android/src/main/java/com/amplitude/android/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import com.amplitude.core.Amplitude
import com.amplitude.core.platform.plugins.AmplitudeDestination
import com.amplitude.core.utilities.AnalyticsIdentityListener
import com.amplitude.id.FileIdentityStorageProvider
import com.amplitude.id.IMIdentityStorageProvider
import com.amplitude.id.IdConfiguration
import com.amplitude.id.IdContainer
import com.amplitude.id.IdentityConfiguration
import com.amplitude.id.IdentityContainer

open class Amplitude(
configuration: Configuration
): Amplitude(configuration) {

override fun build() {
idContainer = IdContainer.getInstance(IdConfiguration(instanceName = configuration.instanceName, apiKey = configuration.apiKey, identityStorageProvider = FileIdentityStorageProvider()))
idContainer = IdentityContainer.getInstance(IdentityConfiguration(instanceName = configuration.instanceName, apiKey = configuration.apiKey, identityStorageProvider = FileIdentityStorageProvider()))
idContainer.identityManager.addIdentityListener(AnalyticsIdentityListener(store))
add(AmplitudeDestination())
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/com/amplitude/core/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import com.amplitude.core.platform.plugins.AmplitudeDestination
import com.amplitude.core.platform.plugins.ContextPlugin
import com.amplitude.core.utilities.AnalyticsIdentityListener
import com.amplitude.id.IMIdentityStorageProvider
import com.amplitude.id.IdConfiguration
import com.amplitude.id.IdContainer
import com.amplitude.id.IdentityConfiguration
import com.amplitude.id.IdentityContainer
import kotlinx.coroutines.*
import java.util.concurrent.Executors

Expand All @@ -28,7 +28,7 @@ open class Amplitude internal constructor(
internal val timeline: Timeline
val storage: Storage
val logger: Logger
protected lateinit var idContainer: IdContainer
protected lateinit var idContainer: IdentityContainer

init {
require(configuration.isValid()) { "invalid configuration" }
Expand All @@ -44,7 +44,7 @@ open class Amplitude internal constructor(
constructor(configuration: Configuration) : this(configuration, State())

open fun build() {
idContainer = IdContainer.getInstance(IdConfiguration(instanceName = configuration.instanceName, apiKey = configuration.apiKey, identityStorageProvider = IMIdentityStorageProvider()))
idContainer = IdentityContainer.getInstance(IdentityConfiguration(instanceName = configuration.instanceName, apiKey = configuration.apiKey, identityStorageProvider = IMIdentityStorageProvider()))
idContainer.identityManager.addIdentityListener(AnalyticsIdentityListener(store))
add(ContextPlugin())
add(AmplitudeDestination())
Expand Down
40 changes: 11 additions & 29 deletions id/src/main/java/com/amplitude/id/FileIdentityStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import com.amplitude.id.utilities.PropertiesFile
import com.amplitude.id.utilities.createDirectory
import java.io.File

class FileIdentityStorage(val configuration: IdConfiguration): IdentityStorage {
lateinit var identityStore: IdentityManager
class FileIdentityStorage(val configuration: IdentityConfiguration) : IdentityStorage {
private val propertiesFile: PropertiesFile

companion object {
Expand All @@ -23,12 +22,6 @@ class FileIdentityStorage(val configuration: IdConfiguration): IdentityStorage {
propertiesFile = PropertiesFile(storageDirectory, instanceName, STORAGE_PREFIX)
}

override fun setup(identityManager: IdentityManager) {
this.identityStore = identityManager
identityManager.addIdentityListener(FileIdentityListener(this))
load()
}

override fun saveUserId(userId: String?) {
propertiesFile.putString(USER_ID_KEY, userId ?: "")
}
Expand All @@ -37,15 +30,19 @@ class FileIdentityStorage(val configuration: IdConfiguration): IdentityStorage {
propertiesFile.putString(DEVICE_ID_KEY, deviceId ?: "")
}

private fun load() {
override fun load(): Identity {
safetyCheck()
val userId = propertiesFile.getString(USER_ID_KEY, null)
val deviceId = propertiesFile.getString(DEVICE_ID_KEY, null)
identityStore.setIdentity(Identity(userId, deviceId), IdentityUpdateType.Initialized)
return Identity(userId, deviceId)
}

private fun safetyCheck() {
if (safeForKey(API_KEY, configuration.apiKey) && safeForKey(EXPERIMENT_API_KEY, configuration.experimentApiKey)) {
if (safeForKey(API_KEY, configuration.apiKey) && safeForKey(
EXPERIMENT_API_KEY,
configuration.experimentApiKey
)
) {
return
}
// api key not matching saved one, clear current value
Expand All @@ -67,23 +64,8 @@ class FileIdentityStorage(val configuration: IdConfiguration): IdentityStorage {
}
}

class FileIdentityStorageProvider: IdentityStorageProvider {
override fun getIdentityStorage(configuration: IdConfiguration): IdentityStorage {
class FileIdentityStorageProvider : IdentityStorageProvider {
override fun getIdentityStorage(configuration: IdentityConfiguration): IdentityStorage {
return FileIdentityStorage(configuration)
}
}

class FileIdentityListener(private val identityStorage: FileIdentityStorage) : IdentityListener {

override fun onUserIdChange(userId: String?) {
identityStorage.saveUserId(userId)
}

override fun onDeviceIdChange(deviceId: String?) {
identityStorage.saveDeviceId(deviceId)
}

override fun onIdentityChanged(identity: Identity, updateType: IdentityUpdateType) {

}
}
}
31 changes: 5 additions & 26 deletions id/src/main/java/com/amplitude/id/IMIdentityStorage.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package com.amplitude.id

class IMIdentityStorage: IdentityStorage {
lateinit var identityStore: IdentityManager
class IMIdentityStorage : IdentityStorage {
var userId: String? = null
var deviceId: String? = null

override fun setup(identityManager: IdentityManager) {
this.identityStore = identityManager
identityManager.addIdentityListener(IMIdentityListener(this))
load()
}

private fun load() {
identityStore.setIdentity(Identity(userId, deviceId), IdentityUpdateType.Initialized)
override fun load(): Identity {
return Identity(userId, deviceId)
}

override fun saveUserId(userId: String?) {
Expand All @@ -24,23 +18,8 @@ class IMIdentityStorage: IdentityStorage {
}
}

class IMIdentityStorageProvider: IdentityStorageProvider {
override fun getIdentityStorage(configuration: IdConfiguration): IdentityStorage {
class IMIdentityStorageProvider : IdentityStorageProvider {
override fun getIdentityStorage(configuration: IdentityConfiguration): IdentityStorage {
return IMIdentityStorage()
}
}

class IMIdentityListener(private val identityStorage: IMIdentityStorage) : IdentityListener {

override fun onUserIdChange(userId: String?) {
identityStorage.saveUserId(userId)
}

override fun onDeviceIdChange(deviceId: String?) {
identityStorage.saveDeviceId(deviceId)
}

override fun onIdentityChanged(identity: Identity, updateType: IdentityUpdateType) {

}
}
9 changes: 4 additions & 5 deletions id/src/main/java/com/amplitude/id/IdentityContainer.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.amplitude.id

class IdentityContainer private constructor(val configuration: IdConfiguration) {
class IdentityContainer private constructor(val configuration: IdentityConfiguration) {
private val identityStorage: IdentityStorage
val identityManager: IdentityManager

Expand All @@ -10,7 +10,7 @@ class IdentityContainer private constructor(val configuration: IdConfiguration)
private val instances = mutableMapOf<String, IdentityContainer>()

@JvmStatic
fun getInstance(configuration: IdConfiguration): IdentityContainer {
fun getInstance(configuration: IdentityConfiguration): IdentityContainer {
return synchronized(instancesLock) {
instances.getOrPut(configuration.instanceName) {
IdentityContainer(configuration)
Expand All @@ -20,8 +20,7 @@ class IdentityContainer private constructor(val configuration: IdConfiguration)
}

init {
identityManager = IdentityManagerImpl()
identityStorage = configuration.identityStorageProvider.getIdentityStorage(configuration)
identityStorage.setup(identityManager)
identityManager = IdentityManagerImpl(identityStorage)
}
}
}
16 changes: 15 additions & 1 deletion id/src/main/java/com/amplitude/id/IdentityManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ interface IdentityManager {
fun isInitialized(): Boolean
}

internal class IdentityManagerImpl: IdentityManager {
internal class IdentityManagerImpl(val identityStorage: IdentityStorage): IdentityManager {

private val identityLock = ReentrantReadWriteLock(true)
private var identity = Identity()
Expand All @@ -55,6 +55,10 @@ internal class IdentityManagerImpl: IdentityManager {
private val listeners: MutableSet<IdentityListener> = mutableSetOf()
private var initialized: Boolean = false

init {
setIdentity(identityStorage.load(), IdentityUpdateType.Initialized)
}

override fun editIdentity(): IdentityManager.Editor {
val originalIdentity = getIdentity()
return object : IdentityManager.Editor {
Expand Down Expand Up @@ -121,6 +125,16 @@ internal class IdentityManagerImpl: IdentityManager {
listeners.toSet()
}

if (updateType != IdentityUpdateType.Initialized) {
if (identity.userId != originalIdentity.userId) {
identityStorage.saveUserId(identity.userId)
}

if (identity.deviceId != originalIdentity.deviceId) {
identityStorage.saveDeviceId(identity.deviceId)
}
}

for (listener in safeListeners) {
if (identity.userId != originalIdentity.userId) {
listener.onUserIdChange(identity.userId)
Expand Down
4 changes: 2 additions & 2 deletions id/src/main/java/com/amplitude/id/IdentityStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package com.amplitude.id

interface IdentityStorage {

fun setup(identityManager: IdentityManager)
fun load():Identity

fun saveUserId(userId: String?)

fun saveDeviceId(deviceId: String?)
}

interface IdentityStorageProvider {
fun getIdentityStorage(configuration: IdConfiguration): IdentityStorage
fun getIdentityStorage(configuration: IdentityConfiguration): IdentityStorage
}

0 comments on commit 0b240ef

Please sign in to comment.