diff --git a/bugsnag-android-core/src/androidTest/java/com/bugsnag/android/UserStoreTest.kt b/bugsnag-android-core/src/androidTest/java/com/bugsnag/android/UserStoreTest.kt index 8125e6ff6a..27bcb70a3e 100644 --- a/bugsnag-android-core/src/androidTest/java/com/bugsnag/android/UserStoreTest.kt +++ b/bugsnag-android-core/src/androidTest/java/com/bugsnag/android/UserStoreTest.kt @@ -6,6 +6,7 @@ import androidx.test.core.app.ApplicationProvider import com.bugsnag.android.internal.ImmutableConfig import org.junit.After import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Before import org.junit.Test @@ -145,11 +146,8 @@ internal class UserStoreTest { prefMigrator, NoopLogger ) - val user = store.load(User()).user - assertEquals("device-id-123", user.id) - assertNull(user.email) - assertNull(user.name) - assertEquals("", file.readText()) + store.load(User()).user + assertFalse(file.exists()) } /** @@ -159,7 +157,7 @@ internal class UserStoreTest { fun saveWithoutPersistUser() { val store = UserStore(generateConfig(false), "", file, prefMigrator, NoopLogger) store.save(User("123", "joe@yahoo.com", "Joe Bloggs")) - assertEquals("", file.readText()) + assertFalse(file.exists()) } /** diff --git a/bugsnag-android-core/src/main/java/com/bugsnag/android/SynchronizedStreamableStore.kt b/bugsnag-android-core/src/main/java/com/bugsnag/android/SynchronizedStreamableStore.kt index 66bf136e56..f7d53367c9 100644 --- a/bugsnag-android-core/src/main/java/com/bugsnag/android/SynchronizedStreamableStore.kt +++ b/bugsnag-android-core/src/main/java/com/bugsnag/android/SynchronizedStreamableStore.kt @@ -13,7 +13,7 @@ import kotlin.concurrent.withLock * This class is made thread safe through the use of a [ReadWriteLock]. */ internal class SynchronizedStreamableStore( - private val file: File + internal val file: File ) { private val lock = ReentrantReadWriteLock() diff --git a/bugsnag-android-core/src/main/java/com/bugsnag/android/UserStore.kt b/bugsnag-android-core/src/main/java/com/bugsnag/android/UserStore.kt index 1bd0b19201..87adf40e16 100644 --- a/bugsnag-android-core/src/main/java/com/bugsnag/android/UserStore.kt +++ b/bugsnag-android-core/src/main/java/com/bugsnag/android/UserStore.kt @@ -3,7 +3,6 @@ package com.bugsnag.android import com.bugsnag.android.internal.ImmutableConfig import com.bugsnag.android.internal.StateObserver import java.io.File -import java.io.IOException import java.util.concurrent.atomic.AtomicReference /** @@ -22,11 +21,6 @@ internal class UserStore @JvmOverloads constructor( private val previousUser = AtomicReference(null) init { - try { - file.createNewFile() - } catch (exc: IOException) { - logger.w("Failed to created device ID file", exc) - } this.synchronizedStreamableStore = SynchronizedStreamableStore(file) } @@ -87,13 +81,19 @@ internal class UserStore @JvmOverloads constructor( val legacyUser = sharedPrefMigrator.loadUser(deviceId) save(legacyUser) legacyUser - } else { - return try { + } else if ( + synchronizedStreamableStore.file.canRead() && + synchronizedStreamableStore.file.length() > 0L && + persist + ) { + try { synchronizedStreamableStore.load(User.Companion::fromReader) } catch (exc: Exception) { logger.w("Failed to load user info", exc) null } + } else { + null } } }