Skip to content

Commit

Permalink
Updated method naming scheme
Browse files Browse the repository at this point in the history
Added new configurations
Added flashing methods and annotated viewModel's uri as deprecated in function
  • Loading branch information
diareuse authored and topjohnwu committed Jul 20, 2019
1 parent 40b6831 commit 12e00c3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,58 @@ open class CompoundDownloadService : SubstrateDownloadService() {

private val context get() = this

override fun onFinished(file: File, subject: DownloadSubject) {
when (subject) {
is DownloadSubject.Magisk -> {
if (subject.configuration == Configuration.FLASH) {
FlashActivity.flashMagisk(this, file)
}
}
is DownloadSubject.Module -> {
if (subject.configuration == Configuration.FLASH) {
FlashActivity.flashModule(this, file)
}
}
}
override fun onFinished(file: File, subject: DownloadSubject) = when (subject) {
is DownloadSubject.Magisk -> onFinishedInternal(file, subject)
is DownloadSubject.Module -> onFinishedInternal(file, subject)
}

// ---

override fun NotificationCompat.Builder.addActions(file: File, subject: DownloadSubject) =
when (subject) {
is DownloadSubject.Magisk -> addMagiskActions(file, subject.configuration)
is DownloadSubject.Module -> addModuleActions(file, subject.configuration)
}
private fun onFinishedInternal(
file: File,
subject: DownloadSubject.Magisk
) = when (subject.configuration) {
Configuration.FLASH -> FlashActivity.flash(this, file)
else -> Unit
}

private fun NotificationCompat.Builder.addMagiskActions(
private fun onFinishedInternal(
file: File,
configuration: Configuration
) = apply {
when (configuration) {
Configuration.FLASH -> {
val inner = FlashActivity.flashMagiskIntent(context, file)
val intent = PendingIntent
.getActivity(context, nextInt(), inner, PendingIntent.FLAG_ONE_SHOT)
subject: DownloadSubject.Module
) = when (subject.configuration) {
Configuration.FLASH -> FlashActivity.install(this, file)
else -> Unit
}

setContentIntent(intent)
}
}
// ---

override fun NotificationCompat.Builder.addActions(
file: File,
subject: DownloadSubject
) = when (subject) {
is DownloadSubject.Magisk -> addActionsInternal(file, subject)
is DownloadSubject.Module -> addActionsInternal(file, subject)
}

private fun NotificationCompat.Builder.addModuleActions(
private fun NotificationCompat.Builder.addActionsInternal(
file: File,
configuration: Configuration
) = apply {
subject: DownloadSubject.Magisk
) = when (subject.configuration) {
Configuration.FLASH -> setContentIntent(FlashActivity.flashIntent(context, file))
else -> this
}

private fun NotificationCompat.Builder.addActionsInternal(
file: File,
subject: DownloadSubject.Module
) = when (subject.configuration) {
Configuration.FLASH -> setContentIntent(FlashActivity.installIntent(context, file))
else -> this
}

@Suppress("ReplaceSingleLineLet")
private fun NotificationCompat.Builder.setContentIntent(intent: Intent) =
PendingIntent.getActivity(context, nextInt(), intent, PendingIntent.FLAG_ONE_SHOT)
.let { setContentIntent(it) }

companion object {

fun download(context: Context, subject: DownloadSubject) =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.topjohnwu.magisk.model.entity.internal

enum class Configuration {
FLASH, DOWNLOAD
FLASH, DOWNLOAD, UNINSTALL, PATCH
}
34 changes: 26 additions & 8 deletions app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,39 @@ open class FlashActivity : MagiskActivity<FlashViewModel, ActivityFlashBinding>(
companion object {

private fun intent(context: Context) = Intent(context, ClassMap[FlashActivity::class.java])
private fun intent(context: Context, file: File) = intent(context).setData(file.toUri())

fun flashMagiskIntent(context: Context, file: File) = intent(context)
.setData(file.toUri())
/* Flashing is understood as installing / flashing magisk itself */

fun flashIntent(context: Context, file: File) = intent(context, file)
.putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_MAGISK)

fun flashMagisk(context: Context, file: File) =
context.startActivity(flashMagiskIntent(context, file))
fun flash(context: Context, file: File) =
context.startActivity(flashIntent(context, file))

/* Patching is understood as injecting img files with magisk */

fun patchIntent(context: Context, file: File) = intent(context, file)
.putExtra(Const.Key.FLASH_ACTION, Const.Value.PATCH_FILE)

fun patch(context: Context, file: File) =
context.startActivity(patchIntent(context, file))

/* Uninstalling is understood as removing magisk entirely */

fun uninstallIntent(context: Context, file: File) = intent(context, file)
.putExtra(Const.Key.FLASH_ACTION, Const.Value.UNINSTALL)

fun uninstall(context: Context, file: File) =
context.startActivity(uninstallIntent(context, file))

/* Installing is understood as flashing modules / zips */

fun flashModuleIntent(context: Context, file: File) = intent(context)
.setData(file.toUri())
fun installIntent(context: Context, file: File) = intent(context, file)
.putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_ZIP)

fun flashModule(context: Context, file: File) =
context.startActivity(flashModuleIntent(context, file))
fun install(context: Context, file: File) =
context.startActivity(installIntent(context, file))

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import java.util.*

class FlashViewModel(
action: String,
uri: Uri?,
uri: Uri?, // FIXME uri is now flashable file, not additional file
private val resources: Resources
) : MagiskViewModel(), FlashResultListener {

Expand Down

0 comments on commit 12e00c3

Please sign in to comment.