Add beta warning when clicking the restore DMG button

This commit is contained in:
Davide Depau 2018-09-01 18:20:43 +02:00
parent aa78d36cb0
commit 25fc67c67b
4 changed files with 109 additions and 10 deletions

View file

@ -15,14 +15,29 @@ import eu.depau.etchdroid.R
import eu.depau.etchdroid.StateKeeper
import eu.depau.etchdroid.enums.FlashMethod
import eu.depau.etchdroid.kotlin_exts.snackbar
import eu.depau.etchdroid.utils.DoNotShowAgainDialogFragment
import kotlinx.android.synthetic.main.activity_start.*
import java.io.File
class StartActivity : ActivityBase() {
val TAG = "StartActivity"
val READ_REQUEST_CODE = 42
val READ_EXTERNAL_STORAGE_PERMISSION = 29
var delayedButtonClicked: View? = null
val DISMISSED_DIALOGS_PREFS = "dismissed_dialogs"
var delayedButtonClicked: Boolean = false
var shouldShowDMGAlertDialog: Boolean
get() {
val settings = getSharedPreferences(DISMISSED_DIALOGS_PREFS, 0)
return !settings.getBoolean("DMG_beta_alert", false)
}
set(value) {
val settings = getSharedPreferences(DISMISSED_DIALOGS_PREFS, 0)
val editor = settings.edit()
editor.putBoolean("DMG_beta_alert", !value)
editor.apply()
}
override fun onCreate(savedInstanceState: Bundle?) {
@ -30,13 +45,37 @@ class StartActivity : ActivityBase() {
setContentView(R.layout.activity_start)
}
fun onButtonClicked(view: View) {
fun onButtonClicked(view: View) = onButtonClicked(view, true)
private fun onButtonClicked(view: View?, showDialog: Boolean = true) {
if (view != null)
StateKeeper.flashMethod = when (view.id) {
R.id.btn_image_raw -> FlashMethod.FLASH_API
R.id.btn_image_dmg -> FlashMethod.FLASH_DMG_API
else -> null
}
if (StateKeeper.flashMethod != FlashMethod.FLASH_DMG_API || !shouldShowDMGAlertDialog || !showDialog)
showFilePicker()
else
showDMGBetaAlertDialog()
}
fun showDMGBetaAlertDialog() {
val dialogFragment = DoNotShowAgainDialogFragment()
dialogFragment.title = getString(R.string.here_be_dragons)
dialogFragment.message = getString(R.string.dmg_alert_dialog_text)
dialogFragment.closeButton = getString(R.string.i_understand)
dialogFragment.listener = object : DoNotShowAgainDialogFragment.DialogListener {
override fun onDialogClose(dialog: DoNotShowAgainDialogFragment, showAgain: Boolean) {
shouldShowDMGAlertDialog = showAgain
showFilePicker()
}
}
dialogFragment.show(supportFragmentManager, "DMGBetaAlertDialogFragment")
}
fun showFilePicker() {
when (StateKeeper.flashMethod) {
FlashMethod.FLASH_API -> {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
@ -62,7 +101,7 @@ class StartActivity : ActivityBase() {
nextStep()
}
} else {
delayedButtonClicked = view
delayedButtonClicked = true
}
}
FlashMethod.FLASH_UNETBOOTIN -> {
@ -95,8 +134,8 @@ class StartActivity : ActivityBase() {
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
READ_EXTERNAL_STORAGE_PERMISSION -> {
if (delayedButtonClicked != null)
onButtonClicked(delayedButtonClicked!!)
if (delayedButtonClicked)
onButtonClicked(null, showDialog = false)
return
}
else -> {

View file

@ -0,0 +1,40 @@
package eu.depau.etchdroid.utils
import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import eu.depau.etchdroid.R
import kotlinx.android.synthetic.main.do_not_show_again.view.*
class DoNotShowAgainDialogFragment() : DialogFragment() {
var title: String? = null
var closeButton: String? = null
var message: String? = null
var listener: DialogListener? = null
interface DialogListener {
fun onDialogClose(dialog: DoNotShowAgainDialogFragment, showAgain: Boolean)
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
// Build the dialog and set up the button click handlers
val builder = AlertDialog.Builder(activity!!)
val inflater = LayoutInflater.from(this.context)
val dnsaLayout = inflater.inflate(R.layout.do_not_show_again, null)
val doNotShowAgainCB = dnsaLayout.do_not_show_again
builder
.setTitle(title)
.setMessage(message)
.setView(dnsaLayout)
.setPositiveButton(closeButton) { _, _ ->
listener?.onDialogClose(this@DoNotShowAgainDialogFragment, !doNotShowAgainCB.isChecked)
}
return builder.create()
}
}

View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dp" >
<CheckBox
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/do_not_show_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/do_not_show_again"
android:layout_marginStart="10dp">
</CheckBox>
</LinearLayout>

View file

@ -91,4 +91,8 @@
<string name="write_apple_dmg">Write Apple® DMG image</string>
<string name="dmg_image_desc">Restore an image created with macOS Disk Utility</string>
<string name="no_usb_drives_detected">No USB drives detected</string>
<string name="do_not_show_again">Do not show again</string>
<string name="here_be_dragons">Here be dragons</string>
<string name="i_understand">I understand, continue</string>
<string name="dmg_alert_dialog_text">While in most cases DMG images can be restored just fine, sometimes there may be inconsistencies. You\'ve been warned.</string>
</resources>