diff --git a/app/src/main/java/com/topjohnwu/magisk/model/flash/Flashing.kt b/app/src/main/java/com/topjohnwu/magisk/model/flash/Flashing.kt new file mode 100644 index 000000000..7ab6a6f8d --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/flash/Flashing.kt @@ -0,0 +1,62 @@ +package com.topjohnwu.magisk.model.flash + +import android.content.Context +import android.net.Uri +import androidx.core.os.postDelayed +import com.topjohnwu.magisk.tasks.FlashZip +import com.topjohnwu.magisk.utils.Utils +import com.topjohnwu.magisk.utils.inject +import com.topjohnwu.superuser.Shell +import com.topjohnwu.superuser.internal.UiThreadHandler + +sealed class Flashing( + uri: Uri, + private val console: MutableList, + log: MutableList, + private val resultListener: (Result) -> Unit +) : FlashZip(uri, console, log) { + + override fun onResult(success: Boolean) { + if (!success) { + console.add("! Installation failed") + } + + resultListener(Result.success(success)) + } + + class Install( + uri: Uri, + console: MutableList, + log: MutableList, + resultListener: (Result) -> Unit = {} + ) : Flashing(uri, console, log, resultListener) { + + override fun onResult(success: Boolean) { + if (success) { + Utils.loadModules() + } + super.onResult(success) + } + + } + + class Uninstall( + uri: Uri, + console: MutableList, + log: MutableList, + resultListener: (Result) -> Unit = {} + ) : Flashing(uri, console, log, resultListener) { + + private val context: Context by inject() + + override fun onResult(success: Boolean) { + if (success) { + UiThreadHandler.handler.postDelayed(3000) { + Shell.su("pm uninstall " + context.packageName).exec() + } + } + super.onResult(success) + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/model/flash/Patching.kt b/app/src/main/java/com/topjohnwu/magisk/model/flash/Patching.kt new file mode 100644 index 000000000..7f6a98920 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/flash/Patching.kt @@ -0,0 +1,51 @@ +package com.topjohnwu.magisk.model.flash + +import android.net.Uri +import com.topjohnwu.magisk.tasks.MagiskInstaller +import com.topjohnwu.superuser.Shell + +sealed class Patching( + private val console: MutableList, + logs: List, + private val resultListener: (Result) -> Unit +) : MagiskInstaller(console, logs) { + + override fun onResult(success: Boolean) { + if (success) { + console.add("- All done!") + } else { + Shell.sh("rm -rf $installDir").submit() + console.add("! Installation failed") + } + resultListener(Result.success(success)) + } + + class File( + private val uri: Uri, + console: MutableList, + logs: List, + resultListener: (Result) -> Unit = {} + ) : Patching(console, logs, resultListener) { + override fun operations() = + extractZip() && handleFile(uri) && patchBoot() && storeBoot() + } + + class SecondSlot( + console: MutableList, + logs: List, + resultListener: (Result) -> Unit = {} + ) : Patching(console, logs, resultListener) { + override fun operations() = + findSecondaryImage() && extractZip() && patchBoot() && flashBoot() && postOTA() + } + + class Direct( + console: MutableList, + logs: List, + resultListener: (Result) -> Unit = {} + ) : Patching(console, logs, resultListener) { + override fun operations() = + findImage() && extractZip() && patchBoot() && flashBoot() + } + +} \ No newline at end of file