Commit 88a39483 authored by Viktor De Pasquale's avatar Viktor De Pasquale Committed by John Wu

Replaced all install methods with the download service

parent f822c1c2
package com.topjohnwu.magisk.view.dialogs package com.topjohnwu.magisk.view.dialogs
import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.net.Uri
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import com.google.android.material.snackbar.Snackbar
import com.topjohnwu.magisk.ClassMap
import com.topjohnwu.magisk.Const import com.topjohnwu.magisk.Const
import com.topjohnwu.magisk.Info
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.model.download.CompoundDownloadService
import com.topjohnwu.magisk.model.entity.internal.Configuration
import com.topjohnwu.magisk.model.entity.internal.DownloadSubject
import com.topjohnwu.magisk.ui.base.MagiskActivity import com.topjohnwu.magisk.ui.base.MagiskActivity
import com.topjohnwu.magisk.ui.flash.FlashActivity
import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.magisk.view.ProgressNotification
import com.topjohnwu.magisk.view.SnackbarMaker
import com.topjohnwu.net.Networking
import com.topjohnwu.superuser.ShellUtils
import java.io.File
internal class InstallMethodDialog(activity: MagiskActivity<*, *>, options: List<String>) : internal class InstallMethodDialog(activity: MagiskActivity<*, *>, options: List<String>) :
AlertDialog.Builder(activity) { AlertDialog.Builder(activity) {
...@@ -27,78 +23,54 @@ internal class InstallMethodDialog(activity: MagiskActivity<*, *>, options: List ...@@ -27,78 +23,54 @@ internal class InstallMethodDialog(activity: MagiskActivity<*, *>, options: List
when (idx) { when (idx) {
0 -> downloadOnly(activity) 0 -> downloadOnly(activity)
1 -> patchBoot(activity) 1 -> patchBoot(activity)
2 -> { 2 -> flash(activity)
val intent = Intent(activity, ClassMap[FlashActivity::class.java])
.putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_MAGISK)
activity.startActivity(intent)
}
3 -> installInactiveSlot(activity) 3 -> installInactiveSlot(activity)
} }
} }
} }
private fun patchBoot(activity: MagiskActivity<*, *>) { @SuppressLint("MissingPermission")
activity.withExternalRW { private fun flash(activity: MagiskActivity<*, *>) = activity.withExternalRW {
onSuccess { onSuccess {
Utils.toast(R.string.patch_file_msg, Toast.LENGTH_LONG) val subject = DownloadSubject.Magisk(Configuration.Flash.Primary)
val intent = Intent(Intent.ACTION_GET_CONTENT) CompoundDownloadService.download(context, subject)
.setType("*/*")
.addCategory(Intent.CATEGORY_OPENABLE)
activity.startActivityForResult(intent, Const.ID.SELECT_BOOT) { resultCode, data ->
if (resultCode == Activity.RESULT_OK && data != null) {
val i = Intent(this, ClassMap[FlashActivity::class.java])
.setData(data.data)
.putExtra(Const.Key.FLASH_ACTION, Const.Value.PATCH_FILE)
startActivity(i)
}
}
}
} }
} }
private fun downloadOnly(activity: MagiskActivity<*, *>) { @SuppressLint("MissingPermission")
activity.withExternalRW { private fun patchBoot(activity: MagiskActivity<*, *>) = activity.withExternalRW {
onSuccess { onSuccess {
val filename = "Magisk-v${Info.remote.magisk.version}" + Utils.toast(R.string.patch_file_msg, Toast.LENGTH_LONG)
"(${Info.remote.magisk.versionCode}).zip" val intent = Intent(Intent.ACTION_GET_CONTENT)
val zip = File(Const.EXTERNAL_PATH, filename) .setType("*/*")
val cachedZip = File(activity.cacheDir, "magisk.zip") .addCategory(Intent.CATEGORY_OPENABLE)
activity.startActivityForResult(intent, Const.ID.SELECT_BOOT) { resultCode, data ->
fun onSuccess() { if (resultCode == Activity.RESULT_OK && data != null) {
SnackbarMaker.make( val safeData = data.data ?: Uri.EMPTY
activity, val subject = DownloadSubject.Magisk(Configuration.Patch(safeData))
activity.getString(R.string.internal_storage, "/Download/$filename"), CompoundDownloadService.download(activity, subject)
Snackbar.LENGTH_LONG
).show()
}
if (ShellUtils.checkSum("MD5", cachedZip, Info.remote.magisk.hash)) {
cachedZip.copyTo(zip, true)
onSuccess()
} else {
val progress = ProgressNotification(filename)
Networking.get(Info.remote.magisk.link)
.setDownloadProgressListener(progress)
.setErrorHandler { _, _ -> progress.dlFail() }
.getAsFile(zip) {
progress.dlDone()
onSuccess()
}
} }
} }
} }
} }
@SuppressLint("MissingPermission")
private fun downloadOnly(activity: MagiskActivity<*, *>) = activity.withExternalRW {
onSuccess {
val subject = DownloadSubject.Magisk(Configuration.Download)
CompoundDownloadService.download(activity, subject)
}
}
@SuppressLint("MissingPermission")
private fun installInactiveSlot(activity: MagiskActivity<*, *>) { private fun installInactiveSlot(activity: MagiskActivity<*, *>) {
CustomAlertDialog(activity) CustomAlertDialog(activity)
.setTitle(R.string.warning) .setTitle(R.string.warning)
.setMessage(R.string.install_inactive_slot_msg) .setMessage(R.string.install_inactive_slot_msg)
.setCancelable(true) .setCancelable(true)
.setPositiveButton(R.string.yes) { _, _ -> .setPositiveButton(R.string.yes) { _, _ ->
val intent = Intent(activity, ClassMap[FlashActivity::class.java]) val subject = DownloadSubject.Magisk(Configuration.Flash.Secondary)
.putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_INACTIVE_SLOT) CompoundDownloadService.download(activity, subject)
activity.startActivity(intent)
} }
.setNegativeButton(R.string.no_thanks, null) .setNegativeButton(R.string.no_thanks, null)
.show() .show()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment