Fixed a renaming issue and unified sharedpreferences

This commit is contained in:
ottoptj 2024-08-09 19:57:37 +03:00
commit 0c8e27dcd5
13 changed files with 189 additions and 152 deletions

View file

@ -19,11 +19,11 @@ 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 {
private val sharedPreferenceManager = SharedPreferenceManager()
private val animations = Animations()
fun setActionListeners(
@ -40,6 +40,8 @@ class AppActionMenu {
mainActivity: LauncherActivityInfo?
){
val sharedPreferenceManager = SharedPreferenceManager(activity)
actionMenu.findViewById<TextView>(R.id.info).setOnClickListener {
if (mainActivity != null) {
launcherApps.startAppDetailsActivity(
@ -100,12 +102,11 @@ class AppActionMenu {
activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(editText.windowToken, 0)
sharedPreferenceManager.setAppName(
activity,
appInfo.packageName,
workProfile,
editText.text.toString()
)
activity.lifecycleScope.launch(Dispatchers.Default) {
activity.lifecycleScope.launch {
activity.applySearch()
}
@ -120,12 +121,11 @@ class AppActionMenu {
activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(editLayout.windowToken, 0)
sharedPreferenceManager.resetAppName(
activity,
app.first.applicationInfo.packageName,
app.second.second
)
activity.lifecycleScope.launch(Dispatchers.Default) {
activity.lifecycleScope.launch {
activity.applySearch()
}
}
@ -136,7 +136,7 @@ class AppActionMenu {
textView.visibility = View.GONE
actionMenu.visibility = View.GONE
activity.lifecycleScope.launch {
sharedPreferenceManager.setAppHidden(activity, appInfo.packageName, workProfile, true)
sharedPreferenceManager.setAppHidden(appInfo.packageName, workProfile, true)
activity.refreshAppMenu()
}
}

View file

@ -22,7 +22,8 @@ import com.google.android.material.textfield.TextInputEditText
class AppMenuAdapter(
private val activity: Context,
private val context: Context,
private var apps: MutableList<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>,
private val itemClickListener: OnItemClickListener,
private val shortcutListener: OnShortcutListener,
@ -33,10 +34,10 @@ class AppMenuAdapter(
var shortcutTextView: TextView? = null
private val sharedPreferenceManager = SharedPreferenceManager()
private var preferences = PreferenceManager.getDefaultSharedPreferences(activity)
private val sharedPreferenceManager = SharedPreferenceManager(context)
private var preferences = PreferenceManager.getDefaultSharedPreferences(context)
private val uiUtils = UIUtils()
private val appUtils = AppUtils()
private val appUtils = AppUtils(context)
interface OnItemClickListener {
fun onItemClick(appInfo: LauncherActivityInfo, userHandle: UserHandle)
@ -112,15 +113,15 @@ class AppMenuAdapter(
val app = apps[position]
if (app.second.second != 0) {
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_work_app, null),null, ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null)
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)
}
else {
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null,ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null)
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(activity, preferences, holder.textView, holder.editText)
uiUtils.setAppAlignment(context, preferences, holder.textView, holder.editText)
uiUtils.setAppSize(preferences, holder.textView, holder.editText)
@ -131,11 +132,10 @@ class AppMenuAdapter(
)
holder.textView.setTextColor(Color.parseColor(preferences?.getString("textColor", "#FFF3F3F3")))
val appLabel: CharSequence = appInfo?.loadLabel(activity.packageManager) ?: "Removing..."
val appLabel: CharSequence = appInfo?.loadLabel(context.packageManager) ?: "Removing..."
if (appInfo != null) {
holder.textView.text = sharedPreferenceManager.getAppName(
activity,
appInfo.packageName,
app.second.second,
appLabel

View file

@ -1,6 +1,5 @@
package eu.ottop.yamlauncher
import android.app.Activity
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.LauncherActivityInfo
@ -11,21 +10,20 @@ import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class AppUtils {
class AppUtils(private val context: Context) {
private val sharedPreferenceManager = SharedPreferenceManager()
private val sharedPreferenceManager = SharedPreferenceManager(context)
suspend fun getInstalledApps(activity: Activity, launcherApps: LauncherApps): List<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>> {
suspend fun getInstalledApps(launcherApps: LauncherApps): List<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>> {
val allApps = mutableListOf<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>()
var sortedApps = listOf<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>()
withContext(Dispatchers.Default) {
for (i in launcherApps.profiles.indices) {
launcherApps.getActivityList(null, launcherApps.profiles[i]).forEach { app ->
if (!sharedPreferenceManager.isAppHidden(
activity,
app.applicationInfo.packageName,
i
) && app.applicationInfo.packageName != activity.applicationInfo.packageName
) && app.applicationInfo.packageName != context.applicationInfo.packageName
) {
allApps.add(Pair(app, Pair(launcherApps.profiles[i], i)))
}
@ -34,10 +32,9 @@ class AppUtils {
sortedApps = allApps.sortedBy {
sharedPreferenceManager.getAppName(
activity,
it.first.applicationInfo.packageName,
it.second.second,
it.first.applicationInfo.loadLabel(activity.packageManager)
it.first.applicationInfo.loadLabel(context.packageManager)
).toString().lowercase()
}
}
@ -45,18 +42,22 @@ class AppUtils {
}
fun getHiddenApps(activity: Activity): List<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>> {
fun getHiddenApps(): List<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>> {
val allApps = mutableListOf<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>()
val launcherApps = activity.getSystemService(AppCompatActivity.LAUNCHER_APPS_SERVICE) as LauncherApps
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(activity, app.applicationInfo.packageName, i)) {
if (sharedPreferenceManager.isAppHidden(app.applicationInfo.packageName, i)) {
allApps.add(Pair(app, Pair(launcherApps.profiles[i], i)))
}
}
}
return allApps.sortedBy {
sharedPreferenceManager.getAppName(activity, it.first.applicationInfo.packageName,it.second.second, activity.packageManager.getApplicationLabel(it.first.applicationInfo)).toString().lowercase()
sharedPreferenceManager.getAppName(
it.first.applicationInfo.packageName,
it.second.second,
context.packageManager.getApplicationLabel(it.first.applicationInfo)
).toString().lowercase()
}
}
@ -72,7 +73,7 @@ class AppUtils {
}
}
fun launchApp(context: Context, launcherApps: LauncherApps, appInfo: LauncherActivityInfo, userHandle: UserHandle) {
fun launchApp(launcherApps: LauncherApps, appInfo: LauncherActivityInfo, userHandle: UserHandle) {
val mainActivity = launcherApps.getActivityList(appInfo.applicationInfo.packageName, userHandle).firstOrNull()
if (mainActivity != null) {
launcherApps.startMainActivity(mainActivity.componentName, userHandle, null, null)

View file

@ -14,14 +14,14 @@ import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.RecyclerView
class GestureAppsAdapter(
private val activity: Context,
private val context: Context,
var apps: MutableList<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>,
private val itemClickListener: OnItemClickListener
) :
RecyclerView.Adapter<GestureAppsAdapter.AppViewHolder>() {
private val sharedPreferenceManager = SharedPreferenceManager()
private var preferences = PreferenceManager.getDefaultSharedPreferences(activity)
private val sharedPreferenceManager = SharedPreferenceManager(context)
private var preferences = PreferenceManager.getDefaultSharedPreferences(context)
private val uiUtils = UIUtils()
interface OnItemClickListener {
@ -52,18 +52,22 @@ class GestureAppsAdapter(
val app = apps[position]
if (app.second.second != 0) {
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_work_app, null),null,null,null)
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_work_app, null),null,null,null)
}
else {
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null,null,null)
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_empty, null),null,null,null)
}
uiUtils.setAppAlignment(activity, preferences, holder.textView)
uiUtils.setAppAlignment(context, preferences, holder.textView)
uiUtils.setAppSize(preferences, holder.textView)
val appInfo = app.first.activityInfo.applicationInfo
holder.textView.text = sharedPreferenceManager.getAppName(activity, app.first.applicationInfo.packageName,app.second.second, holder.itemView.context.packageManager.getApplicationLabel(appInfo))
holder.textView.text = sharedPreferenceManager.getAppName(
app.first.applicationInfo.packageName,
app.second.second,
holder.itemView.context.packageManager.getApplicationLabel(appInfo)
)
holder.textView.visibility = View.VISIBLE
}

View file

@ -1,6 +1,5 @@
package eu.ottop.yamlauncher
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.pm.LauncherActivityInfo
@ -25,9 +24,9 @@ import kotlinx.coroutines.withContext
class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener {
private var adapter: GestureAppsAdapter? = null
private val sharedPreferenceManager = SharedPreferenceManager()
private val sharedPreferenceManager = SharedPreferenceManager(requireContext())
private var stringUtils = StringUtils()
private val appUtils = AppUtils()
private val appUtils = AppUtils(requireContext())
private lateinit var launcherApps: LauncherApps
override fun onCreateView(
@ -49,7 +48,7 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener {
adapter = GestureAppsAdapter(
requireContext(),
appUtils.getInstalledApps(activity as Activity, launcherApps).toMutableList(),
appUtils.getInstalledApps(launcherApps).toMutableList(),
this@GestureAppsFragment
)
}
@ -104,7 +103,7 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener {
val cleanQuery = stringUtils.cleanString(query)
val newFilteredApps = mutableListOf<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>()
val updatedApps = appUtils.getInstalledApps(requireActivity(), launcherApps)
val updatedApps = appUtils.getInstalledApps(launcherApps)
getFilteredApps(cleanQuery, newFilteredApps, updatedApps)
@ -117,7 +116,11 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener {
newFilteredApps.addAll(updatedApps)
} else {
updatedApps.forEach {
val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName(requireActivity(), it.first.applicationInfo.packageName, it.second.second, requireActivity().packageManager.getApplicationLabel(it.first.applicationInfo)).toString())
val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName(
it.first.applicationInfo.packageName,
it.second.second,
requireActivity().packageManager.getApplicationLabel(it.first.applicationInfo)
).toString())
if (cleanItemText != null) {
if (cleanItemText.contains(cleanQuery, ignoreCase = true)) {
newFilteredApps.add(it)
@ -156,7 +159,11 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener {
override fun onItemClick(appInfo: LauncherActivityInfo, profile: Int) {
showConfirmationDialog(appInfo, sharedPreferenceManager.getAppName(requireContext(), appInfo.applicationInfo.packageName,profile, requireContext().packageManager.getApplicationLabel(appInfo.applicationInfo)).toString(), profile)
showConfirmationDialog(appInfo, sharedPreferenceManager.getAppName(
appInfo.applicationInfo.packageName,
profile,
requireContext().packageManager.getApplicationLabel(appInfo.applicationInfo)
).toString(), profile)
}

View file

@ -14,14 +14,14 @@ import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.RecyclerView
class HiddenAppsAdapter(
private val activity: Context,
private val context: Context,
private var apps: MutableList<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>,
private val itemClickListener: OnItemClickListener
) :
RecyclerView.Adapter<HiddenAppsAdapter.AppViewHolder>() {
private val sharedPreferenceManager = SharedPreferenceManager()
private var preferences = PreferenceManager.getDefaultSharedPreferences(activity)
private val sharedPreferenceManager = SharedPreferenceManager(context)
private var preferences = PreferenceManager.getDefaultSharedPreferences(context)
private val uiUtils = UIUtils()
@ -54,18 +54,22 @@ class HiddenAppsAdapter(
val app = apps[position]
if (app.second.second != 0) {
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_work_app, null),null,null,null)
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_work_app, null),null,null,null)
}
else {
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null,null,null)
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_empty, null),null,null,null)
}
uiUtils.setAppAlignment(activity, preferences, holder.textView)
uiUtils.setAppAlignment(context, preferences, holder.textView)
uiUtils.setAppSize(preferences, holder.textView)
val appInfo = app.first.activityInfo.applicationInfo
holder.textView.text = sharedPreferenceManager.getAppName(activity, app.first.applicationInfo.packageName,app.second.second, holder.itemView.context.packageManager.getApplicationLabel(appInfo))
holder.textView.text = sharedPreferenceManager.getAppName(
app.first.applicationInfo.packageName,
app.second.second,
holder.itemView.context.packageManager.getApplicationLabel(appInfo)
)
holder.textView.visibility = View.VISIBLE
}

View file

@ -1,6 +1,5 @@
package eu.ottop.yamlauncher
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.pm.LauncherActivityInfo
@ -18,8 +17,8 @@ import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.textfield.TextInputEditText
class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener {
private val appUtils = AppUtils()
private val sharedPreferenceManager = SharedPreferenceManager()
private lateinit var appUtils: AppUtils
private lateinit var sharedPreferenceManager: SharedPreferenceManager
private var adapter: HiddenAppsAdapter? = null
private var stringUtils = StringUtils()
private val uiUtils = UIUtils()
@ -34,11 +33,16 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
adapter = HiddenAppsAdapter(requireContext(), appUtils.getHiddenApps(activity as Activity).toMutableList(), this)
appUtils = AppUtils(requireContext())
sharedPreferenceManager = SharedPreferenceManager(requireContext())
val recyclerView = view.findViewById<RecyclerView>(R.id.hidden_app_recycler)
val appMenuEdgeFactory = AppMenuEdgeFactory(requireActivity())
val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
adapter = HiddenAppsAdapter(requireContext(), appUtils.getHiddenApps().toMutableList(), this)
recyclerView.edgeEffectFactory = appMenuEdgeFactory
recyclerView.adapter = adapter
@ -85,7 +89,7 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener {
val cleanQuery = stringUtils.cleanString(query)
val newFilteredApps = mutableListOf<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>()
val updatedApps = appUtils.getHiddenApps(requireActivity())
val updatedApps = appUtils.getHiddenApps()
getFilteredApps(cleanQuery, newFilteredApps, updatedApps)
@ -98,7 +102,11 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener {
newFilteredApps.addAll(updatedApps)
} else {
updatedApps.forEach {
val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName(requireActivity(), it.first.applicationInfo.packageName, it.second.second, requireActivity().packageManager.getApplicationLabel(it.first.applicationInfo)).toString())
val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName(
it.first.applicationInfo.packageName,
it.second.second,
requireActivity().packageManager.getApplicationLabel(it.first.applicationInfo)
).toString())
if (cleanItemText != null) {
if (cleanItemText.contains(cleanQuery, ignoreCase = true)) {
newFilteredApps.add(it)
@ -117,7 +125,6 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener {
setTitle("Confirmation")
setMessage("Are you sure you want to unhide $appName?")
setPositiveButton("Yes") { _, _ ->
// Perform action on confirmation
performConfirmedAction(appInfo, profile)
}
@ -127,12 +134,16 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener {
}
private fun performConfirmedAction(appInfo: LauncherActivityInfo, profile: Int) {
sharedPreferenceManager.setAppVisible(requireContext(), appInfo.applicationInfo.packageName, profile)
adapter?.updateApps(appUtils.getHiddenApps(requireActivity()))
sharedPreferenceManager.setAppVisible(appInfo.applicationInfo.packageName, profile)
adapter?.updateApps(appUtils.getHiddenApps())
}
override fun onItemClick(appInfo: LauncherActivityInfo, profile: Int) {
showConfirmationDialog(appInfo, sharedPreferenceManager.getAppName(requireContext(), appInfo.applicationInfo.packageName,profile, requireContext().packageManager.getApplicationLabel(appInfo.applicationInfo)).toString(), profile)
showConfirmationDialog(appInfo, sharedPreferenceManager.getAppName(
appInfo.applicationInfo.packageName,
profile,
requireContext().packageManager.getApplicationLabel(appInfo.applicationInfo)
).toString(), profile)
}
}

View file

@ -21,8 +21,8 @@ import kotlinx.coroutines.withContext
class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener {
private var adapter: LocationListAdapter? = null
private val weatherSystem = WeatherSystem()
private val sharedPreferenceManager = SharedPreferenceManager()
private lateinit var weatherSystem: WeatherSystem
private lateinit var sharedPreferenceManager: SharedPreferenceManager
private val stringUtils = StringUtils()
private val uiUtils = UIUtils()
@ -30,7 +30,6 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener {
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_location, container, false)
}
@ -43,6 +42,10 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener {
val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
weatherSystem = WeatherSystem(requireContext())
sharedPreferenceManager = SharedPreferenceManager(requireContext())
stringUtils.setLink(requireActivity().findViewById(R.id.locationLink), getString(R.string.location_link))
lifecycleScope.launch(Dispatchers.IO) {
@ -108,7 +111,6 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener {
setTitle("Confirmation")
setMessage("Are you sure you want to select $appName?")
setPositiveButton("Yes") { _, _ ->
// Perform action on confirmation
performConfirmedAction(appName, latitude, longitude)
}
setNegativeButton("Cancel") { _, _ ->
@ -119,7 +121,7 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener {
}
private fun performConfirmedAction(appName: String?, latitude: String?, longitude: String?) {
sharedPreferenceManager.setWeatherLocation(requireContext(), "latitude=${latitude}&longitude=${longitude}", appName)
sharedPreferenceManager.setWeatherLocation("latitude=${latitude}&longitude=${longitude}", appName)
requireActivity().supportFragmentManager.popBackStack()
}

View file

@ -44,8 +44,8 @@ import kotlin.math.abs
class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener, AppMenuAdapter.OnItemClickListener, AppMenuAdapter.OnShortcutListener, AppMenuAdapter.OnItemLongClickListener {
private val weatherSystem = WeatherSystem()
private val appUtils = AppUtils()
private lateinit var weatherSystem: WeatherSystem
private lateinit var appUtils: AppUtils
private val stringUtils = StringUtils()
private val uiUtils = UIUtils()
private val gestureUtils = GestureUtils()
@ -54,7 +54,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
private val appMenuLinearLayoutManager = AppMenuLinearLayoutManager(this@MainActivity)
private val appMenuEdgeFactory = AppMenuEdgeFactory(this@MainActivity)
private val sharedPreferenceManager = SharedPreferenceManager()
private lateinit var sharedPreferenceManager: SharedPreferenceManager
private val animations = Animations()
@ -137,6 +137,12 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
launcherApps = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
weatherSystem = WeatherSystem(this@MainActivity)
appUtils = AppUtils(this@MainActivity)
sharedPreferenceManager = SharedPreferenceManager(this@MainActivity)
preferences = PreferenceManager.getDefaultSharedPreferences(this)
}
@ -177,7 +183,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
unsetShortcutSetup(textView)
val savedView = sharedPreferenceManager.getShortcut(this, textView)
val savedView = sharedPreferenceManager.getShortcut(textView)
if (savedView?.get(1) != "e") {
setShortcutSetup(textView, savedView)
@ -399,11 +405,11 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
suspend fun refreshAppMenu() {
try {
if (isJobActive) {
val updatedApps = appUtils.getInstalledApps(this@MainActivity, launcherApps)
val updatedApps = appUtils.getInstalledApps(launcherApps)
if (!listsEqual(installedApps, updatedApps)) {
withContext(Dispatchers.Main) {
updateMenu(updatedApps)
}
updateMenu(updatedApps)
installedApps = updatedApps
}
}
@ -424,8 +430,10 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
return true
}
private fun updateMenu(updatedApps : List<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>) {
adapter?.updateApps(updatedApps)
private suspend fun updateMenu(updatedApps : List<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>) {
withContext(Dispatchers.Main) {
adapter?.updateApps(updatedApps)
}
}
private suspend fun updateWeather() {
@ -444,7 +452,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
}
suspend fun updateWeatherText() {
val temp = weatherSystem.getTemp(this@MainActivity)
val temp = weatherSystem.getTemp()
withContext(Dispatchers.Main) {
modifyDate(temp, 2)
}
@ -452,7 +460,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
private fun setupApps() {
lifecycleScope.launch(Dispatchers.Default) {
installedApps = appUtils.getInstalledApps(this@MainActivity, launcherApps)
installedApps = appUtils.getInstalledApps(launcherApps)
val newApps = installedApps.toMutableList()
setupRecyclerView(newApps)
@ -516,37 +524,37 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
val cleanQuery = stringUtils.cleanString(query)
val newFilteredApps = mutableListOf<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>()
val updatedApps = appUtils.getInstalledApps(this@MainActivity, launcherApps)
val updatedApps = appUtils.getInstalledApps(launcherApps)
getFilteredApps(cleanQuery, newFilteredApps, updatedApps)
applySearchFilter(newFilteredApps)
}
private suspend fun getFilteredApps(cleanQuery: String?, newFilteredApps: MutableList<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>, updatedApps: List<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>) {
if (cleanQuery.isNullOrEmpty()) {
isJobActive = true
refreshAppMenu()
newFilteredApps.addAll(installedApps)
updateMenu(updatedApps)
} else {
isJobActive = false
updatedApps.forEach {
val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName(this@MainActivity, it.first.applicationInfo.packageName, it.second.second, packageManager.getApplicationLabel(it.first.applicationInfo)).toString())
val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName(
it.first.applicationInfo.packageName,
it.second.second,
packageManager.getApplicationLabel(it.first.applicationInfo)
).toString())
if (cleanItemText != null) {
if (cleanItemText.contains(cleanQuery, ignoreCase = true)) {
newFilteredApps.add(it)
}
}
}
applySearchFilter(newFilteredApps)
}
}
private suspend fun applySearchFilter(newFilteredApps: MutableList<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>) {
if (!listsEqual(installedApps, newFilteredApps)) {
withContext(Dispatchers.Main) {
updateMenu(newFilteredApps)
}
updateMenu(newFilteredApps)
installedApps = newFilteredApps
}
}
@ -557,7 +565,6 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
}
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
backToHome()
@ -575,9 +582,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
// Keyboard is sometimes open when going back to the app, so close it.
closeKeyboard()
try {
recyclerView.scrollToPosition(0)
}
catch (_: UninitializedPropertyAccessException) {
@ -591,7 +596,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
}
override fun onItemClick(appInfo: LauncherActivityInfo, userHandle: UserHandle) {
appUtils.launchApp(this@MainActivity, launcherApps, appInfo, userHandle)
appUtils.launchApp(launcherApps, appInfo, userHandle)
}
override fun onShortcut(
@ -639,9 +644,13 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
shortcutView.text = textView.text.toString()
shortcutView.setOnClickListener {
appUtils.launchApp(this@MainActivity, launcherApps, appInfo, userHandle)
appUtils.launchApp(launcherApps, appInfo, userHandle)
}
sharedPreferenceManager.setShortcut(this, shortcutView, appInfo.applicationInfo.packageName, userProfile)
sharedPreferenceManager.setShortcut(
shortcutView,
appInfo.applicationInfo.packageName,
userProfile
)
backToHome()
}
@ -689,12 +698,12 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
val deltaY = e2.y - e1.y
val deltaX = e2.x - e1.x
// Detect swipe up
// Swipe up
if (deltaY < -swipeThreshold && abs(velocityY) > swipeVelocityThreshold) {
openAppMenu()
}
// Detect swipe down
// Swipe down
else if (deltaY > swipeThreshold && abs(velocityY) > swipeVelocityThreshold) {
val statusBarService = getSystemService(Context.STATUS_BAR_SERVICE)
val statusBarManager: Class<*> = Class.forName("android.app.StatusBarManager")
@ -702,7 +711,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
expandMethod.invoke(statusBarService)
}
// Detect swipe left
// Swipe left
else if (deltaX < -swipeThreshold && abs(velocityX) > swipeVelocityThreshold && preferences.getBoolean("leftSwipe", true)){
if (leftSwipeActivity.first != null && leftSwipeActivity.second != null) {
@ -713,7 +722,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
}
// Detect swipe right
// Swipe right
else if (deltaX > -swipeThreshold && abs(velocityX) > swipeVelocityThreshold && preferences.getBoolean("rightSwipe", true)) {
if (rightSwipeActivity.first != null && rightSwipeActivity.second != null) {
launcherApps.startMainActivity(rightSwipeActivity.first!!.componentName, launcherApps.profiles[rightSwipeActivity.second!!], null, null)

View file

@ -5,6 +5,7 @@ import androidx.appcompat.app.AppCompatActivity
import eu.ottop.yamlauncher.databinding.ActivitySettingsBinding
class SettingsActivity : AppCompatActivity() {
private lateinit var binding: ActivitySettingsBinding
override fun onCreate(savedInstanceState: Bundle?) {

View file

@ -11,18 +11,21 @@ import androidx.preference.SwitchPreference
class SettingsFragment : PreferenceFragmentCompat() {
private var manualLocationPref: Preference? = null
private val sharedPreferenceManager = SharedPreferenceManager()
private lateinit var sharedPreferenceManager: SharedPreferenceManager
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
sharedPreferenceManager = SharedPreferenceManager(requireContext())
val gpsLocationPref: SwitchPreference? = findPreference("gps_location")
manualLocationPref = findPreference("manual_location")
val leftSwipePref = findPreference<Preference?>("leftSwipeApp")
val rightSwipePref = findPreference<Preference?>("rightSwipeApp")
val aboutPref = findPreference<Preference?>("about_page")
val hiddenPref = findPreference<Preference?>("hidden_apps")
manualLocationPref?.summary = sharedPreferenceManager.getWeatherRegion(requireContext())
manualLocationPref?.summary = sharedPreferenceManager.getWeatherRegion()
leftSwipePref?.summary = sharedPreferenceManager.getGestureName(requireContext(), "left")
rightSwipePref?.summary = sharedPreferenceManager.getGestureName(requireContext(), "right")
@ -49,7 +52,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}
findPreference<Preference?>("hidden_apps")?.onPreferenceClickListener =
hiddenPref?.onPreferenceClickListener =
Preference.OnPreferenceClickListener {
requireActivity().supportFragmentManager
.beginTransaction()
@ -73,8 +76,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
setPreference("leftSwipeApp", result)
}
sharedPreferenceManager.setGestures(
requireContext(), "left",
appDetails?.get(0)
"left", appDetails?.get(0)
)
val appName = appDetails?.get(0)
leftSwipePref?.summary = appName
@ -96,8 +98,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
setPreference("rightSwipeApp", result)
}
sharedPreferenceManager.setGestures(
requireContext(), "right",
appDetails?.get(0)
"right", appDetails?.get(0)
)
val appName = appDetails?.get(0)
rightSwipePref?.summary = appName
@ -116,7 +117,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
override fun onResume() {
super.onResume()
manualLocationPref?.summary = sharedPreferenceManager.getWeatherRegion(requireContext())
manualLocationPref?.summary = sharedPreferenceManager.getWeatherRegion()
}
private fun setPreference(key: String, value: String) {

View file

@ -3,65 +3,65 @@ package eu.ottop.yamlauncher
import android.content.Context
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
class SharedPreferenceManager {
class SharedPreferenceManager (context: Context) {
fun setShortcut(cont: Context, textView: TextView, packageName: String, profile: Int) {
val editor = cont.getSharedPreferences("shortcuts", AppCompatActivity.MODE_PRIVATE).edit()
val key = textView.id.toString()
private val preferences = PreferenceManager.getDefaultSharedPreferences(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.apply()
}
fun getShortcut(cont: Context, textView: TextView): List<String>? {
val sharedPref = cont.getSharedPreferences("shortcuts", AppCompatActivity.MODE_PRIVATE)
val key = textView.id.toString()
val value = sharedPref.getString(key, "e§splitter§e")
fun getShortcut(textView: TextView): List<String>? {
val key = "shortcut${textView.id}"
val value = preferences.getString(key, "e§splitter§e")
return value?.split("§splitter§")
}
fun setAppHidden(cont: Context, packageName: String, profile: Int, hidden: Boolean) {
val editor = cont.getSharedPreferences("hidden_apps", AppCompatActivity.MODE_PRIVATE).edit()
val key = "$packageName-$profile"
fun setAppHidden(packageName: String, profile: Int, hidden: Boolean) {
val editor = preferences.edit()
val key = "hidden$packageName-$profile"
editor.putBoolean(key, hidden)
editor.apply()
}
fun isAppHidden(cont: Context, packageName: String, profile: Int): Boolean {
val sharedPref = cont.getSharedPreferences("hidden_apps", AppCompatActivity.MODE_PRIVATE)
val key = "$packageName-$profile"
return sharedPref.getBoolean(key, false) // Default to false (visible)
fun isAppHidden(packageName: String, profile: Int): Boolean {
val key = "hidden$packageName-$profile"
return preferences.getBoolean(key, false) // Default to false (visible)
}
fun setAppVisible(cont: Context, packageName: String, profile: Int) {
val editor = cont.getSharedPreferences("hidden_apps", AppCompatActivity.MODE_PRIVATE).edit()
val key = "$packageName-$profile"
fun setAppVisible(packageName: String, profile: Int) {
val editor = preferences.edit()
val key = "hidden$packageName-$profile"
editor.remove(key)
editor.apply()
}
fun setAppName(cont: Context, packageName: String, profile: Int, newName: String) {
val editor = cont.getSharedPreferences("renamed_apps", AppCompatActivity.MODE_PRIVATE).edit()
val key = "$packageName-$profile"
fun setAppName(packageName: String, profile: Int, newName: String) {
val editor = preferences.edit()
val key = "name$packageName-$profile"
editor.putString(key, newName)
editor.apply()
}
fun getAppName(cont: Context, packageName: String, profile: Int, appName: CharSequence): CharSequence? {
val sharedPreferences = cont.getSharedPreferences("renamed_apps", AppCompatActivity.MODE_PRIVATE)
val key = "$packageName-$profile"
return sharedPreferences.getString(key, appName.toString())
fun getAppName(packageName: String, profile: Int, appName: CharSequence): CharSequence? {
val key = "name$packageName-$profile"
return preferences.getString(key, appName.toString())
}
fun resetAppName(cont: Context, packageName: String, profile: Int) {
val editor = cont.getSharedPreferences("renamed_apps", AppCompatActivity.MODE_PRIVATE).edit()
val key = "$packageName-$profile"
fun resetAppName(packageName: String, profile: Int) {
val editor = preferences.edit()
val key = "name$packageName-$profile"
editor.remove(key)
editor.apply()
}
fun setWeatherLocation(cont: Context, location: String, region: String?) {
val editor = cont.getSharedPreferences("weather_location", AppCompatActivity.MODE_PRIVATE).edit()
fun setWeatherLocation(location: String, region: String?) {
val editor = preferences.edit()
val key = "location"
val regionKey = "location_region"
editor.putString(key, location)
@ -69,20 +69,18 @@ class SharedPreferenceManager {
editor.apply()
}
fun getWeatherLocation(cont: Context) : String? {
val sharedPreferences = cont.getSharedPreferences("weather_location", AppCompatActivity.MODE_PRIVATE)
fun getWeatherLocation(): String? {
val key = "location"
return sharedPreferences.getString(key, "")
return preferences.getString(key, "")
}
fun getWeatherRegion(cont: Context) : String? {
val sharedPreferences = cont.getSharedPreferences("weather_location", AppCompatActivity.MODE_PRIVATE)
fun getWeatherRegion(): String? {
val key = "location_region"
return sharedPreferences.getString(key, "")
return preferences.getString(key, "")
}
fun setGestures(cont: Context, direction: String, appName: String?) {
val editor = cont.getSharedPreferences("gestures", AppCompatActivity.MODE_PRIVATE).edit()
fun setGestures(direction: String, appName: String?) {
val editor = preferences.edit()
val nameKey = "$direction-name"
editor.putString(nameKey, appName)
editor.apply()
@ -90,9 +88,8 @@ class SharedPreferenceManager {
fun getGestureName(cont: Context, direction: String) : String? {
val sharedPreferences = cont.getSharedPreferences("gestures", AppCompatActivity.MODE_PRIVATE)
val key = "$direction-name"
return sharedPreferences.getString(key, "")
return preferences.getString(key, "")
}
}

View file

@ -15,15 +15,15 @@ import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL
class WeatherSystem {
class WeatherSystem(private val context: Context) {
private val sharedPreferenceManager = SharedPreferenceManager()
private val sharedPreferenceManager = SharedPreferenceManager(context)
private val stringUtils = StringUtils()
suspend fun setGpsLocation(activity: MainActivity) {
val locationManager = activity.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
activity,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
@ -35,14 +35,14 @@ class WeatherSystem {
locationManager.getCurrentLocation(
LocationManager.GPS_PROVIDER, // Use GPS provider
null, // No cancellation signal
ContextCompat.getMainExecutor(activity)
ContextCompat.getMainExecutor(context)
)
{ location: Location? -> // Lambda expression for the callback
if (location != null) {
CoroutineScope(Dispatchers.IO).launch {
val latitude = location.latitude
val longitude = location.longitude
sharedPreferenceManager.setWeatherLocation(activity, "latitude=${latitude}&longitude=${longitude}", sharedPreferenceManager.getWeatherRegion(activity))
sharedPreferenceManager.setWeatherLocation("latitude=${latitude}&longitude=${longitude}", sharedPreferenceManager.getWeatherRegion())
activity.updateWeatherText()}
}
}
@ -82,13 +82,13 @@ class WeatherSystem {
return foundLocations
}
fun getTemp(context: Context) : String {
fun getTemp() : String {
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
val tempUnits = preferences.getString("tempUnits", "celsius")
var currentWeather = ""
val location = sharedPreferenceManager.getWeatherLocation(context)
val location = sharedPreferenceManager.getWeatherLocation()
if (location != null) {
if (location.isNotEmpty()) {