mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-04-23 11:22:12 +00:00
Improve: Installer Cleanup
This commit is contained in:
parent
2158a7a102
commit
298c39c99a
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
|
||||
private enum class Action(
|
||||
val id: Int,
|
||||
val adapterAction: ProductAdapter.Action,
|
||||
val iconResId: Int
|
||||
val iconResId: Int,
|
||||
) {
|
||||
INSTALL(1, ProductAdapter.Action.INSTALL, R.drawable.ic_download),
|
||||
UPDATE(2, ProductAdapter.Action.UPDATE, R.drawable.ic_download),
|
||||
@ -65,7 +65,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
|
||||
|
||||
private class Installed(
|
||||
val installedItem: InstalledItem, val isSystem: Boolean,
|
||||
val launcherActivities: List<Pair<String, String>>
|
||||
val launcherActivities: List<Pair<String, String>>,
|
||||
)
|
||||
|
||||
val packageName: String
|
||||
@ -370,12 +370,12 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
|
||||
if (state is DownloadService.State.Success && isResumed) {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
state.consume()
|
||||
AppInstaller
|
||||
.getInstance(context)?.defaultInstaller?.install(
|
||||
"",
|
||||
state.release.cacheFileName
|
||||
)
|
||||
}
|
||||
AppInstaller
|
||||
.getInstance(context)?.defaultInstaller?.install(
|
||||
"",
|
||||
state.release.cacheFileName
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -397,7 +397,8 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
|
||||
override fun onActionClick(action: ProductAdapter.Action) {
|
||||
when (action) {
|
||||
ProductAdapter.Action.INSTALL,
|
||||
ProductAdapter.Action.UPDATE -> {
|
||||
ProductAdapter.Action.UPDATE,
|
||||
-> {
|
||||
val installedItem = installed?.installedItem
|
||||
startUpdate(packageName, installedItem, products, downloadConnection)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user