@@ -14,34 +14,31 @@ class IterableKeychainEncryptedDataMigrator(
1414 private val TAG = " IterableKeychainMigrator"
1515
1616 private val encryptedSharedPrefsFileName = " iterable-encrypted-shared-preferences"
17- private val migrationAttemptedKey = " iterable-encrypted-migration-attempted"
1817 private val migrationStartedKey = " iterable-encrypted-migration-started"
1918 private val migrationCompletedKey = " iterable-encrypted-migration-completed"
2019
21- private val emailKey = " iterable-email"
22- private val userIdKey = " iterable-user-id"
23- private val authTokenKey = " iterable-auth-token"
20+ class MigrationException (message : String , cause : Throwable ? = null ) : Exception(message, cause)
2421
2522 fun attemptMigration () {
26- // Skip if migration was already attempted and completed
23+ // Skip if migration was already completed
2724 if (sharedPrefs.getBoolean(migrationCompletedKey, false )) {
2825 IterableLogger .v(TAG , " Migration was already completed, skipping" )
2926 return
3027 }
3128
32- // Check if migration was started but not completed (potential crash during migration)
33- if (sharedPrefs.getBoolean(migrationStartedKey, false )) {
34- IterableLogger .w(TAG , " Previous migration attempt was interrupted, clearing data" )
35- keychain.handleDecryptionError()
36- return
37- }
38-
3929 // Only attempt migration on Android M and above
4030 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
4131 markMigrationCompleted()
4232 return
4333 }
4434
35+ // If previous migration was interrupted, mark as completed and throw exception
36+ if (sharedPrefs.getBoolean(migrationStartedKey, false )) {
37+ IterableLogger .w(TAG , " Previous migration attempt was interrupted" )
38+ markMigrationCompleted()
39+ throw MigrationException (" Previous migration attempt was interrupted" )
40+ }
41+
4542 // Mark migration as started
4643 sharedPrefs.edit()
4744 .putBoolean(migrationStartedKey, true )
@@ -73,17 +70,18 @@ class IterableKeychainEncryptedDataMigrator(
7370
7471 IterableLogger .v(TAG , " Successfully migrated data from encrypted preferences" )
7572 } catch (e: Throwable ) {
76- IterableLogger .w(TAG , " Failed to access encrypted preferences, skipping migration" , e)
77- markMigrationCompleted() // Mark as completed even on failure to prevent retries
73+ IterableLogger .w(TAG , " Failed to access encrypted preferences" , e)
74+ markMigrationCompleted() // Mark as completed even on failure
75+ throw MigrationException (" Failed to migrate data" , e)
7876 }
7977 }.start()
8078 }
8179
8280 private fun migrateData (encryptedPrefs : SharedPreferences ) {
8381 // Use keychain methods to ensure proper encryption
84- encryptedPrefs.getString(emailKey, null )?.let { keychain.saveEmail(it) }
85- encryptedPrefs.getString(userIdKey, null )?.let { keychain.saveUserId(it) }
86- encryptedPrefs.getString(authTokenKey, null )?.let { keychain.saveAuthToken(it) }
82+ encryptedPrefs.getString(keychain. emailKey, null )?.let { keychain.saveEmail(it) }
83+ encryptedPrefs.getString(keychain. userIdKey, null )?.let { keychain.saveUserId(it) }
84+ encryptedPrefs.getString(keychain. authTokenKey, null )?.let { keychain.saveAuthToken(it) }
8785 }
8886
8987 private fun markMigrationCompleted () {
0 commit comments