Added functionality for pinning apps. To-do: visuals

This commit is contained in:
ottoptj 2024-12-22 00:39:48 +02:00
commit 6a789fad6d
4 changed files with 74 additions and 2 deletions

View file

@ -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<TextView>(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

View file

@ -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<Pair<String, Int?>> {
val pinnedApps = mutableListOf<Pair<String, Int?>>()
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)
}

View file

@ -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<Triple<LauncherActivityInfo, UserHandle, Int>> {
!sharedPreferenceManager.isAppPinned(it.first.componentName.flattenToString(), it.third)
}.thenBy {
sharedPreferenceManager.getAppName(
it.first.componentName.flattenToString(),
it.third,
it.first.label
).toString().lowercase()
}
)
}
return sortedApps

View file

@ -83,6 +83,20 @@
android:gravity="center"
android:visibility="invisible">
<TextView
android:id="@+id/pin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/app_action_background"
android:paddingVertical="5dp"
android:singleLine="true"
android:text="@string/info"
android:textAlignment="center"
android:textAppearance="@android:style/TextAppearance.DeviceDefault"
android:textColor="#E3F3F3F3"
app:drawableTopCompat="@drawable/info_24px" />
<TextView
android:id="@+id/info"
android:layout_width="0dp"