Improve: Dispatcher.IO is for IO so use Default for non IO

Improve: Code cleanup
This commit is contained in:
LooKeR 2021-11-23 19:46:16 +05:30
parent cff28f14a3
commit e2781e5e9c
5 changed files with 48 additions and 62 deletions

View File

@ -1,9 +1,8 @@
package com.looker.droidify.installer package com.looker.droidify.installer
import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageInstaller.SessionParams import android.net.Uri
import com.looker.droidify.content.Cache import com.looker.droidify.content.Cache
import com.looker.droidify.utility.extension.android.Android import com.looker.droidify.utility.extension.android.Android
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -26,48 +25,38 @@ class DefaultInstaller(context: Context) : BaseInstaller(context) {
override suspend fun uninstall(packageName: String) = mDefaultUninstaller(packageName) override suspend fun uninstall(packageName: String) = mDefaultUninstaller(packageName)
private fun mDefaultInstaller(cacheFile: File) { private suspend fun mDefaultInstaller(cacheFile: File) {
val sessionInstaller = context.packageManager.packageInstaller val (uri, flags) = if (Android.sdk(24)) {
val sessionParams = Pair(
SessionParams(SessionParams.MODE_FULL_INSTALL) Cache.getReleaseUri(context, cacheFile.name),
Intent.FLAG_GRANT_READ_URI_PERMISSION
if (Android.sdk(31)) { )
sessionParams.setRequireUserAction(SessionParams.USER_ACTION_NOT_REQUIRED) } else {
Pair(Uri.fromFile(cacheFile), 0)
} }
// TODO Handle deprecation
val id = sessionInstaller.createSession(sessionParams) @Suppress("DEPRECATION")
withContext(Dispatchers.Default) {
val session = sessionInstaller.openSession(id) context.startActivity(
Intent(Intent.ACTION_INSTALL_PACKAGE)
session.use { activeSession -> .setDataAndType(uri, "application/vnd.android.package-archive")
activeSession.openWrite("package", 0, cacheFile.length()).use { packageStream -> .setFlags(flags)
cacheFile.inputStream().use { fileStream -> )
fileStream.copyTo(packageStream)
}
}
val intent = Intent(context, InstallerService::class.java)
val flags = if (Android.sdk(31)) PendingIntent.FLAG_MUTABLE else 0
val pendingIntent = PendingIntent.getService(context, id, intent, flags)
session.commit(pendingIntent.intentSender)
} }
} }
private suspend fun mDefaultUninstaller(packageName: String) { private suspend fun mDefaultUninstaller(packageName: String) {
val sessionInstaller = context.packageManager.packageInstaller val uri = Uri.fromParts("package", packageName, null)
val intent = Intent()
val intent = Intent(context, InstallerService::class.java) intent.data = uri
intent.putExtra(InstallerService.KEY_ACTION, InstallerService.ACTION_UNINSTALL) @Suppress("DEPRECATION")
if (Android.sdk(28)) {
val flags = if (Android.sdk(31)) PendingIntent.FLAG_MUTABLE else 0 intent.action = Intent.ACTION_DELETE
} else {
val pendingIntent = PendingIntent.getService(context, -1, intent, flags) intent.action = Intent.ACTION_UNINSTALL_PACKAGE
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true)
withContext(Dispatchers.IO) {
sessionInstaller.uninstall(packageName, pendingIntent.intentSender)
} }
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
withContext(Dispatchers.Default) { context.startActivity(intent) }
} }
} }

View File

