Last active
January 9, 2024 04:57
-
-
Save rogergcc/b761573433f39116dbf0758e28f39ce3 to your computer and use it in GitHub Desktop.
Encrypted SharedPreferences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Application | |
import android.util.Log | |
import com.rogergcc.encryptedsharedpreferencessample.encrypt.EncryptedSharedPreferencesManager | |
import com.rogergcc.encryptedsharedpreferencessample.encrypt.SharedPreferencesMigrator | |
import com.rogergcc.encryptedsharedpreferencessample.preferences.SharedPreferencesManager | |
/** | |
* Created on enero. | |
* year 2024 . | |
*/ | |
class BaseApp : Application() { | |
override fun onCreate() { | |
super.onCreate() | |
SharedPreferencesManager.getInstance(this, FILENAME_PREFERENCES) | |
EncryptedSharedPreferencesManager.getInstance(this, FILENAME_PREFERENCES) | |
migrateSharedPreferences(FILENAME_PREFERENCES, FILENAME_PREFERENCES) | |
} | |
companion object { | |
const val TAG = "BaseApp" | |
const val FILENAME_PREFERENCES: String = "app_prefs" | |
} | |
private fun migrateSharedPreferences(oldFileName: String, newFileName: String) { | |
Log.d(TAG, "migrateSharedPreferences: init") | |
if (sharedPreferencesExist(oldFileName)) { | |
Log.d(TAG, "migrateSharedPreferences: ANTERIORES") | |
val oldPrefs = SharedPreferencesManager.getInstance(this, oldFileName).getSharedPreferences() | |
val encryptedPrefs = EncryptedSharedPreferencesManager.getInstance(this,newFileName).getSharedPreferences() | |
val migrator = SharedPreferencesMigrator(oldPrefs, encryptedPrefs) | |
migrator.migrateAllData() | |
}else{ | |
Log.d(TAG, "migrateSharedPreferences: Not SharedPreferences found ") | |
} | |
} | |
private fun sharedPreferencesExist(fileName: String): Boolean { | |
return try { | |
SharedPreferencesManager.getInstance(this, fileName).getSharedPreferences().all.isNotEmpty() | |
} catch (e: Exception) { | |
false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample
Migrate SharedPreference to EncryptedSharedPreferences
v.1.0,0 SharedPreferences to v1.1.0 EncryptedSharedPreferences