Improve: Use single Installer instance in whole app

This commit is contained in:
LooKeR 2021-11-16 13:00:15 +05:30
parent 8b7294d467
commit bd55becfc9
6 changed files with 26 additions and 24 deletions

View File

@ -12,17 +12,15 @@ abstract class AppInstaller {
fun getInstance(context: Context?): AppInstaller? { fun getInstance(context: Context?): AppInstaller? {
if (INSTANCE == null) { if (INSTANCE == null) {
synchronized(AppInstaller::class.java) { synchronized(AppInstaller::class.java) {
if (INSTANCE == null) { context?.let {
context?.let { INSTANCE = object : AppInstaller() {
INSTANCE = object : AppInstaller() { override val defaultInstaller: BaseInstaller
override val defaultInstaller: BaseInstaller get() {
get() { return when (rootInstallerEnabled) {
return when (rootInstallerEnabled) { false -> DefaultInstaller(it)
false -> DefaultInstaller(it) true -> RootInstaller(it)
true -> RootInstaller(it)
}
} }
} }
} }
} }
} }

View File

@ -11,6 +11,10 @@ import kotlinx.coroutines.withContext
import java.io.File import java.io.File
class DefaultInstaller(context: Context) : BaseInstaller(context) { 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) { override suspend fun install(packageName: String, cacheFileName: String) {
val cacheFile = Cache.getReleaseFile(context, cacheFileName) val cacheFile = Cache.getReleaseFile(context, cacheFileName)

View File

@ -3,6 +3,8 @@ package com.looker.droidify.installer
import java.io.File import java.io.File
interface InstallationEvents { interface InstallationEvents {
suspend fun install(cacheFileName: String)
suspend fun install(packageName: String, cacheFileName: String) suspend fun install(packageName: String, cacheFileName: String)
suspend fun install(packageName: String, cacheFile: File) suspend fun install(packageName: String, cacheFile: File)

View File

@ -11,6 +11,10 @@ import kotlinx.coroutines.withContext
import java.io.File import java.io.File
class RootInstaller(context: Context) : BaseInstaller(context) { 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) { override suspend fun install(packageName: String, cacheFileName: String) {
val cacheFile = Cache.getReleaseFile(context, cacheFileName) val cacheFile = Cache.getReleaseFile(context, cacheFileName)

View File

@ -19,7 +19,6 @@ import com.looker.droidify.R
import com.looker.droidify.content.ProductPreferences import com.looker.droidify.content.ProductPreferences
import com.looker.droidify.database.Database import com.looker.droidify.database.Database
import com.looker.droidify.entity.* import com.looker.droidify.entity.*
import com.looker.droidify.installer.AppInstaller
import com.looker.droidify.service.Connection import com.looker.droidify.service.Connection
import com.looker.droidify.service.DownloadService import com.looker.droidify.service.DownloadService
import com.looker.droidify.utility.RxUtils import com.looker.droidify.utility.RxUtils
@ -358,13 +357,9 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
} }
(recyclerView?.adapter as? ProductAdapter)?.setStatus(status) (recyclerView?.adapter as? ProductAdapter)?.setStatus(status)
if (state is DownloadService.State.Success && isResumed) { if (state is DownloadService.State.Success && isResumed) {
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.Default) {
state.consume() state.consume()
AppInstaller screenActivity.defaultInstaller?.install(state.release.cacheFileName)
.getInstance(context)?.defaultInstaller?.install(
"",
state.release.cacheFileName
)
} }
} }
} }
@ -411,8 +406,8 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
) )
} }
ProductAdapter.Action.UNINSTALL -> { ProductAdapter.Action.UNINSTALL -> {
lifecycleScope.launch { lifecycleScope.launch(Dispatchers.Default) {
AppInstaller.getInstance(context)?.defaultInstaller?.uninstall(packageName) screenActivity.defaultInstaller?.uninstall(packageName)
} }
Unit Unit
} }

View File

@ -69,6 +69,8 @@ abstract class ScreenActivity : AppCompatActivity() {
return supportFragmentManager.findFragmentById(R.id.main_content) return supportFragmentManager.findFragmentById(R.id.main_content)
} }
val defaultInstaller = AppInstaller.getInstance(this)?.defaultInstaller
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
setTheme(Preferences[Preferences.Key.Theme].getResId(resources.configuration)) setTheme(Preferences[Preferences.Key.Theme].getResId(resources.configuration))
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -218,12 +220,9 @@ abstract class ScreenActivity : AppCompatActivity() {
is SpecialIntent.Install -> { is SpecialIntent.Install -> {
val packageName = specialIntent.packageName val packageName = specialIntent.packageName
if (!packageName.isNullOrEmpty()) { if (!packageName.isNullOrEmpty()) {
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.Default) {
specialIntent.cacheFileName?.let { specialIntent.cacheFileName?.let {
AppInstaller defaultInstaller?.install(packageName, it)
.getInstance(
this@ScreenActivity
)?.defaultInstaller?.install(packageName, it)
} }
} }
} }