Add possibility to delete pinned shortcuts

This commit is contained in:
MM20 2022-08-05 13:16:26 +02:00
parent b957826227
commit 20f6acc389
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
3 changed files with 46 additions and 2 deletions

View File

@ -5,9 +5,9 @@ import android.content.pm.LauncherActivityInfo
import android.content.pm.LauncherApps
import android.content.pm.PackageManager
import android.os.Process
import android.util.Log
import androidx.core.content.getSystemService
import de.mm20.launcher2.ktx.normalize
import de.mm20.launcher2.ktx.romanize
import de.mm20.launcher2.permissions.PermissionGroup
import de.mm20.launcher2.permissions.PermissionsManager
import de.mm20.launcher2.preferences.LauncherDataStore
@ -28,6 +28,8 @@ interface AppShortcutRepository {
): List<AppShortcut>
fun search(query: String): Flow<List<AppShortcut>>
fun removePinnedShortcut(shortcut: AppShortcut)
}
internal class AppShortcutRepositoryImpl(
@ -127,6 +129,27 @@ internal class AppShortcutRepositoryImpl(
}
}
override fun removePinnedShortcut(shortcut: AppShortcut) {
val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
if (!launcherApps.hasShortcutHostPermission()) return
val pinnedShortcutsQuery = LauncherApps.ShortcutQuery().apply {
setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED)
}
val userHandle = shortcut.launcherShortcut.userHandle
val allPinned = launcherApps.getShortcuts(pinnedShortcutsQuery, userHandle)
if (allPinned == null) {
Log.e("MM20", "Could not remove shortcut ${shortcut.key}: shortcut query returned null")
return
}
launcherApps.pinShortcuts(
shortcut.launcherShortcut.`package`,
allPinned.filter { it.id != shortcut.launcherShortcut.id }.map { it.id },
userHandle
)
}
private fun matches(label: String, query: String): Boolean {
val labelLatin = label.normalize()

View File

@ -141,6 +141,14 @@ fun AppShortcutItem(
action = { edit = true }
))
if (viewModel.canDelete) {
toolbarActions.add(DefaultToolbarAction(
label = stringResource(R.string.menu_delete),
icon = Icons.Rounded.Delete,
action = { viewModel.deleteShortcut() }
))
}
val isHidden by viewModel.isHidden.collectAsState(false)
val hideAction = if (isHidden) {
DefaultToolbarAction(

View File

@ -4,11 +4,20 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import android.util.Log
import de.mm20.launcher2.appshortcuts.AppShortcutRepository
import de.mm20.launcher2.ktx.tryStartActivity
import de.mm20.launcher2.search.data.AppShortcut
import de.mm20.launcher2.ui.launcher.search.common.SearchableItemVM
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
class ShortcutItemVM(private val shortcut: AppShortcut) : SearchableItemVM(shortcut), KoinComponent {
private val shortcutRepository: AppShortcutRepository by inject()
val canDelete = shortcut.launcherShortcut.isPinned
class ShortcutItemVM(private val shortcut: AppShortcut) : SearchableItemVM(shortcut) {
fun openAppInfo(context: Context) {
context.tryStartActivity(
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
@ -17,4 +26,8 @@ class ShortcutItemVM(private val shortcut: AppShortcut) : SearchableItemVM(short
}
)
}
fun deleteShortcut() {
shortcutRepository.removePinnedShortcut(shortcut)
}
}