Move edit configuration logic out of WifiP2pDialogFragment

This commit is contained in:
Mygod
2018-09-17 23:35:18 +08:00
parent dbe43624ed
commit 204145ef4f
3 changed files with 31 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
package be.mygod.vpnhotspot.manage package be.mygod.vpnhotspot.manage
import android.content.ComponentName import android.content.ComponentName
import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.content.ServiceConnection import android.content.ServiceConnection
import android.net.wifi.WifiConfiguration import android.net.wifi.WifiConfiguration
@@ -111,7 +112,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
WifiP2pDialogFragment().apply { WifiP2pDialogFragment().apply {
arguments = bundleOf(Pair(WifiP2pDialogFragment.KEY_CONFIGURATION, wifi), arguments = bundleOf(Pair(WifiP2pDialogFragment.KEY_CONFIGURATION, wifi),
Pair(WifiP2pDialogFragment.KEY_CONFIGURER, conf)) Pair(WifiP2pDialogFragment.KEY_CONFIGURER, conf))
setTargetFragment(parent, 0) setTargetFragment(parent, TetheringFragment.REPEATER_EDIT_CONFIGURATION)
}.show(parent.fragmentManager, WifiP2pDialogFragment.TAG) }.show(parent.fragmentManager, WifiP2pDialogFragment.TAG)
return return
} }
@@ -147,4 +148,19 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
binder.groupChanged -= this binder.groupChanged -= this
data.onStatusChanged() data.onStatusChanged()
} }
fun onEditResult(which: Int, data: Intent) {
when (which) {
DialogInterface.BUTTON_POSITIVE -> try {
data.getParcelableExtra<P2pSupplicantConfiguration>(WifiP2pDialogFragment.KEY_CONFIGURER)
.update(data.getParcelableExtra(WifiP2pDialogFragment.KEY_CONFIGURATION))
app.handler.postDelayed(binder!!::requestGroupUpdate, 1000)
} catch (e: RuntimeException) {
e.printStackTrace()
Crashlytics.logException(e)
SmartSnackbar.make(e.localizedMessage).show()
}
DialogInterface.BUTTON_NEUTRAL -> binder!!.resetCredentials()
}
}
} }

View File

@@ -33,6 +33,7 @@ import java.net.SocketException
class TetheringFragment : Fragment(), ServiceConnection { 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
} }
inner class ManagerAdapter : ListAdapter<Manager, RecyclerView.ViewHolder>(Manager) { inner class ManagerAdapter : ListAdapter<Manager, RecyclerView.ViewHolder>(Manager) {
@@ -109,6 +110,13 @@ class TetheringFragment : Fragment(), ServiceConnection {
if (Build.VERSION.SDK_INT >= 27) ManageBar.Data.notifyChange() if (Build.VERSION.SDK_INT >= 27) ManageBar.Data.notifyChange()
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
REPEATER_EDIT_CONFIGURATION -> adapter.repeaterManager.onEditResult(resultCode, data!!)
else -> super.onActivityResult(requestCode, resultCode, data)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if (requestCode == START_LOCAL_ONLY_HOTSPOT) @TargetApi(26) { if (requestCode == START_LOCAL_ONLY_HOTSPOT) @TargetApi(26) {
if (grantResults.firstOrNull() == PackageManager.PERMISSION_GRANTED) { if (grantResults.firstOrNull() == PackageManager.PERMISSION_GRANTED) {

View File

@@ -1,6 +1,7 @@
package be.mygod.vpnhotspot.net.wifi package be.mygod.vpnhotspot.net.wifi
import android.content.DialogInterface import android.content.DialogInterface
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.os.Bundle
@@ -11,11 +12,7 @@ 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 androidx.fragment.app.DialogFragment
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.manage.TetheringFragment
import be.mygod.vpnhotspot.widget.SmartSnackbar
import com.crashlytics.android.Crashlytics
import com.google.android.material.textfield.TextInputLayout import com.google.android.material.textfield.TextInputLayout
import java.nio.charset.Charset import java.nio.charset.Charset
@@ -56,7 +53,7 @@ class WifiP2pDialogFragment : DialogFragment(), TextWatcher, DialogInterface.OnC
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), this@WifiP2pDialogFragment) setPositiveButton(context.getString(R.string.wifi_save), this@WifiP2pDialogFragment)
setNegativeButton(context.getString(R.string.wifi_cancel), this@WifiP2pDialogFragment) 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), this@WifiP2pDialogFragment)
val arguments = arguments!! val arguments = arguments!!
configurer = arguments.getParcelable(KEY_CONFIGURER)!! configurer = arguments.getParcelable(KEY_CONFIGURER)!!
@@ -88,18 +85,9 @@ class WifiP2pDialogFragment : DialogFragment(), TextWatcher, DialogInterface.OnC
override fun afterTextChanged(editable: Editable) = validate() override fun afterTextChanged(editable: Editable) = validate()
override fun onClick(dialog: DialogInterface?, which: Int) { override fun onClick(dialog: DialogInterface?, which: Int) {
when (which) { targetFragment!!.onActivityResult(targetRequestCode, which, Intent().apply {
DialogInterface.BUTTON_POSITIVE -> try { putExtra(KEY_CONFIGURATION, config)
configurer.update(config!!) putExtra(KEY_CONFIGURER, configurer)
app.handler.postDelayed((targetFragment as TetheringFragment).adapter.repeaterManager })
.binder!!::requestGroupUpdate, 1000)
} catch (e: RuntimeException) {
e.printStackTrace()
Crashlytics.logException(e)
SmartSnackbar.make(e.localizedMessage).show()
}
DialogInterface.BUTTON_NEUTRAL ->
(targetFragment as TetheringFragment).adapter.repeaterManager.binder!!.resetCredentials()
}
} }
} }