Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import com.topjohnwu.magisk.core.R
import com.topjohnwu.magisk.events.DialogBuilder
import com.topjohnwu.magisk.view.MagiskDialog

class DownloadDialog(private val callback: (Uri) -> Unit) : DialogBuilder {
class DownloadDialog(
private val callback: (Uri) -> Unit,
private val onCancel: (() -> Unit)? = null,
) : DialogBuilder {

override fun build(dialog: MagiskDialog) {
val editText = EditText(dialog.context).apply {
Expand All @@ -17,6 +20,8 @@ class DownloadDialog(private val callback: (Uri) -> Unit) : DialogBuilder {
requestFocus()
}

var confirmed = false

dialog.apply {
setTitle(R.string.download_dialog_title)
setView(editText)
Expand All @@ -26,6 +31,7 @@ class DownloadDialog(private val callback: (Uri) -> Unit) : DialogBuilder {
val url = editText.text.toString().trim()
isValidUrl(url)?.let {
doNotDismiss = false
confirmed = true
callback(it)
} ?: run {
doNotDismiss = true
Expand All @@ -37,6 +43,9 @@ class DownloadDialog(private val callback: (Uri) -> Unit) : DialogBuilder {
text = android.R.string.cancel
}
setCancelable(true)
setOnDismissListener {
if (!confirmed) onCancel?.invoke()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.widget.Toast
import androidx.databinding.Bindable
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R
Expand Down Expand Up @@ -46,31 +47,55 @@ class InstallViewModel(svc: NetworkService, markwon: Markwon) : BaseViewModel()
set(value) = set(value, field, { field = it }, BR.step)

private var methodId = -1
private var spuriousMethodId = -1

@get:Bindable
var method
get() = methodId
set(value) = set(value, methodId, { methodId = it }, BR.method) {
if (it == spuriousMethodId) {
spuriousMethodId = -1
return@set
}
when (it) {
R.id.method_patch -> {
GetContentEvent("*/*", UriCallback()).publish()
}
R.id.method_download -> {
DownloadDialog { url -> uri.value = url }.show()
DownloadDialog(
callback = { url -> _uri.value = url },
onCancel = { resetMethod() },
).show()
}
R.id.method_inactive_slot -> {
SecondSlotWarningDialog().show()
}
}
}

val data: LiveData<Uri?> get() = uri
private fun resetMethod() {
spuriousMethodId = methodId
method = -1
}

private val _uri = MutableLiveData<Uri?>()
val data: LiveData<Uri?> get() = _uri

private val sourceObserver = Observer<PatchSource?> { source ->
when (source) {
is PatchSource.File -> _uri.value = source.uri
PatchSource.Cancelled -> resetMethod()
null -> return@Observer
}
patchSource.value = null
}

@get:Bindable
var notes: Spanned = SpannedString("")
set(value) = set(value, field, { field = it }, BR.notes)

init {
patchSource.observeForever(sourceObserver)
viewModelScope.launch(Dispatchers.IO) {
try {
val noteFile = File(AppContext.cacheDir, "${APP_VERSION_CODE}.md")
Expand Down Expand Up @@ -125,14 +150,28 @@ class InstallViewModel(svc: NetworkService, markwon: Markwon) : BaseViewModel()
}
}

override fun onCleared() {
patchSource.removeObserver(sourceObserver)
super.onCleared()
}

private sealed interface PatchSource {
data class File(val uri: Uri) : PatchSource
data object Cancelled : PatchSource
}

@Parcelize
class UriCallback : ContentResultCallback {
override fun onActivityLaunch() {
AppContext.toast(CoreR.string.patch_file_msg, Toast.LENGTH_LONG)
}

override fun onActivityResult(result: Uri) {
uri.value = result
patchSource.value = PatchSource.File(result)
}

override fun onActivityCancel() {
patchSource.value = PatchSource.Cancelled
}
}

Expand All @@ -147,6 +186,6 @@ class InstallViewModel(svc: NetworkService, markwon: Markwon) : BaseViewModel()

companion object {
private const val INSTALL_STATE_KEY = "install_state"
private val uri = MutableLiveData<Uri?>()
private val patchSource = MutableLiveData<PatchSource?>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.topjohnwu.magisk.core.utils.RequestInstall

interface ContentResultCallback: ActivityResultCallback<Uri>, Parcelable {
fun onActivityLaunch() {}
fun onActivityCancel() {}
// Make the result type explicitly non-null
override fun onActivityResult(result: Uri)
}
Expand Down Expand Up @@ -65,6 +66,7 @@ class ActivityExtension(private val activity: ComponentActivity) {
private var contentCallback: ContentResultCallback? = null
private val getContent = activity.registerForActivityResult(GetContent()) {
if (it != null) contentCallback?.onActivityResult(it)
else contentCallback?.onActivityCancel()
contentCallback = null
}

Expand Down