Merge pull request #1047 from Bnyro/master

migrate preference files
This commit is contained in:
Bnyro 2022-08-13 19:23:23 +02:00 committed by GitHub
commit abd379c51f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package com.github.libretube
import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.os.StrictMode
import android.os.StrictMode.VmPolicy
@ -49,6 +50,11 @@ class MyApp : Application() {
val defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
val exceptionHandler = ExceptionHandler(defaultExceptionHandler)
Thread.setDefaultUncaughtExceptionHandler(exceptionHandler)
/**
* legacy preference file migration
*/
prefFileMigration()
}
/**
@ -93,6 +99,9 @@ class MyApp : Application() {
)
}
/**
* Creates a [NotificationChannel]
*/
private fun createNotificationChannel(
id: String,
name: String,
@ -109,4 +118,22 @@ class MyApp : Application() {
notificationManager.createNotificationChannel(channel)
}
}
/**
* Migration from old preference files to new one
*/
private fun prefFileMigration() {
val legacyUserPrefs = getSharedPreferences("username", Context.MODE_PRIVATE)
val username = legacyUserPrefs.getString("username", "")!!
if (username != "") {
PreferenceHelper.setUsername(username)
legacyUserPrefs.edit().putString("username", "")
}
val legacyTokenPrefs = getSharedPreferences("token", Context.MODE_PRIVATE)
val token = legacyUserPrefs.getString("token", "")!!
if (token != "") {
PreferenceHelper.setToken(token)
legacyTokenPrefs.edit().putString("token", "")
}
}
}

View File

@ -15,8 +15,19 @@ object PreferenceHelper {
private val TAG = "PreferenceHelper"
private lateinit var prefContext: Context
/**
* for normal preferences
*/
private lateinit var settings: SharedPreferences
private lateinit var editor: SharedPreferences.Editor
/**
* For sensitive data (like token)
*/
private lateinit var authSettings: SharedPreferences
private lateinit var authEditor: SharedPreferences.Editor
private val mapper = ObjectMapper()
/**
@ -24,8 +35,12 @@ object PreferenceHelper {
*/
fun setContext(context: Context) {
prefContext = context
settings = getDefaultSharedPreferences(prefContext)
editor = settings.edit()
authSettings = getAuthenticationPreferences(context)
authEditor = authSettings.edit()
}
fun getString(key: String?, defValue: String?): String {
@ -45,23 +60,19 @@ object PreferenceHelper {
}
fun getToken(): String {
val sharedPref = prefContext.getSharedPreferences("token", Context.MODE_PRIVATE)
return sharedPref?.getString("token", "")!!
return authSettings.getString(PreferenceKeys.TOKEN, "")!!
}
fun setToken(newValue: String) {
val editor = prefContext.getSharedPreferences("token", Context.MODE_PRIVATE).edit()
editor.putString("token", newValue).apply()
authEditor.putString(PreferenceKeys.TOKEN, newValue).apply()
}
fun getUsername(): String {
val sharedPref = prefContext.getSharedPreferences("username", Context.MODE_PRIVATE)
return sharedPref.getString("username", "")!!
return authSettings.getString(PreferenceKeys.USERNAME, "")!!
}
fun setUsername(newValue: String) {
val editor = prefContext.getSharedPreferences("username", Context.MODE_PRIVATE).edit()
editor.putString("username", newValue).apply()
authEditor.putString(PreferenceKeys.USERNAME, newValue).apply()
}
fun saveCustomInstance(customInstance: CustomInstance) {
@ -266,4 +277,8 @@ object PreferenceHelper {
private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context)
}
private fun getAuthenticationPreferences(context: Context): SharedPreferences {
return context.getSharedPreferences(PreferenceKeys.AUTH_PREF_FILE, Context.MODE_PRIVATE)
}
}

View File

@ -4,6 +4,13 @@ package com.github.libretube.preferences
* keys for the shared preferences
*/
object PreferenceKeys {
/**
* Authentication
*/
const val AUTH_PREF_FILE = "auth"
const val TOKEN = "token"
const val USERNAME = "username"
/**
* General
*/