From bd55becfc9219ff5d5ece173175a719b5c7008a9 Mon Sep 17 00:00:00 2001 From: LooKeR Date: Tue, 16 Nov 2021 13:00:15 +0530 Subject: [PATCH] Improve: Use single Installer instance in whole app --- .../looker/droidify/installer/AppInstaller.kt | 18 ++++++++---------- .../droidify/installer/DefaultInstaller.kt | 4 ++++ .../droidify/installer/InstallationEvents.kt | 2 ++ .../looker/droidify/installer/RootInstaller.kt | 4 ++++ .../looker/droidify/screen/ProductFragment.kt | 13 ++++--------- .../looker/droidify/screen/ScreenActivity.kt | 9 ++++----- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/installer/AppInstaller.kt b/src/main/kotlin/com/looker/droidify/installer/AppInstaller.kt index fca683a6..435210d3 100644 --- a/src/main/kotlin/com/looker/droidify/installer/AppInstaller.kt +++ b/src/main/kotlin/com/looker/droidify/installer/AppInstaller.kt @@ -12,17 +12,15 @@ abstract class AppInstaller { fun getInstance(context: Context?): AppInstaller? { if (INSTANCE == null) { synchronized(AppInstaller::class.java) { - if (INSTANCE == null) { - context?.let { - INSTANCE = object : AppInstaller() { - override val defaultInstaller: BaseInstaller - get() { - return when (rootInstallerEnabled) { - false -> DefaultInstaller(it) - true -> RootInstaller(it) - } + context?.let { + INSTANCE = object : AppInstaller() { + override val defaultInstaller: BaseInstaller + get() { + return when (rootInstallerEnabled) { + false -> DefaultInstaller(it) + true -> RootInstaller(it) } - } + } } } } diff --git a/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt b/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt index 49787bac..35e82526 100644 --- a/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt +++ b/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt @@ -11,6 +11,10 @@ import kotlinx.coroutines.withContext import java.io.File class DefaultInstaller(context: Context) : BaseInstaller(context) { + override suspend fun install(cacheFileName: String) { + val cacheFile = Cache.getReleaseFile(context, cacheFileName) + mDefaultInstaller(cacheFile) + } override suspend fun install(packageName: String, cacheFileName: String) { val cacheFile = Cache.getReleaseFile(context, cacheFileName) diff --git a/src/main/kotlin/com/looker/droidify/installer/InstallationEvents.kt b/src/main/kotlin/com/looker/droidify/installer/InstallationEvents.kt index 38c349c6..78196423 100644 --- a/src/main/kotlin/com/looker/droidify/installer/InstallationEvents.kt +++ b/src/main/kotlin/com/looker/droidify/installer/InstallationEvents.kt @@ -3,6 +3,8 @@ package com.looker.droidify.installer import java.io.File interface InstallationEvents { + suspend fun install(cacheFileName: String) + suspend fun install(packageName: String, cacheFileName: String) suspend fun install(packageName: String, cacheFile: File) diff --git a/src/main/kotlin/com/looker/droidify/installer/RootInstaller.kt b/src/main/kotlin/com/looker/droidify/installer/RootInstaller.kt index acbc0242..4bdc1774 100644 --- a/src/main/kotlin/com/looker/droidify/installer/RootInstaller.kt +++ b/src/main/kotlin/com/looker/droidify/installer/RootInstaller.kt @@ -11,6 +11,10 @@ import kotlinx.coroutines.withContext import java.io.File class RootInstaller(context: Context) : BaseInstaller(context) { + override suspend fun install(cacheFileName: String) { + val cacheFile = Cache.getReleaseFile(context, cacheFileName) + mRootInstaller(cacheFile) + } override suspend fun install(packageName: String, cacheFileName: String) { val cacheFile = Cache.getReleaseFile(context, cacheFileName) diff --git a/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt b/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt index 9f9495db..3bbfc179 100644 --- a/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt +++ b/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt @@ -19,7 +19,6 @@ 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 @@ -358,13 +357,9 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { } (recyclerView?.adapter as? ProductAdapter)?.setStatus(status) if (state is DownloadService.State.Success && isResumed) { - lifecycleScope.launch(Dispatchers.IO) { + lifecycleScope.launch(Dispatchers.Default) { state.consume() - AppInstaller - .getInstance(context)?.defaultInstaller?.install( - "", - state.release.cacheFileName - ) + screenActivity.defaultInstaller?.install(state.release.cacheFileName) } } } @@ -411,8 +406,8 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { ) } ProductAdapter.Action.UNINSTALL -> { - lifecycleScope.launch { - AppInstaller.getInstance(context)?.defaultInstaller?.uninstall(packageName) + lifecycleScope.launch(Dispatchers.Default) { + screenActivity.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 9032277e..aa5c0b16 100644 --- a/src/main/kotlin/com/looker/droidify/screen/ScreenActivity.kt +++ b/src/main/kotlin/com/looker/droidify/screen/ScreenActivity.kt @@ -69,6 +69,8 @@ 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) @@ -218,12 +220,9 @@ abstract class ScreenActivity : AppCompatActivity() { is SpecialIntent.Install -> { val packageName = specialIntent.packageName if (!packageName.isNullOrEmpty()) { - lifecycleScope.launch(Dispatchers.IO) { + lifecycleScope.launch(Dispatchers.Default) { specialIntent.cacheFileName?.let { - AppInstaller - .getInstance( - this@ScreenActivity - )?.defaultInstaller?.install(packageName, it) + defaultInstaller?.install(packageName, it) } } }