From 6a72194106083464e6bedda3bed90270cd8c3ae6 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 11 May 2023 13:50:26 +0200 Subject: [PATCH] ui: use modal corruption alert instead of snackbar Signed-off-by: Jason A. Donenfeld --- .../android/updater/SnackbarUpdateShower.kt | 33 ++++++++++++------- ui/src/main/res/values/strings.xml | 3 +- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/ui/src/main/java/com/wireguard/android/updater/SnackbarUpdateShower.kt b/ui/src/main/java/com/wireguard/android/updater/SnackbarUpdateShower.kt index 8c1a8124..30da3b07 100644 --- a/ui/src/main/java/com/wireguard/android/updater/SnackbarUpdateShower.kt +++ b/ui/src/main/java/com/wireguard/android/updater/SnackbarUpdateShower.kt @@ -12,6 +12,7 @@ import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.snackbar.BaseTransientBottomBar import com.google.android.material.snackbar.Snackbar import com.wireguard.android.R @@ -34,7 +35,6 @@ class SnackbarUpdateShower(private val fragment: Fragment) { private val statusSnackbar = makeSnackbar(fragment, view, anchor) private var showingAction: Boolean = false private var showingStatus: Boolean = false - private var permanentAction: Boolean = false private fun makeSnackbar(fragment: Fragment, view: View, anchor: View?): Snackbar { val snackbar = Snackbar.make(fragment.requireContext(), view, "", Snackbar.LENGTH_INDEFINITE) @@ -49,7 +49,7 @@ class SnackbarUpdateShower(private val fragment: Fragment) { snackbar.addCallback(object : BaseTransientBottomBar.BaseCallback() { override fun onDismissed(snackbar: Snackbar?, @DismissEvent event: Int) { super.onDismissed(snackbar, event) - if (event == DISMISS_EVENT_MANUAL || (event == DISMISS_EVENT_ACTION && !permanentAction) || + if (event == DISMISS_EVENT_MANUAL || event == DISMISS_EVENT_ACTION || (snackbar == actionSnackbar && !showingAction) || (snackbar == statusSnackbar && !showingStatus) ) return @@ -62,12 +62,11 @@ class SnackbarUpdateShower(private val fragment: Fragment) { return snackbar } - fun showAction(text: String, action: String, permanent: Boolean = false, listener: View.OnClickListener) { + fun showAction(text: String, action: String, listener: View.OnClickListener) { if (showingStatus) { showingStatus = false statusSnackbar.dismiss() } - permanentAction = permanent actionSnackbar.setText(text) actionSnackbar.setAction(action, listener) if (!showingAction) { @@ -148,15 +147,25 @@ class SnackbarUpdateShower(private val fragment: Fragment) { } is Updater.Progress.Corrupt -> { - snackbar.showAction(context.getString(R.string.updater_corrupt), context.getString(R.string.updater_corrupt_navigate), true) { - val intent = Intent(Intent.ACTION_VIEW) - intent.data = Uri.parse(progress.downloadUrl) - try { + MaterialAlertDialogBuilder(context) + .setTitle(R.string.updater_corrupt_title) + .setMessage(R.string.updater_corrupt_message) + .setPositiveButton(R.string.updater_corrupt_navigate) { _, _ -> + val intent = Intent(Intent.ACTION_VIEW) + intent.data = Uri.parse(progress.downloadUrl) + try { + context.startActivity(intent) + } catch (e: Throwable) { + Toast.makeText(context, ErrorMessages[e], Toast.LENGTH_SHORT).show() + } + }.setCancelable(false).setOnDismissListener { + val intent = Intent(Intent.ACTION_MAIN) + intent.addCategory(Intent.CATEGORY_HOME) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) context.startActivity(intent) - } catch (e: Throwable) { - Toast.makeText(context, ErrorMessages[e], Toast.LENGTH_SHORT).show() - } - } + System.exit(0) + }.show() } } }.launchIn(fragment.lifecycleScope) diff --git a/ui/src/main/res/values/strings.xml b/ui/src/main/res/values/strings.xml index acaa9dcc..4c9ab2d4 100644 --- a/ui/src/main/res/values/strings.xml +++ b/ui/src/main/res/values/strings.xml @@ -240,7 +240,8 @@ Downloading update: %s Installing update… Update failure: %s. Will retry momentarily… - This application is corrupt. Please re-download it. + Application Corrupt + This application is corrupt. Please re-download the APK from the website linked below. After, uninstall this application, and reinstall it from downloaded APK. Open Website %1$s backend %2$s Checking %s backend version