@ -1,7 +1,6 @@
package com.looker.droidify.installer package com.looker.droidify.installer
import android.content.Context import android.content.Context
import android.util.Log
import com.looker.droidify.content.Cache import com.looker.droidify.content.Cache
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -37,7 +36,7 @@ class RootInstaller(context: Context) : BaseInstaller(context) {
getUtilBoxPath, getUtilBoxPath,
cacheFile.absolutePath.quote cacheFile.absolutePath.quote
) )
withContext(Dispatchers.IO) { withContext(Dispatchers.Default) {
Shell.su(installCommand).submit { if (it.isSuccess) Shell.su(deleteCommand).submit() } Shell.su(installCommand).submit { if (it.isSuccess) Shell.su(deleteCommand).submit() }
} }
} }
@ -45,7 +44,7 @@ class RootInstaller(context: Context) : BaseInstaller(context) {
private suspend fun mRootUninstaller(packageName: String) { private suspend fun mRootUninstaller(packageName: String) {
val uninstallCommand = val uninstallCommand =
String.format(ROOT_UNINSTALL_PACKAGE, getCurrentUserState, packageName) String.format(ROOT_UNINSTALL_PACKAGE, getCurrentUserState, packageName)
withContext(Dispatchers.IO) { Shell.su(uninstallCommand).submit() } withContext(Dispatchers.Default) { Shell.su(uninstallCommand).submit() }
} }
private val getCurrentUserState: String = private val getCurrentUserState: String =

View File

@ -33,6 +33,7 @@ import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.schedulers.Schedulers import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
companion object { companion object {
@ -306,11 +307,11 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
toolbar.menu.findItem(action.id).isEnabled = !downloading toolbar.menu.findItem(action.id).isEnabled = !downloading
} }
this.actions = Pair(actions, primaryAction) this.actions = Pair(actions, primaryAction)
updateToolbarButtons() lifecycleScope.launch { updateToolbarButtons() }
} }
private fun updateToolbarTitle() { private suspend fun updateToolbarTitle() {
lifecycleScope.launch(Dispatchers.IO) { withContext(Dispatchers.Default) {
val showPackageName = recyclerView val showPackageName = recyclerView
?.let { (it.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() != 0 } == true ?.let { (it.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() != 0 } == true
collapsingToolbar.title = collapsingToolbar.title =
@ -319,8 +320,8 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
} }
} }
private fun updateToolbarButtons() { private suspend fun updateToolbarButtons() {
lifecycleScope.launch(Dispatchers.IO) { withContext(Dispatchers.Default) {
val (actions, primaryAction) = actions val (actions, primaryAction) = actions
val showPrimaryAction = recyclerView val showPrimaryAction = recyclerView
?.let { (it.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() != 0 } == true ?.let { (it.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() != 0 } == true
@ -356,7 +357,7 @@ 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 {
state.consume() state.consume()
AppInstaller.getInstance(context)?.defaultInstaller?.install(state.release.cacheFileName) AppInstaller.getInstance(context)?.defaultInstaller?.install(state.release.cacheFileName)
} }
@ -372,8 +373,10 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
val lastPosition = lastPosition val lastPosition = lastPosition
this.lastPosition = position this.lastPosition = position
if ((lastPosition == 0) != (position == 0)) { if ((lastPosition == 0) != (position == 0)) {
updateToolbarTitle() lifecycleScope.launch {
updateToolbarButtons() launch { updateToolbarTitle() }
launch { updateToolbarButtons() }
}
} }
} }
} }
@ -405,7 +408,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
) )
} }
ProductAdapter.Action.UNINSTALL -> { ProductAdapter.Action.UNINSTALL -> {
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch {
AppInstaller.getInstance(context)?.defaultInstaller?.uninstall(packageName) AppInstaller.getInstance(context)?.defaultInstaller?.uninstall(packageName)
} }
Unit Unit
@ -425,10 +428,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
) )
type = "text/plain" type = "text/plain"
} }
startActivity(Intent.createChooser(sendIntent, null))
val shareIntent = Intent.createChooser(sendIntent, null)
startActivity(shareIntent)
} }
}::class }::class
} }

View File

@ -17,7 +17,6 @@ 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 import kotlinx.coroutines.launch
abstract class ScreenActivity : AppCompatActivity() { abstract class ScreenActivity : AppCompatActivity() {
@ -218,11 +217,10 @@ 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 {
specialIntent.cacheFileName?.let { specialIntent.cacheFileName?.let {
AppInstaller.getInstance(this@ScreenActivity)?.defaultInstaller?.install( AppInstaller.getInstance(this@ScreenActivity)
packageName, ?.defaultInstaller?.install(packageName, it)
it)
} }
} }
} }

View File

@ -83,8 +83,8 @@ object Utils {
} }
val rootInstallerEnabled: Boolean val rootInstallerEnabled: Boolean
get() = Preferences[Preferences.Key.RootPermission] && (Shell.getCachedShell()?.isRoot get() = Preferences[Preferences.Key.RootPermission] &&
?: Shell.getShell().isRoot) (Shell.getCachedShell()?.isRoot ?: Shell.getShell().isRoot)
fun startUpdate( fun startUpdate(
packageName: String, packageName: String,