2
0

App.kt 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package org.mjdev.desktop.data
  2. import android.annotation.SuppressLint
  3. import android.content.ComponentName
  4. import android.content.Context
  5. import android.content.Intent
  6. import android.content.pm.ApplicationInfo
  7. import android.content.pm.LauncherActivityInfo
  8. import android.content.pm.PackageInfo
  9. import android.os.Build
  10. import androidx.annotation.RequiresApi
  11. import org.mjdev.desktop.extensions.JSonExt.toJson
  12. import org.mjdev.desktop.interfaces.IApp
  13. @Suppress("unused")
  14. class App(
  15. private val context: Context?,
  16. private val info: LauncherActivityInfo,
  17. override val name: String = info.label.toString(),
  18. override val fullAppName: String = info.label.toString(),
  19. override val comment: String = info.applicationInfo.packageName,
  20. override val cmd: String = info.applicationInfo.packageName, // todo intent
  21. override val fullTextString: String = info.toJson(),
  22. override val categories: List<Category> = listOf(guessCategory(context, info))
  23. ) : IApp {
  24. override var isStarting: Boolean = false
  25. override var isRunning: Boolean = false
  26. override suspend fun start() {
  27. context?.startActivity(Intent(
  28. Intent.ACTION_MAIN
  29. ).apply {
  30. addCategory(Intent.CATEGORY_LAUNCHER)
  31. setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
  32. setComponent(info.componentName)
  33. })
  34. }
  35. override fun toString(): String = name
  36. private fun PackageInfo.isSystemPackage(): Boolean {
  37. return ((applicationInfo?.flags ?: 0) and ApplicationInfo.FLAG_SYSTEM) != 0
  38. }
  39. companion object {
  40. @SuppressLint("NewApi")
  41. private fun guessCategory(
  42. context: Context?,
  43. info: LauncherActivityInfo
  44. ): Category {
  45. return try {
  46. ApplicationInfo.getCategoryTitle(
  47. context,
  48. info.applicationInfo.category
  49. )?.let { Category(it.toString()) } ?: Category.Empty
  50. } catch (e: Throwable) {
  51. Category.Empty
  52. }
  53. }
  54. }
  55. }