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

View File

@@ -6,11 +6,11 @@ import android.content.Intent
import android.content.ServiceConnection import android.content.ServiceConnection
import android.net.wifi.WifiConfiguration import android.net.wifi.WifiConfiguration
import android.net.wifi.p2p.WifiP2pGroup import android.net.wifi.p2p.WifiP2pGroup
import android.os.Bundle
import android.os.IBinder import android.os.IBinder
import android.view.WindowManager import android.view.WindowManager
import android.widget.EditText import android.widget.EditText
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialog
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.databinding.BaseObservable import androidx.databinding.BaseObservable
@@ -83,19 +83,10 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
} }
fun wps() { fun wps() {
if (binder?.active != true) return if (binder?.active == true) WpsDialogFragment().run {
val dialog = AlertDialog.Builder(parent.requireContext()) setTargetFragment(parent, TetheringFragment.REPEATER_WPS)
.setTitle(R.string.repeater_wps_dialog_title) show(parent.fragmentManager, "WpsDialogFragment")
.setView(R.layout.dialog_wps) }
.setPositiveButton(android.R.string.ok) { dialog, _ ->
binder?.startWps((dialog as AppCompatDialog)
.findViewById<EditText>(android.R.id.edit)!!.text.toString())
}
.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() { 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 { init {
ServiceForegroundConnector(parent, this, RepeaterService::class) ServiceForegroundConnector(parent, this, RepeaterService::class)
} }
@@ -149,6 +160,13 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
data.onStatusChanged() 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) { fun onEditResult(which: Int, data: Intent) {
when (which) { when (which) {
DialogInterface.BUTTON_POSITIVE -> try { DialogInterface.BUTTON_POSITIVE -> try {

View File

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

View File

@@ -4,14 +4,13 @@ import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.net.wifi.WifiConfiguration import android.net.wifi.WifiConfiguration
import android.net.wifi.WifiConfiguration.AuthAlgorithm import android.net.wifi.WifiConfiguration.AuthAlgorithm
import android.os.Bundle
import android.text.Editable import android.text.Editable
import android.text.TextWatcher import android.text.TextWatcher
import android.view.View import android.view.View
import android.widget.EditText import android.widget.EditText
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment import be.mygod.vpnhotspot.AlertDialogFragment
import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.R
import com.google.android.material.textfield.TextInputLayout import com.google.android.material.textfield.TextInputLayout
import java.nio.charset.Charset 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. * 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 * Related: https://android.googlesource.com/platform/packages/apps/Settings/+/defb1183ecb00d6231bac7d934d07f58f90261ea
*/ */
class WifiP2pDialogFragment : DialogFragment(), TextWatcher, DialogInterface.OnClickListener { class WifiP2pDialogFragment : AlertDialogFragment(), TextWatcher, DialogInterface.OnClickListener {
companion object { companion object {
const val TAG = "WifiP2pDialogFragment" const val TAG = "WifiP2pDialogFragment"
const val KEY_CONFIGURATION = "configuration" const val KEY_CONFIGURATION = "configuration"
@@ -45,26 +44,30 @@ class WifiP2pDialogFragment : DialogFragment(), TextWatcher, DialogInterface.OnC
return config return config
} }
override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog = override fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener) {
AlertDialog.Builder(requireContext()).apply { mView = requireActivity().layoutInflater.inflate(R.layout.dialog_wifi_ap, null)
mView = requireActivity().layoutInflater.inflate(R.layout.dialog_wifi_ap, null) setView(mView)
setView(mView) setTitle(R.string.repeater_configure)
setTitle(R.string.repeater_configure) mSsid = mView.findViewById(R.id.ssid)
mSsid = mView.findViewById(R.id.ssid) mPassword = mView.findViewById(R.id.password)
mPassword = mView.findViewById(R.id.password) setPositiveButton(context.getString(R.string.wifi_save), listener)
setPositiveButton(context.getString(R.string.wifi_save), this@WifiP2pDialogFragment) setNegativeButton(context.getString(R.string.wifi_cancel), null)
setNegativeButton(context.getString(R.string.wifi_cancel), null) setNeutralButton(context.getString(R.string.repeater_reset_credentials), listener)
setNeutralButton(context.getString(R.string.repeater_reset_credentials), this@WifiP2pDialogFragment) val arguments = arguments!!
val arguments = arguments!! configurer = arguments.getParcelable(KEY_CONFIGURER)!!
configurer = arguments.getParcelable(KEY_CONFIGURER)!! val mWifiConfig = arguments.getParcelable<WifiConfiguration>(KEY_CONFIGURATION)
val mWifiConfig = arguments.getParcelable<WifiConfiguration>(KEY_CONFIGURATION) if (mWifiConfig != null) {
if (mWifiConfig != null) { mSsid.text = mWifiConfig.SSID
mSsid.text = mWifiConfig.SSID mPassword.setText(mWifiConfig.preSharedKey)
mPassword.setText(mWifiConfig.preSharedKey) }
} mSsid.addTextChangedListener(this@WifiP2pDialogFragment)
mSsid.addTextChangedListener(this@WifiP2pDialogFragment) mPassword.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() { override fun onStart() {
super.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 onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { }
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { } override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { }
override fun afterTextChanged(editable: Editable) = validate() 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)
})
}
} }