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 android.content.Context
import com.looker.droidify.utility.Utils.rootInstallerEnabled import com.looker.droidify.utility.Utils.rootInstallerEnabled
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
abstract class AppInstaller { abstract class AppInstaller {
abstract val defaultInstaller: BaseInstaller? abstract val defaultInstaller: BaseInstaller?
@ -9,19 +11,21 @@ abstract class AppInstaller {
companion object { companion object {
@Volatile @Volatile
private var INSTANCE: AppInstaller? = null private var INSTANCE: AppInstaller? = null
fun getInstance(context: Context?): AppInstaller? { suspend fun getInstance(context: Context?): AppInstaller? {
if (INSTANCE == null) { if (INSTANCE == null) {
synchronized(AppInstaller::class.java) { withContext(Dispatchers.IO) {
if (INSTANCE == null) { synchronized(AppInstaller::class.java) {
context?.let { if (INSTANCE == null) {
INSTANCE = object : AppInstaller() { context?.let {
override val defaultInstaller: BaseInstaller INSTANCE = object : AppInstaller() {
get() { override val defaultInstaller: BaseInstaller
return when (rootInstallerEnabled) { get() {
false -> DefaultInstaller(it) return when (rootInstallerEnabled) {
true -> RootInstaller(it) false -> DefaultInstaller(it)
true -> RootInstaller(it)
}
} }
} }
} }
} }
} }

View File

@ -7,10 +7,8 @@ import android.content.pm.ApplicationInfo
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.Settings import android.provider.Settings
import android.view.LayoutInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout import android.widget.FrameLayout
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
@ -330,7 +328,8 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
(it.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() != 0 (it.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() != 0
} == true } == 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() { private fun updateToolbarButtons() {
@ -369,8 +368,8 @@ 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) {
state.consume() lifecycleScope.launch(Dispatchers.IO) {
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO){ state.consume()
AppInstaller AppInstaller
.getInstance(context)?.defaultInstaller?.install( .getInstance(context)?.defaultInstaller?.install(
"", "",
@ -421,7 +420,9 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
) )
} }
ProductAdapter.Action.UNINSTALL -> { ProductAdapter.Action.UNINSTALL -> {
AppInstaller.getInstance(context)?.defaultInstaller?.uninstall(packageName) lifecycleScope.launch {
AppInstaller.getInstance(context)?.defaultInstaller?.uninstall(packageName)
}
Unit Unit
} }
ProductAdapter.Action.CANCEL -> { ProductAdapter.Action.CANCEL -> {

View File

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