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

View File

@ -1,7 +1,6 @@
package com.looker.droidify.installer
import android.content.Context
import android.util.Log
import com.looker.droidify.content.Cache
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.Dispatchers
@ -37,7 +36,7 @@ class RootInstaller(context: Context) : BaseInstaller(context) {
getUtilBoxPath,
cacheFile.absolutePath.quote
)
withContext(Dispatchers.IO) {
withContext(Dispatchers.Default) {
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) {
val uninstallCommand =
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 =

View File

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

View File

@ -17,7 +17,6 @@ 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() {
@ -218,11 +217,10 @@ abstract class ScreenActivity : AppCompatActivity() {
is SpecialIntent.Install -> {
val packageName = specialIntent.packageName
if (!packageName.isNullOrEmpty()) {
lifecycleScope.launch(Dispatchers.IO) {
lifecycleScope.launch {
specialIntent.cacheFileName?.let {
AppInstaller.getInstance(this@ScreenActivity)?.defaultInstaller?.install(
packageName,
it)
AppInstaller.getInstance(this@ScreenActivity)
?.defaultInstaller?.install(packageName, it)
}
}
}

View File

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