From 73137b8dcc26c2d11c8fe4e4bf6b80a087b66940 Mon Sep 17 00:00:00 2001 From: Mygod Date: Mon, 18 Mar 2019 17:13:31 +0800 Subject: [PATCH] Prevent main thread queries --- build.gradle | 2 +- mobile/build.gradle | 6 +++--- .../be/mygod/vpnhotspot/client/ClientsFragment.kt | 13 +++++++------ .../mygod/vpnhotspot/net/monitor/TrafficRecorder.kt | 5 +++-- .../java/be/mygod/vpnhotspot/room/AppDatabase.kt | 1 - .../java/be/mygod/vpnhotspot/room/ClientRecord.kt | 6 +++--- .../java/be/mygod/vpnhotspot/room/TrafficRecord.kt | 4 ++-- 7 files changed, 19 insertions(+), 18 deletions(-) diff --git a/build.gradle b/build.gradle index 49b8806c..2ce3be2c 100644 --- a/build.gradle +++ b/build.gradle @@ -15,7 +15,7 @@ buildscript { classpath "com.android.tools.build:gradle:3.3.2" classpath 'com.github.ben-manes:gradle-versions-plugin:0.21.0' classpath 'com.google.gms:google-services:4.2.0' - classpath 'io.fabric.tools:gradle:1.27.1' + classpath 'io.fabric.tools:gradle:1.28.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" } } diff --git a/mobile/build.gradle b/mobile/build.gradle index f744b922..1a838663 100644 --- a/mobile/build.gradle +++ b/mobile/build.gradle @@ -65,10 +65,10 @@ androidExtensions { def aux = [ 'com.crashlytics.sdk.android:crashlytics:2.9.9', - 'com.google.firebase:firebase-core:16.0.7', + 'com.google.firebase:firebase-core:16.0.8', ] def lifecycleVersion = '2.0.0' -def roomVersion = '2.1.0-alpha04' +def roomVersion = '2.1.0-alpha05' dependencies { kapt "androidx.lifecycle:lifecycle-compiler:$lifecycleVersion" kapt "androidx.room:room-compiler:$roomVersion" @@ -79,7 +79,7 @@ dependencies { implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleVersion" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion" implementation 'androidx.preference:preference:1.1.0-alpha04' - implementation "androidx.room:room-coroutines:$roomVersion" + implementation "androidx.room:room-ktx:$roomVersion" implementation 'com.android.billingclient:billing:1.2.2' implementation 'com.github.luongvo:BadgeView:1.1.5' implementation 'com.github.topjohnwu.libsu:core:2.3.1' 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 d74283b3..30d5bc68 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt @@ -42,7 +42,6 @@ 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.CoroutineStart import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @@ -67,10 +66,12 @@ class ClientsFragment : Fragment(), MainScope by MainScope.Supervisor() { override fun onClick(dialog: DialogInterface?, which: Int) { when (which) { - DialogInterface.BUTTON_POSITIVE -> GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED) { + DialogInterface.BUTTON_POSITIVE -> { MacLookup.abort(arg.mac) - AppDatabase.instance.clientRecordDao.upsert(arg.mac) { - nickname = this@NicknameDialogFragment.dialog!!.findViewById(android.R.id.edit).text + GlobalScope.launch(Dispatchers.Unconfined) { + AppDatabase.instance.clientRecordDao.upsert(arg.mac) { + nickname = this@NicknameDialogFragment.dialog!!.findViewById(android.R.id.edit).text + } } } DialogInterface.BUTTON_NEUTRAL -> MacLookup.perform(arg.mac, true) @@ -140,7 +141,7 @@ class ClientsFragment : Fragment(), MainScope by MainScope.Supervisor() { val wasWorking = TrafficRecorder.isWorking(client.mac) client.obtainRecord().apply { blocked = !blocked - AppDatabase.instance.clientRecordDao.update(this) + launch(Dispatchers.Unconfined) { AppDatabase.instance.clientRecordDao.update(this@apply) } } IpNeighbourMonitor.instance?.flush() if (!wasWorking && item.itemId == R.id.block) { @@ -150,7 +151,7 @@ class ClientsFragment : Fragment(), MainScope by MainScope.Supervisor() { } R.id.stats -> { binding.client?.let { client -> - launch(start = CoroutineStart.UNDISPATCHED) { + launch(Dispatchers.Unconfined) { StatsDialogFragment().withArg(StatsArg( client.title.value ?: return@launch, AppDatabase.instance.trafficRecordDao.queryStats(client.mac) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TrafficRecorder.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TrafficRecorder.kt index 38d8486f..0d98bc55 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TrafficRecorder.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TrafficRecorder.kt @@ -11,6 +11,7 @@ import be.mygod.vpnhotspot.util.RootSession import be.mygod.vpnhotspot.util.parseNumericAddress import be.mygod.vpnhotspot.util.putIfAbsentCompat import be.mygod.vpnhotspot.widget.SmartSnackbar +import kotlinx.coroutines.runBlocking import timber.log.Timber import java.net.InetAddress import java.util.concurrent.TimeUnit @@ -26,7 +27,7 @@ object TrafficRecorder { fun register(ip: InetAddress, downstream: String, mac: Long) { val record = TrafficRecord(mac = mac, ip = ip, downstream = downstream) - AppDatabase.instance.trafficRecordDao.insert(record) + runBlocking { AppDatabase.instance.trafficRecordDao.insert(record) } synchronized(this) { DebugHelper.log(TAG, "Registering $ip%$downstream") check(records.putIfAbsentCompat(Pair(ip, downstream), record) == null) @@ -119,7 +120,7 @@ object TrafficRecorder { check(record.sentBytes >= 0) check(record.receivedPackets >= 0) check(record.receivedBytes >= 0) - AppDatabase.instance.trafficRecordDao.insert(record) + runBlocking { AppDatabase.instance.trafficRecordDao.insert(record) } } foregroundListeners(records.values, oldRecords) } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/room/AppDatabase.kt b/mobile/src/main/java/be/mygod/vpnhotspot/room/AppDatabase.kt index d97dda58..f8ab1baf 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/room/AppDatabase.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/room/AppDatabase.kt @@ -19,7 +19,6 @@ abstract class AppDatabase : RoomDatabase() { .addMigrations( Migration2 ) - .allowMainThreadQueries() .build() } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/room/ClientRecord.kt b/mobile/src/main/java/be/mygod/vpnhotspot/room/ClientRecord.kt index d7159858..90ffea00 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/room/ClientRecord.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/room/ClientRecord.kt @@ -21,11 +21,11 @@ data class ClientRecord(@PrimaryKey abstract fun lookupSync(mac: Long): LiveData @Insert(onConflict = OnConflictStrategy.REPLACE) - protected abstract fun updateInternal(value: ClientRecord): Long - fun update(value: ClientRecord) = check(updateInternal(value) == value.mac) + protected abstract suspend fun updateInternal(value: ClientRecord): Long + suspend fun update(value: ClientRecord) = check(updateInternal(value) == value.mac) @Transaction - open fun upsert(mac: Long, operation: ClientRecord.() -> Unit) = runBlocking { lookupOrDefault(mac) }.apply { + open suspend fun upsert(mac: Long, operation: suspend ClientRecord.() -> Unit) = lookupOrDefault(mac).apply { operation() update(this) } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/room/TrafficRecord.kt b/mobile/src/main/java/be/mygod/vpnhotspot/room/TrafficRecord.kt index 7ebc224d..7cab74d7 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/room/TrafficRecord.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/room/TrafficRecord.kt @@ -41,8 +41,8 @@ data class TrafficRecord( @androidx.room.Dao abstract class Dao { @Insert - protected abstract fun insertInternal(value: TrafficRecord): Long - fun insert(value: TrafficRecord) { + protected abstract suspend fun insertInternal(value: TrafficRecord): Long + suspend fun insert(value: TrafficRecord) { check(value.id == null) value.id = insertInternal(value) }