diff --git a/mobile/build.gradle b/mobile/build.gradle index 000ae5ac..b01418f0 100644 --- a/mobile/build.gradle +++ b/mobile/build.gradle @@ -67,7 +67,7 @@ def aux = [ 'com.crashlytics.sdk.android:crashlytics:2.10.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' dependencies { kapt "androidx.room:room-compiler:$roomVersion" @@ -78,6 +78,7 @@ dependencies { implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion" implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleVersion" implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion" + implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycleVersion" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion" implementation 'androidx.preference:preference:1.1.0-rc01' implementation "androidx.room:room-ktx:$roomVersion" diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt index b9549ab6..067d0074 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt @@ -19,6 +19,7 @@ import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.get +import androidx.lifecycle.lifecycleScope import androidx.lifecycle.observe import androidx.recyclerview.widget.DefaultItemAnimator 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.widget.SmartSnackbar 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 class ClientsFragment : Fragment() { @@ -149,11 +153,13 @@ class ClientsFragment : Fragment() { } R.id.stats -> { binding.client?.let { client -> - scope.launch { - StatsDialogFragment().withArg(StatsArg( - client.title.value ?: return@launch, - AppDatabase.instance.trafficRecordDao.queryStats(client.mac) - )).show(this@ClientsFragment) + lifecycleScope.launchWhenCreated { + withContext(Dispatchers.Unconfined) { + StatsDialogFragment().withArg(StatsArg( + client.title.value ?: return@withContext, + AppDatabase.instance.trafficRecordDao.queryStats(client.mac) + )).show(this@ClientsFragment) + } } } true @@ -204,7 +210,6 @@ class ClientsFragment : Fragment() { } } - private val scope = MainScope() + Dispatchers.Unconfined private lateinit var binding: FragmentClientsBinding private val adapter = ClientAdapter() private var rates = HashMap, TrafficRate>() @@ -236,9 +241,4 @@ class ClientsFragment : Fragment() { TrafficRecorder.foregroundListeners -= this super.onStop() } - - override fun onDestroy() { - scope.cancel() - super.onDestroy() - } } 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 24121e71..8fcc5dbb 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt @@ -31,6 +31,8 @@ import be.mygod.vpnhotspot.util.ServiceForegroundConnector import be.mygod.vpnhotspot.util.formatAddresses import be.mygod.vpnhotspot.widget.SmartSnackbar import kotlinx.android.parcel.Parcelize +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import timber.log.Timber import java.net.NetworkInterface import java.net.SocketException @@ -197,7 +199,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic SmartSnackbar.make(R.string.repeater_configure_failure).show() return null } - fun updateConfiguration(config: WifiConfiguration) { + suspend fun updateConfiguration(config: WifiConfiguration) { if (Build.VERSION.SDK_INT >= 29) { RepeaterService.networkName = config.SSID RepeaterService.passphrase = config.preSharedKey @@ -209,7 +211,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic } } else @Suppress("DEPRECATION") holder.config?.let { master -> 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 } catch (e: Exception) { Timber.w(e) 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 2845f60d..c82febe4 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt @@ -14,6 +14,7 @@ import androidx.appcompat.widget.Toolbar import androidx.core.content.ContextCompat import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.LinearLayoutManager 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.widget.SmartSnackbar import kotlinx.android.synthetic.main.activity_main.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import timber.log.Timber import java.lang.IllegalArgumentException import java.lang.reflect.InvocationTargetException @@ -126,10 +129,11 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick true } R.id.configuration_repeater -> { - WifiApDialogFragment().withArg(WifiApDialogFragment.Arg( - adapter.repeaterManager.configuration ?: return false, - p2pMode = true - )).show(this, CONFIGURE_REPEATER) + lifecycleScope.launchWhenCreated { + WifiApDialogFragment().withArg(WifiApDialogFragment.Arg(withContext(Dispatchers.Default) { + adapter.repeaterManager.configuration + } ?: return@launchWhenCreated, p2pMode = true)).show(this@TetheringFragment, CONFIGURE_REPEATER) + } true } R.id.configuration_temp_hotspot -> { @@ -185,7 +189,7 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick val configuration by lazy { AlertDialogFragment.getRet(data!!).configuration } when (requestCode) { 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) } CONFIGURE_AP -> if (resultCode == DialogInterface.BUTTON_POSITIVE) try {