mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
105 lines
4.6 KiB
Kotlin
105 lines
4.6 KiB
Kotlin
package com.droiddeck.launcher
|
|
|
|
import android.app.ActivityOptions
|
|
import android.app.role.RoleManager
|
|
import android.graphics.Bitmap
|
|
import android.content.ComponentName
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.content.pm.PackageManager
|
|
import android.os.Build
|
|
import android.provider.Settings
|
|
import androidx.core.graphics.drawable.toBitmap
|
|
import com.droiddeck.launcher.session.SessionService
|
|
import com.droiddeck.launcher.session.SessionState
|
|
|
|
/** Android Home role and the launchable Android apps shown by the in-session app drawer. */
|
|
object HomeApp {
|
|
data class LaunchableApp(
|
|
val label: String,
|
|
val packageName: String,
|
|
val className: String,
|
|
val icon: Bitmap?,
|
|
)
|
|
|
|
fun isDefault(context: Context): Boolean {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
val roles = context.getSystemService(RoleManager::class.java)
|
|
if (roles?.isRoleAvailable(RoleManager.ROLE_HOME) == true) {
|
|
return roles.isRoleHeld(RoleManager.ROLE_HOME)
|
|
}
|
|
}
|
|
val home = Intent(Intent.ACTION_MAIN)
|
|
.addCategory(Intent.CATEGORY_HOME)
|
|
.addCategory(Intent.CATEGORY_DEFAULT)
|
|
return context.packageManager.resolveActivity(home, PackageManager.MATCH_DEFAULT_ONLY)
|
|
?.activityInfo?.packageName == context.packageName
|
|
}
|
|
|
|
fun defaultLabel(context: Context): String? {
|
|
val pm = context.packageManager
|
|
val home = Intent(Intent.ACTION_MAIN)
|
|
.addCategory(Intent.CATEGORY_HOME)
|
|
.addCategory(Intent.CATEGORY_DEFAULT)
|
|
val resolved = pm.resolveActivity(home, PackageManager.MATCH_DEFAULT_ONLY) ?: return null
|
|
val activity = resolved.activityInfo ?: return null
|
|
val label = runCatching { pm.getApplicationLabel(activity.applicationInfo).toString() }.getOrNull()
|
|
return label?.trim()?.takeIf { it.isNotEmpty() }
|
|
?: resolved.loadLabel(pm)?.toString()?.trim()?.takeIf { it.isNotEmpty() }
|
|
}
|
|
|
|
fun roleRequestIntent(context: Context): Intent? {
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) return null
|
|
val roles = context.getSystemService(RoleManager::class.java) ?: return null
|
|
if (!roles.isRoleAvailable(RoleManager.ROLE_HOME) || roles.isRoleHeld(RoleManager.ROLE_HOME)) return null
|
|
return roles.createRequestRoleIntent(RoleManager.ROLE_HOME)
|
|
}
|
|
|
|
fun settingsIntent(): Intent = Intent(Settings.ACTION_HOME_SETTINGS)
|
|
|
|
fun launchableApps(context: Context): List<LaunchableApp> {
|
|
val pm = context.packageManager
|
|
val launch = Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)
|
|
val apps = pm.queryIntentActivities(launch, 0)
|
|
.asSequence()
|
|
.mapNotNull { result ->
|
|
val activity = result.activityInfo ?: return@mapNotNull null
|
|
if (!activity.exported || activity.packageName == context.packageName) return@mapNotNull null
|
|
val label = result.loadLabel(pm)?.toString()?.trim().orEmpty().ifEmpty { activity.packageName }
|
|
val icon = runCatching { result.loadIcon(pm).toBitmap(48, 48) }.getOrNull()
|
|
LaunchableApp(label, activity.packageName, activity.name, icon)
|
|
}
|
|
.distinctBy { it.packageName }
|
|
.toList()
|
|
return apps.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.label })
|
|
}
|
|
|
|
fun launch(context: Context, app: LaunchableApp, displayId: Int? = null) {
|
|
val intent = Intent(Intent.ACTION_MAIN)
|
|
.addCategory(Intent.CATEGORY_LAUNCHER)
|
|
.setComponent(ComponentName(app.packageName, app.className))
|
|
if (displayId == null) {
|
|
context.startActivity(intent)
|
|
return
|
|
}
|
|
// Keep the target app's task separate from DroidDeck. Without this, Android can put the
|
|
// app on top of the caller's task and move the entire task to the chosen display.
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
val options = ActivityOptions.makeBasic().setLaunchDisplayId(displayId)
|
|
context.startActivity(intent, options.toBundle())
|
|
}
|
|
|
|
fun isActiveSteamSession(): Boolean =
|
|
SessionState.running && SessionState.mode == SessionService.MODE_STEAM
|
|
|
|
fun openSystemHomeSettings(context: Context) {
|
|
try {
|
|
context.startActivity(settingsIntent())
|
|
} catch (_: Exception) {
|
|
// A role request remains available on Android 10+ if the settings page is missing.
|
|
val request = roleRequestIntent(context)
|
|
if (request != null) context.startActivity(request)
|
|
}
|
|
}
|
|
}
|