From 830c45cb63167d3c2ca2bd5412af9c95372155e8 Mon Sep 17 00:00:00 2001 From: ottoptj Date: Sat, 10 Aug 2024 18:54:06 +0300 Subject: [PATCH] Unified rest of preference management into SharedPreferenceManager --- .../eu/ottop/yamlauncher/AppActionMenu.kt | 2 - .../eu/ottop/yamlauncher/AppMenuAdapter.kt | 14 +-- .../java/eu/ottop/yamlauncher/AppUtils.kt | 8 +- .../eu/ottop/yamlauncher/BatteryReceiver.kt | 2 - .../ottop/yamlauncher/GestureAppsAdapter.kt | 6 +- .../ottop/yamlauncher/GestureAppsFragment.kt | 18 +-- .../eu/ottop/yamlauncher/HiddenAppsAdapter.kt | 7 +- .../ottop/yamlauncher/HiddenAppsFragment.kt | 17 +-- .../eu/ottop/yamlauncher/LocationFragment.kt | 10 +- .../ottop/yamlauncher/LocationListAdapter.kt | 6 +- .../java/eu/ottop/yamlauncher/MainActivity.kt | 94 +++++---------- .../yamlauncher/SharedPreferenceManager.kt | 109 +++++++++++++----- .../main/java/eu/ottop/yamlauncher/UIUtils.kt | 55 +++++---- .../eu/ottop/yamlauncher/WeatherSystem.kt | 3 +- 14 files changed, 182 insertions(+), 169 deletions(-) diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt index 6f86f50..63bf259 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt @@ -18,8 +18,6 @@ import android.widget.TextView import androidx.appcompat.widget.AppCompatButton import androidx.lifecycle.lifecycleScope import eu.ottop.yamlauncher.databinding.ActivityMainBinding -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay import kotlinx.coroutines.launch class AppActionMenu { diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt index 332303c..b9deccb 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt @@ -7,7 +7,6 @@ import android.content.pm.LauncherActivityInfo import android.content.pm.LauncherApps import android.graphics.BlendMode import android.graphics.BlendModeColorFilter -import android.graphics.Color import android.os.UserHandle import android.view.LayoutInflater import android.view.View @@ -16,7 +15,6 @@ import android.widget.FrameLayout import android.widget.LinearLayout import android.widget.TextView import androidx.core.content.res.ResourcesCompat -import androidx.preference.PreferenceManager import androidx.recyclerview.widget.RecyclerView import com.google.android.material.textfield.TextInputEditText @@ -35,9 +33,8 @@ class AppMenuAdapter( var shortcutTextView: TextView? = null private val sharedPreferenceManager = SharedPreferenceManager(context) - private var preferences = PreferenceManager.getDefaultSharedPreferences(context) private val uiUtils = UIUtils(context) - private val appUtils = AppUtils(context) + private val appUtils = AppUtils(context, launcherApps) interface OnItemClickListener { fun onItemClick(appInfo: LauncherActivityInfo, userHandle: UserHandle) @@ -115,23 +112,22 @@ class AppMenuAdapter( if (app.second.second != 0) { holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_work_app, null),null, ResourcesCompat.getDrawable(context.resources, R.drawable.ic_empty, null),null) holder.textView.compoundDrawables[0].colorFilter = - BlendModeColorFilter(Color.parseColor(preferences?.getString("textColor", "#FFF3F3F3")), BlendMode.SRC_ATOP) + BlendModeColorFilter(sharedPreferenceManager.getTextColor(), BlendMode.SRC_ATOP) } else { holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_empty, null),null,ResourcesCompat.getDrawable(context.resources, R.drawable.ic_empty, null),null) } - uiUtils.setAppAlignment(context, preferences, holder.textView, holder.editText) + uiUtils.setAppAlignment(holder.textView, holder.editText) - uiUtils.setAppSize(preferences, holder.textView, holder.editText) + uiUtils.setAppSize(holder.textView, holder.editText) val appInfo = appUtils.getAppInfo( - launcherApps, app.first.applicationInfo.packageName, app.second.second ) - holder.textView.setTextColor(Color.parseColor(preferences?.getString("textColor", "#FFF3F3F3"))) + holder.textView.setTextColor(sharedPreferenceManager.getTextColor()) val appLabel: CharSequence = appInfo?.loadLabel(context.packageManager) ?: "Removing..." if (appInfo != null) { diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppUtils.kt b/app/src/main/java/eu/ottop/yamlauncher/AppUtils.kt index bf113f8..f511495 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppUtils.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppUtils.kt @@ -10,11 +10,11 @@ import androidx.appcompat.app.AppCompatActivity import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext -class AppUtils(private val context: Context) { +class AppUtils(private val context: Context, private val launcherApps: LauncherApps) { private val sharedPreferenceManager = SharedPreferenceManager(context) - suspend fun getInstalledApps(launcherApps: LauncherApps): List>> { + suspend fun getInstalledApps(): List>> { val allApps = mutableListOf>>() var sortedApps = listOf>>() withContext(Dispatchers.Default) { @@ -44,7 +44,6 @@ class AppUtils(private val context: Context) { fun getHiddenApps(): List>> { val allApps = mutableListOf>>() - val launcherApps = context.getSystemService(AppCompatActivity.LAUNCHER_APPS_SERVICE) as LauncherApps for (i in launcherApps.profiles.indices) { launcherApps.getActivityList(null, launcherApps.profiles[i]).forEach { app -> if (sharedPreferenceManager.isAppHidden(app.applicationInfo.packageName, i)) { @@ -62,7 +61,6 @@ class AppUtils(private val context: Context) { } fun getAppInfo( - launcherApps: LauncherApps, packageName: String, profile: Int ): ApplicationInfo? { @@ -73,7 +71,7 @@ class AppUtils(private val context: Context) { } } - fun launchApp(launcherApps: LauncherApps, appInfo: LauncherActivityInfo, userHandle: UserHandle) { + fun launchApp(appInfo: LauncherActivityInfo, userHandle: UserHandle) { val mainActivity = launcherApps.getActivityList(appInfo.applicationInfo.packageName, userHandle).firstOrNull() if (mainActivity != null) { launcherApps.startMainActivity(mainActivity.componentName, userHandle, null, null) diff --git a/app/src/main/java/eu/ottop/yamlauncher/BatteryReceiver.kt b/app/src/main/java/eu/ottop/yamlauncher/BatteryReceiver.kt index f1f68b2..da8c9bc 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/BatteryReceiver.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/BatteryReceiver.kt @@ -5,8 +5,6 @@ import android.content.Context import android.content.Intent import android.content.IntentFilter import android.os.BatteryManager -import androidx.lifecycle.lifecycleScope -import kotlinx.coroutines.launch class BatteryReceiver(private val activity: MainActivity) : BroadcastReceiver() { diff --git a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsAdapter.kt index 0002ab7..bcc3762 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsAdapter.kt @@ -10,7 +10,6 @@ import android.view.ViewGroup import android.widget.FrameLayout import android.widget.TextView import androidx.core.content.res.ResourcesCompat -import androidx.preference.PreferenceManager import androidx.recyclerview.widget.RecyclerView class GestureAppsAdapter( @@ -21,7 +20,6 @@ class GestureAppsAdapter( RecyclerView.Adapter() { private val sharedPreferenceManager = SharedPreferenceManager(context) - private var preferences = PreferenceManager.getDefaultSharedPreferences(context) private val uiUtils = UIUtils(context) interface OnItemClickListener { @@ -58,9 +56,9 @@ class GestureAppsAdapter( holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_empty, null),null,null,null) } - uiUtils.setAppAlignment(context, preferences, holder.textView) + uiUtils.setAppAlignment(holder.textView) - uiUtils.setAppSize(preferences, holder.textView) + uiUtils.setAppSize(holder.textView) val appInfo = app.first.activityInfo.applicationInfo holder.textView.text = sharedPreferenceManager.getAppName( diff --git a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt index 36d2123..9a2d2fe 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt @@ -2,7 +2,6 @@ package eu.ottop.yamlauncher import android.app.AlertDialog import android.content.Context -import android.content.SharedPreferences import android.content.pm.LauncherActivityInfo import android.content.pm.LauncherApps import android.os.Bundle @@ -41,25 +40,26 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - appUtils = AppUtils(requireContext()) + launcherApps = requireContext().getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps + + appUtils = AppUtils(requireContext(), launcherApps) sharedPreferenceManager = SharedPreferenceManager(requireContext()) lifecycleScope.launch { withContext(Dispatchers.Default) { - launcherApps = requireContext().getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps + adapter = GestureAppsAdapter( requireContext(), - appUtils.getInstalledApps(launcherApps).toMutableList(), + appUtils.getInstalledApps().toMutableList(), this@GestureAppsFragment ) } val recyclerView = view.findViewById(R.id.gesture_app_recycler) val appMenuEdgeFactory = AppMenuEdgeFactory(requireActivity()) val uiUtils = UIUtils(requireContext()) - val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) recyclerView.edgeEffectFactory = appMenuEdgeFactory recyclerView.adapter = adapter @@ -68,9 +68,9 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener { val searchView = view.findViewById(R.id.gestureAppSearch) - uiUtils.setMenuTitleAlignment(preferences, view.findViewById(R.id.gesture_menutitle)) - uiUtils.setSearchAlignment(preferences, searchView) - uiUtils.setSearchSize(preferences, searchView) + uiUtils.setMenuTitleAlignment(view.findViewById(R.id.gesture_menutitle)) + uiUtils.setSearchAlignment(searchView) + uiUtils.setSearchSize(searchView) recyclerView.addOnLayoutChangeListener { _, _, top, _, bottom, _, oldTop, _, oldBottom -> @@ -107,7 +107,7 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener { val cleanQuery = stringUtils.cleanString(query) val newFilteredApps = mutableListOf>>() - val updatedApps = appUtils.getInstalledApps(launcherApps) + val updatedApps = appUtils.getInstalledApps() getFilteredApps(cleanQuery, newFilteredApps, updatedApps) diff --git a/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsAdapter.kt index 6508239..a01b0d2 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsAdapter.kt @@ -10,7 +10,6 @@ import android.view.ViewGroup import android.widget.FrameLayout import android.widget.TextView import androidx.core.content.res.ResourcesCompat -import androidx.preference.PreferenceManager import androidx.recyclerview.widget.RecyclerView class HiddenAppsAdapter( @@ -21,8 +20,6 @@ class HiddenAppsAdapter( RecyclerView.Adapter() { private val sharedPreferenceManager = SharedPreferenceManager(context) - private var preferences = PreferenceManager.getDefaultSharedPreferences(context) - private val uiUtils = UIUtils(context) interface OnItemClickListener { @@ -60,9 +57,9 @@ class HiddenAppsAdapter( holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_empty, null),null,null,null) } - uiUtils.setAppAlignment(context, preferences, holder.textView) + uiUtils.setAppAlignment(holder.textView) - uiUtils.setAppSize(preferences, holder.textView) + uiUtils.setAppSize(holder.textView) val appInfo = app.first.activityInfo.applicationInfo holder.textView.text = sharedPreferenceManager.getAppName( diff --git a/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsFragment.kt index beed689..be57010 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsFragment.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsFragment.kt @@ -3,6 +3,7 @@ package eu.ottop.yamlauncher import android.app.AlertDialog import android.content.Context import android.content.pm.LauncherActivityInfo +import android.content.pm.LauncherApps import android.os.Bundle import android.os.UserHandle import android.text.Editable @@ -17,11 +18,13 @@ import androidx.recyclerview.widget.RecyclerView import com.google.android.material.textfield.TextInputEditText class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener { - private lateinit var appUtils: AppUtils + private lateinit var sharedPreferenceManager: SharedPreferenceManager private var adapter: HiddenAppsAdapter? = null private var stringUtils = StringUtils() private lateinit var uiUtils: UIUtils + private lateinit var appUtils: AppUtils + private lateinit var launcherApps: LauncherApps override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -33,13 +36,13 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + launcherApps = requireContext().getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps uiUtils = UIUtils(requireContext()) - appUtils = AppUtils(requireContext()) + appUtils = AppUtils(requireContext(), launcherApps) sharedPreferenceManager = SharedPreferenceManager(requireContext()) val recyclerView = view.findViewById(R.id.hidden_app_recycler) val appMenuEdgeFactory = AppMenuEdgeFactory(requireActivity()) - val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) adapter = HiddenAppsAdapter(requireContext(), appUtils.getHiddenApps().toMutableList(), this) @@ -51,9 +54,9 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener { val searchView = view.findViewById(R.id.hiddenAppSearch) - uiUtils.setMenuTitleAlignment(preferences, view.findViewById(R.id.hidden_menutitle)) - uiUtils.setSearchAlignment(preferences, searchView) - uiUtils.setSearchSize(preferences, searchView) + uiUtils.setMenuTitleAlignment(view.findViewById(R.id.hidden_menutitle)) + uiUtils.setSearchAlignment(searchView) + uiUtils.setSearchSize(searchView) recyclerView.addOnLayoutChangeListener { _, _, top, _, bottom, _, oldTop, _, oldBottom -> @@ -78,7 +81,7 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener { } }) - if (PreferenceManager.getDefaultSharedPreferences(requireContext()).getBoolean("autoKeyboard", false)) { + if (sharedPreferenceManager.isAutoKeyboardEnabled()) { val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager searchView.requestFocus() diff --git a/app/src/main/java/eu/ottop/yamlauncher/LocationFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/LocationFragment.kt index 10be865..7bb98a4 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/LocationFragment.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/LocationFragment.kt @@ -43,8 +43,6 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener { var locationList = mutableListOf>() - val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) - stringUtils.setLink(requireActivity().findViewById(R.id.locationLink), getString(R.string.location_link)) lifecycleScope.launch(Dispatchers.IO) { @@ -56,9 +54,9 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener { adapter = LocationListAdapter(requireContext(), locationList, this) val recyclerView = view.findViewById(R.id.locationrecycler) val appMenuEdgeFactory = AppMenuEdgeFactory(requireActivity()) - uiUtils.setMenuTitleAlignment(preferences, view.findViewById(R.id.location_menutitle)) - uiUtils.setSearchAlignment(preferences, searchView) - uiUtils.setSearchSize(preferences, searchView) + uiUtils.setMenuTitleAlignment(view.findViewById(R.id.location_menutitle)) + uiUtils.setSearchAlignment(searchView) + uiUtils.setSearchSize(searchView) recyclerView.edgeEffectFactory = appMenuEdgeFactory recyclerView.adapter = adapter @@ -97,7 +95,7 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener { } }) - if (PreferenceManager.getDefaultSharedPreferences(requireContext()).getBoolean("autoKeyboard", false)) { + if (sharedPreferenceManager.isAutoKeyboardEnabled()) { val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager searchView.requestFocus() diff --git a/app/src/main/java/eu/ottop/yamlauncher/LocationListAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/LocationListAdapter.kt index f46bd1b..676b17d 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/LocationListAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/LocationListAdapter.kt @@ -7,7 +7,6 @@ import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.constraintlayout.widget.ConstraintLayout -import androidx.preference.PreferenceManager import androidx.recyclerview.widget.RecyclerView class LocationListAdapter( @@ -17,7 +16,6 @@ class LocationListAdapter( ) : RecyclerView.Adapter() { - private var preferences = PreferenceManager.getDefaultSharedPreferences(context) private val uiUtils = UIUtils(context) interface OnItemClickListener { @@ -51,9 +49,9 @@ class LocationListAdapter( override fun onBindViewHolder(holder: AppViewHolder, position: Int) { val app = apps[position] - uiUtils.setAppAlignment(context, preferences, holder.textView, null ,holder.regionText) + uiUtils.setAppAlignment(holder.textView, null, holder.regionText) - uiUtils.setAppSize(preferences, holder.textView, null, holder.regionText) + uiUtils.setAppSize(holder.textView, null, holder.regionText) holder.textView.text = app["name"] holder.regionText.text = context.getString(R.string.region_text, app["region"], app["country"]) diff --git a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt index 11374c4..5eece38 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt @@ -121,8 +121,10 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh } private fun setMainVariables() { + launcherApps = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps + weatherSystem = WeatherSystem(this@MainActivity) - appUtils = AppUtils(this@MainActivity) + appUtils = AppUtils(this@MainActivity, launcherApps) uiUtils = UIUtils(this@MainActivity) gestureUtils = GestureUtils(this@MainActivity) sharedPreferenceManager = SharedPreferenceManager(this@MainActivity) @@ -141,8 +143,6 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh searchView = findViewById(R.id.searchView) - launcherApps = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps - preferences = PreferenceManager.getDefaultSharedPreferences(this) } @@ -152,14 +152,14 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh uiUtils.setSearchColors(searchView) uiUtils.setClockAlignment(clock, dateText) - uiUtils.setSearchAlignment(preferences, searchView) + uiUtils.setSearchAlignment(searchView) - uiUtils.setClockSize(preferences, clock) - uiUtils.setDateSize(preferences, dateText) - uiUtils.setShortcutSize(preferences, binding.homeView) - uiUtils.setSearchSize(preferences, searchView) + uiUtils.setClockSize(clock) + uiUtils.setDateSize(dateText) + uiUtils.setShortcutSize(binding.homeView) + uiUtils.setSearchSize(searchView) - uiUtils.setStatusBar(window, preferences) + uiUtils.setStatusBar(window) leftSwipeActivity = gestureUtils.getSwipeInfo(launcherApps, "left") rightSwipeActivity = gestureUtils.getSwipeInfo(launcherApps, "right") @@ -172,7 +172,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh val textView = findViewById(shortcuts[i]) - val shortcutNo = preferences.getString("shortcutNo", "4")?.toInt() + val shortcutNo = sharedPreferenceManager.getShortcutNumber() if (i >= shortcutNo!!) { textView.visibility = View.GONE @@ -189,7 +189,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh setShortcutSetup(textView, savedView) } - uiUtils.setShortcutAlignment(preferences, binding.homeView) + uiUtils.setShortcutAlignment(binding.homeView) } } @@ -213,7 +213,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh Toast.makeText(this, "Long click to select an app", Toast.LENGTH_SHORT).show() } textView.setOnLongClickListener { - uiUtils.setMenuTitleAlignment(preferences, binding.menutitle) + uiUtils.setMenuTitleAlignment(binding.menutitle) binding.menutitle.visibility = View.VISIBLE adapter?.shortcutTextView = textView @@ -226,7 +226,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh private fun toAppMenu() { animations.showApps(binding) animations.backgroundIn(this@MainActivity) - if (preferences.getBoolean("autoKeyboard", false)) { + if (sharedPreferenceManager.isAutoKeyboardEnabled()) { val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager searchView.requestFocus() @@ -260,7 +260,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh private fun setHomeListeners() { registerBatteryReceiver() - if (!preferences.getBoolean("battery_enabled", false)) { + if (!sharedPreferenceManager.isBatteryEnabled()) { unregisterBatteryReceiver() } @@ -310,31 +310,31 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh } "shortcutAlignment" -> { - uiUtils.setShortcutAlignment(preferences, binding.homeView) + uiUtils.setShortcutAlignment(binding.homeView) } "searchAlignment" -> { - uiUtils.setSearchAlignment(preferences, searchView) + uiUtils.setSearchAlignment(searchView) } "clockSize" -> { - uiUtils.setClockSize(preferences, clock) + uiUtils.setClockSize(clock) } "dateSize" -> { - uiUtils.setDateSize(preferences, dateText) + uiUtils.setDateSize(dateText) } "shortcutSize" -> { - uiUtils.setShortcutSize(preferences, binding.homeView) + uiUtils.setShortcutSize(binding.homeView) } "searchSize" -> { - uiUtils.setSearchSize(preferences, searchView) + uiUtils.setSearchSize(searchView) } "barVisibility" -> { - uiUtils.setStatusBar(window, preferences) + uiUtils.setStatusBar(window) } "leftSwipeApp" -> { @@ -346,7 +346,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh } "battery_enabled" -> { - if (preferences.getBoolean(key, false)) { + if (sharedPreferenceManager.isBatteryEnabled()) { registerBatteryReceiver() } else { unregisterBatteryReceiver() @@ -405,7 +405,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh suspend fun refreshAppMenu() { try { if (isJobActive) { - val updatedApps = appUtils.getInstalledApps(launcherApps) + val updatedApps = appUtils.getInstalledApps() if (!listsEqual(installedApps, updatedApps)) { updateMenu(updatedApps) @@ -438,8 +438,8 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh private suspend fun updateWeather() { withContext(Dispatchers.IO) { - if (preferences.getBoolean("weather_enabled", false)) { - if (preferences.getBoolean("gps_location", false)) { + if (sharedPreferenceManager.isWeatherEnabled()) { + if (sharedPreferenceManager.isWeatherGPS()) { weatherSystem.setGpsLocation(this@MainActivity) } else { updateWeatherText() @@ -462,7 +462,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh private fun setupApps() { lifecycleScope.launch(Dispatchers.Default) { - installedApps = appUtils.getInstalledApps(launcherApps) + installedApps = appUtils.getInstalledApps() val newApps = installedApps.toMutableList() setupRecyclerView(newApps) @@ -526,7 +526,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh val cleanQuery = stringUtils.cleanString(query) val newFilteredApps = mutableListOf>>() - val updatedApps = appUtils.getInstalledApps(launcherApps) + val updatedApps = appUtils.getInstalledApps() getFilteredApps(cleanQuery, newFilteredApps, updatedApps) } @@ -598,7 +598,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh } override fun onItemClick(appInfo: LauncherActivityInfo, userHandle: UserHandle) { - appUtils.launchApp(launcherApps, appInfo, userHandle) + appUtils.launchApp(appInfo, userHandle) } override fun onShortcut( @@ -615,38 +615,9 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh shortcutView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_empty, null),null,null,null) } - when (preferences.getString("shortcutAlignment", "left")) { - "left" -> { - shortcutView.setCompoundDrawablesWithIntrinsicBounds( - textView.compoundDrawables.filterNotNull().first(), null, null, null - ) - shortcutView.gravity = Gravity.CENTER_VERTICAL or Gravity.START - } - - "center" -> { - shortcutView.setCompoundDrawablesWithIntrinsicBounds( - shortcutView.compoundDrawables.filterNotNull().first(), - null, - shortcutView.compoundDrawables.filterNotNull().first(), - null - ) - shortcutView.gravity = Gravity.CENTER - } - - "right" -> { - shortcutView.setCompoundDrawablesWithIntrinsicBounds( - null, - null, - shortcutView.compoundDrawables.filterNotNull().first(), - null - ) - shortcutView.gravity = Gravity.CENTER_VERTICAL or Gravity.END - } - } - shortcutView.text = textView.text.toString() shortcutView.setOnClickListener { - appUtils.launchApp(launcherApps, appInfo, userHandle) + appUtils.launchApp(appInfo, userHandle) } sharedPreferenceManager.setShortcut( shortcutView, @@ -714,7 +685,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh } // Swipe left - else if (deltaX < -swipeThreshold && abs(velocityX) > swipeVelocityThreshold && preferences.getBoolean("leftSwipe", true)){ + else if (deltaX < -swipeThreshold && abs(velocityX) > swipeVelocityThreshold && sharedPreferenceManager.isGestureEnabled("left")){ if (leftSwipeActivity.first != null && leftSwipeActivity.second != null) { launcherApps.startMainActivity(leftSwipeActivity.first!!.componentName, launcherApps.profiles[leftSwipeActivity.second!!], null, null) @@ -725,7 +696,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh // Swipe right - else if (deltaX > -swipeThreshold && abs(velocityX) > swipeVelocityThreshold && preferences.getBoolean("rightSwipe", true)) { + else if (deltaX > -swipeThreshold && abs(velocityX) > swipeVelocityThreshold && sharedPreferenceManager.isGestureEnabled("right")) { if (rightSwipeActivity.first != null && rightSwipeActivity.second != null) { launcherApps.startMainActivity(rightSwipeActivity.first!!.componentName, launcherApps.profiles[rightSwipeActivity.second!!], null, null) } else { @@ -742,7 +713,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh } override fun onDoubleTap(e: MotionEvent): Boolean { - if (preferences.getBoolean("doubleTap", false)) { + if (sharedPreferenceManager.isDoubleTapEnabled()) { if (gestureUtils.isAccessibilityServiceEnabled( ScreenLockService::class.java ) @@ -772,5 +743,4 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh } } - } \ No newline at end of file diff --git a/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt b/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt index 18345bb..a227bce 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt @@ -11,84 +11,74 @@ class SharedPreferenceManager (context: Context) { fun setShortcut(textView: TextView, packageName: String, profile: Int) { val editor = preferences.edit() - val key = "shortcut${textView.id}" - editor.putString(key, "$packageName§splitter§$profile§splitter§${textView.text}") + editor.putString("shortcut${textView.id}", "$packageName§splitter§$profile§splitter§${textView.text}") editor.apply() } fun getShortcut(textView: TextView): List? { - val key = "shortcut${textView.id}" - val value = preferences.getString(key, "e§splitter§e") + val value = preferences.getString("shortcut${textView.id}", "e§splitter§e") return value?.split("§splitter§") } + fun getShortcutNumber(): Int? { + return preferences.getString("shortcutNo", "4")?.toInt() + } + fun setAppHidden(packageName: String, profile: Int, hidden: Boolean) { val editor = preferences.edit() - val key = "hidden$packageName-$profile" - editor.putBoolean(key, hidden) + editor.putBoolean("hidden$packageName-$profile", hidden) editor.apply() } fun isAppHidden(packageName: String, profile: Int): Boolean { - val key = "hidden$packageName-$profile" - return preferences.getBoolean(key, false) // Default to false (visible) + return preferences.getBoolean("hidden$packageName-$profile", false) // Default to false (visible) } fun setAppVisible(packageName: String, profile: Int) { val editor = preferences.edit() - val key = "hidden$packageName-$profile" - editor.remove(key) + editor.remove("hidden$packageName-$profile") editor.apply() } fun setAppName(packageName: String, profile: Int, newName: String) { val editor = preferences.edit() - val key = "name$packageName-$profile" - editor.putString(key, newName) + editor.putString("name$packageName-$profile", newName) editor.apply() } fun getAppName(packageName: String, profile: Int, appName: CharSequence): CharSequence? { - val key = "name$packageName-$profile" - return preferences.getString(key, appName.toString()) + return preferences.getString("name$packageName-$profile", appName.toString()) } fun resetAppName(packageName: String, profile: Int) { val editor = preferences.edit() - val key = "name$packageName-$profile" - editor.remove(key) + editor.remove("name$packageName-$profile") editor.apply() } fun setWeatherLocation(location: String, region: String?) { val editor = preferences.edit() - val key = "location" - val regionKey = "location_region" - editor.putString(key, location) - editor.putString(regionKey, region) + editor.putString("location", location) + editor.putString("location_region", region) editor.apply() } fun getWeatherLocation(): String? { - val key = "location" - return preferences.getString(key, "") + return preferences.getString("location", "") } fun getWeatherRegion(): String? { - val key = "location_region" - return preferences.getString(key, "") + return preferences.getString("location_region", "") } fun setGestures(direction: String, appName: String?) { val editor = preferences.edit() - val nameKey = "${direction}SwipeApp" - editor.putString(nameKey, appName) + editor.putString("${direction}SwipeApp", appName) editor.apply() } fun getGestureName(direction: String) : String? { - val key = "${direction}SwipeApp" - val name = preferences.getString(key, "")?.split("§splitter§") + val name = preferences.getString("${direction}SwipeApp", "")?.split("§splitter§") return name?.get(0) } @@ -96,6 +86,14 @@ class SharedPreferenceManager (context: Context) { return preferences.getString("${direction}SwipeApp", "")?.split("§splitter§") } + fun isGestureEnabled(direction: String) : Boolean { + return preferences.getBoolean("${direction}Swipe", false) + } + + fun isDoubleTapEnabled(): Boolean { + return preferences.getBoolean("doubleTap", false) + } + fun getBgColor(): Int { return Color.parseColor(preferences.getString("bgColor", "#00000000")) } @@ -108,4 +106,59 @@ class SharedPreferenceManager (context: Context) { return preferences.getString("clockAlignment", "left") } + fun getShortcutAlignment(): String? { + return preferences.getString("shortcutAlignment", "left") + } + + fun getAppAlignment(): String? { + return preferences.getString("appMenuAlignment", "left") + } + + fun getSearchAlignment(): String? { + return preferences.getString("searchAlignment", "left") + } + + fun getClockSize(): String? { + return preferences.getString("clockSize","medium") + } + + fun getDateSize(): String? { + return preferences.getString("dateSize", "medium") + } + + fun getShortcutSize(): String? { + return preferences.getString("shortcutSize", "medium") + } + + fun getAppSize(): String? { + return preferences.getString("appMenuSize", "medium") + } + + fun getSearchSize(): String? { + return preferences.getString("searchSize", "medium") + } + + fun isBarVisible(): Boolean { + return preferences.getBoolean("barVisibility", false) + } + + fun isAutoKeyboardEnabled(): Boolean { + return preferences.getBoolean("autoKeyboard", false) + } + + fun getTempUnits(): String? { + return preferences.getString("tempUnits", "celsius") + } + + fun isWeatherEnabled(): Boolean { + return preferences.getBoolean("weather_enabled", false) + } + + fun isWeatherGPS(): Boolean { + return preferences.getBoolean("gps_location", false) + } + + fun isBatteryEnabled(): Boolean { + return preferences.getBoolean("battery_enabled", false) + } } \ No newline at end of file diff --git a/app/src/main/java/eu/ottop/yamlauncher/UIUtils.kt b/app/src/main/java/eu/ottop/yamlauncher/UIUtils.kt index feea72c..ac8c7a1 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/UIUtils.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/UIUtils.kt @@ -1,7 +1,6 @@ package eu.ottop.yamlauncher import android.content.Context -import android.content.SharedPreferences import android.graphics.BlendMode import android.graphics.BlendModeColorFilter import android.graphics.Color @@ -20,7 +19,7 @@ import androidx.core.content.res.ResourcesCompat import androidx.core.view.children import com.google.android.material.textfield.TextInputEditText -class UIUtils(context: Context) { +class UIUtils(private val context: Context) { private val sharedPreferenceManager = SharedPreferenceManager(context) @@ -95,13 +94,13 @@ class UIUtils(context: Context) { setTextAlignment(dateText, alignment) } - fun setShortcutAlignment(preferences: SharedPreferences, shortcuts: LinearLayout) { + fun setShortcutAlignment(shortcuts: LinearLayout) { shortcuts.children.forEach { if (it is TextView) { try { - when (preferences.getString("shortcutAlignment", "left")) { + when (sharedPreferenceManager.getShortcutAlignment()) { "left" -> { it.setCompoundDrawablesWithIntrinsicBounds( it.compoundDrawables.filterNotNull().first(), null, null, null @@ -134,8 +133,12 @@ class UIUtils(context: Context) { } } - fun setAppAlignment(activity: Context, preferences: SharedPreferences, textView: TextView, editText: TextInputEditText? = null, regionText: TextView? = null) { - val alignment = preferences.getString("appMenuAlignment", "left") + fun setAppAlignment( + textView: TextView, + editText: TextInputEditText? = null, + regionText: TextView? = null + ) { + val alignment = sharedPreferenceManager.getAppAlignment() setTextGravity(textView, alignment) if (regionText != null) { @@ -146,7 +149,7 @@ class UIUtils(context: Context) { when (alignment) { "left" -> { - textView.setCompoundDrawablesWithIntrinsicBounds(textView.compoundDrawables.filterNotNull().first(),null, ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null), null) + textView.setCompoundDrawablesWithIntrinsicBounds(textView.compoundDrawables.filterNotNull().first(),null, ResourcesCompat.getDrawable(context.resources, R.drawable.ic_empty, null), null) editText?.gravity = Gravity.CENTER_VERTICAL or Gravity.START } @@ -156,19 +159,19 @@ class UIUtils(context: Context) { } "right" -> { - textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null, textView.compoundDrawables.filterNotNull().first(), null) + textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_empty, null),null, textView.compoundDrawables.filterNotNull().first(), null) editText?.gravity = Gravity.CENTER_VERTICAL or Gravity.END } } } - fun setSearchAlignment(preferences: SharedPreferences, searchView: TextInputEditText) { - setTextAlignment(searchView, preferences.getString("searchAlignment", "left")) + fun setSearchAlignment(searchView: TextInputEditText) { + setTextAlignment(searchView, sharedPreferenceManager.getSearchAlignment()) } - fun setMenuTitleAlignment(preferences: SharedPreferences, menuTitle: TextView) { - setTextGravity(menuTitle, preferences.getString("appMenuAlignment", "left")) + fun setMenuTitleAlignment(menuTitle: TextView) { + setTextGravity(menuTitle, sharedPreferenceManager.getAppAlignment()) } @@ -196,15 +199,15 @@ class UIUtils(context: Context) { } } - fun setClockSize(preferences: SharedPreferences, clock: TextClock) { - setTextSize(clock, preferences.getString("clockSize","medium"), 48F, 58F, 68F) + fun setClockSize(clock: TextClock) { + setTextSize(clock, sharedPreferenceManager.getClockSize(), 48F, 58F, 68F) } - fun setDateSize(preferences: SharedPreferences, dateText: TextClock) { - setTextSize(dateText, preferences.getString("dateSize", "medium"), 17F, 20F, 23F) + fun setDateSize(dateText: TextClock) { + setTextSize(dateText, sharedPreferenceManager.getDateSize(), 17F, 20F, 23F) } - fun setShortcutSize(preferences: SharedPreferences, shortcuts: LinearLayout) { + fun setShortcutSize(shortcuts: LinearLayout) { val viewTreeObserver = shortcuts.viewTreeObserver val globalLayoutListener = object : ViewTreeObserver.OnGlobalLayoutListener { @@ -213,7 +216,7 @@ class UIUtils(context: Context) { shortcuts.children.forEach { if (it is TextView) { - when (preferences.getString("shortcutSize", "medium")) { + when (sharedPreferenceManager.getShortcutSize()) { "small" -> { it.setPadding( it.paddingLeft, @@ -249,8 +252,12 @@ class UIUtils(context: Context) { } } - fun setAppSize(preferences: SharedPreferences, textView: TextView, editText: TextInputEditText? = null, regionText: TextView? = null) { - val size = preferences.getString("appMenuSize", "medium") + fun setAppSize( + textView: TextView, + editText: TextInputEditText? = null, + regionText: TextView? = null + ) { + val size = sharedPreferenceManager.getAppSize() setTextSize(textView, size, 24F, 26F, 28F) if (editText != null) { setTextSize(editText, size, 24F, 26F, 28F) @@ -260,8 +267,8 @@ class UIUtils(context: Context) { } } - fun setSearchSize(preferences: SharedPreferences, searchView: TextInputEditText) { - setTextSize(searchView, preferences.getString("searchSize", "medium"), 21F, 23F, 25F) + fun setSearchSize(searchView: TextInputEditText) { + setTextSize(searchView, sharedPreferenceManager.getSearchSize(), 21F, 23F, 25F) } private fun setTextSize(view: TextView, size: String?, s: Float, m: Float, l: Float) { @@ -276,11 +283,11 @@ class UIUtils(context: Context) { } } - fun setStatusBar(window: Window, preferences: SharedPreferences) { + fun setStatusBar(window: Window) { val windowInsetsController = window.insetsController windowInsetsController?.let { - if (preferences.getBoolean("barVisibility", false)) { + if (sharedPreferenceManager.isBarVisible()) { it.show(WindowInsets.Type.statusBars()) } else { diff --git a/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt b/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt index 06793ae..35929d0 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt @@ -84,8 +84,7 @@ class WeatherSystem(private val context: Context) { fun getTemp() : String { - val preferences = PreferenceManager.getDefaultSharedPreferences(context) - val tempUnits = preferences.getString("tempUnits", "celsius") + val tempUnits = sharedPreferenceManager.getTempUnits() var currentWeather = "" val location = sharedPreferenceManager.getWeatherLocation()