@@ -4,8 +4,10 @@ import android.annotation.SuppressLint
44import android.app.Application
55import android.content.Context
66import android.content.Intent
7+ import android.content.pm.ApplicationInfo
78import android.content.pm.PackageInfo
89import android.content.pm.PackageManager
10+ import android.content.pm.PackageManager.ApplicationInfoFlags
911import android.content.pm.PackageManager.NameNotFoundException
1012import android.content.pm.PackageManager.PackageInfoFlags
1113import android.content.pm.Signature
@@ -41,6 +43,7 @@ class PM(
4143
4244 val application: Application get() = app
4345
46+ @Suppress(" DEPRECATION" )
4447 fun getPackageInfo (packageName : String , flags : Int = 0): PackageInfo ? =
4548 try {
4649 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU )
@@ -51,6 +54,17 @@ class PM(
5154 null
5255 }
5356
57+ @Suppress(" DEPRECATION" )
58+ fun getApplicationInfo (packageName : String , flags : Int = 0): ApplicationInfo ? =
59+ try {
60+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU )
61+ app.packageManager.getApplicationInfo(packageName, ApplicationInfoFlags .of(flags.toLong()))
62+ else
63+ app.packageManager.getApplicationInfo(packageName, flags)
64+ } catch (_: NameNotFoundException ) {
65+ null
66+ }
67+
5468 fun getPackageInfo (file : File ): PackageInfo ? {
5569 val path = file.absolutePath
5670 val flags = PackageManager .GET_META_DATA or PackageManager .GET_ACTIVITIES
@@ -212,6 +226,21 @@ class PM(
212226 }
213227}
214228
229+ fun File.sha256OrNull (): String? = runCatching {
230+ if (! isFile) return @runCatching null
231+ val digest = MessageDigest .getInstance(" SHA-256" )
232+ inputStream().use { input ->
233+ val buffer = ByteArray (DEFAULT_BUFFER_SIZE )
234+ while (! Thread .currentThread().isInterrupted) {
235+ val read = input.read(buffer)
236+ if (read < 0 ) break
237+ digest.update(buffer, 0 , read)
238+ }
239+ }
240+ if (Thread .currentThread().isInterrupted) return @runCatching null
241+ digest.digest().joinToString(" " ) { byte -> " %02x" .format(byte) }
242+ }.getOrNull()
243+
215244/* * Opens the system screen that lets the user grant the "install unknown apps" permission. */
216245object RequestInstallAppsContract : ActivityResultContract<String, Boolean>(), KoinComponent {
217246 private val pm: PM by inject()
0 commit comments