diff --git a/android/src/main/java/com/amplitude/android/Amplitude.kt b/android/src/main/java/com/amplitude/android/Amplitude.kt index 6128b12c..2a540df7 100644 --- a/android/src/main/java/com/amplitude/android/Amplitude.kt +++ b/android/src/main/java/com/amplitude/android/Amplitude.kt @@ -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()) } diff --git a/core/src/main/java/com/amplitude/core/Amplitude.kt b/core/src/main/java/com/amplitude/core/Amplitude.kt index a9183635..733eeec1 100644 --- a/core/src/main/java/com/amplitude/core/Amplitude.kt +++ b/core/src/main/java/com/amplitude/core/Amplitude.kt @@ -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 @@ -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" } @@ -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()) diff --git a/id/src/main/java/com/amplitude/id/FileIdentityStorage.kt b/id/src/main/java/com/amplitude/id/FileIdentityStorage.kt index 6510d919..b8ca13bc 100644 --- a/id/src/main/java/com/amplitude/id/FileIdentityStorage.kt +++ b/id/src/main/java/com/amplitude/id/FileIdentityStorage.kt @@ -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 { @@ -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 ?: "") } @@ -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 @@ -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) { - - } -} +} \ No newline at end of file diff --git a/id/src/main/java/com/amplitude/id/IMIdentityStorage.kt b/id/src/main/java/com/amplitude/id/IMIdentityStorage.kt index 4e47d201..d8b292eb 100644 --- a/id/src/main/java/com/amplitude/id/IMIdentityStorage.kt +++ b/id/src/main/java/com/amplitude/id/IMIdentityStorage.kt @@ -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?) { @@ -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) { - - } -} diff --git a/id/src/main/java/com/amplitude/id/IdentityContainer.kt b/id/src/main/java/com/amplitude/id/IdentityContainer.kt index 10ee117a..1ff6cab7 100644 --- a/id/src/main/java/com/amplitude/id/IdentityContainer.kt +++ b/id/src/main/java/com/amplitude/id/IdentityContainer.kt @@ -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 @@ -10,7 +10,7 @@ class IdentityContainer private constructor(val configuration: IdConfiguration) private val instances = mutableMapOf() @JvmStatic - fun getInstance(configuration: IdConfiguration): IdentityContainer { + fun getInstance(configuration: IdentityConfiguration): IdentityContainer { return synchronized(instancesLock) { instances.getOrPut(configuration.instanceName) { IdentityContainer(configuration) @@ -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) } -} \ No newline at end of file +} diff --git a/id/src/main/java/com/amplitude/id/IdentityManager.kt b/id/src/main/java/com/amplitude/id/IdentityManager.kt index ea400475..4ea8b52f 100644 --- a/id/src/main/java/com/amplitude/id/IdentityManager.kt +++ b/id/src/main/java/com/amplitude/id/IdentityManager.kt @@ -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() @@ -55,6 +55,10 @@ internal class IdentityManagerImpl: IdentityManager { private val listeners: MutableSet = mutableSetOf() private var initialized: Boolean = false + init { + setIdentity(identityStorage.load(), IdentityUpdateType.Initialized) + } + override fun editIdentity(): IdentityManager.Editor { val originalIdentity = getIdentity() return object : IdentityManager.Editor { @@ -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) diff --git a/id/src/main/java/com/amplitude/id/IdentityStorage.kt b/id/src/main/java/com/amplitude/id/IdentityStorage.kt index 83f93a18..81ea93fd 100644 --- a/id/src/main/java/com/amplitude/id/IdentityStorage.kt +++ b/id/src/main/java/com/amplitude/id/IdentityStorage.kt @@ -2,7 +2,7 @@ package com.amplitude.id interface IdentityStorage { - fun setup(identityManager: IdentityManager) + fun load():Identity fun saveUserId(userId: String?) @@ -10,5 +10,5 @@ interface IdentityStorage { } interface IdentityStorageProvider { - fun getIdentityStorage(configuration: IdConfiguration): IdentityStorage + fun getIdentityStorage(configuration: IdentityConfiguration): IdentityStorage }