Don't unpin shortcuts when they are unavailable

This commit is contained in:
MM20
2023-10-13 15:03:01 +02:00
parent ad5772b3db
commit d797263c0a
6 changed files with 123 additions and 6 deletions
@@ -13,6 +13,7 @@ import de.mm20.launcher2.search.SearchableDeserializer
import de.mm20.launcher2.search.SearchableSerializer
import de.mm20.launcher2.search.data.LauncherShortcut
import de.mm20.launcher2.search.data.LegacyShortcut
import de.mm20.launcher2.search.data.UnavailableShortcut
import org.json.JSONObject
import org.koin.core.component.KoinComponent
@@ -38,12 +39,16 @@ class LauncherShortcutDeserializer(
override fun deserialize(serialized: String): SavableSearchable? {
val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
if (!launcherApps.hasShortcutHostPermission()) return null
val json = JSONObject(serialized)
val packageName = json.getString("packagename")
val id = json.getString("id")
val userSerial = json.optLong("user")
if (!launcherApps.hasShortcutHostPermission()) {
return UnavailableShortcut(context, id, packageName, userSerial)
}
else {
val json = JSONObject(serialized)
val packageName = json.getString("packagename")
val id = json.getString("id")
val userSerial = json.optLong("user")
val query = LauncherApps.ShortcutQuery()
query.setPackage(packageName)
query.setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED or
@@ -0,0 +1,77 @@
package de.mm20.launcher2.search.data
import android.content.Context
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Process
import android.os.UserManager
import de.mm20.launcher2.appshortcuts.R
import de.mm20.launcher2.icons.ColorLayer
import de.mm20.launcher2.icons.StaticLauncherIcon
import de.mm20.launcher2.icons.TintedIconLayer
import de.mm20.launcher2.search.SavableSearchable
/**
* Shortcut class that is used when a [LauncherShortcut] is not available, e.g. missing permissions
* when Kvaesitso is not set as default launcher.
*/
class UnavailableShortcut(
override val label: String,
override val appName: String?,
val packageName: String,
val shortcutId: String,
val isMainProfile: Boolean,
val userSerial: Long,
): AppShortcut {
override val key: String
get() = if (isMainProfile) {
"$domain://${packageName}/${shortcutId}"
} else {
"$domain://${packageName}/${shortcutId}:userSerial"
}
override val labelOverride: String?
get() = null
override fun getPlaceholderIcon(context: Context): StaticLauncherIcon {
return StaticLauncherIcon(
foregroundLayer = TintedIconLayer(
icon = context.getDrawable(R.drawable.ic_file_android)!!,
color = 0xFF333333.toInt()
),
backgroundLayer = ColorLayer(0xFF333333.toInt()),
)
}
override val domain: String
get() = LauncherShortcut.Domain
override fun overrideLabel(label: String): SavableSearchable {
return this
}
override fun launch(context: Context, options: Bundle?): Boolean {
return false
}
companion object {
internal operator fun invoke(context: Context, id: String, packageName: String, userSerial: Long): UnavailableShortcut? {
val appInfo = try {
context.packageManager.getApplicationInfo(packageName, 0)
} catch (e: PackageManager.NameNotFoundException) {
return null
}
val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager
return UnavailableShortcut(
label = context.getString(R.string.shortcut_label_unavailable),
appName = appInfo.loadLabel(context.packageManager).toString(),
packageName = packageName,
shortcutId = id,
isMainProfile = userManager.getUserForSerialNumber(userSerial) == Process.myUserHandle(),
userSerial = userSerial,
)
}
}
}