Add: New Installer Logic

This commit is contained in:
LooKeR
2021-10-24 18:03:35 +05:30
parent 8b3fd09f27
commit 2aaec7e022
11 changed files with 261 additions and 124 deletions

View File

@ -0,0 +1,33 @@
package com.looker.droidify.installer
import android.content.Context
import com.looker.droidify.utility.Utils.rootInstallerEnabled
abstract class AppInstaller {
abstract val defaultInstaller: BaseInstaller?
companion object {
@Volatile
private var INSTANCE: AppInstaller? = null
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)
}
}
}
}
}
}
}
return INSTANCE
}
}
}

View File

@ -0,0 +1,56 @@
package com.looker.droidify.installer
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInstaller
import android.net.Uri
import com.looker.droidify.utility.extension.android.Android
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
abstract class BaseInstaller(val context: Context) : InstallationEvents {
companion object {
const val ROOT_INSTALL_PACKAGE = "cat %s | pm install --user %s -t -r -S %s"
const val ROOT_UNINSTALL_PACKAGE = "pm uninstall --user %s %s"
}
private val job = Job()
val scope = CoroutineScope(Dispatchers.IO + job)
fun getStatusString(context: Context, status: Int): String {
return when (status) {
PackageInstaller.STATUS_FAILURE -> "context.getString(R.string.installer_status_failure)"
PackageInstaller.STATUS_FAILURE_ABORTED -> "context.getString(R.string.installer_status_failure_aborted)"
PackageInstaller.STATUS_FAILURE_BLOCKED -> "context.getString(R.string.installer_status_failure_blocked)"
PackageInstaller.STATUS_FAILURE_CONFLICT -> "context.getString(R.string.installer_status_failure_conflict)"
PackageInstaller.STATUS_FAILURE_INCOMPATIBLE -> "context.getString(R.string.installer_status_failure_incompatible)"
PackageInstaller.STATUS_FAILURE_INVALID -> "context.getString(R.string.installer_status_failure_invalid)"
PackageInstaller.STATUS_FAILURE_STORAGE -> "context.getString(R.string.installer_status_failure_storage)"
PackageInstaller.STATUS_PENDING_USER_ACTION -> "context.getString(R.string.installer_status_user_action)"
PackageInstaller.STATUS_SUCCESS -> "context.getString(R.string.installer_status_success)"
else -> "context.getString(R.string.installer_status_unknown)"
}
}
override fun uninstall(packageName: String) {
val uri = Uri.fromParts("package", packageName, null)
val intent = Intent()
intent.data = uri
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)
context.startActivity(intent)
}
sealed class Event {
object INSTALL : Event()
object UNINSTALL : Event()
}
}

View File

@ -0,0 +1,43 @@
package com.looker.droidify.installer
import android.content.Context
import android.content.Intent
import android.net.Uri
import com.looker.droidify.content.Cache
import com.looker.droidify.utility.extension.android.Android
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
class DefaultInstaller(context: Context) : BaseInstaller(context) {
override fun install(packageName: String, cacheFileName: String) {
val cacheFile = Cache.getReleaseFile(context, cacheFileName)
scope.launch { mDefaultInstaller(cacheFile) }
}
override fun install(packageName: String, cacheFile: File) {
scope.launch { mDefaultInstaller(cacheFile) }
}
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)
}
// TODO Handle deprecation
@Suppress("DEPRECATION")
withContext(Dispatchers.IO) {
context.startActivity(
Intent(Intent.ACTION_INSTALL_PACKAGE)
.setDataAndType(uri, "application/vnd.android.package-archive").setFlags(flags)
)
}
}
}

View File

@ -0,0 +1,11 @@
package com.looker.droidify.installer
import java.io.File
interface InstallationEvents {
fun install(packageName: String, cacheFileName: String)
fun install(packageName: String, cacheFile: File)
fun uninstall(packageName: String)
}

View File

@ -0,0 +1,91 @@
package com.looker.droidify.installer
import android.content.Context
import android.util.Log
import com.looker.droidify.content.Cache
import com.looker.droidify.utility.Utils.rootInstallerEnabled
import com.looker.droidify.utility.extension.android.Android
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
class RootInstaller(context: Context) : BaseInstaller(context) {
override fun install(packageName: String, cacheFileName: String) {
val cacheFile = Cache.getReleaseFile(context, cacheFileName)
scope.launch { mRootInstaller(cacheFile) }
}
override fun install(packageName: String, cacheFile: File) {
scope.launch { mRootInstaller(cacheFile) }
}
override fun uninstall(packageName: String) {
scope.launch { mRootUninstaller(packageName) }
}
private suspend fun mRootInstaller(cacheFile: File) {
if (rootInstallerEnabled) {
val installCommand =
String.format(
ROOT_INSTALL_PACKAGE,
cacheFile.absolutePath,
getCurrentUserState,
cacheFile.length()
)
Log.e("Install", installCommand)
withContext(Dispatchers.IO) {
launch {
val result = Shell.su(installCommand).exec()
launch {
if (result.isSuccess) {
Shell.su("$getUtilBoxPath rm ${cacheFile.absolutePath.quote}")
.submit()
}
}
}
}
}
}
private suspend fun mRootUninstaller(packageName: String) {
if (rootInstallerEnabled) {
val uninstallCommand =
String.format(ROOT_UNINSTALL_PACKAGE, getCurrentUserState, packageName)
withContext(Dispatchers.IO) { launch { Shell.su(uninstallCommand).exec() } }
}
}
private val getCurrentUserState: String =
if (Android.sdk(25)) Shell.su("am get-current-user").exec().out[0]
else Shell.su("dumpsys activity | grep mCurrentUser").exec().out[0].trim()
.removePrefix("mCurrentUser=")
private val String.quote
get() = "\"${this.replace(Regex("""[\\$"`]""")) { c -> "\\${c.value}" }}\""
private val getUtilBoxPath: String
get() {
listOf("toybox", "busybox").forEach {
var shellResult = Shell.su("which $it").exec()
if (shellResult.out.isNotEmpty()) {
val utilBoxPath = shellResult.out.joinToString("")
if (utilBoxPath.isNotEmpty()) {
val utilBoxQuoted = utilBoxPath.quote
shellResult = Shell.su("$utilBoxQuoted --version").exec()
if (shellResult.out.isNotEmpty()) {
val utilBoxVersion = shellResult.out.joinToString("")
Log.i(
this.javaClass.canonicalName,
"Using Utilbox $it : $utilBoxQuoted $utilBoxVersion"
)
}
return utilBoxQuoted
}
}
}
return ""
}
}