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.Application
import android.app.NotificationChannel import android.app.NotificationChannel
import android.app.NotificationManager import android.app.NotificationManager
import android.content.Context
import android.os.Build import android.os.Build
import android.os.StrictMode import android.os.StrictMode
import android.os.StrictMode.VmPolicy import android.os.StrictMode.VmPolicy
@ -49,6 +50,11 @@ class MyApp : Application() {
val defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler() val defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
val exceptionHandler = ExceptionHandler(defaultExceptionHandler) val exceptionHandler = ExceptionHandler(defaultExceptionHandler)
Thread.setDefaultUncaughtExceptionHandler(exceptionHandler) Thread.setDefaultUncaughtExceptionHandler(exceptionHandler)
/**
* legacy preference file migration
*/
prefFileMigration()
} }
/** /**
@ -93,6 +99,9 @@ class MyApp : Application() {
) )
} }
/**
* Creates a [NotificationChannel]
*/
private fun createNotificationChannel( private fun createNotificationChannel(
id: String, id: String,
name: String, name: String,
@ -109,4 +118,22 @@ class MyApp : Application() {
notificationManager.createNotificationChannel(channel) 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 val TAG = "PreferenceHelper"
private lateinit var prefContext: Context private lateinit var prefContext: Context
/**
* for normal preferences
*/
private lateinit var settings: SharedPreferences private lateinit var settings: SharedPreferences
private lateinit var editor: SharedPreferences.Editor 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() private val mapper = ObjectMapper()
/** /**
@ -24,8 +35,12 @@ object PreferenceHelper {
*/ */
fun setContext(context: Context) { fun setContext(context: Context) {
prefContext = context prefContext = context
settings = getDefaultSharedPreferences(prefContext) settings = getDefaultSharedPreferences(prefContext)
editor = settings.edit() editor = settings.edit()
authSettings = getAuthenticationPreferences(context)
authEditor = authSettings.edit()
} }
fun getString(key: String?, defValue: String?): String { fun getString(key: String?, defValue: String?): String {
@ -45,23 +60,19 @@ object PreferenceHelper {
} }
fun getToken(): String { fun getToken(): String {
val sharedPref = prefContext.getSharedPreferences("token", Context.MODE_PRIVATE) return authSettings.getString(PreferenceKeys.TOKEN, "")!!
return sharedPref?.getString("token", "")!!
} }
fun setToken(newValue: String) { fun setToken(newValue: String) {
val editor = prefContext.getSharedPreferences("token", Context.MODE_PRIVATE).edit() authEditor.putString(PreferenceKeys.TOKEN, newValue).apply()
editor.putString("token", newValue).apply()
} }
fun getUsername(): String { fun getUsername(): String {
val sharedPref = prefContext.getSharedPreferences("username", Context.MODE_PRIVATE) return authSettings.getString(PreferenceKeys.USERNAME, "")!!
return sharedPref.getString("username", "")!!
} }
fun setUsername(newValue: String) { fun setUsername(newValue: String) {
val editor = prefContext.getSharedPreferences("username", Context.MODE_PRIVATE).edit() authEditor.putString(PreferenceKeys.USERNAME, newValue).apply()
editor.putString("username", newValue).apply()
} }
fun saveCustomInstance(customInstance: CustomInstance) { fun saveCustomInstance(customInstance: CustomInstance) {
@ -266,4 +277,8 @@ object PreferenceHelper {
private fun getDefaultSharedPreferences(context: Context): SharedPreferences { private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context) 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 * keys for the shared preferences
*/ */
object PreferenceKeys { object PreferenceKeys {
/**
* Authentication
*/
const val AUTH_PREF_FILE = "auth"
const val TOKEN = "token"
const val USERNAME = "username"
/** /**
* General * General
*/ */