From 4292ddd0ae64b579976b9ded9f843cad9b5b32e5 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Tue, 8 Oct 2019 20:29:11 +0200 Subject: [PATCH] Added custom install dialogs --- .../magisk/model/events/dialog/DialogEvent.kt | 16 +++ .../events/dialog/MagiskInstallDialog.kt | 81 ++++++++++++ .../events/dialog/ManagerInstallDialog.kt | 36 ++++++ .../magisk/redesign/home/HomeViewModel.kt | 14 ++- .../com/topjohnwu/magisk/view/MagiskDialog.kt | 10 +- .../main/res/layout/dialog_magisk_base.xml | 115 ++++++++++++++---- app/src/main/res/layout/fragment_home_md2.xml | 4 + 7 files changed, 249 insertions(+), 27 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/events/dialog/DialogEvent.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/events/dialog/MagiskInstallDialog.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/events/dialog/ManagerInstallDialog.kt diff --git a/app/src/main/java/com/topjohnwu/magisk/model/events/dialog/DialogEvent.kt b/app/src/main/java/com/topjohnwu/magisk/model/events/dialog/DialogEvent.kt new file mode 100644 index 000000000..dc41fe709 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/events/dialog/DialogEvent.kt @@ -0,0 +1,16 @@ +package com.topjohnwu.magisk.model.events.dialog + +import android.content.Context +import com.skoumal.teanity.viewevents.ViewEvent +import com.topjohnwu.magisk.model.events.ContextExecutor +import com.topjohnwu.magisk.view.MagiskDialog + +abstract class DialogEvent : ViewEvent(), ContextExecutor { + + override fun invoke(context: Context) { + MagiskDialog(context).apply(this::build).reveal() + } + + abstract fun build(dialog: MagiskDialog) + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/model/events/dialog/MagiskInstallDialog.kt b/app/src/main/java/com/topjohnwu/magisk/model/events/dialog/MagiskInstallDialog.kt new file mode 100644 index 000000000..d25f7415d --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/events/dialog/MagiskInstallDialog.kt @@ -0,0 +1,81 @@ +package com.topjohnwu.magisk.model.events.dialog + +import com.topjohnwu.magisk.Info +import com.topjohnwu.magisk.R +import com.topjohnwu.magisk.extensions.res +import com.topjohnwu.magisk.model.events.OpenInappLinkEvent +import com.topjohnwu.magisk.view.MagiskDialog +import com.topjohnwu.magisk.view.MarkDownWindow +import com.topjohnwu.superuser.Shell +import com.topjohnwu.superuser.ShellUtils + +class MagiskInstallDialog : DialogEvent() { + + override fun build(dialog: MagiskDialog) { + with(dialog) { + + val filename = + "Magisk v${Info.remote.magisk.version}(${Info.remote.magisk.versionCode})" + applyTitle(R.string.repo_install_title.res(R.string.magisk.res())) + applyMessage(R.string.repo_install_msg.res(filename)) + setCancelable(true) + applyButton(MagiskDialog.ButtonType.POSITIVE) { + titleRes = R.string.install + preventDismiss = true + onClick { + updateForInstallMethod(dialog) + } + } + if (Info.remote.magisk.note.isEmpty()) return + applyButton(MagiskDialog.ButtonType.NEGATIVE) { + titleRes = R.string.release_notes + onClick { + if (Info.remote.magisk.note.contains("forum.xda-developers")) { + OpenInappLinkEvent(Info.remote.magisk.note).invoke(context) + } else { + MarkDownWindow.show(context, null, Info.remote.magisk.note) + } + } + } + } + } + + private fun updateForInstallMethod(dialog: MagiskDialog) { + with(dialog) { + applyTitle(R.string.select_method) + applyMessage("") + applyButton(MagiskDialog.ButtonType.POSITIVE) { + titleRes = R.string.download_zip_only + preventDismiss = false + onClick { + TODO() + } + } + applyButton(MagiskDialog.ButtonType.NEUTRAL) { + titleRes = R.string.select_patch_file + onClick { + TODO() + } + } + if (!Shell.rootAccess()) return + applyButton(MagiskDialog.ButtonType.NEGATIVE) { + titleRes = R.string.direct_install + onClick { + TODO() + } + } + if (!isABDevice()) return + applyButton(MagiskDialog.ButtonType.IDGAF) { + titleRes = R.string.install_inactive_slot + onClick { + TODO() + } + } + } + } + + private fun isABDevice() = ShellUtils + .fastCmd("grep_prop ro.build.ab_update") + .let { it.isNotEmpty() && it.toBoolean() } + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/model/events/dialog/ManagerInstallDialog.kt b/app/src/main/java/com/topjohnwu/magisk/model/events/dialog/ManagerInstallDialog.kt new file mode 100644 index 000000000..1eb563602 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/events/dialog/ManagerInstallDialog.kt @@ -0,0 +1,36 @@ +package com.topjohnwu.magisk.model.events.dialog + +import com.topjohnwu.magisk.Info +import com.topjohnwu.magisk.R +import com.topjohnwu.magisk.extensions.res +import com.topjohnwu.magisk.model.download.DownloadService +import com.topjohnwu.magisk.model.entity.internal.Configuration +import com.topjohnwu.magisk.model.entity.internal.DownloadSubject +import com.topjohnwu.magisk.view.MagiskDialog +import com.topjohnwu.magisk.view.MarkDownWindow + +class ManagerInstallDialog : DialogEvent() { + + override fun build(dialog: MagiskDialog) { + with(dialog) { + val subject = DownloadSubject.Manager(Configuration.APK.Upgrade) + + applyTitle(R.string.repo_install_title.res(R.string.app_name.res())) + applyMessage(R.string.repo_install_msg.res(subject.title)) + + setCancelable(true) + + applyButton(MagiskDialog.ButtonType.POSITIVE) { + titleRes = R.string.install + onClick { DownloadService(context) { this.subject = subject } } + } + + if (Info.remote.app.note.isEmpty()) return + applyButton(MagiskDialog.ButtonType.NEGATIVE) { + titleRes = R.string.app_changelog + onClick { MarkDownWindow.show(context, null, Info.remote.app.note) } + } + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeViewModel.kt index 2a6dd94f0..79862dc58 100644 --- a/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/home/HomeViewModel.kt @@ -15,6 +15,8 @@ import com.topjohnwu.magisk.model.entity.ManagerJson import com.topjohnwu.magisk.model.entity.UpdateInfo import com.topjohnwu.magisk.model.entity.recycler.HomeItem import com.topjohnwu.magisk.model.events.OpenInappLinkEvent +import com.topjohnwu.magisk.model.events.dialog.MagiskInstallDialog +import com.topjohnwu.magisk.model.events.dialog.ManagerInstallDialog import com.topjohnwu.magisk.model.observer.Observer import com.topjohnwu.magisk.redesign.compat.CompatViewModel import com.topjohnwu.magisk.ui.home.MagiskState @@ -50,11 +52,10 @@ class HomeViewModel( val stateVersionUpdateManager = KObservableField("") val stateHideManagerName = R.string.manager.res().let { - val result = R.string.manager.res() if (!statePackageOriginal) { - result.replaceRandomWithSpecial(3) + it.replaceRandomWithSpecial(3) } else { - result + it } } @@ -102,9 +103,14 @@ class HomeViewModel( } } - fun onDeletePressed() {} fun onLinkPressed(link: String) = OpenInappLinkEvent(link).publish() + fun onDeletePressed() {} + + fun onManagerPressed() = ManagerInstallDialog().publish() + + fun onMagiskPressed() = MagiskInstallDialog().publish() + } @Suppress("unused") diff --git a/app/src/main/java/com/topjohnwu/magisk/view/MagiskDialog.kt b/app/src/main/java/com/topjohnwu/magisk/view/MagiskDialog.kt index a9ad818ab..fdcc01a1c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/view/MagiskDialog.kt +++ b/app/src/main/java/com/topjohnwu/magisk/view/MagiskDialog.kt @@ -58,10 +58,13 @@ class MagiskDialog @JvmOverloads constructor( val isEnabled = KObservableField(true) var onClickAction: OnDialogButtonClickListener = {} + var preventDismiss = false fun clicked() { onClickAction(this@MagiskDialog) - dismiss() + if (!preventDismiss) { + dismiss() + } } } @@ -86,6 +89,11 @@ class MagiskDialog @JvmOverloads constructor( set(value) { button.isEnabled.value = value } + var preventDismiss: Boolean + get() = button.preventDismiss + set(value) { + button.preventDismiss = value + } fun onClick(listener: OnDialogButtonClickListener) { button.onClickAction = listener diff --git a/app/src/main/res/layout/dialog_magisk_base.xml b/app/src/main/res/layout/dialog_magisk_base.xml index 134113f17..f18c144b6 100644 --- a/app/src/main/res/layout/dialog_magisk_base.xml +++ b/app/src/main/res/layout/dialog_magisk_base.xml @@ -19,6 +19,7 @@ style="@style/Widget.Card" android:layout_width="0dp" android:layout_height="wrap_content" + app:cardUseCompatPadding="true" app:cardElevation="@dimen/margin_generic" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" @@ -26,7 +27,7 @@ app:layout_constraintTop_toTopOf="parent" app:layout_constraintWidth_default="percent" app:layout_constraintWidth_max="400dp" - app:layout_constraintWidth_percent=".9"> + app:layout_constraintWidth_percent="1"> @@ -75,12 +76,12 @@ @@ -124,7 +125,7 @@ @@ -132,13 +133,30 @@ + gone="@{data.buttonPositive.icon == 0 && data.buttonPositive.title.length == + 0}" - + + + gone="@{data.buttonNeutral.icon == 0 && data.buttonNeutral.title.length == + 0}" - + + + gone="@{data.buttonNegative.icon == 0 && data.buttonNegative.title.length == + 0}" - + + + gone="@{data.buttonIDGAF.icon == 0 && data.buttonIDGAF.title.length == 0}" - + + diff --git a/app/src/main/res/layout/fragment_home_md2.xml b/app/src/main/res/layout/fragment_home_md2.xml index 39825bea0..4e360995b 100644 --- a/app/src/main/res/layout/fragment_home_md2.xml +++ b/app/src/main/res/layout/fragment_home_md2.xml @@ -109,6 +109,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" + android:onClick="@{() -> viewModel.onMagiskPressed()}" android:text="@string/update" app:icon="@drawable/ic_update_md2" app:iconGravity="textEnd" @@ -123,6 +124,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" + android:onClick="@{() -> viewModel.onMagiskPressed()}" android:text="@string/install" app:icon="@drawable/ic_install" app:iconGravity="textEnd" @@ -208,6 +210,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" + android:onClick="@{() -> viewModel.onManagerPressed()}" android:text="@string/update" app:icon="@drawable/ic_update_md2" app:iconGravity="textEnd" @@ -220,6 +223,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" + android:onClick="@{() -> viewModel.onManagerPressed()}" android:text="@string/install" app:icon="@drawable/ic_install" app:iconGravity="textEnd"