97 lines
2.7 KiB
Kotlin
Raw Normal View History

2019-05-03 04:42:57 -04:00
package com.topjohnwu.magisk.tasks
import android.net.Uri
import com.skoumal.teanity.extensions.subscribeK
2019-05-03 04:42:57 -04:00
import com.topjohnwu.magisk.App
import com.topjohnwu.magisk.Const
import com.topjohnwu.magisk.utils.fileName
import com.topjohnwu.magisk.utils.inject
import com.topjohnwu.magisk.utils.readUri
import com.topjohnwu.magisk.utils.unzip
2019-05-03 04:42:57 -04:00
import com.topjohnwu.superuser.Shell
import io.reactivex.Single
2019-05-03 04:42:57 -04:00
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
abstract class FlashZip(
private val mUri: Uri,
private val console: MutableList<String>,
private val logs: MutableList<String>
) {
2019-05-03 04:42:57 -04:00
private val app: App by inject()
private val tmpFile: File = File(app.cacheDir, "install.zip")
2019-05-03 04:42:57 -04:00
@Throws(IOException::class)
private fun unzipAndCheck(): Boolean {
val parentFile = tmpFile.parentFile ?: return false
tmpFile.unzip(parentFile, "META-INF/com/google/android", true)
val updaterScript = File(parentFile, "updater-script")
return Shell
.su("grep -q '#MAGISK' $updaterScript")
.exec()
.isSuccess
2019-05-03 04:42:57 -04:00
}
@Throws(IOException::class)
private fun flash(): Boolean {
console.add("- Copying zip to temp directory")
runCatching {
app.readUri(mUri).use { input ->
2019-05-03 04:42:57 -04:00
tmpFile.outputStream().use { out -> input.copyTo(out) }
}
}.getOrElse {
when (it) {
is FileNotFoundException -> console.add("! Invalid Uri")
is IOException -> console.add("! Cannot copy to cache")
}
throw it
2019-05-03 04:42:57 -04:00
}
val isMagiskModule = runCatching {
unzipAndCheck()
}.getOrElse {
2019-05-03 04:42:57 -04:00
console.add("! Unzip error")
throw it
}
if (!isMagiskModule) {
console.add("! This zip is not a Magisk Module!")
return false
2019-05-03 04:42:57 -04:00
}
console.add("- Installing ${mUri.fileName}")
val parentFile = tmpFile.parent ?: return false
return Shell
.su(
"cd $parentFile",
"BOOTMODE=true sh update-binary dummy 1 $tmpFile"
)
.to(console, logs)
.exec().isSuccess
2019-05-03 04:42:57 -04:00
}
fun exec() = Single
.fromCallable {
runCatching {
2019-05-03 04:42:57 -04:00
flash()
}.getOrElse {
it.printStackTrace()
2019-05-03 04:42:57 -04:00
false
}.apply {
Shell.su("cd /", "rm -rf ${tmpFile.parent} ${Const.TMP_FOLDER_PATH}")
.submit()
2019-05-03 04:42:57 -04:00
}
}
.subscribeK(onError = { onResult(false) }) { onResult(it) }
.let { Unit } // ignores result disposable
2019-05-03 04:42:57 -04:00
protected abstract fun onResult(success: Boolean)
}