Fix editing Repeater config

This commit is contained in:
Mygod
2019-07-17 17:14:27 +08:00
parent 7a4264e2df
commit 3bd81b942f
4 changed files with 27 additions and 20 deletions

View File

@@ -67,7 +67,7 @@ def aux = [
'com.crashlytics.sdk.android:crashlytics:2.10.1', 'com.crashlytics.sdk.android:crashlytics:2.10.1',
'com.google.firebase:firebase-core:17.0.1', 'com.google.firebase:firebase-core:17.0.1',
] ]
def lifecycleVersion = '2.1.0-rc01' def lifecycleVersion = '2.2.0-alpha02'
def roomVersion = '2.1.0' def roomVersion = '2.1.0'
dependencies { dependencies {
kapt "androidx.room:room-compiler:$roomVersion" kapt "androidx.room:room-compiler:$roomVersion"
@@ -78,6 +78,7 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion" implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleVersion" implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion" implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
implementation 'androidx.preference:preference:1.1.0-rc01' implementation 'androidx.preference:preference:1.1.0-rc01'
implementation "androidx.room:room-ktx:$roomVersion" implementation "androidx.room:room-ktx:$roomVersion"

View File

@@ -19,6 +19,7 @@ import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.get import androidx.lifecycle.get
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.observe import androidx.lifecycle.observe
import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@@ -41,7 +42,10 @@ import be.mygod.vpnhotspot.util.computeIfAbsentCompat
import be.mygod.vpnhotspot.util.toPluralInt import be.mygod.vpnhotspot.util.toPluralInt
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.text.NumberFormat import java.text.NumberFormat
class ClientsFragment : Fragment() { class ClientsFragment : Fragment() {
@@ -149,11 +153,13 @@ class ClientsFragment : Fragment() {
} }
R.id.stats -> { R.id.stats -> {
binding.client?.let { client -> binding.client?.let { client ->
scope.launch { lifecycleScope.launchWhenCreated {
StatsDialogFragment().withArg(StatsArg( withContext(Dispatchers.Unconfined) {
client.title.value ?: return@launch, StatsDialogFragment().withArg(StatsArg(
AppDatabase.instance.trafficRecordDao.queryStats(client.mac) client.title.value ?: return@withContext,
)).show(this@ClientsFragment) AppDatabase.instance.trafficRecordDao.queryStats(client.mac)
)).show(this@ClientsFragment)
}
} }
} }
true true
@@ -204,7 +210,6 @@ class ClientsFragment : Fragment() {
} }
} }
private val scope = MainScope() + Dispatchers.Unconfined
private lateinit var binding: FragmentClientsBinding private lateinit var binding: FragmentClientsBinding
private val adapter = ClientAdapter() private val adapter = ClientAdapter()
private var rates = HashMap<Pair<String, Long>, TrafficRate>() private var rates = HashMap<Pair<String, Long>, TrafficRate>()
@@ -236,9 +241,4 @@ class ClientsFragment : Fragment() {
TrafficRecorder.foregroundListeners -= this TrafficRecorder.foregroundListeners -= this
super.onStop() super.onStop()
} }
override fun onDestroy() {
scope.cancel()
super.onDestroy()
}
} }

View File

@@ -31,6 +31,8 @@ import be.mygod.vpnhotspot.util.ServiceForegroundConnector
import be.mygod.vpnhotspot.util.formatAddresses import be.mygod.vpnhotspot.util.formatAddresses
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import java.net.NetworkInterface import java.net.NetworkInterface
import java.net.SocketException import java.net.SocketException
@@ -197,7 +199,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
SmartSnackbar.make(R.string.repeater_configure_failure).show() SmartSnackbar.make(R.string.repeater_configure_failure).show()
return null return null
} }
fun updateConfiguration(config: WifiConfiguration) { suspend fun updateConfiguration(config: WifiConfiguration) {
if (Build.VERSION.SDK_INT >= 29) { if (Build.VERSION.SDK_INT >= 29) {
RepeaterService.networkName = config.SSID RepeaterService.networkName = config.SSID
RepeaterService.passphrase = config.preSharedKey RepeaterService.passphrase = config.preSharedKey
@@ -209,7 +211,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
} }
} else @Suppress("DEPRECATION") holder.config?.let { master -> } else @Suppress("DEPRECATION") holder.config?.let { master ->
if (binder?.group?.networkName != config.SSID || master.psk != config.preSharedKey) try { if (binder?.group?.networkName != config.SSID || master.psk != config.preSharedKey) try {
master.update(config.SSID, config.preSharedKey) withContext(Dispatchers.Default) { master.update(config.SSID, config.preSharedKey) }
binder!!.group = null binder!!.group = null
} catch (e: Exception) { } catch (e: Exception) {
Timber.w(e) Timber.w(e)

View File

@@ -14,6 +14,7 @@ import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.ListAdapter
@@ -31,6 +32,8 @@ import be.mygod.vpnhotspot.util.broadcastReceiver
import be.mygod.vpnhotspot.util.isNotGone import be.mygod.vpnhotspot.util.isNotGone
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import java.lang.IllegalArgumentException import java.lang.IllegalArgumentException
import java.lang.reflect.InvocationTargetException import java.lang.reflect.InvocationTargetException
@@ -126,10 +129,11 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick
true true
} }
R.id.configuration_repeater -> { R.id.configuration_repeater -> {
WifiApDialogFragment().withArg(WifiApDialogFragment.Arg( lifecycleScope.launchWhenCreated {
adapter.repeaterManager.configuration ?: return false, WifiApDialogFragment().withArg(WifiApDialogFragment.Arg(withContext(Dispatchers.Default) {
p2pMode = true adapter.repeaterManager.configuration
)).show(this, CONFIGURE_REPEATER) } ?: return@launchWhenCreated, p2pMode = true)).show(this@TetheringFragment, CONFIGURE_REPEATER)
}
true true
} }
R.id.configuration_temp_hotspot -> { R.id.configuration_temp_hotspot -> {
@@ -185,7 +189,7 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick
val configuration by lazy { AlertDialogFragment.getRet<WifiApDialogFragment.Arg>(data!!).configuration } val configuration by lazy { AlertDialogFragment.getRet<WifiApDialogFragment.Arg>(data!!).configuration }
when (requestCode) { when (requestCode) {
REPEATER_WPS -> adapter.repeaterManager.onWpsResult(resultCode, data) REPEATER_WPS -> adapter.repeaterManager.onWpsResult(resultCode, data)
CONFIGURE_REPEATER -> if (resultCode == DialogInterface.BUTTON_POSITIVE) { CONFIGURE_REPEATER -> if (resultCode == DialogInterface.BUTTON_POSITIVE) lifecycleScope.launchWhenCreated {
adapter.repeaterManager.updateConfiguration(configuration) adapter.repeaterManager.updateConfiguration(configuration)
} }
CONFIGURE_AP -> if (resultCode == DialogInterface.BUTTON_POSITIVE) try { CONFIGURE_AP -> if (resultCode == DialogInterface.BUTTON_POSITIVE) try {