From 6a789fad6d880c1f4f67a06eb2811a3f525a2d6e Mon Sep 17 00:00:00 2001 From: ottoptj Date: Sun, 22 Dec 2024 00:39:48 +0200 Subject: [PATCH] Added functionality for pinning apps. To-do: visuals --- .../eu/ottop/yamlauncher/AppActionMenu.kt | 13 ++++++ .../settings/SharedPreferenceManager.kt | 41 +++++++++++++++++++ .../eu/ottop/yamlauncher/utils/AppUtils.kt | 8 +++- app/src/main/res/layout/app_item_layout.xml | 14 +++++++ 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt index 3e13e8b..4a0abb5 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt @@ -37,7 +37,16 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act userHandle: UserHandle, workProfile: Int ){ + ViewCompat.addAccessibilityAction(textView, activity.getString(R.string.accessibility_info)) { _, _ -> + appInfo(appActivity, userHandle) + true + } + actionMenu.findViewById(R.id.pin).setOnClickListener { + pinApp(appActivity, workProfile) + animations.fadeViewOut(actionMenu) + textView.visibility = View.VISIBLE + } ViewCompat.addAccessibilityAction(textView, activity.getString(R.string.accessibility_info)) { _, _ -> appInfo(appActivity, userHandle) @@ -85,6 +94,10 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act } } + private fun pinApp(appActivity: LauncherActivityInfo, workProfile: Int) { + sharedPreferenceManager.setPinnedApp(appActivity.componentName.flattenToString(), workProfile) + } + private fun appInfo( appActivity: LauncherActivityInfo?, userHandle: UserHandle diff --git a/app/src/main/java/eu/ottop/yamlauncher/settings/SharedPreferenceManager.kt b/app/src/main/java/eu/ottop/yamlauncher/settings/SharedPreferenceManager.kt index 77c3862..312a5a6 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/settings/SharedPreferenceManager.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/settings/SharedPreferenceManager.kt @@ -123,6 +123,47 @@ class SharedPreferenceManager (private val context: Context) { return preferences.getBoolean("lockShortcuts", false) } + fun setPinnedApp(componentName: String, profile: Int) { + val editor = preferences.edit() + println(isAppPinned(componentName, profile)) + val pinnedAppString = when (isAppPinned(componentName, profile)) { + true -> { + getPinnedAppString()?.replace("§section§$componentName§splitter§$profile", "") + } + false -> { + "${getPinnedAppString()}§section§$componentName§splitter§$profile" + } + } + + editor.putString( + "pinnedApps", + pinnedAppString + ) + editor.apply() + } + + private fun getPinnedAppString(): String? { + return preferences.getString("pinnedApps", "") + } + + private fun getPinnedApps(): List> { + val pinnedApps = mutableListOf>() + val pinnedAppsList = getPinnedAppString()?.split("§section§") + + pinnedAppsList?.forEach { + val app = it.split("§splitter§") + if (app.size > 1) { + pinnedApps.add(Pair(app[0], app[1].toIntOrNull())) + } + } + + return pinnedApps + } + + fun isAppPinned(componentName: String, profile: Int): Boolean { + return getPinnedApps().contains(Pair(componentName, profile)) + } + fun isBatteryEnabled(): Boolean { return preferences.getBoolean("batteryEnabled", false) } diff --git a/app/src/main/java/eu/ottop/yamlauncher/utils/AppUtils.kt b/app/src/main/java/eu/ottop/yamlauncher/utils/AppUtils.kt index 2248d9f..4fcd5c8 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/utils/AppUtils.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/utils/AppUtils.kt @@ -33,13 +33,17 @@ class AppUtils(private val context: Context, private val launcherApps: LauncherA } // Sort apps by name - sortedApps = allApps.sortedBy { + sortedApps = allApps.sortedWith( + compareBy> { + !sharedPreferenceManager.isAppPinned(it.first.componentName.flattenToString(), it.third) + }.thenBy { sharedPreferenceManager.getAppName( it.first.componentName.flattenToString(), it.third, it.first.label ).toString().lowercase() - } + } + ) } return sortedApps diff --git a/app/src/main/res/layout/app_item_layout.xml b/app/src/main/res/layout/app_item_layout.xml index d2eac0c..ccf0bec 100644 --- a/app/src/main/res/layout/app_item_layout.xml +++ b/app/src/main/res/layout/app_item_layout.xml @@ -83,6 +83,20 @@ android:gravity="center" android:visibility="invisible"> + +