Use DialogFragments everywhere

This commit is contained in:
Mygod
2018-09-18 17:09:50 +08:00
parent 204145ef4f
commit 268376a7d5
5 changed files with 95 additions and 53 deletions

View File

@@ -0,0 +1,19 @@
package be.mygod.vpnhotspot
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
abstract class AlertDialogFragment : DialogFragment(), DialogInterface.OnClickListener {
protected abstract fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener)
open val data: Intent? get() = null
override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog =
AlertDialog.Builder(requireContext()).also { it.prepare(this) }.create()
override fun onClick(dialog: DialogInterface?, which: Int) {
targetFragment!!.onActivityResult(targetRequestCode, which, data)
}
}

View File

@@ -1,6 +1,5 @@
package be.mygod.vpnhotspot
import android.app.AlertDialog
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
@@ -11,7 +10,9 @@ import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.Spinner
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import com.android.billingclient.api.*
import com.crashlytics.android.Crashlytics
@@ -23,6 +24,17 @@ class EBegFragment : DialogFragment(), PurchasesUpdatedListener, BillingClientSt
SkuDetailsResponseListener, ConsumeResponseListener {
companion object {
private const val TAG = "EBegFragment"
private const val KEY_TITLE = "title"
private const val KEY_MESSAGE = "message"
}
class MessageDialogFragment : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?) = AlertDialog.Builder(requireContext()).apply {
val arguments = arguments!!
setTitle(arguments.getInt(KEY_TITLE, 0))
setMessage(arguments.getInt(KEY_MESSAGE, 0))
setNeutralButton(R.string.donations__button_close, null)
}.create()
}
private lateinit var billingClient: BillingClient
@@ -59,14 +71,9 @@ class EBegFragment : DialogFragment(), PurchasesUpdatedListener, BillingClientSt
}
}
private fun openDialog(@StringRes title: Int, @StringRes message: Int) {
AlertDialog.Builder(activity ?: return).apply {
setTitle(title)
setMessage(message)
isCancelable = true
setNeutralButton(R.string.donations__button_close) { dialog, _ -> dialog.dismiss() }
}.show()
}
private fun openDialog(@StringRes title: Int, @StringRes message: Int) = MessageDialogFragment().apply {
arguments = bundleOf(Pair(KEY_TITLE, title), Pair(KEY_MESSAGE, message))
}.show(fragmentManager, "MessageDialogFragment")
override fun onBillingServiceDisconnected() {
skus = null

View File

@@ -6,11 +6,11 @@ import android.content.Intent
import android.content.ServiceConnection
import android.net.wifi.WifiConfiguration
import android.net.wifi.p2p.WifiP2pGroup
import android.os.Bundle
import android.os.IBinder
import android.view.WindowManager
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialog
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.databinding.BaseObservable
@@ -83,19 +83,10 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
}
fun wps() {
if (binder?.active != true) return
val dialog = AlertDialog.Builder(parent.requireContext())
.setTitle(R.string.repeater_wps_dialog_title)
.setView(R.layout.dialog_wps)
.setPositiveButton(android.R.string.ok) { dialog, _ ->
binder?.startWps((dialog as AppCompatDialog)
.findViewById<EditText>(android.R.id.edit)!!.text.toString())
if (binder?.active == true) WpsDialogFragment().run {
setTargetFragment(parent, TetheringFragment.REPEATER_WPS)
show(parent.fragmentManager, "WpsDialogFragment")
}
.setNegativeButton(android.R.string.cancel, null)
.setNeutralButton(R.string.repeater_wps_dialog_pbc) { _, _ -> binder?.startWps(null) }
.create()
dialog.window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
dialog.show()
}
fun editConfigurations() {
@@ -121,6 +112,26 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
}
}
class WpsDialogFragment : AlertDialogFragment() {
companion object {
const val KEY_PIN = "pin"
}
override fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener) {
setTitle(R.string.repeater_wps_dialog_title)
setView(R.layout.dialog_wps)
setPositiveButton(android.R.string.ok, listener)
setNegativeButton(android.R.string.cancel, null)
setNeutralButton(R.string.repeater_wps_dialog_pbc, listener)
}
override val data: Intent get() = Intent()
.putExtra(KEY_PIN, dialog.findViewById<EditText>(android.R.id.edit)!!.text.toString())
override fun onCreateDialog(savedInstanceState: Bundle?) = super.onCreateDialog(savedInstanceState).apply {
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
}
}
init {
ServiceForegroundConnector(parent, this, RepeaterService::class)
}
@@ -149,6 +160,13 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
data.onStatusChanged()
}
fun onWpsResult(which: Int, data: Intent) {
when (which) {
DialogInterface.BUTTON_POSITIVE -> binder?.startWps(data.getStringExtra(WpsDialogFragment.KEY_PIN))
DialogInterface.BUTTON_NEUTRAL -> binder?.startWps(null)
}
}
fun onEditResult(which: Int, data: Intent) {
when (which) {
DialogInterface.BUTTON_POSITIVE -> try {

View File

@@ -34,6 +34,7 @@ class TetheringFragment : Fragment(), ServiceConnection {
companion object {
const val START_LOCAL_ONLY_HOTSPOT = 1
const val REPEATER_EDIT_CONFIGURATION = 2
const val REPEATER_WPS = 3
}
inner class ManagerAdapter : ListAdapter<Manager, RecyclerView.ViewHolder>(Manager) {
@@ -112,6 +113,7 @@ class TetheringFragment : Fragment(), ServiceConnection {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
REPEATER_WPS -> adapter.repeaterManager.onWpsResult(resultCode, data!!)
REPEATER_EDIT_CONFIGURATION -> adapter.repeaterManager.onEditResult(resultCode, data!!)
else -> super.onActivityResult(requestCode, resultCode, data)
}

View File

@@ -4,14 +4,13 @@ import android.content.DialogInterface
import android.content.Intent
import android.net.wifi.WifiConfiguration
import android.net.wifi.WifiConfiguration.AuthAlgorithm
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import be.mygod.vpnhotspot.AlertDialogFragment
import be.mygod.vpnhotspot.R
import com.google.android.material.textfield.TextInputLayout
import java.nio.charset.Charset
@@ -22,7 +21,7 @@ import java.nio.charset.Charset
* This dialog has been deprecated in API 28, but we are still using it since it works better for our purposes.
* Related: https://android.googlesource.com/platform/packages/apps/Settings/+/defb1183ecb00d6231bac7d934d07f58f90261ea
*/
class WifiP2pDialogFragment : DialogFragment(), TextWatcher, DialogInterface.OnClickListener {
class WifiP2pDialogFragment : AlertDialogFragment(), TextWatcher, DialogInterface.OnClickListener {
companion object {
const val TAG = "WifiP2pDialogFragment"
const val KEY_CONFIGURATION = "configuration"
@@ -45,16 +44,15 @@ class WifiP2pDialogFragment : DialogFragment(), TextWatcher, DialogInterface.OnC
return config
}
override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog =
AlertDialog.Builder(requireContext()).apply {
override fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener) {
mView = requireActivity().layoutInflater.inflate(R.layout.dialog_wifi_ap, null)
setView(mView)
setTitle(R.string.repeater_configure)
mSsid = mView.findViewById(R.id.ssid)
mPassword = mView.findViewById(R.id.password)
setPositiveButton(context.getString(R.string.wifi_save), this@WifiP2pDialogFragment)
setPositiveButton(context.getString(R.string.wifi_save), listener)
setNegativeButton(context.getString(R.string.wifi_cancel), null)
setNeutralButton(context.getString(R.string.repeater_reset_credentials), this@WifiP2pDialogFragment)
setNeutralButton(context.getString(R.string.repeater_reset_credentials), listener)
val arguments = arguments!!
configurer = arguments.getParcelable(KEY_CONFIGURER)!!
val mWifiConfig = arguments.getParcelable<WifiConfiguration>(KEY_CONFIGURATION)
@@ -64,7 +62,12 @@ class WifiP2pDialogFragment : DialogFragment(), TextWatcher, DialogInterface.OnC
}
mSsid.addTextChangedListener(this@WifiP2pDialogFragment)
mPassword.addTextChangedListener(this@WifiP2pDialogFragment)
}.create()
}
override val data get() = Intent().apply {
putExtra(KEY_CONFIGURATION, config)
putExtra(KEY_CONFIGURER, configurer)
}
override fun onStart() {
super.onStart()
@@ -83,11 +86,4 @@ class WifiP2pDialogFragment : DialogFragment(), TextWatcher, DialogInterface.OnC
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { }
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { }
override fun afterTextChanged(editable: Editable) = validate()
override fun onClick(dialog: DialogInterface?, which: Int) {
targetFragment!!.onActivityResult(targetRequestCode, which, Intent().apply {
putExtra(KEY_CONFIGURATION, config)
putExtra(KEY_CONFIGURER, configurer)
})
}
}