mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-06-16 04:19:19 +00:00
Improve: Installer Cleanup
This commit is contained in:
@ -1,18 +1,11 @@
|
||||
package com.looker.droidify.installer
|
||||
|
||||
import android.content.Context
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
|
||||
abstract class BaseInstaller(val context: Context) : InstallationEvents {
|
||||
|
||||
companion object {
|
||||
const val ROOT_INSTALL_PACKAGE = "cat %s | pm install --user %s -t -r -S %s"
|
||||
const val ROOT_UNINSTALL_PACKAGE = "pm uninstall --user %s %s"
|
||||
const val DELETE_PACKAGE = "%s rm %s"
|
||||
}
|
||||
|
||||
private val job = Job()
|
||||
val scope = CoroutineScope(Dispatchers.IO + job)
|
||||
}
|
@ -12,18 +12,15 @@ import java.io.File
|
||||
|
||||
class DefaultInstaller(context: Context) : BaseInstaller(context) {
|
||||
|
||||
override fun install(packageName: String, cacheFileName: String) {
|
||||
override suspend fun install(packageName: String, cacheFileName: String) {
|
||||
val cacheFile = Cache.getReleaseFile(context, cacheFileName)
|
||||
scope.launch { mDefaultInstaller(cacheFile) }
|
||||
mDefaultInstaller(cacheFile)
|
||||
}
|
||||
|
||||
override fun install(packageName: String, cacheFile: File) {
|
||||
scope.launch { mDefaultInstaller(cacheFile) }
|
||||
}
|
||||
override suspend fun install(packageName: String, cacheFile: File) =
|
||||
mDefaultInstaller(cacheFile)
|
||||
|
||||
override fun uninstall(packageName: String) {
|
||||
scope.launch { mDefaultUninstaller(packageName) }
|
||||
}
|
||||
override suspend fun uninstall(packageName: String) = mDefaultUninstaller(packageName)
|
||||
|
||||
private suspend fun mDefaultInstaller(cacheFile: File) {
|
||||
val (uri, flags) = if (Android.sdk(24)) {
|
||||
@ -37,17 +34,21 @@ class DefaultInstaller(context: Context) : BaseInstaller(context) {
|
||||
// TODO Handle deprecation
|
||||
@Suppress("DEPRECATION")
|
||||
withContext(Dispatchers.IO) {
|
||||
context.startActivity(
|
||||
Intent(Intent.ACTION_INSTALL_PACKAGE)
|
||||
.setDataAndType(uri, "application/vnd.android.package-archive").setFlags(flags)
|
||||
)
|
||||
launch {
|
||||
context.startActivity(
|
||||
Intent(Intent.ACTION_INSTALL_PACKAGE)
|
||||
.setDataAndType(uri, "application/vnd.android.package-archive")
|
||||
.setFlags(flags)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun mDefaultUninstaller(packageName: String) {
|
||||
private suspend fun mDefaultUninstaller(packageName: String) {
|
||||
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 {
|
||||
@ -55,6 +56,6 @@ class DefaultInstaller(context: Context) : BaseInstaller(context) {
|
||||
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true)
|
||||
}
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
context.startActivity(intent)
|
||||
withContext(Dispatchers.IO) { launch { context.startActivity(intent) } }
|
||||
}
|
||||
}
|
@ -3,9 +3,9 @@ package com.looker.droidify.installer
|
||||
import java.io.File
|
||||
|
||||
interface InstallationEvents {
|
||||
fun install(packageName: String, cacheFileName: String)
|
||||
suspend fun install(packageName: String, cacheFileName: String)
|
||||
|
||||
fun install(packageName: String, cacheFile: File)
|
||||
suspend fun install(packageName: String, cacheFile: File)
|
||||
|
||||
fun uninstall(packageName: String)
|
||||
suspend fun uninstall(packageName: String)
|
||||
}
|
@ -13,20 +13,16 @@ import java.io.File
|
||||
|
||||
class RootInstaller(context: Context) : BaseInstaller(context) {
|
||||
|
||||
override fun install(packageName: String, cacheFileName: String) {
|
||||
override suspend fun install(packageName: String, cacheFileName: String) {
|
||||
val cacheFile = Cache.getReleaseFile(context, cacheFileName)
|
||||
scope.launch { mRootInstaller(cacheFile) }
|
||||
mRootInstaller(cacheFile)
|
||||
}
|
||||
|
||||
override fun install(packageName: String, cacheFile: File) {
|
||||
scope.launch { mRootInstaller(cacheFile) }
|
||||
}
|
||||
override suspend fun install(packageName: String, cacheFile: File) = mRootInstaller(cacheFile)
|
||||
|
||||
override fun uninstall(packageName: String) {
|
||||
scope.launch { mRootUninstaller(packageName) }
|
||||
}
|
||||
override suspend fun uninstall(packageName: String) = mRootUninstaller(packageName)
|
||||
|
||||
private fun mRootInstaller(cacheFile: File) {
|
||||
private suspend fun mRootInstaller(cacheFile: File) {
|
||||
if (rootInstallerEnabled) {
|
||||
val installCommand =
|
||||
String.format(
|
||||
@ -41,10 +37,12 @@ class RootInstaller(context: Context) : BaseInstaller(context) {
|
||||
getUtilBoxPath,
|
||||
cacheFile.absolutePath.quote
|
||||
)
|
||||
scope.launch {
|
||||
Shell.su(installCommand).submit {
|
||||
if (it.isSuccess) {
|
||||
Shell.su(deleteCommand).submit()
|
||||
withContext(Dispatchers.IO) {
|
||||
launch {
|
||||
Shell.su(installCommand).submit {
|
||||
if (it.isSuccess) {
|
||||
Shell.su(deleteCommand).submit()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user