Fix: Freeze while installing caused by new installer

This commit is contained in:
LooKeR 2021-10-25 10:17:09 +05:30
parent 07e032268f
commit f6ed45dda2
3 changed files with 32 additions and 22 deletions

View File

@ -2,6 +2,8 @@ package com.looker.droidify.installer
import android.content.Context
import com.looker.droidify.utility.Utils.rootInstallerEnabled
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
abstract class AppInstaller {
abstract val defaultInstaller: BaseInstaller?
@ -9,19 +11,21 @@ abstract class AppInstaller {
companion object {
@Volatile
private var INSTANCE: AppInstaller? = null
fun getInstance(context: Context?): AppInstaller? {
suspend 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)
withContext(Dispatchers.IO) {
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)
}
}
}
}
}
}
}

View File

@ -7,10 +7,8 @@ import android.content.pm.ApplicationInfo
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
@ -330,7 +328,8 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
(it.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() != 0
} == true
(toolbar.parent as CollapsingToolbarLayout).title = if (showPackageName) products[0].first.name else getString(R.string.application)
(toolbar.parent as CollapsingToolbarLayout).title =
if (showPackageName) products[0].first.name else getString(R.string.application)
}
private fun updateToolbarButtons() {
@ -369,8 +368,8 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
}
(recyclerView?.adapter as? ProductAdapter)?.setStatus(status)
if (state is DownloadService.State.Success && isResumed) {
state.consume()
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO){
lifecycleScope.launch(Dispatchers.IO) {
state.consume()
AppInstaller
.getInstance(context)?.defaultInstaller?.install(
"",
@ -421,7 +420,9 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
)
}
ProductAdapter.Action.UNINSTALL -> {
AppInstaller.getInstance(context)?.defaultInstaller?.uninstall(packageName)
lifecycleScope.launch {
AppInstaller.getInstance(context)?.defaultInstaller?.uninstall(packageName)
}
Unit
}
ProductAdapter.Action.CANCEL -> {

View File

@ -9,6 +9,7 @@ import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.looker.droidify.R
import com.looker.droidify.content.Preferences
import com.looker.droidify.database.CursorOwner
@ -16,6 +17,8 @@ import com.looker.droidify.installer.AppInstaller
import com.looker.droidify.utility.KParcelable
import com.looker.droidify.utility.extension.resources.getDrawableFromAttr
import com.looker.droidify.utility.extension.text.nullIfEmpty
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
abstract class ScreenActivity : AppCompatActivity() {
companion object {
@ -215,11 +218,13 @@ abstract class ScreenActivity : AppCompatActivity() {
is SpecialIntent.Install -> {
val packageName = specialIntent.packageName
if (!packageName.isNullOrEmpty()) {
specialIntent.cacheFileName?.let {
AppInstaller
.getInstance(
this@ScreenActivity
)?.defaultInstaller?.install(packageName, it)
lifecycleScope.launch(Dispatchers.IO) {
specialIntent.cacheFileName?.let {
AppInstaller
.getInstance(
this@ScreenActivity
)?.defaultInstaller?.install(packageName, it)
}
}
}
Unit