From c2360f492adf3ab25bdf47715c07669cec3d3376 Mon Sep 17 00:00:00 2001 From: LooKeR Date: Thu, 18 Nov 2021 11:59:04 +0530 Subject: [PATCH] Fix: Freeze with Root Installer --- .../looker/droidify/installer/DefaultInstaller.kt | 15 ++++++--------- .../looker/droidify/installer/RootInstaller.kt | 10 ++-------- .../com/looker/droidify/screen/ProductFragment.kt | 11 ++++++----- .../com/looker/droidify/screen/ScreenActivity.kt | 8 ++++---- 4 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt b/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt index 35e82526..60058142 100644 --- a/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt +++ b/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt @@ -6,7 +6,6 @@ import android.net.Uri import com.looker.droidify.content.Cache import com.looker.droidify.utility.extension.android.Android import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.File @@ -38,13 +37,11 @@ class DefaultInstaller(context: Context) : BaseInstaller(context) { // TODO Handle deprecation @Suppress("DEPRECATION") withContext(Dispatchers.IO) { - launch { - context.startActivity( - Intent(Intent.ACTION_INSTALL_PACKAGE) - .setDataAndType(uri, "application/vnd.android.package-archive") - .setFlags(flags) - ) - } + context.startActivity( + Intent(Intent.ACTION_INSTALL_PACKAGE) + .setDataAndType(uri, "application/vnd.android.package-archive") + .setFlags(flags) + ) } } @@ -60,6 +57,6 @@ class DefaultInstaller(context: Context) : BaseInstaller(context) { intent.putExtra(Intent.EXTRA_RETURN_RESULT, true) } intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - withContext(Dispatchers.IO) { launch { context.startActivity(intent) } } + withContext(Dispatchers.IO) { context.startActivity(intent) } } } \ 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 1717efac..6af458eb 100644 --- a/src/main/kotlin/com/looker/droidify/installer/RootInstaller.kt +++ b/src/main/kotlin/com/looker/droidify/installer/RootInstaller.kt @@ -5,7 +5,6 @@ import android.util.Log import com.looker.droidify.content.Cache import com.topjohnwu.superuser.Shell import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.File @@ -25,7 +24,6 @@ class RootInstaller(context: Context) : BaseInstaller(context) { override suspend fun uninstall(packageName: String) = mRootUninstaller(packageName) private suspend fun mRootInstaller(cacheFile: File) { - Log.e("UserID", getCurrentUserState) val installCommand = String.format( ROOT_INSTALL_PACKAGE, @@ -40,18 +38,14 @@ class RootInstaller(context: Context) : BaseInstaller(context) { cacheFile.absolutePath.quote ) withContext(Dispatchers.IO) { - launch { - Shell.su(installCommand).submit { - if (it.isSuccess) Shell.su(deleteCommand).submit() - } - } + Shell.su(installCommand).submit { if (it.isSuccess) Shell.su(deleteCommand).submit() } } } private suspend fun mRootUninstaller(packageName: String) { val uninstallCommand = String.format(ROOT_UNINSTALL_PACKAGE, getCurrentUserState, packageName) - withContext(Dispatchers.IO) { launch { Shell.su(uninstallCommand).submit() } } + withContext(Dispatchers.IO) { Shell.su(uninstallCommand).submit() } } private val getCurrentUserState: String = diff --git a/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt b/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt index 6043b6a7..71f335a4 100644 --- a/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt +++ b/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt @@ -19,6 +19,7 @@ import com.looker.droidify.R import com.looker.droidify.content.ProductPreferences import com.looker.droidify.database.Database import com.looker.droidify.entity.* +import com.looker.droidify.installer.AppInstaller import com.looker.droidify.service.Connection import com.looker.droidify.service.DownloadService import com.looker.droidify.utility.RxUtils @@ -50,7 +51,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { private enum class Action( val id: Int, - val adapterAction: ProductAdapter.Action + val adapterAction: ProductAdapter.Action, ) { INSTALL(1, ProductAdapter.Action.INSTALL), UPDATE(2, ProductAdapter.Action.UPDATE), @@ -356,9 +357,9 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { } (recyclerView?.adapter as? ProductAdapter)?.setStatus(status) if (state is DownloadService.State.Success && isResumed) { - lifecycleScope.launch(Dispatchers.Default) { + lifecycleScope.launch(Dispatchers.IO) { state.consume() - screenActivity.defaultInstaller?.install(state.release.cacheFileName) + AppInstaller.getInstance(context)?.defaultInstaller?.install(state.release.cacheFileName) } } } @@ -405,8 +406,8 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { ) } ProductAdapter.Action.UNINSTALL -> { - lifecycleScope.launch(Dispatchers.Default) { - screenActivity.defaultInstaller?.uninstall(packageName) + lifecycleScope.launch(Dispatchers.IO) { + AppInstaller.getInstance(context)?.defaultInstaller?.uninstall(packageName) } Unit } diff --git a/src/main/kotlin/com/looker/droidify/screen/ScreenActivity.kt b/src/main/kotlin/com/looker/droidify/screen/ScreenActivity.kt index aa5c0b16..aaf69bd7 100644 --- a/src/main/kotlin/com/looker/droidify/screen/ScreenActivity.kt +++ b/src/main/kotlin/com/looker/droidify/screen/ScreenActivity.kt @@ -69,8 +69,6 @@ abstract class ScreenActivity : AppCompatActivity() { return supportFragmentManager.findFragmentById(R.id.main_content) } - val defaultInstaller = AppInstaller.getInstance(this)?.defaultInstaller - override fun onCreate(savedInstanceState: Bundle?) { setTheme(Preferences[Preferences.Key.Theme].getResId(resources.configuration)) super.onCreate(savedInstanceState) @@ -220,9 +218,11 @@ abstract class ScreenActivity : AppCompatActivity() { is SpecialIntent.Install -> { val packageName = specialIntent.packageName if (!packageName.isNullOrEmpty()) { - lifecycleScope.launch(Dispatchers.Default) { + lifecycleScope.launch(Dispatchers.IO) { specialIntent.cacheFileName?.let { - defaultInstaller?.install(packageName, it) + AppInstaller.getInstance(this@ScreenActivity)?.defaultInstaller?.install( + packageName, + it) } } }