From abcc1870fc52071c44ec2f48219fac9bb19b3567 Mon Sep 17 00:00:00 2001 From: Finn M Glas Date: Wed, 24 Jun 2020 09:38:21 +0200 Subject: [PATCH] Cleanup: Centralise preference keys Put them into `Functions.kt` --- .../java/com/finnmglas/launcher/Functions.kt | 136 ++++++++---------- .../com/finnmglas/launcher/HomeActivity.kt | 12 +- .../settings/theme/SettingsFragmentTheme.kt | 18 +-- .../launcher/tutorial/TutorialActivity.kt | 4 +- .../tutorial/tabs/TutorialFragmentFinish.kt | 8 +- 5 files changed, 79 insertions(+), 99 deletions(-) diff --git a/app/src/main/java/com/finnmglas/launcher/Functions.kt b/app/src/main/java/com/finnmglas/launcher/Functions.kt index f372ec2..4005254 100644 --- a/app/src/main/java/com/finnmglas/launcher/Functions.kt +++ b/app/src/main/java/com/finnmglas/launcher/Functions.kt @@ -28,6 +28,31 @@ import kotlin.math.roundToInt /* Preferences (global, initialised when app is started) */ 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 */ lateinit var appListViewAdapter: AppsRecyclerAdapter @@ -203,24 +228,24 @@ fun openNewTabWindow(urls: String, context : Context) { /* Settings related functions */ fun getSavedTheme(context : Context) : String { - return launcherPreferences.getString("theme", "finn").toString() + return launcherPreferences.getString(PREF_THEME, "finn").toString() } fun saveTheme(themeName : String) : String { launcherPreferences.edit() - .putString("theme", themeName) + .putString(PREF_THEME, themeName) .apply() return themeName } -fun openAppSettings(pkg :String, context:Context){ +fun openAppSettings(pkg :String, context:Context) { val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) intent.data = Uri.parse("package:$pkg") context.startActivity(intent) } -fun openSettings(activity: Activity){ +fun openSettings(activity: Activity) { activity.startActivity(Intent(activity, SettingsActivity::class.java)) } @@ -235,22 +260,22 @@ fun openAppsList(activity: Activity){ activity.startActivity(intent) } -fun loadSettings(){ - upApp = launcherPreferences.getString("action_upApp", "").toString() - downApp = launcherPreferences.getString("action_downApp", "").toString() - rightApp = launcherPreferences.getString("action_rightApp", "").toString() - leftApp = launcherPreferences.getString("action_leftApp", "").toString() - volumeUpApp = launcherPreferences.getString("action_volumeUpApp", "").toString() - volumeDownApp = launcherPreferences.getString("action_volumeDownApp", "").toString() +fun loadSettings() { + upApp = launcherPreferences.getString(ACTION_UP, "")!! + downApp = launcherPreferences.getString(ACTION_DOWN, "")!! + rightApp = launcherPreferences.getString(ACTION_RIGHT, "")!! + leftApp = launcherPreferences.getString(ACTION_LEFT, "")!! + volumeUpApp = launcherPreferences.getString(ACTION_VOL_UP, "")!! + volumeDownApp = launcherPreferences.getString(ACTION_VOL_DOWN, "")!! - doubleClickApp = launcherPreferences.getString("action_doubleClickApp", "").toString() - longClickApp = launcherPreferences.getString("action_longClickApp", "").toString() + doubleClickApp = launcherPreferences.getString(ACTION_DOUBLE_CLICK, "")!! + longClickApp = launcherPreferences.getString(ACTION_LONG_CLICK, "")!! - calendarApp = launcherPreferences.getString("action_calendarApp", "").toString() - clockApp = launcherPreferences.getString("action_clockApp", "").toString() + calendarApp = launcherPreferences.getString(ACTION_CALENDAR, "")!! + clockApp = launcherPreferences.getString(ACTION_CLOCK, "")!! - dominantColor = launcherPreferences.getInt("custom_dominant", 0) - vibrantColor = launcherPreferences.getInt("custom_vibrant", 0) + dominantColor = launcherPreferences.getInt(PREF_DOMINANT, 0) + vibrantColor = launcherPreferences.getInt(PREF_VIBRANT, 0) } fun resetSettings(context: Context) { @@ -262,77 +287,32 @@ fun resetSettings(context: Context) { vibrantColor = context.resources.getColor(R.color.finnmglasTheme_accent_color) launcherPreferences.edit() - .putString("background_uri", "") - .putInt("custom_dominant", dominantColor) - .putInt("custom_vibrant", vibrantColor) + .putString(PREF_WALLPAPER, "") + .putInt(PREF_DOMINANT, dominantColor) + .putInt(PREF_VIBRANT, vibrantColor) .apply() saveTheme("finn") - editor.putString( - "action_upApp", - pickDefaultApp("action_upApp", 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) - ) + // load action defaults + for (actionKey in ACTIONS) + editor.putString(actionKey, pickDefaultApp(actionKey, context)) editor.apply() } fun pickDefaultApp(action: String, context: Context) : String { val arrayResource = when (action) { - "action_upApp" -> R.array.default_up - "action_downApp" -> R.array.default_down - "action_rightApp" -> R.array.default_right - "action_leftApp" -> R.array.default_left - "action_volumeUpApp" -> R.array.default_volume_up - "action_volumeDownApp" -> R.array.default_volume_down - "action_doubleClickApp" -> R.array.default_double_click - "action_longClickApp" -> R.array.default_long_click - "action_clockApp" -> R.array.default_clock + ACTION_UP -> R.array.default_up + ACTION_DOWN -> R.array.default_down + ACTION_RIGHT -> R.array.default_right + ACTION_LEFT -> R.array.default_left + ACTION_VOL_UP -> R.array.default_volume_up + ACTION_VOL_DOWN -> R.array.default_volume_down + ACTION_DOUBLE_CLICK -> R.array.default_double_click + ACTION_LONG_CLICK -> R.array.default_long_click + ACTION_CLOCK -> R.array.default_clock + ACTION_CALENDAR -> R.array.default_left else -> return "" // just prevent crashing on unknown input } diff --git a/app/src/main/java/com/finnmglas/launcher/HomeActivity.kt b/app/src/main/java/com/finnmglas/launcher/HomeActivity.kt index c174b89..537077e 100644 --- a/app/src/main/java/com/finnmglas/launcher/HomeActivity.kt +++ b/app/src/main/java/com/finnmglas/launcher/HomeActivity.kt @@ -57,7 +57,7 @@ class HomeActivity: UIObject, AppCompatActivity(), } // First time opening the app: show Tutorial - if (!launcherPreferences.getBoolean("startedBefore", false)) + if (!launcherPreferences.getBoolean(PREF_STARTED, false)) startActivity(Intent(this, TutorialActivity::class.java)) // Initialise layout @@ -186,11 +186,11 @@ class HomeActivity: UIObject, AppCompatActivity(), home_settings_icon.setTextColor(vibrantColor) home_container.setBackgroundColor(dominantColor) - if (launcherPreferences.getString("background_uri", "") != "") { + if (launcherPreferences.getString(PREF_WALLPAPER, "") != "") { try { background = MediaStore.Images.Media.getBitmap( this.contentResolver, Uri.parse( - launcherPreferences.getString("background_uri", "") + launcherPreferences.getString(PREF_WALLPAPER, "") ) ) } catch (e: Exception) { } @@ -200,9 +200,9 @@ class HomeActivity: UIObject, AppCompatActivity(), vibrantColor = resources.getColor(R.color.finnmglasTheme_accent_color) launcherPreferences.edit() - .putString("background_uri", "") - .putInt("custom_dominant", dominantColor) - .putInt("custom_vibrant", vibrantColor) + .putString(PREF_WALLPAPER, "") + .putInt(PREF_DOMINANT, dominantColor) + .putInt(PREF_VIBRANT, vibrantColor) .apply() saveTheme("finn") diff --git a/app/src/main/java/com/finnmglas/launcher/settings/theme/SettingsFragmentTheme.kt b/app/src/main/java/com/finnmglas/launcher/settings/theme/SettingsFragmentTheme.kt index e667745..4094423 100644 --- a/app/src/main/java/com/finnmglas/launcher/settings/theme/SettingsFragmentTheme.kt +++ b/app/src/main/java/com/finnmglas/launcher/settings/theme/SettingsFragmentTheme.kt @@ -91,9 +91,9 @@ class SettingsFragmentTheme : Fragment(), UIObject { /* Save image Uri as string */ launcherPreferences.edit() - .putString("background_uri", imageUri.toString()) - .putInt("custom_dominant", dominantColor) - .putInt("custom_vibrant", vibrantColor) + .putString(PREF_WALLPAPER, imageUri.toString()) + .putInt(PREF_DOMINANT, dominantColor) + .putInt(PREF_VIBRANT, vibrantColor) .apply() saveTheme("custom") @@ -127,9 +127,9 @@ class SettingsFragmentTheme : Fragment(), UIObject { vibrantColor = resources.getColor(R.color.darkTheme_accent_color) launcherPreferences.edit() - .putString("background_uri", "") - .putInt("custom_dominant", dominantColor) - .putInt("custom_vibrant", vibrantColor) + .putString(PREF_WALLPAPER, "") + .putInt(PREF_DOMINANT, dominantColor) + .putInt(PREF_VIBRANT, vibrantColor) .apply() saveTheme("dark") @@ -142,9 +142,9 @@ class SettingsFragmentTheme : Fragment(), UIObject { vibrantColor = resources.getColor(R.color.finnmglasTheme_accent_color) launcherPreferences.edit() - .putString("background_uri", "") - .putInt("custom_dominant", dominantColor) - .putInt("custom_vibrant", vibrantColor) + .putString(PREF_WALLPAPER, "") + .putInt(PREF_DOMINANT, dominantColor) + .putInt(PREF_VIBRANT, vibrantColor) .apply() saveTheme("finn") diff --git a/app/src/main/java/com/finnmglas/launcher/tutorial/TutorialActivity.kt b/app/src/main/java/com/finnmglas/launcher/tutorial/TutorialActivity.kt index 5dc594f..3042dec 100644 --- a/app/src/main/java/com/finnmglas/launcher/tutorial/TutorialActivity.kt +++ b/app/src/main/java/com/finnmglas/launcher/tutorial/TutorialActivity.kt @@ -30,7 +30,7 @@ class TutorialActivity: AppCompatActivity(), UIObject { setContentView(R.layout.tutorial) // Check if the app was started before - if (launcherPreferences.getBoolean("startedBefore", false)) + if (launcherPreferences.getBoolean(PREF_STARTED, false)) tutorial_appbar.visibility = View.VISIBLE else resetSettings(this) @@ -78,7 +78,7 @@ class TutorialActivity: AppCompatActivity(), UIObject { // Prevent going back, allow if viewed again later override fun onBackPressed() { - if (launcherPreferences.getBoolean("startedBefore", false)) + if (launcherPreferences.getBoolean(PREF_STARTED, false)) super.onBackPressed() } diff --git a/app/src/main/java/com/finnmglas/launcher/tutorial/tabs/TutorialFragmentFinish.kt b/app/src/main/java/com/finnmglas/launcher/tutorial/tabs/TutorialFragmentFinish.kt index 3f551f0..9af79ab 100644 --- a/app/src/main/java/com/finnmglas/launcher/tutorial/tabs/TutorialFragmentFinish.kt +++ b/app/src/main/java/com/finnmglas/launcher/tutorial/tabs/TutorialFragmentFinish.kt @@ -41,15 +41,15 @@ class TutorialFragmentFinish(): Fragment(), UIObject { super.adjustLayout() // 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" } private fun finishTutorial() { - if (!launcherPreferences.getBoolean("startedBefore", false)){ + if (!launcherPreferences.getBoolean(PREF_STARTED, false)){ launcherPreferences.edit() - .putBoolean("startedBefore", true) // never auto run this again - .putLong("firstStartup", System.currentTimeMillis() / 1000L) // record first startup timestamp + .putBoolean(PREF_STARTED, true) // never auto run this again + .putLong(PREF_STARTED_TIME, System.currentTimeMillis() / 1000L) // record first startup timestamp .apply() } activity!!.finish()