Skip to content

Commit

Permalink
perf: highlight uninstalled apps in Apps
Browse files Browse the repository at this point in the history
  • Loading branch information
aistra0528 committed Jul 21, 2024
1 parent c619809 commit 17cae83
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
6 changes: 3 additions & 3 deletions app/src/main/kotlin/com/aistra/hail/app/AppInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class AppInfo(
val applicationInfo: ApplicationInfo? get() = HPackages.getApplicationInfoOrNull(packageName)
val name get() = applicationInfo?.loadLabel(app.packageManager) ?: packageName

var state: Int = STATE_UNKNOWN
var state: Int = STATE_NOT_FOUND
fun getCurrentState(): Int = when {
applicationInfo == null -> STATE_UNKNOWN
applicationInfo == null -> STATE_NOT_FOUND
AppManager.isAppFrozen(packageName) -> STATE_FROZEN
else -> STATE_UNFROZEN
}
Expand All @@ -26,7 +26,7 @@ class AppInfo(
override fun hashCode(): Int = packageName.hashCode()

companion object {
const val STATE_UNKNOWN = 0
const val STATE_NOT_FOUND = 0
const val STATE_UNFROZEN = 1
const val STATE_FROZEN = 2
}
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/kotlin/com/aistra/hail/ui/apps/AppsAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.widget.CompoundButton
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.aistra.hail.R
import com.aistra.hail.app.AppManager
import com.aistra.hail.app.HailData
import com.aistra.hail.databinding.ItemAppsBinding
Expand All @@ -22,9 +23,8 @@ class AppsAdapter : ListAdapter<ApplicationInfo, AppsAdapter.ViewHolder>(DIFF) {
oldItem: ApplicationInfo, newItem: ApplicationInfo
): Boolean = oldItem.packageName == newItem.packageName

override fun areContentsTheSame(
oldItem: ApplicationInfo, newItem: ApplicationInfo
): Boolean = areItemsTheSame(oldItem, newItem)
override fun areContentsTheSame(oldItem: ApplicationInfo, newItem: ApplicationInfo): Boolean =
oldItem.flags and ApplicationInfo.FLAG_INSTALLED == newItem.flags and ApplicationInfo.FLAG_INSTALLED
}
}

Expand Down Expand Up @@ -78,6 +78,8 @@ class AppsAdapter : ListAdapter<ApplicationInfo, AppsAdapter.ViewHolder>(DIFF) {
val name = info.loadLabel(context.packageManager)
text = if (!HailData.grayscaleIcon && frozen) "❄️$name" else name
isEnabled = !HailData.grayscaleIcon || !frozen
if (HPackages.isAppUninstalled(pkg)) setTextColor(context.getColorStateList(R.color.color_warn))
else setTextAppearance(com.google.android.material.R.style.TextAppearance_Material3_BodyMedium)
}
binding.appDesc.apply {
text = pkg
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/kotlin/com/aistra/hail/ui/apps/AppsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ class AppsFragment : MainFragment(), AppsAdapter.OnItemClickListener, AppsAdapte

R.id.action_extract_apk -> extractApk(pkg)
R.id.action_uninstall -> uninstallApp(name, pkg)
R.id.action_reinstall -> if (!AppManager.reinstallApp(pkg)) HUI.showToast(R.string.operation_failed, name)
R.id.action_reinstall -> {
if (AppManager.reinstallApp(pkg)) updateAppList()
else HUI.showToast(R.string.operation_failed, name)
}

else -> return super.onContextItemSelected(item)
}
return true
Expand All @@ -168,9 +172,7 @@ class AppsFragment : MainFragment(), AppsAdapter.OnItemClickListener, AppsAdapte

private fun uninstallApp(name: CharSequence, pkg: String) {
when {
HPackages.getApplicationInfoOrNull(
pkg, 0
) == null && !HPackages.isAppHidden(pkg) -> HUI.showToast(R.string.app_not_installed)
HPackages.isAppUninstalled(pkg) -> HUI.showToast(R.string.app_not_installed)

pkg == app.packageName -> {
when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class PagerAdapter(private val selectedList: List<AppInfo>) :
MaterialColors.getColor(this, androidx.appcompat.R.attr.colorPrimary)
)

info.state == AppInfo.STATE_UNKNOWN -> setTextColor(context.getColorStateList(R.color.color_warn))
info.state == AppInfo.STATE_NOT_FOUND -> setTextColor(context.getColorStateList(R.color.color_warn))
else -> setTextAppearance(com.google.android.material.R.style.TextAppearance_Material3_BodyMedium)
}
setTextSize(TypedValue.COMPLEX_UNIT_SP, HailData.homeFontSize.toFloat())
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/kotlin/com/aistra/hail/utils/HPackages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ object HPackages {
}
} ?: false

fun isAppUninstalled(packageName: String): Boolean =
getApplicationInfoOrNull(packageName)?.run { flags and ApplicationInfo.FLAG_INSTALLED != ApplicationInfo.FLAG_INSTALLED }
?: true

fun isPrivilegedApp(packageName: String): Boolean = getApplicationInfoOrNull(packageName)?.let {
(ApplicationInfo::class.java.getField("privateFlags").get(it) as Int) and 8 == 8
} ?: false
Expand Down
12 changes: 1 addition & 11 deletions app/src/main/kotlin/com/aistra/hail/utils/HShizuku.kt
Original file line number Diff line number Diff line change
Expand Up @@ -227,24 +227,14 @@ object HShizuku {
).intentSender
)
true
} else if (HTarget.Q) {
val pm = asInterface("android.content.pm.IPackageManager", "package")
HiddenApiBypass.invoke(
pm::class.java, pm, "setSystemAppInstallState", packageName, false, HPackages.myUserId
) as Boolean
} else execute("pm uninstall --user current $packageName").first == 0
}.getOrElse {
HLog.e(it)
false
}

fun reinstallApp(packageName: String): Boolean = runCatching {
if (HTarget.Q && !HPackages.canUninstallNormally(packageName)) {
val pm = asInterface("android.content.pm.IPackageManager", "package")
HiddenApiBypass.invoke(
pm::class.java, pm, "setSystemAppInstallState", packageName, true, HPackages.myUserId
) as Boolean
} else execute("pm install-existing --user current $packageName").first == 0
execute("pm install-existing --user current $packageName").first == 0
}.getOrElse {
HLog.e(it)
false
Expand Down

0 comments on commit 17cae83

Please sign in to comment.