mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-08-03 00:51:59 +00:00
Improve: Dispatcher.IO is for IO so use Default for non IO
Improve: Code cleanup
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
package com.looker.droidify.installer
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageInstaller.SessionParams
|
||||
import android.net.Uri
|
||||
import com.looker.droidify.content.Cache
|
||||
import com.looker.droidify.utility.extension.android.Android
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -26,48 +25,38 @@ class DefaultInstaller(context: Context) : BaseInstaller(context) {
|
||||
|
||||
override suspend fun uninstall(packageName: String) = mDefaultUninstaller(packageName)
|
||||
|
||||
private fun mDefaultInstaller(cacheFile: File) {
|
||||
val sessionInstaller = context.packageManager.packageInstaller
|
||||
val sessionParams =
|
||||
SessionParams(SessionParams.MODE_FULL_INSTALL)
|
||||
|
||||
if (Android.sdk(31)) {
|
||||
sessionParams.setRequireUserAction(SessionParams.USER_ACTION_NOT_REQUIRED)
|
||||
private suspend fun mDefaultInstaller(cacheFile: File) {
|
||||
val (uri, flags) = if (Android.sdk(24)) {
|
||||
Pair(
|
||||
Cache.getReleaseUri(context, cacheFile.name),
|
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION
|
||||
)
|
||||
} else {
|
||||
Pair(Uri.fromFile(cacheFile), 0)
|
||||
}
|
||||
|
||||
val id = sessionInstaller.createSession(sessionParams)
|
||||
|
||||
val session = sessionInstaller.openSession(id)
|
||||
|
||||
session.use { activeSession ->
|
||||
activeSession.openWrite("package", 0, cacheFile.length()).use { packageStream ->
|
||||
cacheFile.inputStream().use { fileStream ->
|
||||
fileStream.copyTo(packageStream)
|
||||
}
|
||||
}
|
||||
|
||||
val intent = Intent(context, InstallerService::class.java)
|
||||
|
||||
val flags = if (Android.sdk(31)) PendingIntent.FLAG_MUTABLE else 0
|
||||
|
||||
val pendingIntent = PendingIntent.getService(context, id, intent, flags)
|
||||
|
||||
session.commit(pendingIntent.intentSender)
|
||||
// TODO Handle deprecation
|
||||
@Suppress("DEPRECATION")
|
||||
withContext(Dispatchers.Default) {
|
||||
context.startActivity(
|
||||
Intent(Intent.ACTION_INSTALL_PACKAGE)
|
||||
.setDataAndType(uri, "application/vnd.android.package-archive")
|
||||
.setFlags(flags)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun mDefaultUninstaller(packageName: String) {
|
||||
val sessionInstaller = context.packageManager.packageInstaller
|
||||
|
||||
val intent = Intent(context, InstallerService::class.java)
|
||||
intent.putExtra(InstallerService.KEY_ACTION, InstallerService.ACTION_UNINSTALL)
|
||||
|
||||
val flags = if (Android.sdk(31)) PendingIntent.FLAG_MUTABLE else 0
|
||||
|
||||
val pendingIntent = PendingIntent.getService(context, -1, intent, flags)
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
sessionInstaller.uninstall(packageName, pendingIntent.intentSender)
|
||||
val uri = Uri.fromParts("package", packageName, null)
|
||||
val intent = Intent()
|
||||
intent.data = uri
|
||||
@Suppress("DEPRECATION")
|
||||
if (Android.sdk(28)) {
|
||||
intent.action = Intent.ACTION_DELETE
|
||||
} else {
|
||||
intent.action = Intent.ACTION_UNINSTALL_PACKAGE
|
||||
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true)
|
||||
}
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
withContext(Dispatchers.Default) { context.startActivity(intent) }
|
||||
}
|
||||
}
|
@@ -1,7 +1,6 @@
|
||||
package com.looker.droidify.installer
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.looker.droidify.content.Cache
|
||||
import com.topjohnwu.superuser.Shell
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -37,7 +36,7 @@ class RootInstaller(context: Context) : BaseInstaller(context) {
|
||||
getUtilBoxPath,
|
||||
cacheFile.absolutePath.quote
|
||||
)
|
||||
withContext(Dispatchers.IO) {
|
||||
withContext(Dispatchers.Default) {
|
||||
Shell.su(installCommand).submit { if (it.isSuccess) Shell.su(deleteCommand).submit() }
|
||||
}
|
||||
}
|
||||
@@ -45,7 +44,7 @@ class RootInstaller(context: Context) : BaseInstaller(context) {
|
||||
private suspend fun mRootUninstaller(packageName: String) {
|
||||
val uninstallCommand =
|
||||
String.format(ROOT_UNINSTALL_PACKAGE, getCurrentUserState, packageName)
|
||||
withContext(Dispatchers.IO) { Shell.su(uninstallCommand).submit() }
|
||||
withContext(Dispatchers.Default) { Shell.su(uninstallCommand).submit() }
|
||||
}
|
||||
|
||||
private val getCurrentUserState: String =
|
||||
|
Reference in New Issue
Block a user