mirror of
https://github.com/topjohnwu/Magisk.git
synced 2026-01-27 15:25:08 -08:00
Fixed rewritten java code being java-styled in kotlin
Fixed accessing kotlin code illegally via companion helper
This commit is contained in:
@@ -1,73 +1,96 @@
|
||||
package com.topjohnwu.magisk.tasks
|
||||
|
||||
import android.net.Uri
|
||||
|
||||
import com.skoumal.teanity.extensions.subscribeK
|
||||
import com.topjohnwu.magisk.App
|
||||
import com.topjohnwu.magisk.Const
|
||||
import com.topjohnwu.magisk.utils.*
|
||||
import com.topjohnwu.magisk.utils.fileName
|
||||
import com.topjohnwu.magisk.utils.inject
|
||||
import com.topjohnwu.magisk.utils.readUri
|
||||
import com.topjohnwu.magisk.utils.unzip
|
||||
import com.topjohnwu.superuser.Shell
|
||||
import com.topjohnwu.superuser.internal.UiThreadHandler
|
||||
|
||||
import io.reactivex.Single
|
||||
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>) {
|
||||
abstract class FlashZip(
|
||||
private val mUri: Uri,
|
||||
private val console: MutableList<String>,
|
||||
private val logs: MutableList<String>
|
||||
) {
|
||||
|
||||
private val tmpFile: File = File(App.self.cacheDir, "install.zip")
|
||||
private val app: App by inject()
|
||||
private val tmpFile: File = File(app.cacheDir, "install.zip")
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun unzipAndCheck(): Boolean {
|
||||
unzip(tmpFile, tmpFile.parentFile!!, "META-INF/com/google/android", true)
|
||||
return Shell.su("grep -q '#MAGISK' ${File(tmpFile.parentFile, "updater-script")}")
|
||||
.exec().isSuccess
|
||||
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
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun flash(): Boolean {
|
||||
console.add("- Copying zip to temp directory")
|
||||
try {
|
||||
App.self.readUri(mUri).use { input ->
|
||||
|
||||
runCatching {
|
||||
app.readUri(mUri).use { input ->
|
||||
tmpFile.outputStream().use { out -> input.copyTo(out) }
|
||||
}
|
||||
} catch (e: FileNotFoundException) {
|
||||
console.add("! Invalid Uri")
|
||||
throw e
|
||||
} catch (e: IOException) {
|
||||
console.add("! Cannot copy to cache")
|
||||
throw e
|
||||
}.getOrElse {
|
||||
when (it) {
|
||||
is FileNotFoundException -> console.add("! Invalid Uri")
|
||||
is IOException -> console.add("! Cannot copy to cache")
|
||||
}
|
||||
throw it
|
||||
}
|
||||
|
||||
try {
|
||||
if (!unzipAndCheck()) {
|
||||
console.add("! This zip is not a Magisk Module!")
|
||||
return false
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
val isMagiskModule = runCatching {
|
||||
unzipAndCheck()
|
||||
}.getOrElse {
|
||||
console.add("! Unzip error")
|
||||
throw e
|
||||
throw it
|
||||
}
|
||||
|
||||
if (!isMagiskModule) {
|
||||
console.add("! This zip is not a Magisk Module!")
|
||||
return false
|
||||
}
|
||||
|
||||
console.add("- Installing ${mUri.fileName}")
|
||||
return Shell.su("cd " + tmpFile.parent!!,
|
||||
"BOOTMODE=true sh update-binary dummy 1 $tmpFile")
|
||||
.to(console, logs)
|
||||
.exec().isSuccess
|
||||
|
||||
val parentFile = tmpFile.parent ?: return false
|
||||
|
||||
return Shell
|
||||
.su(
|
||||
"cd $parentFile",
|
||||
"BOOTMODE=true sh update-binary dummy 1 $tmpFile"
|
||||
)
|
||||
.to(console, logs)
|
||||
.exec().isSuccess
|
||||
}
|
||||
|
||||
fun exec() {
|
||||
App.THREAD_POOL.execute {
|
||||
val success = try {
|
||||
fun exec() = Single
|
||||
.fromCallable {
|
||||
runCatching {
|
||||
flash()
|
||||
} catch (e: IOException) {
|
||||
}.getOrElse {
|
||||
it.printStackTrace()
|
||||
false
|
||||
}.apply {
|
||||
Shell.su("cd /", "rm -rf ${tmpFile.parent} ${Const.TMP_FOLDER_PATH}")
|
||||
.submit()
|
||||
}
|
||||
Shell.su("cd /", "rm -rf ${tmpFile.parent} ${Const.TMP_FOLDER_PATH}").submit()
|
||||
UiThreadHandler.run { onResult(success) }
|
||||
}
|
||||
}
|
||||
.subscribeK(onError = { onResult(false) }) { onResult(it) }
|
||||
.let { Unit } // ignores result disposable
|
||||
|
||||
|
||||
protected abstract fun onResult(success: Boolean)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user