some tests

This commit is contained in:
Josia Pietsch 2025-03-14 18:06:48 +01:00
parent e02ca4091f
commit 5b74d106b0
Signed by: jrpie
GPG key ID: E70B571D66986A2D
3 changed files with 66 additions and 5 deletions

View file

@ -89,6 +89,33 @@ fun getUserFromId(userId: Int?, context: Context): UserHandle {
return profiles.firstOrNull { it.hashCode() == userId } ?: profiles[0]
}
@RequiresApi(Build.VERSION_CODES.R)
fun getAllShortcuts(context: Context): List<DetailedPinnedShortcutInfo> {
val launcherApps = context.getSystemService(Service.LAUNCHER_APPS_SERVICE) as LauncherApps
fun getShortcuts(profile: UserHandle): MutableList<ShortcutInfo>? {
return try {
launcherApps.getShortcuts(
ShortcutQuery().apply {
setQueryFlags((ShortcutQuery.FLAG_MATCH_PINNED
or ShortcutQuery.FLAG_MATCH_DYNAMIC
or ShortcutQuery.FLAG_MATCH_MANIFEST
or ShortcutQuery.FLAG_MATCH_PINNED_BY_ANY_LAUNCHER ))
},
profile
)
} catch (e: Exception) {
return null
}
}
val userManager = context.getSystemService(Service.USER_SERVICE) as UserManager
return userManager.userProfiles.filter { !userManager.isQuietModeEnabled(it) }
.mapNotNull { getShortcuts(it) }
.reduce {a, b -> a.addAll(b); a}
.map { s -> DetailedPinnedShortcutInfo(context, s)}
.toList()
}
@RequiresApi(Build.VERSION_CODES.N_MR1)
fun removeUnusedShortcuts(context: Context) {
val launcherApps = context.getSystemService(Service.LAUNCHER_APPS_SERVICE) as LauncherApps
@ -142,6 +169,7 @@ fun openTutorial(context: Context) {
/**
* Load all apps.
*/
@RequiresApi(Build.VERSION_CODES.R)
fun getApps(
packageManager: PackageManager,
context: Context
@ -200,6 +228,11 @@ fun getApps(
Log.i(LOG_TAG, "${loadList.size} apps loaded (${end - start}ms)")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
start = System.currentTimeMillis()
loadList.addAll(getAllShortcuts(context))
end = System.currentTimeMillis()
Log.i(LOG_TAG, "shortcuts loaded (${end - start}ms)")
/*
LauncherPreferences.apps().pinnedShortcuts()
?.mapNotNull { DetailedPinnedShortcutInfo.fromPinnedShortcutInfo(it, context) }
?.let {
@ -207,6 +240,8 @@ fun getApps(
Log.i(LOG_TAG, "${it.size} shortcuts loaded (${end - start}ms)")
loadList.addAll(it)
}
*/
}
return loadList

View file

@ -5,9 +5,15 @@ import android.content.Context
import android.content.pm.LauncherApps
import android.content.pm.ShortcutInfo
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable
import android.os.Build
import android.os.UserHandle
import android.view.Gravity
import androidx.annotation.GravityInt
import androidx.annotation.RequiresApi
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.DrawableCompat
import de.jrpie.android.launcher.R
import de.jrpie.android.launcher.actions.Action
import de.jrpie.android.launcher.actions.ShortcutAction
import de.jrpie.android.launcher.getUserFromId
@ -15,6 +21,7 @@ import de.jrpie.android.launcher.getUserFromId
@RequiresApi(Build.VERSION_CODES.N_MR1)
class DetailedPinnedShortcutInfo(
private val shortcutInfo: PinnedShortcutInfo,
private val appInfo: DetailedAppInfo?,
private val label: String,
private val icon: Drawable,
private val privateSpace: Boolean
@ -22,9 +29,10 @@ class DetailedPinnedShortcutInfo(
constructor(context: Context, shortcut: ShortcutInfo) : this(
PinnedShortcutInfo(shortcut),
shortcut.longLabel.toString(),
DetailedAppInfo.fromAppInfo(AppInfo(shortcut.`package`, shortcut.activity?.className, shortcut.userHandle.hashCode()), context),
(shortcut.longLabel ?: shortcut.shortLabel ?: shortcut.`package`).toString(),
(context.getSystemService(Service.LAUNCHER_APPS_SERVICE) as LauncherApps)
.getShortcutBadgedIconDrawable(shortcut, 0),
.getShortcutBadgedIconDrawable(shortcut, 0) ?: ResourcesCompat.getDrawable(context.resources, R.drawable.baseline_question_mark_24, context.theme)!!,
shortcut.userHandle == getPrivateSpaceUser(context)
)
@ -33,11 +41,25 @@ class DetailedPinnedShortcutInfo(
}
override fun getLabel(): String {
return label
if (appInfo == null) {
return label
}
// TODO different for pinned shortcuts
return "${appInfo.getLabel()}: $label"
}
override fun getIcon(context: Context): Drawable {
return icon
// TODO different for pinned shortcuts
if (appInfo == null ) {
return icon
}
val width = icon.intrinsicWidth
val height = icon.intrinsicHeight
return LayerDrawable(arrayOf(icon, appInfo.getIcon(context))).apply {
setLayerWidth(1,width / 2)
setLayerHeight(1,height / 2)
setLayerGravity(1, Gravity.TOP or Gravity.END)
}
}
override fun getUser(context: Context): UserHandle {

View file

@ -31,7 +31,11 @@ class PinnedShortcutInfo(
return try {
launcherApps.getShortcuts(
ShortcutQuery().apply {
setQueryFlags(ShortcutQuery.FLAG_MATCH_PINNED)
setQueryFlags(
ShortcutQuery.FLAG_MATCH_CACHED
or ShortcutQuery.FLAG_MATCH_DYNAMIC
or ShortcutQuery.FLAG_MATCH_MANIFEST
or ShortcutQuery.FLAG_MATCH_PINNED_BY_ANY_LAUNCHER )
setPackage(packageName)
setActivity(ComponentName(packageName, activityName))
setShortcutIds(listOf(id))