Enhanced AlertDialogFragment
This commit is contained in:
@@ -12,13 +12,13 @@ import android.view.WindowManager
|
||||
import android.widget.EditText
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.os.bundleOf
|
||||
import androidx.databinding.BaseObservable
|
||||
import androidx.databinding.Bindable
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import androidx.lifecycle.get
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.versionedparcelable.VersionedParcelable
|
||||
import be.mygod.vpnhotspot.*
|
||||
import be.mygod.vpnhotspot.App.Companion.app
|
||||
import be.mygod.vpnhotspot.databinding.ListitemRepeaterBinding
|
||||
@@ -28,7 +28,6 @@ import be.mygod.vpnhotspot.util.ServiceForegroundConnector
|
||||
import be.mygod.vpnhotspot.util.formatAddresses
|
||||
import be.mygod.vpnhotspot.widget.SmartSnackbar
|
||||
import timber.log.Timber
|
||||
import java.lang.RuntimeException
|
||||
import java.net.NetworkInterface
|
||||
import java.net.SocketException
|
||||
|
||||
@@ -96,13 +95,10 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
|
||||
if (group != null) try {
|
||||
val config = P2pSupplicantConfiguration(group, binder?.thisDevice?.deviceAddress)
|
||||
holder.config = config
|
||||
WifiP2pDialogFragment().apply {
|
||||
arguments = bundleOf(Pair(WifiP2pDialogFragment.KEY_CONFIGURATION, WifiConfiguration().apply {
|
||||
SSID = group.networkName
|
||||
preSharedKey = config.psk
|
||||
}))
|
||||
setTargetFragment(parent, TetheringFragment.REPEATER_EDIT_CONFIGURATION)
|
||||
}.show(parent.fragmentManager ?: return, WifiP2pDialogFragment.TAG)
|
||||
WifiP2pDialogFragment().withArg(WifiP2pDialogFragment.Arg(WifiConfiguration().apply {
|
||||
SSID = group.networkName
|
||||
preSharedKey = config.psk
|
||||
})).show(parent.fragmentManager ?: return, "WifiP2pDialogFragment")
|
||||
return
|
||||
} catch (e: RuntimeException) {
|
||||
Timber.w(e)
|
||||
@@ -111,11 +107,8 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
|
||||
}
|
||||
}
|
||||
|
||||
class WpsDialogFragment : AlertDialogFragment() {
|
||||
companion object {
|
||||
const val KEY_PIN = "pin"
|
||||
}
|
||||
|
||||
data class WpsRet(val pin: String?) : VersionedParcelable
|
||||
class WpsDialogFragment : AlertDialogFragment<Empty, WpsRet>() {
|
||||
override fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener) {
|
||||
setTitle(R.string.repeater_wps_dialog_title)
|
||||
setView(R.layout.dialog_wps)
|
||||
@@ -123,8 +116,8 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
|
||||
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 val ret get() = WpsRet(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)
|
||||
@@ -166,7 +159,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
|
||||
|
||||
fun onWpsResult(which: Int, data: Intent?) {
|
||||
when (which) {
|
||||
DialogInterface.BUTTON_POSITIVE -> binder?.startWps(data!!.getStringExtra(WpsDialogFragment.KEY_PIN))
|
||||
DialogInterface.BUTTON_POSITIVE -> binder?.startWps(AlertDialogFragment.getRet<WpsRet>(data!!).pin)
|
||||
DialogInterface.BUTTON_NEUTRAL -> binder?.startWps(null)
|
||||
}
|
||||
}
|
||||
@@ -175,7 +168,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
|
||||
when (which) {
|
||||
DialogInterface.BUTTON_POSITIVE -> try {
|
||||
val master = holder.config ?: return
|
||||
val config = data!!.getParcelableExtra<WifiConfiguration>(WifiP2pDialogFragment.KEY_CONFIGURATION)
|
||||
val config = AlertDialogFragment.getRet<WifiP2pDialogFragment.Arg>(data!!).configuration
|
||||
master.update(config.SSID, config.preSharedKey)
|
||||
binder!!.group = null
|
||||
} catch (e: Exception) {
|
||||
|
||||
Reference in New Issue
Block a user