From 268376a7d5ef998a3f13fb13561ba184af209b85 Mon Sep 17 00:00:00 2001 From: Mygod Date: Tue, 18 Sep 2018 17:09:50 +0800 Subject: [PATCH] Use DialogFragments everywhere --- .../mygod/vpnhotspot/AlertDialogFragment.kt | 19 +++++++ .../java/be/mygod/vpnhotspot/EBegFragment.kt | 25 ++++++--- .../vpnhotspot/manage/RepeaterManager.kt | 46 ++++++++++----- .../vpnhotspot/manage/TetheringFragment.kt | 2 + .../net/wifi/WifiP2pDialogFragment.kt | 56 +++++++++---------- 5 files changed, 95 insertions(+), 53 deletions(-) create mode 100644 mobile/src/main/java/be/mygod/vpnhotspot/AlertDialogFragment.kt diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/AlertDialogFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/AlertDialogFragment.kt new file mode 100644 index 00000000..9d44cb06 --- /dev/null +++ b/mobile/src/main/java/be/mygod/vpnhotspot/AlertDialogFragment.kt @@ -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) + } +} diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/EBegFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/EBegFragment.kt index 07e7352b..6f590176 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/EBegFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/EBegFragment.kt @@ -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 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 8afcde6c..992dddb2 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt @@ -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(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() + if (binder?.active == true) WpsDialogFragment().run { + setTargetFragment(parent, TetheringFragment.REPEATER_WPS) + show(parent.fragmentManager, "WpsDialogFragment") + } } 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(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 { 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 b6fa3b65..15d78e8c 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt @@ -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) { @@ -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) } 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 06b800d2..bfe6df7d 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 @@ -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,26 +44,30 @@ class WifiP2pDialogFragment : DialogFragment(), TextWatcher, DialogInterface.OnC return config } - override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog = - AlertDialog.Builder(requireContext()).apply { - 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) - 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)!! - val mWifiConfig = arguments.getParcelable(KEY_CONFIGURATION) - if (mWifiConfig != null) { - mSsid.text = mWifiConfig.SSID - mPassword.setText(mWifiConfig.preSharedKey) - } - mSsid.addTextChangedListener(this@WifiP2pDialogFragment) - mPassword.addTextChangedListener(this@WifiP2pDialogFragment) - }.create() + 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), listener) + setNegativeButton(context.getString(R.string.wifi_cancel), null) + setNeutralButton(context.getString(R.string.repeater_reset_credentials), listener) + val arguments = arguments!! + configurer = arguments.getParcelable(KEY_CONFIGURER)!! + val mWifiConfig = arguments.getParcelable(KEY_CONFIGURATION) + if (mWifiConfig != null) { + mSsid.text = mWifiConfig.SSID + mPassword.setText(mWifiConfig.preSharedKey) + } + mSsid.addTextChangedListener(this@WifiP2pDialogFragment) + mPassword.addTextChangedListener(this@WifiP2pDialogFragment) + } + + 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) - }) - } }