From 298c39c99af8dedb4b80ccee5dfb4a06b8ca1e4d Mon Sep 17 00:00:00 2001 From: LooKeR Date: Tue, 9 Nov 2021 10:56:15 +0530 Subject: [PATCH] Improve: Installer Cleanup --- .../droidify/installer/BaseInstaller.kt | 7 ----- .../droidify/installer/DefaultInstaller.kt | 29 ++++++++++--------- .../droidify/installer/InstallationEvents.kt | 6 ++-- .../droidify/installer/RootInstaller.kt | 24 +++++++-------- .../looker/droidify/screen/ProductFragment.kt | 17 ++++++----- 5 files changed, 38 insertions(+), 45 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/installer/BaseInstaller.kt b/src/main/kotlin/com/looker/droidify/installer/BaseInstaller.kt index 26387d06..68c14d1d 100644 --- a/src/main/kotlin/com/looker/droidify/installer/BaseInstaller.kt +++ b/src/main/kotlin/com/looker/droidify/installer/BaseInstaller.kt @@ -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) } \ No newline at end of file diff --git a/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt b/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt index 14bc9f55..49787bac 100644 --- a/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt +++ b/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt @@ -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) } } } } \ No newline at end of file diff --git a/src/main/kotlin/com/looker/droidify/installer/InstallationEvents.kt b/src/main/kotlin/com/looker/droidify/installer/InstallationEvents.kt index 106da4c3..38c349c6 100644 --- a/src/main/kotlin/com/looker/droidify/installer/InstallationEvents.kt +++ b/src/main/kotlin/com/looker/droidify/installer/InstallationEvents.kt @@ -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) } \ No newline at end of file diff --git a/src/main/kotlin/com/looker/droidify/installer/RootInstaller.kt b/src/main/kotlin/com/looker/droidify/installer/RootInstaller.kt index f9049dd1..6bc4c423 100644 --- a/src/main/kotlin/com/looker/droidify/installer/RootInstaller.kt +++ b/src/main/kotlin/com/looker/droidify/installer/RootInstaller.kt @@ -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() + } } } } diff --git a/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt b/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt index 97cadc8e..fff16319 100644 --- a/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt +++ b/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt @@ -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> + val launcherActivities: List>, ) 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) }