Prevent main thread queries

This commit is contained in:
Mygod
2019-03-18 17:13:31 +08:00
parent 160aaecbe5
commit 73137b8dcc
7 changed files with 19 additions and 18 deletions

View File

@@ -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"
}
}

View File

@@ -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'

View File

@@ -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,12 +66,14 @@ 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)
GlobalScope.launch(Dispatchers.Unconfined) {
AppDatabase.instance.clientRecordDao.upsert(arg.mac) {
nickname = this@NicknameDialogFragment.dialog!!.findViewById<EditText>(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)

View File

@@ -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)
}

View File

@@ -19,7 +19,6 @@ abstract class AppDatabase : RoomDatabase() {
.addMigrations(
Migration2
)
.allowMainThreadQueries()
.build()
}
}

View File

@@ -21,11 +21,11 @@ data class ClientRecord(@PrimaryKey
abstract fun lookupSync(mac: Long): LiveData<ClientRecord>
@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)
}

View File

@@ -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)
}