Cleanup: Centralise preference keys

Put them into `Functions.kt`
This commit is contained in:
Finn M Glas 2020-06-24 09:38:21 +02:00
parent f46c1a278c
commit abcc1870fc
No known key found for this signature in database
GPG key ID: 902A30146014DFBF
5 changed files with 79 additions and 99 deletions

View file

@ -28,6 +28,31 @@ import kotlin.math.roundToInt
/* Preferences (global, initialised when app is started) */ /* Preferences (global, initialised when app is started) */
lateinit var launcherPreferences: SharedPreferences lateinit var launcherPreferences: SharedPreferences
/* Preference Key Constants */
const val ACTION_UP = "action_upApp"
const val ACTION_DOWN = "action_downApp"
const val ACTION_RIGHT = "action_rightApp"
const val ACTION_LEFT = "action_leftApp"
const val ACTION_VOL_UP = "action_volumeUpApp"
const val ACTION_VOL_DOWN = "action_volumeDownApp"
const val ACTION_DOUBLE_CLICK = "action_doubleClickApp"
const val ACTION_LONG_CLICK = "action_longClickApp"
const val ACTION_CALENDAR = "action_calendarApp"
const val ACTION_CLOCK = "action_clockApp"
val ACTIONS = listOf(ACTION_UP, ACTION_DOWN, ACTION_RIGHT, ACTION_LEFT,
ACTION_VOL_UP, ACTION_VOL_DOWN, ACTION_DOUBLE_CLICK, ACTION_LONG_CLICK,
ACTION_CALENDAR, ACTION_CLOCK)
const val PREF_DOMINANT = "custom_dominant"
const val PREF_VIBRANT = "custom_vibrant"
const val PREF_WALLPAPER = "background_uri"
const val PREF_THEME = "theme"
const val PREF_STARTED = "startedBefore"
const val PREF_STARTED_TIME = "firstStartup"
/* Objects used by multiple activities */ /* Objects used by multiple activities */
lateinit var appListViewAdapter: AppsRecyclerAdapter lateinit var appListViewAdapter: AppsRecyclerAdapter
@ -203,24 +228,24 @@ fun openNewTabWindow(urls: String, context : Context) {
/* Settings related functions */ /* Settings related functions */
fun getSavedTheme(context : Context) : String { fun getSavedTheme(context : Context) : String {
return launcherPreferences.getString("theme", "finn").toString() return launcherPreferences.getString(PREF_THEME, "finn").toString()
} }
fun saveTheme(themeName : String) : String { fun saveTheme(themeName : String) : String {
launcherPreferences.edit() launcherPreferences.edit()
.putString("theme", themeName) .putString(PREF_THEME, themeName)
.apply() .apply()
return themeName return themeName
} }
fun openAppSettings(pkg :String, context:Context){ fun openAppSettings(pkg :String, context:Context) {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.data = Uri.parse("package:$pkg") intent.data = Uri.parse("package:$pkg")
context.startActivity(intent) context.startActivity(intent)
} }
fun openSettings(activity: Activity){ fun openSettings(activity: Activity) {
activity.startActivity(Intent(activity, SettingsActivity::class.java)) activity.startActivity(Intent(activity, SettingsActivity::class.java))
} }
@ -235,22 +260,22 @@ fun openAppsList(activity: Activity){
activity.startActivity(intent) activity.startActivity(intent)
} }
fun loadSettings(){ fun loadSettings() {
upApp = launcherPreferences.getString("action_upApp", "").toString() upApp = launcherPreferences.getString(ACTION_UP, "")!!
downApp = launcherPreferences.getString("action_downApp", "").toString() downApp = launcherPreferences.getString(ACTION_DOWN, "")!!
rightApp = launcherPreferences.getString("action_rightApp", "").toString() rightApp = launcherPreferences.getString(ACTION_RIGHT, "")!!
leftApp = launcherPreferences.getString("action_leftApp", "").toString() leftApp = launcherPreferences.getString(ACTION_LEFT, "")!!
volumeUpApp = launcherPreferences.getString("action_volumeUpApp", "").toString() volumeUpApp = launcherPreferences.getString(ACTION_VOL_UP, "")!!
volumeDownApp = launcherPreferences.getString("action_volumeDownApp", "").toString() volumeDownApp = launcherPreferences.getString(ACTION_VOL_DOWN, "")!!
doubleClickApp = launcherPreferences.getString("action_doubleClickApp", "").toString() doubleClickApp = launcherPreferences.getString(ACTION_DOUBLE_CLICK, "")!!
longClickApp = launcherPreferences.getString("action_longClickApp", "").toString() longClickApp = launcherPreferences.getString(ACTION_LONG_CLICK, "")!!
calendarApp = launcherPreferences.getString("action_calendarApp", "").toString() calendarApp = launcherPreferences.getString(ACTION_CALENDAR, "")!!
clockApp = launcherPreferences.getString("action_clockApp", "").toString() clockApp = launcherPreferences.getString(ACTION_CLOCK, "")!!
dominantColor = launcherPreferences.getInt("custom_dominant", 0) dominantColor = launcherPreferences.getInt(PREF_DOMINANT, 0)
vibrantColor = launcherPreferences.getInt("custom_vibrant", 0) vibrantColor = launcherPreferences.getInt(PREF_VIBRANT, 0)
} }
fun resetSettings(context: Context) { fun resetSettings(context: Context) {
@ -262,77 +287,32 @@ fun resetSettings(context: Context) {
vibrantColor = context.resources.getColor(R.color.finnmglasTheme_accent_color) vibrantColor = context.resources.getColor(R.color.finnmglasTheme_accent_color)
launcherPreferences.edit() launcherPreferences.edit()
.putString("background_uri", "") .putString(PREF_WALLPAPER, "")
.putInt("custom_dominant", dominantColor) .putInt(PREF_DOMINANT, dominantColor)
.putInt("custom_vibrant", vibrantColor) .putInt(PREF_VIBRANT, vibrantColor)
.apply() .apply()
saveTheme("finn") saveTheme("finn")
editor.putString( // load action defaults
"action_upApp", for (actionKey in ACTIONS)
pickDefaultApp("action_upApp", context) editor.putString(actionKey, pickDefaultApp(actionKey, context))
)
editor.putString(
"action_downApp",
pickDefaultApp("action_downApp", context)
)
editor.putString(
"action_rightApp",
pickDefaultApp("action_rightApp", context)
)
editor.putString(
"action_leftApp",
pickDefaultApp("action_leftApp", context)
)
editor.putString(
"action_calendarApp",
pickDefaultApp("action_leftApp", context)
)
editor.putString(
"action_volumeUpApp",
pickDefaultApp("action_volumeUpApp",context)
)
editor.putString(
"action_volumeDownApp",
pickDefaultApp("action_volumeDownApp",context)
)
editor.putString(
"action_doubleClickApp",
pickDefaultApp("action_doubleClickApp", context)
)
editor.putString(
"action_longClickApp",
pickDefaultApp("action_longClickApp", context)
)
editor.putString(
"action_clockApp",
pickDefaultApp("action_clockApp", context)
)
editor.apply() editor.apply()
} }
fun pickDefaultApp(action: String, context: Context) : String { fun pickDefaultApp(action: String, context: Context) : String {
val arrayResource = when (action) { val arrayResource = when (action) {
"action_upApp" -> R.array.default_up ACTION_UP -> R.array.default_up
"action_downApp" -> R.array.default_down ACTION_DOWN -> R.array.default_down
"action_rightApp" -> R.array.default_right ACTION_RIGHT -> R.array.default_right
"action_leftApp" -> R.array.default_left ACTION_LEFT -> R.array.default_left
"action_volumeUpApp" -> R.array.default_volume_up ACTION_VOL_UP -> R.array.default_volume_up
"action_volumeDownApp" -> R.array.default_volume_down ACTION_VOL_DOWN -> R.array.default_volume_down
"action_doubleClickApp" -> R.array.default_double_click ACTION_DOUBLE_CLICK -> R.array.default_double_click
"action_longClickApp" -> R.array.default_long_click ACTION_LONG_CLICK -> R.array.default_long_click
"action_clockApp" -> R.array.default_clock ACTION_CLOCK -> R.array.default_clock
ACTION_CALENDAR -> R.array.default_left
else -> return "" // just prevent crashing on unknown input else -> return "" // just prevent crashing on unknown input
} }

View file

@ -57,7 +57,7 @@ class HomeActivity: UIObject, AppCompatActivity(),
} }
// First time opening the app: show Tutorial // First time opening the app: show Tutorial
if (!launcherPreferences.getBoolean("startedBefore", false)) if (!launcherPreferences.getBoolean(PREF_STARTED, false))
startActivity(Intent(this, TutorialActivity::class.java)) startActivity(Intent(this, TutorialActivity::class.java))
// Initialise layout // Initialise layout
@ -186,11 +186,11 @@ class HomeActivity: UIObject, AppCompatActivity(),
home_settings_icon.setTextColor(vibrantColor) home_settings_icon.setTextColor(vibrantColor)
home_container.setBackgroundColor(dominantColor) home_container.setBackgroundColor(dominantColor)
if (launcherPreferences.getString("background_uri", "") != "") { if (launcherPreferences.getString(PREF_WALLPAPER, "") != "") {
try { try {
background = MediaStore.Images.Media.getBitmap( background = MediaStore.Images.Media.getBitmap(
this.contentResolver, Uri.parse( this.contentResolver, Uri.parse(
launcherPreferences.getString("background_uri", "") launcherPreferences.getString(PREF_WALLPAPER, "")
) )
) )
} catch (e: Exception) { } } catch (e: Exception) { }
@ -200,9 +200,9 @@ class HomeActivity: UIObject, AppCompatActivity(),
vibrantColor = resources.getColor(R.color.finnmglasTheme_accent_color) vibrantColor = resources.getColor(R.color.finnmglasTheme_accent_color)
launcherPreferences.edit() launcherPreferences.edit()
.putString("background_uri", "") .putString(PREF_WALLPAPER, "")
.putInt("custom_dominant", dominantColor) .putInt(PREF_DOMINANT, dominantColor)
.putInt("custom_vibrant", vibrantColor) .putInt(PREF_VIBRANT, vibrantColor)
.apply() .apply()
saveTheme("finn") saveTheme("finn")

View file

@ -91,9 +91,9 @@ class SettingsFragmentTheme : Fragment(), UIObject {
/* Save image Uri as string */ /* Save image Uri as string */
launcherPreferences.edit() launcherPreferences.edit()
.putString("background_uri", imageUri.toString()) .putString(PREF_WALLPAPER, imageUri.toString())
.putInt("custom_dominant", dominantColor) .putInt(PREF_DOMINANT, dominantColor)
.putInt("custom_vibrant", vibrantColor) .putInt(PREF_VIBRANT, vibrantColor)
.apply() .apply()
saveTheme("custom") saveTheme("custom")
@ -127,9 +127,9 @@ class SettingsFragmentTheme : Fragment(), UIObject {
vibrantColor = resources.getColor(R.color.darkTheme_accent_color) vibrantColor = resources.getColor(R.color.darkTheme_accent_color)
launcherPreferences.edit() launcherPreferences.edit()
.putString("background_uri", "") .putString(PREF_WALLPAPER, "")
.putInt("custom_dominant", dominantColor) .putInt(PREF_DOMINANT, dominantColor)
.putInt("custom_vibrant", vibrantColor) .putInt(PREF_VIBRANT, vibrantColor)
.apply() .apply()
saveTheme("dark") saveTheme("dark")
@ -142,9 +142,9 @@ class SettingsFragmentTheme : Fragment(), UIObject {
vibrantColor = resources.getColor(R.color.finnmglasTheme_accent_color) vibrantColor = resources.getColor(R.color.finnmglasTheme_accent_color)
launcherPreferences.edit() launcherPreferences.edit()
.putString("background_uri", "") .putString(PREF_WALLPAPER, "")
.putInt("custom_dominant", dominantColor) .putInt(PREF_DOMINANT, dominantColor)
.putInt("custom_vibrant", vibrantColor) .putInt(PREF_VIBRANT, vibrantColor)
.apply() .apply()
saveTheme("finn") saveTheme("finn")

View file

@ -30,7 +30,7 @@ class TutorialActivity: AppCompatActivity(), UIObject {
setContentView(R.layout.tutorial) setContentView(R.layout.tutorial)
// Check if the app was started before // Check if the app was started before
if (launcherPreferences.getBoolean("startedBefore", false)) if (launcherPreferences.getBoolean(PREF_STARTED, false))
tutorial_appbar.visibility = View.VISIBLE tutorial_appbar.visibility = View.VISIBLE
else resetSettings(this) else resetSettings(this)
@ -78,7 +78,7 @@ class TutorialActivity: AppCompatActivity(), UIObject {
// Prevent going back, allow if viewed again later // Prevent going back, allow if viewed again later
override fun onBackPressed() { override fun onBackPressed() {
if (launcherPreferences.getBoolean("startedBefore", false)) if (launcherPreferences.getBoolean(PREF_STARTED, false))
super.onBackPressed() super.onBackPressed()
} }

View file

@ -41,15 +41,15 @@ class TutorialFragmentFinish(): Fragment(), UIObject {
super.adjustLayout() super.adjustLayout()
// Different text if opened again later (from settings) // Different text if opened again later (from settings)
if (launcherPreferences.getBoolean("startedBefore", false)) if (launcherPreferences.getBoolean(PREF_STARTED, false))
tutorial_finish_button_start.text = "Back to Settings" tutorial_finish_button_start.text = "Back to Settings"
} }
private fun finishTutorial() { private fun finishTutorial() {
if (!launcherPreferences.getBoolean("startedBefore", false)){ if (!launcherPreferences.getBoolean(PREF_STARTED, false)){
launcherPreferences.edit() launcherPreferences.edit()
.putBoolean("startedBefore", true) // never auto run this again .putBoolean(PREF_STARTED, true) // never auto run this again
.putLong("firstStartup", System.currentTimeMillis() / 1000L) // record first startup timestamp .putLong(PREF_STARTED_TIME, System.currentTimeMillis() / 1000L) // record first startup timestamp
.apply() .apply()
} }
activity!!.finish() activity!!.finish()