From 870ee56b88d559d5538608cdacd76b56f5e5d6e0 Mon Sep 17 00:00:00 2001 From: Josia Pietsch Date: Mon, 9 Sep 2024 21:23:01 +0200 Subject: [PATCH] chore: migrated preferences to eu.jonahbauer.android.preference --- app/build.gradle | 4 + app/src/main/AndroidManifest.xml | 1 + .../de/jrpie/android/launcher/Application.kt | 15 + .../de/jrpie/android/launcher/Functions.kt | 129 +----- .../java/de/jrpie/android/launcher/Gesture.kt | 8 +- .../de/jrpie/android/launcher/HomeActivity.kt | 59 +-- .../launcher/LauncherPreferences$Config.java | 48 +++ .../de/jrpie/android/launcher/Preferences.kt | 70 +++ .../android/launcher/list/ListActivity.kt | 8 +- .../launcher/list/apps/AppsRecyclerAdapter.kt | 11 +- .../launcher/list/apps/ListFragmentApps.kt | 6 +- .../launcher/settings/SettingsActivity.kt | 2 +- .../actions/SettingsFragmentActions.kt | 6 +- .../SettingsFragmentActionsRecycler.kt | 15 +- .../launcher/SettingsFragmentLauncher.kt | 139 +----- .../settings/meta/SettingsFragmentMeta.kt | 3 +- .../launcher/tutorial/TutorialActivity.kt | 11 +- .../tutorial/tabs/TutorialFragmentFinish.kt | 19 +- .../tutorial/tabs/TutorialFragmentStart.kt | 1 + app/src/main/res/layout/settings_launcher.xml | 402 ------------------ app/src/main/res/values-de/strings.xml | 26 +- app/src/main/res/values-es/strings.xml | 14 +- app/src/main/res/values-fr/strings.xml | 26 +- app/src/main/res/values-zh-rCN/strings.xml | 26 +- app/src/main/res/values/donottranslate.xml | 97 +++++ app/src/main/res/values/strings.xml | 50 +-- app/src/main/res/xml/preferences.xml | 75 ++++ 27 files changed, 446 insertions(+), 825 deletions(-) create mode 100644 app/src/main/java/de/jrpie/android/launcher/Application.kt create mode 100644 app/src/main/java/de/jrpie/android/launcher/LauncherPreferences$Config.java delete mode 100644 app/src/main/res/layout/settings_launcher.xml create mode 100644 app/src/main/res/values/donottranslate.xml create mode 100644 app/src/main/res/xml/preferences.xml diff --git a/app/build.gradle b/app/build.gradle index 8cd05df..93b3e60 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -63,6 +63,10 @@ dependencies { implementation 'androidx.core:core-ktx:1.13.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' + implementation "eu.jonahbauer:android-preference-annotations:1.1.2" + implementation 'androidx.preference:preference-ktx:1.2.1' + //kapt "eu.jonahbauer:android-preference-annotations:1.1.2" + //annotationProcessor "eu.jonahbauer:android-preference-annotations:1.1.2" testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.2.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6aa938b..6b4cbae 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,6 +10,7 @@ = ArrayList() /* Variables containing settings */ val displayMetrics = DisplayMetrics() -var dominantColor = 0 -var vibrantColor = 0 - /* REQUEST CODES */ const val REQUEST_CHOOSE_APP = 1 -const val REQUEST_UNINSTALL = 2 +const val REQUEST_CHOOSE_APP_FROM_FAVORITES = 2 +const val REQUEST_UNINSTALL = 3 const val REQUEST_SET_DEFAULT_HOME = 42 @@ -109,13 +82,6 @@ fun View.blink( }) } -fun getPreferences(context: Context): SharedPreferences{ - return context.getSharedPreferences( - context.getString(R.string.preference_file_key), - Context.MODE_PRIVATE - ) -} - fun setDefaultHomeScreen(context: Context, checkDefault: Boolean = false) { if (checkDefault @@ -223,16 +189,16 @@ fun audioVolumeDown(activity: Activity) { ) } -fun expandNotificationsPanel(activity: Activity) { +fun expandNotificationsPanel(context: Context) { /* https://stackoverflow.com/a/15582509 */ try { @Suppress("SpellCheckingInspection") - val statusBarService: Any? = activity.getSystemService("statusbar") + val statusBarService: Any? = context.getSystemService("statusbar") val statusBarManager = Class.forName("android.app.StatusBarManager") val showStatusBar = statusBarManager.getMethod("expandNotificationsPanel") showStatusBar.invoke(statusBarService) } catch (e: Exception) { - Toast.makeText(activity, activity.getString(R.string.alert_cant_expand_notifications_panel), Toast.LENGTH_LONG).show() + Toast.makeText(context, context.getString(R.string.alert_cant_expand_notifications_panel), Toast.LENGTH_LONG).show() } } @@ -315,49 +281,6 @@ fun openNewTabWindow(urls: String, context: Context) { context.startActivity(intents) } -/* Settings related functions */ - -fun getSavedTheme(context: Context) : String { - return getPreferences(context).getString(PREF_THEME, "finn").toString() -} - -fun saveTheme(context: Context, themeName: String) : String { - getPreferences(context).edit() - .putString(PREF_THEME, themeName) - .apply() - - return themeName -} - -fun resetToDefaultTheme(activity: Activity) { - dominantColor = activity.resources.getColor(R.color.finnmglasTheme_background_color) - vibrantColor = activity.resources.getColor(R.color.finnmglasTheme_accent_color) - - getPreferences(activity).edit() - .putInt(PREF_DOMINANT, dominantColor) - .putInt(PREF_VIBRANT, vibrantColor) - .apply() - - saveTheme(activity,"finn") - loadSettings(activity) - - activity.recreate() -} - -fun resetToDarkTheme(activity: Activity) { - dominantColor = activity.resources.getColor(R.color.darkTheme_background_color) - vibrantColor = activity.resources.getColor(R.color.darkTheme_accent_color) - - getPreferences(activity).edit() - .putInt(PREF_DOMINANT, dominantColor) - .putInt(PREF_VIBRANT, vibrantColor) - .apply() - - saveTheme(activity,"dark") - - activity.recreate() -} - fun openAppSettings(packageName: String, user: Int?, context: Context, sourceBounds: Rect? = null, opts: Bundle? = null) { val launcherApps = context.getSystemService(Service.LAUNCHER_APPS_SERVICE) as LauncherApps @@ -436,45 +359,10 @@ fun loadApps(packageManager: PackageManager, context: Context) { appsList.addAll(loadList) } -fun loadSettings(context: Context) { - val preferences = getPreferences(context) - dominantColor = preferences.getInt(PREF_DOMINANT, 0) - vibrantColor = preferences.getInt(PREF_VIBRANT, 0) -} - - -fun resetSettings(context: Context) { - - val editor = getPreferences(context).edit() - - // set default theme - dominantColor = context.resources.getColor(R.color.finnmglasTheme_background_color) - vibrantColor = context.resources.getColor(R.color.finnmglasTheme_accent_color) - - editor - .putInt(PREF_DOMINANT, dominantColor) - .putInt(PREF_VIBRANT, vibrantColor) - .putString(PREF_THEME, "finn") - .putBoolean(PREF_SCREEN_TIMEOUT_DISABLED, false) - .putBoolean(PREF_SEARCH_AUTO_LAUNCH, false) - .putBoolean(PREF_DATE_VISIBLE, true) - .putBoolean(PREF_TIME_VISIBLE, true) - .putBoolean(PREF_DATE_TIME_FLIP, false) - .putBoolean(PREF_DATE_LOCALIZED, false) - .putBoolean(PREF_SCREEN_FULLSCREEN, true) - .putBoolean(PREF_DOUBLE_ACTIONS_ENABLED, true) - .putBoolean(PREF_EDGE_ACTIONS_ENABLED, true) - Gesture.values().forEach { editor.putString(it.id, it.pickDefaultApp(context)) } - - editor.apply() -} - fun setWindowFlags(window: Window) { window.setFlags(0, 0) // clear flags - - val preferences = getPreferences(window.context) // Display notification bar - if (preferences.getBoolean(PREF_SCREEN_FULLSCREEN, true)) + if (LauncherPreferences.display().fullScreen()) window.setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN @@ -482,7 +370,7 @@ fun setWindowFlags(window: Window) { else window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) // Screen Timeout - if (preferences.getBoolean(PREF_SCREEN_TIMEOUT_DISABLED, false)) + if (LauncherPreferences.display().screenTimeoutDisabled()) window.setFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON @@ -499,8 +387,6 @@ fun saveListActivityChoice(context: Context, data: Intent?) { val forGesture = data?.getStringExtra("forGesture") ?: return Gesture.byId(forGesture)?.setApp(context, value.toString(), user) - - loadSettings(context) } // Taken from https://stackoverflow.com/a/50743764/12787264 @@ -512,7 +398,6 @@ fun openSoftKeyboard(context: Context, view: View) { } /* Bitmaps */ - fun setButtonColor(btn: Button, color: Int) { if (Build.VERSION.SDK_INT >= 29) btn.background.colorFilter = BlendModeColorFilter(color, BlendMode.MULTIPLY) diff --git a/app/src/main/java/de/jrpie/android/launcher/Gesture.kt b/app/src/main/java/de/jrpie/android/launcher/Gesture.kt index fc48d1d..9bf5204 100644 --- a/app/src/main/java/de/jrpie/android/launcher/Gesture.kt +++ b/app/src/main/java/de/jrpie/android/launcher/Gesture.kt @@ -42,7 +42,7 @@ enum class Gesture (val id: String, private val labelResource: Int, } fun getApp(context: Context): Pair { - val preferences = getPreferences(context) + val preferences = LauncherPreferences.getSharedPreferences() var packageName = preferences.getString(this.id, "")!! var u: Int? = preferences.getInt(this.id + "_user", INVALID_USER) u = if(u == INVALID_USER) null else u @@ -50,18 +50,18 @@ enum class Gesture (val id: String, private val labelResource: Int, } fun removeApp(context: Context) { - getPreferences(context).edit() + LauncherPreferences.getSharedPreferences().edit() .putString(this.id, "") // clear it .apply() } fun setApp(context: Context, app: String, user: Int?) { - getPreferences(context).edit() + LauncherPreferences.getSharedPreferences().edit() .putString(this.id, app) .apply() val u = user?: INVALID_USER - getPreferences(context).edit() + LauncherPreferences.getSharedPreferences().edit() .putInt(this.id + "_user", u) .apply() } diff --git a/app/src/main/java/de/jrpie/android/launcher/HomeActivity.kt b/app/src/main/java/de/jrpie/android/launcher/HomeActivity.kt index d82acda..cd690cc 100644 --- a/app/src/main/java/de/jrpie/android/launcher/HomeActivity.kt +++ b/app/src/main/java/de/jrpie/android/launcher/HomeActivity.kt @@ -7,6 +7,7 @@ import android.view.GestureDetector import android.view.KeyEvent import android.view.MotionEvent import androidx.appcompat.app.AppCompatActivity +import androidx.core.app.AppLaunchChecker import androidx.core.view.GestureDetectorCompat import androidx.core.view.isVisible import de.jrpie.android.launcher.BuildConfig.VERSION_NAME @@ -50,38 +51,14 @@ class HomeActivity: UIObject, AppCompatActivity(), override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val preferences = getPreferences(this) - windowManager.defaultDisplay.getMetrics(displayMetrics) - - loadSettings(this) // First time opening the app: show Tutorial, else: check versions - if (!preferences.getBoolean(PREF_STARTED, false)) + if (!LauncherPreferences.internal().started()) { + + LauncherPreferences.internal().started(true) startActivity(Intent(this, TutorialActivity::class.java)) - else when (preferences.getString(PREF_VERSION, "")) { - // Check versions, make sure transitions between versions go well - - VERSION_NAME -> { /* the version installed and used previously are the same */ } - "" -> { /* The version used before was pre- v1.3.0, - as version tracking started then */ - - /* - * before, the dominant and vibrant color of the `finn` and `dark` theme - * were not stored anywhere. Now they have to be stored: - * -> we just reset them using newly implemented functions - */ - when (getSavedTheme(this)) { - "finn" -> resetToDefaultTheme(this) - "dark" -> resetToDarkTheme(this) - } - - preferences.edit() - .putString(PREF_VERSION, VERSION_NAME) // save new version - .apply() - - // show the new tutorial - startActivity(Intent(this, TutorialActivity::class.java)) - } + } else { + migrateToNewVersion(this) } // Preload apps to speed up the Apps Recycler @@ -98,21 +75,18 @@ class HomeActivity: UIObject, AppCompatActivity(), mDetector = GestureDetectorCompat(this, this) mDetector.setOnDoubleTapListener(this) - // for if the settings changed - loadSettings(this) super.onStart() } private fun updateClock() { - clockTimer?.cancel() - val preferences = getPreferences(this) + clockTimer.cancel() val locale = Locale.getDefault() - val dateVisible = preferences.getBoolean(PREF_DATE_VISIBLE, true) - val timeVisible = preferences.getBoolean(PREF_TIME_VISIBLE, true) + val dateVisible = LauncherPreferences.clock().dateVisible() + val timeVisible = LauncherPreferences.clock().timeVisible() var dateFMT = "yyyy-MM-dd" var timeFMT = "HH:mm:ss" - if (preferences.getBoolean(PREF_DATE_LOCALIZED, false)) { + if (LauncherPreferences.clock().localized()) { dateFMT = android.text.format.DateFormat.getBestDateTimePattern(locale, dateFMT) timeFMT = android.text.format.DateFormat.getBestDateTimePattern(locale, timeFMT) } @@ -122,7 +96,7 @@ class HomeActivity: UIObject, AppCompatActivity(), var upperVisible = dateVisible var lowerVisible = timeVisible - if(preferences.getBoolean(PREF_DATE_TIME_FLIP, false)) { + if(LauncherPreferences.clock().flipDateTime()) { upperFormat = lowerFormat.also { lowerFormat = upperFormat } upperVisible = lowerVisible.also { lowerVisible = upperVisible } } @@ -176,10 +150,8 @@ class HomeActivity: UIObject, AppCompatActivity(), val diffX = e1.x - e2.x val diffY = e1.y - e2.y - val preferences = getPreferences(this) - - val doubleActions = preferences.getBoolean(PREF_DOUBLE_ACTIONS_ENABLED, false) - val edgeActions = preferences.getBoolean(PREF_EDGE_ACTIONS_ENABLED, false) + val doubleActions = LauncherPreferences.enabled_gestures().doubleSwipe() + val edgeActions = LauncherPreferences.enabled_gestures().edgeSwipe() val edgeStrictness = 0.15 var gesture = if(abs(diffX) > abs(diffY)) { // horizontal swipe @@ -250,9 +222,8 @@ class HomeActivity: UIObject, AppCompatActivity(), override fun setOnClicks() { - val preferences = getPreferences(this) binding.homeUpperView.setOnClickListener { - if(preferences.getBoolean(PREF_DATE_TIME_FLIP, false)) { + if(LauncherPreferences.clock().flipDateTime()) { Gesture.TIME(this) } else { Gesture.DATE(this) @@ -260,7 +231,7 @@ class HomeActivity: UIObject, AppCompatActivity(), } binding.homeLowerView.setOnClickListener { - if(preferences.getBoolean(PREF_DATE_TIME_FLIP, false)) { + if(LauncherPreferences.clock().flipDateTime()) { Gesture.DATE(this) } else { Gesture.TIME(this) diff --git a/app/src/main/java/de/jrpie/android/launcher/LauncherPreferences$Config.java b/app/src/main/java/de/jrpie/android/launcher/LauncherPreferences$Config.java new file mode 100644 index 0000000..9b5a047 --- /dev/null +++ b/app/src/main/java/de/jrpie/android/launcher/LauncherPreferences$Config.java @@ -0,0 +1,48 @@ +package de.jrpie.android.launcher; + +import eu.jonahbauer.android.preference.annotations.Preference; +import eu.jonahbauer.android.preference.annotations.PreferenceGroup; +import eu.jonahbauer.android.preference.annotations.Preferences; + +@Preferences( + name = "de.jrpie.android.launcher.LauncherPreferences", + makeFile = true, + r = R.class, + value = { + @PreferenceGroup(name = "internal", prefix = "settings_internal_", suffix = "_key", value = { + @Preference(name = "started", type = boolean.class, defaultValue = "false"), + @Preference(name = "started_time", type = long.class), + @Preference(name = "version_code", type = int.class, defaultValue = "-1"), + }), + @PreferenceGroup( name = "gestures", prefix = "settings_gesture_", suffix = "_key", value = { + }), + @PreferenceGroup(name = "general", prefix = "settings_general_", suffix = "_key", value = { + @Preference(name = "choose_home_screen", type = void.class) + }), + @PreferenceGroup(name = "theme", prefix = "settings_theme_", suffix = "_key", value = { + @Preference(name = "wallpaper", type = void.class), + @Preference(name = "theme", type = String.class), // TODO: change to enum + @Preference(name = "dominant", type = int.class), + @Preference(name = "vibrant", type = int.class), + }), + @PreferenceGroup(name = "clock", prefix = "settings_clock_", suffix = "_key", value = { + @Preference(name = "date_visible", type = boolean.class, defaultValue = "true"), + @Preference(name = "time_visible", type = boolean.class, defaultValue = "true"), + @Preference(name = "flip_date_time", type = boolean.class, defaultValue = "false"), + @Preference(name = "localized", type = boolean.class, defaultValue = "false"), + }), + @PreferenceGroup(name = "display", prefix = "settings_display_", suffix = "_key", value = { + @Preference(name = "screen_timeout_disabled", type = boolean.class, defaultValue = "false"), + @Preference(name = "full_screen", type = boolean.class, defaultValue = "true"), + }), + @PreferenceGroup( name = "functionality", prefix = "settings_functionality_", suffix = "_key", value = { + @Preference(name = "search_auto_launch", type = boolean.class, defaultValue = "true"), + @Preference(name = "search_auto_open_keyboard", type = boolean.class, defaultValue = "true"), + }), + @PreferenceGroup(name = "enabled_gestures", prefix = "settings_enabled_gestures_", suffix = "_key", value = { + @Preference(name = "double_swipe", type = boolean.class, defaultValue = "true"), + @Preference(name = "edge_swipe", type = boolean.class, defaultValue = "true"), + }), + }) +public final class LauncherPreferences$Config { +} diff --git a/app/src/main/java/de/jrpie/android/launcher/Preferences.kt b/app/src/main/java/de/jrpie/android/launcher/Preferences.kt index 7882a6f..8aa73ae 100644 --- a/app/src/main/java/de/jrpie/android/launcher/Preferences.kt +++ b/app/src/main/java/de/jrpie/android/launcher/Preferences.kt @@ -1,2 +1,72 @@ package de.jrpie.android.launcher +import android.app.Activity +import android.content.Context +import android.content.Intent +import de.jrpie.android.launcher.BuildConfig.VERSION_CODE +import de.jrpie.android.launcher.tutorial.TutorialActivity + + +fun migrateToNewVersion(activity: Activity) { + when (LauncherPreferences.internal().versionCode()) { + // Check versions, make sure transitions between versions go well + + VERSION_CODE -> { /* the version installed and used previously are the same */ } + 21,22,23 -> { + // TODO + } else -> { /* The version used before was pre- v1.3.0, + as version tracking started then */ + + /* + * before, the dominant and vibrant color of the `finn` and `dark` theme + * were not stored anywhere. Now they have to be stored: + * -> we just reset them using newly implemented functions + */ + when (LauncherPreferences.theme().theme()) { + "finn" -> resetToDefaultTheme(activity) + "dark" -> resetToDarkTheme(activity) + } + LauncherPreferences.internal().versionCode(VERSION_CODE) + // show the new tutorial + activity.startActivity(Intent(activity, TutorialActivity::class.java)) + } + } +} + +fun resetSettings(context: Context) { + + val editor = LauncherPreferences.getSharedPreferences().edit() + Gesture.values().forEach { editor.putString(it.id, it.pickDefaultApp(context)) } + editor.apply() + + // set default theme + val dominantColor = context.resources.getColor(R.color.finnmglasTheme_background_color) + val vibrantColor = context.resources.getColor(R.color.finnmglasTheme_accent_color) + + + LauncherPreferences.theme().dominant(dominantColor) + LauncherPreferences.theme().vibrant(vibrantColor) +} + +fun resetToDefaultTheme(activity: Activity) { + val dominantColor = activity.resources.getColor(R.color.finnmglasTheme_background_color) + val vibrantColor = activity.resources.getColor(R.color.finnmglasTheme_accent_color) + + LauncherPreferences.theme().dominant(dominantColor) + LauncherPreferences.theme().vibrant(vibrantColor) + + activity.recreate() +} + +fun resetToDarkTheme(activity: Activity) { + val dominantColor = activity.resources.getColor(R.color.darkTheme_background_color) + val vibrantColor = activity.resources.getColor(R.color.darkTheme_accent_color) + + LauncherPreferences.theme().dominant(dominantColor) + LauncherPreferences.theme().vibrant(vibrantColor) + + activity.recreate() +} + + + diff --git a/app/src/main/java/de/jrpie/android/launcher/list/ListActivity.kt b/app/src/main/java/de/jrpie/android/launcher/list/ListActivity.kt index e7916a7..946fa1a 100644 --- a/app/src/main/java/de/jrpie/android/launcher/list/ListActivity.kt +++ b/app/src/main/java/de/jrpie/android/launcher/list/ListActivity.kt @@ -14,16 +14,14 @@ import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentPagerAdapter import androidx.viewpager.widget.ViewPager import com.google.android.material.tabs.TabLayout -import de.jrpie.android.launcher.PREF_SCREEN_FULLSCREEN +import de.jrpie.android.launcher.LauncherPreferences import de.jrpie.android.launcher.R import de.jrpie.android.launcher.REQUEST_UNINSTALL import de.jrpie.android.launcher.UIObject import de.jrpie.android.launcher.databinding.ListBinding -import de.jrpie.android.launcher.getPreferences import de.jrpie.android.launcher.list.apps.ListFragmentApps import de.jrpie.android.launcher.list.other.LauncherAction import de.jrpie.android.launcher.list.other.ListFragmentOther -import de.jrpie.android.launcher.vibrantColor // TODO: Better solution for this intercommunication functionality (used in list-fragments) @@ -67,7 +65,7 @@ class ListActivity : AppCompatActivity(), UIObject { binding.listContainer.context.resources.displayMetrics.heightPixels val diff = height - r.bottom if (diff != 0 && - getPreferences(this).getBoolean(PREF_SCREEN_FULLSCREEN, true)) { + LauncherPreferences.display().fullScreen()) { if (binding.listContainer.paddingBottom !== diff) { binding.listContainer.setPadding(0, 0, 0, diff) } @@ -107,7 +105,7 @@ class ListActivity : AppCompatActivity(), UIObject { override fun applyTheme() { // list_close.setTextColor(vibrantColor) - binding.listTabs.setSelectedTabIndicatorColor(vibrantColor) + binding.listTabs.setSelectedTabIndicatorColor(LauncherPreferences.theme().vibrant()) } override fun setOnClicks() { diff --git a/app/src/main/java/de/jrpie/android/launcher/list/apps/AppsRecyclerAdapter.kt b/app/src/main/java/de/jrpie/android/launcher/list/apps/AppsRecyclerAdapter.kt index 6273782..155d7e2 100644 --- a/app/src/main/java/de/jrpie/android/launcher/list/apps/AppsRecyclerAdapter.kt +++ b/app/src/main/java/de/jrpie/android/launcher/list/apps/AppsRecyclerAdapter.kt @@ -12,12 +12,10 @@ import android.widget.ImageView import android.widget.PopupMenu import android.widget.TextView import androidx.recyclerview.widget.RecyclerView -import de.jrpie.android.launcher.PREF_SEARCH_AUTO_LAUNCH +import de.jrpie.android.launcher.LauncherPreferences import de.jrpie.android.launcher.R import de.jrpie.android.launcher.REQUEST_CHOOSE_APP import de.jrpie.android.launcher.appsList -import de.jrpie.android.launcher.getPreferences -import de.jrpie.android.launcher.getSavedTheme import de.jrpie.android.launcher.launch import de.jrpie.android.launcher.launchApp import de.jrpie.android.launcher.list.ListActivity @@ -68,7 +66,7 @@ class AppsRecyclerAdapter(val activity: Activity, viewHolder.textView.text = appLabel viewHolder.img.setImageDrawable(appIcon) - if (getSavedTheme(activity) == "dark") transformGrayscale( + if (LauncherPreferences.theme().theme() == "dark") transformGrayscale( viewHolder.img ) @@ -188,11 +186,8 @@ class AppsRecyclerAdapter(val activity: Activity, appsListDisplayed.addAll(appsSecondary) } - // Launch apps automatically if only one result is found and the user wants it - // Disabled at the moment. The Setting 'PREF_SEARCH_AUTO_LAUNCH' may be - // modifiable at some later point. if (appsListDisplayed.size == 1 && intention == ListActivity.ListActivityIntention.VIEW - && getPreferences(activity).getBoolean(PREF_SEARCH_AUTO_LAUNCH, false)) { + && LauncherPreferences.functionality().searchAutoLaunch()) { val info = appsListDisplayed[0] launch(info.packageName.toString(), info.user, activity) diff --git a/app/src/main/java/de/jrpie/android/launcher/list/apps/ListFragmentApps.kt b/app/src/main/java/de/jrpie/android/launcher/list/apps/ListFragmentApps.kt index 86b2ff1..70f303d 100644 --- a/app/src/main/java/de/jrpie/android/launcher/list/apps/ListFragmentApps.kt +++ b/app/src/main/java/de/jrpie/android/launcher/list/apps/ListFragmentApps.kt @@ -6,10 +6,9 @@ import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager -import de.jrpie.android.launcher.PREF_SEARCH_AUTO_KEYBOARD +import de.jrpie.android.launcher.LauncherPreferences import de.jrpie.android.launcher.UIObject import de.jrpie.android.launcher.databinding.ListAppsBinding -import de.jrpie.android.launcher.getPreferences import de.jrpie.android.launcher.list.ListActivity import de.jrpie.android.launcher.list.forGesture import de.jrpie.android.launcher.list.intention @@ -70,8 +69,7 @@ class ListFragmentApps : Fragment(), UIObject { }) if (intention == ListActivity.ListActivityIntention.VIEW - && getPreferences(requireContext()) - .getBoolean(PREF_SEARCH_AUTO_KEYBOARD, true)) { + && LauncherPreferences.functionality().searchAutoOpenKeyboard()){ openSoftKeyboard(requireContext(), binding.listAppsSearchview) } } diff --git a/app/src/main/java/de/jrpie/android/launcher/settings/SettingsActivity.kt b/app/src/main/java/de/jrpie/android/launcher/settings/SettingsActivity.kt index 18b1bd5..f543242 100644 --- a/app/src/main/java/de/jrpie/android/launcher/settings/SettingsActivity.kt +++ b/app/src/main/java/de/jrpie/android/launcher/settings/SettingsActivity.kt @@ -51,7 +51,7 @@ class SettingsActivity: AppCompatActivity(), UIObject { override fun applyTheme() { //settings_system.setTextColor(vibrantColor) //settings_close.setTextColor(vibrantColor) - binding.settingsTabs.setSelectedTabIndicatorColor(vibrantColor) + binding.settingsTabs.setSelectedTabIndicatorColor(LauncherPreferences.theme().vibrant()) } override fun setOnClicks(){ diff --git a/app/src/main/java/de/jrpie/android/launcher/settings/actions/SettingsFragmentActions.kt b/app/src/main/java/de/jrpie/android/launcher/settings/actions/SettingsFragmentActions.kt index b7e2d24..61346ea 100644 --- a/app/src/main/java/de/jrpie/android/launcher/settings/actions/SettingsFragmentActions.kt +++ b/app/src/main/java/de/jrpie/android/launcher/settings/actions/SettingsFragmentActions.kt @@ -8,12 +8,12 @@ import android.view.View import android.view.ViewGroup import android.widget.Toast import androidx.fragment.app.Fragment +import de.jrpie.android.launcher.LauncherPreferences import de.jrpie.android.launcher.R import de.jrpie.android.launcher.UIObject import de.jrpie.android.launcher.databinding.SettingsActionsBinding import de.jrpie.android.launcher.list.ListActivity import de.jrpie.android.launcher.setButtonColor -import de.jrpie.android.launcher.vibrantColor /** @@ -44,6 +44,7 @@ SettingsFragmentActions : Fragment(), UIObject { } override fun applyTheme() { + val vibrantColor = LauncherPreferences.theme().vibrant() setButtonColor(binding!!.settingsActionsButtonViewApps, vibrantColor) setButtonColor(binding!!.settingsActionsButtonInstallApps, vibrantColor) } @@ -67,7 +68,4 @@ SettingsFragmentActions : Fragment(), UIObject { } } - override fun onDestroy() { - super.onDestroy() - } } diff --git a/app/src/main/java/de/jrpie/android/launcher/settings/actions/SettingsFragmentActionsRecycler.kt b/app/src/main/java/de/jrpie/android/launcher/settings/actions/SettingsFragmentActionsRecycler.kt index 9f6c216..9ff4f5a 100644 --- a/app/src/main/java/de/jrpie/android/launcher/settings/actions/SettingsFragmentActionsRecycler.kt +++ b/app/src/main/java/de/jrpie/android/launcher/settings/actions/SettingsFragmentActionsRecycler.kt @@ -56,13 +56,13 @@ class SettingsFragmentActionsRecycler : Fragment(), UIObject { layoutManager = actionViewManager adapter = actionViewAdapter } - getPreferences(requireContext()).registerOnSharedPreferenceChangeListener(sharedPreferencesListener) + LauncherPreferences.getSharedPreferences().registerOnSharedPreferenceChangeListener(sharedPreferencesListener) super.onStart() } override fun onDestroy() { - getPreferences(requireContext()).unregisterOnSharedPreferenceChangeListener(sharedPreferencesListener) + LauncherPreferences.getSharedPreferences().unregisterOnSharedPreferenceChangeListener(sharedPreferencesListener) super.onDestroy() } @@ -116,9 +116,10 @@ class ActionsRecyclerAdapter(val activity: Activity): override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) { val gesture = gesturesList[i] + val vibrantColor = LauncherPreferences.theme().vibrant() viewHolder.textView.text = gesture.getLabel(activity) setButtonColor(viewHolder.chooseButton, vibrantColor) - if (getSavedTheme(activity) == "dark") transformGrayscale( + if (LauncherPreferences.theme().theme() == "dark") transformGrayscale( viewHolder.img ) updateViewHolder(gesture, viewHolder) @@ -136,16 +137,16 @@ class ActionsRecyclerAdapter(val activity: Activity): } init { - val doubleActions = getPreferences(activity).getBoolean(PREF_DOUBLE_ACTIONS_ENABLED, false) - val edgeActions = getPreferences(activity).getBoolean(PREF_EDGE_ACTIONS_ENABLED, false) + val doubleActions = LauncherPreferences.enabled_gestures().doubleSwipe() + val edgeActions = LauncherPreferences.enabled_gestures().edgeSwipe() gesturesList = Gesture.values().filter { (doubleActions || !it.isDoubleVariant()) && (edgeActions || !it.isEdgeVariant())} as ArrayList } fun updateActions() { - val doubleActions = getPreferences(activity).getBoolean(PREF_DOUBLE_ACTIONS_ENABLED, false) - val edgeActions = getPreferences(activity).getBoolean(PREF_EDGE_ACTIONS_ENABLED, false) + val doubleActions = LauncherPreferences.enabled_gestures().doubleSwipe() + val edgeActions = LauncherPreferences.enabled_gestures().edgeSwipe() this.gesturesList.clear() gesturesList.addAll(Gesture.values().filter { (doubleActions || !it.isDoubleVariant()) diff --git a/app/src/main/java/de/jrpie/android/launcher/settings/launcher/SettingsFragmentLauncher.kt b/app/src/main/java/de/jrpie/android/launcher/settings/launcher/SettingsFragmentLauncher.kt index 39ec80b..2d4b29b 100644 --- a/app/src/main/java/de/jrpie/android/launcher/settings/launcher/SettingsFragmentLauncher.kt +++ b/app/src/main/java/de/jrpie/android/launcher/settings/launcher/SettingsFragmentLauncher.kt @@ -1,36 +1,13 @@ package de.jrpie.android.launcher.settings.launcher +import android.content.Context import android.content.Intent import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.AdapterView -import android.widget.ArrayAdapter -import android.widget.Switch -import androidx.fragment.app.Fragment -import de.jrpie.android.launcher.PREF_DATE_LOCALIZED -import de.jrpie.android.launcher.PREF_DATE_TIME_FLIP -import de.jrpie.android.launcher.PREF_DATE_VISIBLE -import de.jrpie.android.launcher.PREF_DOUBLE_ACTIONS_ENABLED -import de.jrpie.android.launcher.PREF_EDGE_ACTIONS_ENABLED -import de.jrpie.android.launcher.PREF_SCREEN_FULLSCREEN -import de.jrpie.android.launcher.PREF_SCREEN_TIMEOUT_DISABLED -import de.jrpie.android.launcher.PREF_SEARCH_AUTO_KEYBOARD -import de.jrpie.android.launcher.PREF_SEARCH_AUTO_LAUNCH -import de.jrpie.android.launcher.PREF_TIME_VISIBLE +import androidx.preference.PreferenceFragmentCompat +import de.jrpie.android.launcher.LauncherPreferences import de.jrpie.android.launcher.R -import de.jrpie.android.launcher.UIObject -import de.jrpie.android.launcher.getPreferences -import de.jrpie.android.launcher.getSavedTheme -import de.jrpie.android.launcher.resetToDarkTheme -import de.jrpie.android.launcher.resetToDefaultTheme -import de.jrpie.android.launcher.setButtonColor -import de.jrpie.android.launcher.setSwitchColor -import de.jrpie.android.launcher.setWindowFlags -import de.jrpie.android.launcher.vibrantColor -import de.jrpie.android.launcher.databinding.SettingsLauncherBinding import de.jrpie.android.launcher.setDefaultHomeScreen +import eu.jonahbauer.android.preference.annotations.Preference /** @@ -38,111 +15,27 @@ import de.jrpie.android.launcher.setDefaultHomeScreen * * It is used to change themes, select wallpapers ... theme related stuff */ -class SettingsFragmentLauncher : Fragment(), UIObject { +class SettingsFragmentLauncher : PreferenceFragmentCompat() { - private lateinit var binding: SettingsLauncherBinding - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - binding = SettingsLauncherBinding.inflate(inflater, container, false) - return binding.root - } + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + setPreferencesFromResource(R.xml.preferences, rootKey) + preferenceManager.sharedPreferencesName = getString(R.string.preference_file_key) + preferenceManager.sharedPreferencesMode = Context.MODE_PRIVATE - override fun onStart(){ - super.onStart() - super.onStart() - } - - - override fun applyTheme() { - - setButtonColor(binding.settingsLauncherButtonChooseHomeScreen, vibrantColor) - setSwitchColor(binding.settingsLauncherSwitchScreenTimeout, vibrantColor) - setSwitchColor(binding.settingsLauncherSwitchScreenFull, vibrantColor) - setSwitchColor(binding.settingsLauncherSwitchAutoLaunch, vibrantColor) - setSwitchColor(binding.settingsLauncherSwitchAutoKeyboard, vibrantColor) - setSwitchColor(binding.settingsLauncherSwitchEnableDouble, vibrantColor) - setSwitchColor(binding.settingsLauncherSwitchEnableEdge, vibrantColor) - - setSwitchColor(binding.settingsLauncherSwitchDateLocalized, vibrantColor) - setSwitchColor(binding.settingsLauncherSwitchDateVisible, vibrantColor) - setSwitchColor(binding.settingsLauncherSwitchTimeVisible, vibrantColor) - setSwitchColor(binding.settingsLauncherSwitchDateTimeFlip, vibrantColor) - - setButtonColor(binding.settingsLauncherButtonChooseWallpaper, vibrantColor) - } - - override fun setOnClicks() { - - val preferences = getPreferences(requireActivity()) - - fun bindSwitchToPref(switch: Switch, pref: String, default: Boolean, onChange: (Boolean) -> Unit){ - switch.isChecked = preferences.getBoolean(pref, default) - switch.setOnCheckedChangeListener { _, isChecked -> // Toggle double actions - preferences.edit() - .putBoolean(pref, isChecked) - .apply() - onChange(isChecked) - } - } - - binding.settingsLauncherButtonChooseHomeScreen.setOnClickListener { - setDefaultHomeScreen(requireContext(), checkDefault = false) - } - - binding.settingsLauncherButtonChooseWallpaper.setOnClickListener { + val selectWallpaper = findPreference(LauncherPreferences.theme().keys().wallpaper()) + selectWallpaper?.setOnPreferenceClickListener { // https://github.com/LineageOS/android_packages_apps_Trebuchet/blob/6caab89b21b2b91f0a439e1fd8c4510dcb255819/src/com/android/launcher3/views/OptionsPopupView.java#L271 val intent = Intent(Intent.ACTION_SET_WALLPAPER) //.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) .putExtra("com.android.wallpaper.LAUNCH_SOURCE", "app_launched_launcher") .putExtra("com.android.launcher3.WALLPAPER_FLAVOR", "focus_wallpaper") startActivity(intent) + true } - - bindSwitchToPref(binding.settingsLauncherSwitchDateLocalized, PREF_DATE_LOCALIZED, false) { } - bindSwitchToPref(binding.settingsLauncherSwitchDateVisible, PREF_DATE_VISIBLE, true) {} - bindSwitchToPref(binding.settingsLauncherSwitchTimeVisible, PREF_TIME_VISIBLE, true) {} - bindSwitchToPref(binding.settingsLauncherSwitchDateTimeFlip, PREF_DATE_TIME_FLIP, false) {} - - bindSwitchToPref(binding.settingsLauncherSwitchScreenTimeout, PREF_SCREEN_TIMEOUT_DISABLED, false) { - activity?.let{setWindowFlags(it.window)} - } - bindSwitchToPref(binding.settingsLauncherSwitchScreenFull, PREF_SCREEN_FULLSCREEN, true) { - activity?.let{setWindowFlags(it.window)} - } - bindSwitchToPref(binding.settingsLauncherSwitchAutoLaunch, PREF_SEARCH_AUTO_LAUNCH, false) {} - bindSwitchToPref(binding.settingsLauncherSwitchAutoKeyboard, PREF_SEARCH_AUTO_KEYBOARD, true) {} - bindSwitchToPref(binding.settingsLauncherSwitchEnableDouble, PREF_DOUBLE_ACTIONS_ENABLED, false) {} - bindSwitchToPref(binding.settingsLauncherSwitchEnableEdge, PREF_EDGE_ACTIONS_ENABLED, false) {} - } - - override fun adjustLayout() { - - // Load values into the theme spinner - val staticThemeAdapter = ArrayAdapter.createFromResource( - requireActivity(), R.array.settings_launcher_theme_spinner_items, - android.R.layout.simple_spinner_item ) - - staticThemeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) - binding.settingsLauncherThemeSpinner.adapter = staticThemeAdapter - - val themeInt = when (getSavedTheme(requireActivity())) { - "finn" -> 0 - "dark" -> 1 - else -> 0 - } - - binding.settingsLauncherThemeSpinner.setSelection(themeInt) - - binding.settingsLauncherThemeSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { - override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { - when (position) { - 0 -> if (getSavedTheme(activity!!) != "finn") resetToDefaultTheme(activity!!) - 1 -> if (getSavedTheme(activity!!) != "dark") resetToDarkTheme(activity!!) - } - } - override fun onNothingSelected(parent: AdapterView<*>?) { } + val chooseHomeScreen = findPreference(LauncherPreferences.general().keys().chooseHomeScreen()) + chooseHomeScreen?.setOnPreferenceClickListener { + setDefaultHomeScreen(requireContext(), checkDefault = false) + true } } } diff --git a/app/src/main/java/de/jrpie/android/launcher/settings/meta/SettingsFragmentMeta.kt b/app/src/main/java/de/jrpie/android/launcher/settings/meta/SettingsFragmentMeta.kt index 28f321f..0c9e27b 100644 --- a/app/src/main/java/de/jrpie/android/launcher/settings/meta/SettingsFragmentMeta.kt +++ b/app/src/main/java/de/jrpie/android/launcher/settings/meta/SettingsFragmentMeta.kt @@ -8,13 +8,13 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment +import de.jrpie.android.launcher.LauncherPreferences import de.jrpie.android.launcher.R import de.jrpie.android.launcher.UIObject import de.jrpie.android.launcher.openNewTabWindow import de.jrpie.android.launcher.resetSettings import de.jrpie.android.launcher.setButtonColor import de.jrpie.android.launcher.tutorial.TutorialActivity -import de.jrpie.android.launcher.vibrantColor import de.jrpie.android.launcher.databinding.SettingsMetaBinding /** @@ -57,6 +57,7 @@ class SettingsFragmentMeta : Fragment(), UIObject { } override fun applyTheme() { + val vibrantColor = LauncherPreferences.theme().vibrant() setButtonColor(binding.settingsMetaButtonViewTutorial, vibrantColor) setButtonColor(binding.settingsMetaButtonResetSettings, vibrantColor) setButtonColor(binding.settingsMetaButtonReportBug, vibrantColor) diff --git a/app/src/main/java/de/jrpie/android/launcher/tutorial/TutorialActivity.kt b/app/src/main/java/de/jrpie/android/launcher/tutorial/TutorialActivity.kt index 5ec137d..75cf85b 100644 --- a/app/src/main/java/de/jrpie/android/launcher/tutorial/TutorialActivity.kt +++ b/app/src/main/java/de/jrpie/android/launcher/tutorial/TutorialActivity.kt @@ -8,12 +8,10 @@ import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentPagerAdapter import androidx.viewpager.widget.ViewPager import com.google.android.material.tabs.TabLayout -import de.jrpie.android.launcher.PREF_STARTED +import de.jrpie.android.launcher.LauncherPreferences import de.jrpie.android.launcher.R import de.jrpie.android.launcher.REQUEST_CHOOSE_APP import de.jrpie.android.launcher.UIObject -import de.jrpie.android.launcher.getPreferences -import de.jrpie.android.launcher.loadSettings import de.jrpie.android.launcher.resetSettings import de.jrpie.android.launcher.saveListActivityChoice import de.jrpie.android.launcher.tutorial.tabs.TutorialFragmentConcept @@ -37,13 +35,10 @@ class TutorialActivity: AppCompatActivity(), UIObject { // Initialise layout setContentView(R.layout.tutorial) - val preferences = getPreferences(this) // Check if the app was started before - if (!preferences.getBoolean(PREF_STARTED, false)) + if(!LauncherPreferences.internal().started()) resetSettings(this) - loadSettings(this) - // set up tabs and swiping in settings val sectionsPagerAdapter = TutorialSectionsPagerAdapter(supportFragmentManager) val viewPager: ViewPager = findViewById(R.id.tutorial_viewpager) @@ -66,7 +61,7 @@ class TutorialActivity: AppCompatActivity(), UIObject { // Default: prevent going back, allow if viewed again later override fun onBackPressed() { - if (getPreferences(this).getBoolean(PREF_STARTED, false)) + if (LauncherPreferences.internal().started()) super.onBackPressed() } diff --git a/app/src/main/java/de/jrpie/android/launcher/tutorial/tabs/TutorialFragmentFinish.kt b/app/src/main/java/de/jrpie/android/launcher/tutorial/tabs/TutorialFragmentFinish.kt index ef9dd02..72edb1f 100644 --- a/app/src/main/java/de/jrpie/android/launcher/tutorial/tabs/TutorialFragmentFinish.kt +++ b/app/src/main/java/de/jrpie/android/launcher/tutorial/tabs/TutorialFragmentFinish.kt @@ -6,7 +6,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import de.jrpie.android.launcher.* -import de.jrpie.android.launcher.BuildConfig.VERSION_NAME +import de.jrpie.android.launcher.BuildConfig.VERSION_CODE import de.jrpie.android.launcher.databinding.TutorialFinishBinding /** @@ -32,6 +32,7 @@ class TutorialFragmentFinish : Fragment(), UIObject { } override fun applyTheme() { + val vibrantColor = LauncherPreferences.theme().vibrant() setButtonColor(binding.tutorialFinishButtonStart, vibrantColor) binding.tutorialFinishButtonStart.blink() } @@ -42,20 +43,12 @@ class TutorialFragmentFinish : Fragment(), UIObject { } private fun finishTutorial() { - context?.let { getPreferences(it) }?.let { - if (!it.getBoolean(PREF_STARTED, false)) { - it.edit() - .putBoolean(PREF_STARTED, true) // never auto run this again - .putLong( - PREF_STARTED_TIME, - System.currentTimeMillis() / 1000L - ) // record first startup timestamp - .putString(PREF_VERSION, VERSION_NAME) // save current launcher version - .apply() - } + if(!LauncherPreferences.internal().started()) { + LauncherPreferences.internal().started(true) + LauncherPreferences.internal().startedTime(System.currentTimeMillis() / 1000L) + LauncherPreferences.internal().versionCode(VERSION_CODE) } context?.let { setDefaultHomeScreen(it, checkDefault = true) } - activity?.finish() } } diff --git a/app/src/main/java/de/jrpie/android/launcher/tutorial/tabs/TutorialFragmentStart.kt b/app/src/main/java/de/jrpie/android/launcher/tutorial/tabs/TutorialFragmentStart.kt index 0fdd08e..cd21970 100644 --- a/app/src/main/java/de/jrpie/android/launcher/tutorial/tabs/TutorialFragmentStart.kt +++ b/app/src/main/java/de/jrpie/android/launcher/tutorial/tabs/TutorialFragmentStart.kt @@ -30,6 +30,7 @@ class TutorialFragmentStart : Fragment(), UIObject { } override fun applyTheme() { + val vibrantColor = LauncherPreferences.theme().vibrant() binding.tutorialStartIconRight.setTextColor(vibrantColor) binding.tutorialStartIconRight.blink() diff --git a/app/src/main/res/layout/settings_launcher.xml b/app/src/main/res/layout/settings_launcher.xml deleted file mode 100644 index 5d085cf..0000000 --- a/app/src/main/res/layout/settings_launcher.xml +++ /dev/null @@ -1,402 +0,0 @@ - - - - - - -