From 204145ef4fb5cc33f680b151c91e6f3660df40bc Mon Sep 17 00:00:00 2001 From: Mygod Date: Mon, 17 Sep 2018 23:35:18 +0800 Subject: [PATCH] Move edit configuration logic out of WifiP2pDialogFragment --- .../vpnhotspot/manage/RepeaterManager.kt | 18 +++++++++++++- .../vpnhotspot/manage/TetheringFragment.kt | 8 +++++++ .../net/wifi/WifiP2pDialogFragment.kt | 24 +++++-------------- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt index e3f4765f..8afcde6c 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt @@ -1,6 +1,7 @@ package be.mygod.vpnhotspot.manage import android.content.ComponentName +import android.content.DialogInterface import android.content.Intent import android.content.ServiceConnection import android.net.wifi.WifiConfiguration @@ -111,7 +112,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic WifiP2pDialogFragment().apply { arguments = bundleOf(Pair(WifiP2pDialogFragment.KEY_CONFIGURATION, wifi), Pair(WifiP2pDialogFragment.KEY_CONFIGURER, conf)) - setTargetFragment(parent, 0) + setTargetFragment(parent, TetheringFragment.REPEATER_EDIT_CONFIGURATION) }.show(parent.fragmentManager, WifiP2pDialogFragment.TAG) return } @@ -147,4 +148,19 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic binder.groupChanged -= this data.onStatusChanged() } + + fun onEditResult(which: Int, data: Intent) { + when (which) { + DialogInterface.BUTTON_POSITIVE -> try { + data.getParcelableExtra(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() + } + } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt index 22322909..b6fa3b65 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt @@ -33,6 +33,7 @@ import java.net.SocketException class TetheringFragment : Fragment(), ServiceConnection { companion object { const val START_LOCAL_ONLY_HOTSPOT = 1 + const val REPEATER_EDIT_CONFIGURATION = 2 } inner class ManagerAdapter : ListAdapter(Manager) { @@ -109,6 +110,13 @@ class TetheringFragment : Fragment(), ServiceConnection { 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, grantResults: IntArray) { if (requestCode == START_LOCAL_ONLY_HOTSPOT) @TargetApi(26) { if (grantResults.firstOrNull() == PackageManager.PERMISSION_GRANTED) { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pDialogFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pDialogFragment.kt index ecde3210..06b800d2 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pDialogFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pDialogFragment.kt @@ -1,6 +1,7 @@ package be.mygod.vpnhotspot.net.wifi import android.content.DialogInterface +import android.content.Intent import android.net.wifi.WifiConfiguration import android.net.wifi.WifiConfiguration.AuthAlgorithm import android.os.Bundle @@ -11,11 +12,7 @@ import android.widget.EditText import android.widget.TextView import androidx.appcompat.app.AlertDialog import androidx.fragment.app.DialogFragment -import be.mygod.vpnhotspot.App.Companion.app 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 java.nio.charset.Charset @@ -56,7 +53,7 @@ class WifiP2pDialogFragment : DialogFragment(), TextWatcher, DialogInterface.OnC mSsid = mView.findViewById(R.id.ssid) mPassword = mView.findViewById(R.id.password) 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) val arguments = arguments!! configurer = arguments.getParcelable(KEY_CONFIGURER)!! @@ -88,18 +85,9 @@ class WifiP2pDialogFragment : DialogFragment(), TextWatcher, DialogInterface.OnC override fun afterTextChanged(editable: Editable) = validate() override fun onClick(dialog: DialogInterface?, which: Int) { - when (which) { - DialogInterface.BUTTON_POSITIVE -> try { - configurer.update(config!!) - 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() - } + targetFragment!!.onActivityResult(targetRequestCode, which, Intent().apply { + putExtra(KEY_CONFIGURATION, config) + putExtra(KEY_CONFIGURER, configurer) + }) } }