Improve: Installer Cleanup

This commit is contained in:
LooKeR 2021-11-09 10:56:15 +05:30
parent 2158a7a102
commit 298c39c99a
5 changed files with 38 additions and 45 deletions

View File

@ -1,18 +1,11 @@
package com.looker.droidify.installer package com.looker.droidify.installer
import android.content.Context import android.content.Context
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
abstract class BaseInstaller(val context: Context) : InstallationEvents { abstract class BaseInstaller(val context: Context) : InstallationEvents {
companion object { companion object {
const val ROOT_INSTALL_PACKAGE = "cat %s | pm install --user %s -t -r -S %s" 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 ROOT_UNINSTALL_PACKAGE = "pm uninstall --user %s %s"
const val DELETE_PACKAGE = "%s rm %s" const val DELETE_PACKAGE = "%s rm %s"
} }
private val job = Job()
val scope = CoroutineScope(Dispatchers.IO + job)
} }

View File

@ -12,18 +12,15 @@ import java.io.File
class DefaultInstaller(context: Context) : BaseInstaller(context) { 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) val cacheFile = Cache.getReleaseFile(context, cacheFileName)
scope.launch { mDefaultInstaller(cacheFile) } mDefaultInstaller(cacheFile)
} }
override fun install(packageName: String, cacheFile: File) { override suspend fun install(packageName: String, cacheFile: File) =
scope.launch { mDefaultInstaller(cacheFile) } mDefaultInstaller(cacheFile)
}
override fun uninstall(packageName: String) { override suspend fun uninstall(packageName: String) = mDefaultUninstaller(packageName)
scope.launch { mDefaultUninstaller(packageName) }
}
private suspend fun mDefaultInstaller(cacheFile: File) { private suspend fun mDefaultInstaller(cacheFile: File) {
val (uri, flags) = if (Android.sdk(24)) { val (uri, flags) = if (Android.sdk(24)) {
@ -37,17 +34,21 @@ class DefaultInstaller(context: Context) : BaseInstaller(context) {
// TODO Handle deprecation // TODO Handle deprecation
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
context.startActivity( launch {
Intent(Intent.ACTION_INSTALL_PACKAGE) context.startActivity(
.setDataAndType(uri, "application/vnd.android.package-archive").setFlags(flags) 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 uri = Uri.fromParts("package", packageName, null)
val intent = Intent() val intent = Intent()
intent.data = uri intent.data = uri
@Suppress("DEPRECATION")
if (Android.sdk(28)) { if (Android.sdk(28)) {
intent.action = Intent.ACTION_DELETE intent.action = Intent.ACTION_DELETE
} else { } else {
@ -55,6 +56,6 @@ class DefaultInstaller(context: Context) : BaseInstaller(context) {
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true) intent.putExtra(Intent.EXTRA_RETURN_RESULT, true)
} }
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent) withContext(Dispatchers.IO) { launch { context.startActivity(intent) } }
} }
} }

View File

@ -3,9 +3,9 @@ package com.looker.droidify.installer
import java.io.File import java.io.File
interface InstallationEvents { 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)
} }

View File

@ -13,20 +13,16 @@ import java.io.File
class RootInstaller(context: Context) : BaseInstaller(context) { 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) val cacheFile = Cache.getReleaseFile(context, cacheFileName)
scope.launch { mRootInstaller(cacheFile) } mRootInstaller(cacheFile)
} }
override fun install(packageName: String, cacheFile: File) { override suspend fun install(packageName: String, cacheFile: File) = mRootInstaller(cacheFile)
scope.launch { mRootInstaller(cacheFile) }
}
override fun uninstall(packageName: String) { override suspend fun uninstall(packageName: String) = mRootUninstaller(packageName)
scope.launch { mRootUninstaller(packageName) }
}
private fun mRootInstaller(cacheFile: File) { private suspend fun mRootInstaller(cacheFile: File) {
if (rootInstallerEnabled) { if (rootInstallerEnabled) {
val installCommand = val installCommand =
String.format( String.format(
@ -41,10 +37,12 @@ class RootInstaller(context: Context) : BaseInstaller(context) {
getUtilBoxPath, getUtilBoxPath,
cacheFile.absolutePath.quote cacheFile.absolutePath.quote
) )
scope.launch { withContext(Dispatchers.IO) {
Shell.su(installCommand).submit { launch {
if (it.isSuccess) { Shell.su(installCommand).submit {
Shell.su(deleteCommand).submit() if (it.isSuccess) {
Shell.su(deleteCommand).submit()
}
} }
} }
} }

View File

@ -53,7 +53,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
private enum class Action( private enum class Action(
val id: Int, val id: Int,
val adapterAction: ProductAdapter.Action, val adapterAction: ProductAdapter.Action,
val iconResId: Int val iconResId: Int,
) { ) {
INSTALL(1, ProductAdapter.Action.INSTALL, R.drawable.ic_download), INSTALL(1, ProductAdapter.Action.INSTALL, R.drawable.ic_download),
UPDATE(2, ProductAdapter.Action.UPDATE, 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( private class Installed(
val installedItem: InstalledItem, val isSystem: Boolean, val installedItem: InstalledItem, val isSystem: Boolean,
val launcherActivities: List<Pair<String, String>> val launcherActivities: List<Pair<String, String>>,
) )
val packageName: String val packageName: String
@ -370,12 +370,12 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
if (state is DownloadService.State.Success && isResumed) { if (state is DownloadService.State.Success && isResumed) {
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
state.consume() 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) { override fun onActionClick(action: ProductAdapter.Action) {
when (action) { when (action) {
ProductAdapter.Action.INSTALL, ProductAdapter.Action.INSTALL,
ProductAdapter.Action.UPDATE -> { ProductAdapter.Action.UPDATE,
-> {
val installedItem = installed?.installedItem val installedItem = installed?.installedItem
startUpdate(packageName, installedItem, products, downloadConnection) startUpdate(packageName, installedItem, products, downloadConnection)
} }