FairEmail/app/src/main/java/com/bugsnag/android/DeviceIdStore.kt

53 lines
1.8 KiB
Kotlin
Raw Normal View History

2021-05-15 20:03:05 +00:00
package com.bugsnag.android
import android.content.Context
import java.io.File
import java.util.UUID
/**
2022-06-23 18:23:15 +00:00
* This class is responsible for persisting and retrieving the device ID and internal device ID,
* which uniquely identify this device in various contexts.
2021-05-15 20:03:05 +00:00
*/
internal class DeviceIdStore @JvmOverloads constructor(
context: Context,
2022-06-23 18:23:15 +00:00
deviceIdfile: File = File(context.filesDir, "device-id"),
deviceIdGenerator: () -> UUID = { UUID.randomUUID() },
internalDeviceIdfile: File = File(context.filesDir, "internal-device-id"),
internalDeviceIdGenerator: () -> UUID = { UUID.randomUUID() },
2021-05-15 20:03:05 +00:00
private val sharedPrefMigrator: SharedPrefMigrator,
2022-06-23 18:23:15 +00:00
logger: Logger
2021-05-15 20:03:05 +00:00
) {
2022-06-23 18:23:15 +00:00
private val persistence: DeviceIdPersistence
private val internalPersistence: DeviceIdPersistence
2021-05-15 20:03:05 +00:00
init {
2022-06-23 18:23:15 +00:00
persistence = DeviceIdFilePersistence(deviceIdfile, deviceIdGenerator, logger)
internalPersistence = DeviceIdFilePersistence(internalDeviceIdfile, internalDeviceIdGenerator, logger)
2021-05-15 20:03:05 +00:00
}
/**
2022-06-23 18:23:15 +00:00
* Loads the device ID from
2021-05-15 20:03:05 +00:00
* Loads the device ID from its file system location. Device IDs are UUIDs which are
* persisted on a per-install basis. This method is thread-safe and multi-process safe.
*
* If no device ID exists then the legacy value stored in [SharedPreferences] will
* be used. If no value is present then a random UUID will be generated and persisted.
*/
fun loadDeviceId(): String? {
2022-06-23 18:23:15 +00:00
var result = persistence.loadDeviceId(false)
if (result != null) {
return result
2021-05-15 20:03:05 +00:00
}
2022-06-23 18:23:15 +00:00
result = sharedPrefMigrator.loadDeviceId(false)
if (result != null) {
return result
2021-05-15 20:03:05 +00:00
}
2022-06-23 18:23:15 +00:00
return persistence.loadDeviceId(true)
2021-05-15 20:03:05 +00:00
}
2022-06-23 18:23:15 +00:00
fun loadInternalDeviceId(): String? {
return internalPersistence.loadDeviceId(true)
2021-05-15 20:03:05 +00:00
}
}