Skip to content

Commit 303841d

Browse files
Merge pull request #68 from seemoo-lab/development
Fixing that the scan would only be saved with the startDate
2 parents 7160ed5 + 7aa9836 commit 303841d

File tree

6 files changed

+50
-20
lines changed

6 files changed

+50
-20
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ android {
2424
applicationId "de.seemoo.at_tracking_detection"
2525
minSdkVersion 21
2626
targetSdkVersion 31
27-
versionCode 33
27+
versionCode 34
2828
versionName "1.3.4"
2929

3030
buildConfigField "String", "API_KEY", apiProperties["API_KEY"]

app/src/androidTest/java/de/seemoo/at_tracking_detection/ScanDBTests.kt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,15 @@ import androidx.room.Room
77
import androidx.test.ext.junit.runners.AndroidJUnit4
88
import androidx.test.platform.app.InstrumentationRegistry
99
import de.seemoo.at_tracking_detection.database.AppDatabase
10-
import de.seemoo.at_tracking_detection.database.daos.ScanDao
1110
import de.seemoo.at_tracking_detection.database.models.Scan
1211
import de.seemoo.at_tracking_detection.database.repository.ScanRepository
1312
import de.seemoo.at_tracking_detection.util.risk.RiskLevelEvaluator
1413
import kotlinx.coroutines.ExperimentalCoroutinesApi
15-
import kotlinx.coroutines.GlobalScope
16-
import kotlinx.coroutines.delay
17-
import kotlinx.coroutines.launch
18-
import kotlinx.coroutines.test.runBlockingTest
1914
import kotlinx.coroutines.test.runTest
20-
import org.junit.Rule
2115
import org.junit.Test
2216
import org.junit.runner.RunWith
2317
import java.io.IOException
24-
import java.time.LocalDate
2518
import java.time.LocalDateTime
26-
import javax.inject.Inject
2719

2820
@RunWith(AndroidJUnit4::class)
2921
class ScanDBTests() {
@@ -99,6 +91,32 @@ class ScanDBTests() {
9991
assert(scan == null)
10092
}
10193

94+
@Test
95+
fun updateScan() = runTest {
96+
val scan = Scan(
97+
startDate = LocalDateTime.now(),
98+
isManual = true,
99+
scanMode = ScanSettings.SCAN_MODE_LOW_LATENCY
100+
)
101+
val scanId = scanRepository.insert(scan)
102+
103+
val scanDB = scanRepository.scanWithId(scanId.toInt())
104+
assert(scanDB != null)
105+
106+
scanDB?.endDate = LocalDateTime.now()
107+
scanDB?.duration = 5
108+
scanDB?.noDevicesFound = 10
109+
110+
scanRepository.update(scanDB!!)
111+
112+
val updatedScan = scanRepository.scanWithId(scanId.toInt())
113+
assert(updatedScan != null)
114+
115+
assert(scanDB?.endDate == updatedScan?.endDate)
116+
assert(updatedScan?.noDevicesFound == 10)
117+
assert(updatedScan?.duration == 5)
118+
}
119+
102120
@Test
103121
fun getUnfinishedScans() = runTest {
104122
val unfinished = scanRepository.relevantUnfinishedScans

app/src/main/java/de/seemoo/at_tracking_detection/database/daos/ScanDao.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ interface ScanDao {
1212
@Query("SELECT * FROM scan WHERE endDate >= :since ORDER by endDate DESC")
1313
fun getScansSince(since: LocalDateTime): List<Scan>
1414

15+
@Query("SELECT * FROM scan WHERE startDate >= :since ORDER by startDate DESC")
16+
fun getDebugScansSince(since: LocalDateTime): List<Scan>
17+
1518
@Query("SELECT * FROM scan WHERE endDate >= :since AND isManual = :manual ORDER by endDate DESC LIMIT :limit")
1619
fun getScansSince(since: LocalDateTime, manual: Boolean, limit: Int): List<Scan>
1720

@@ -24,6 +27,9 @@ interface ScanDao {
2427
@Query("SELECT * FROM scan ORDER by endDate DESC")
2528
fun getFlowAllScans(): Flow<List<Scan>>
2629

30+
@Query("SELECT * FROM scan WHERE startDate >= :since ORDER by startDate DESC")
31+
fun getFlowDebugRelevantScans(since: LocalDateTime): Flow<List<Scan>>
32+
2733
@Query("SELECT COUNT(*) FROM scan WHERE endDate >= :since ORDER by endDate DESC")
2834
fun getNumberOfScansSince(since: LocalDateTime): Int
2935

app/src/main/java/de/seemoo/at_tracking_detection/database/repository/ScanRepository.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ class ScanRepository @Inject constructor(
1919

2020
fun relevantScans(manual: Boolean, limit: Int): List<Scan> = scanDao.getScansSince(RiskLevelEvaluator.relevantTrackingDate, manual, limit)
2121

22+
val relevantDebugScans = scanDao.getDebugScansSince(RiskLevelEvaluator.relevantTrackingDate)
23+
2224
var flowRelevantScans =
2325
scanDao.getFlowScansSince(RiskLevelEvaluator.relevantTrackingDate)
2426

27+
val flowDebugScans = scanDao.getFlowDebugRelevantScans(RiskLevelEvaluator.relevantTrackingDate)
28+
2529
var allScans: List<Scan> = scanDao.getAllScans()
2630

2731
var flowAllScans: Flow<List<Scan>> = scanDao.getFlowAllScans()

app/src/main/java/de/seemoo/at_tracking_detection/detection/ScanBluetoothWorker.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ class ScanBluetoothWorker @AssistedInject constructor(
5555
override suspend fun doWork(): Result {
5656
Timber.d("Bluetooth scanning worker started!")
5757
val scanMode = getScanMode()
58-
val scan = Scan(startDate = LocalDateTime.now(), isManual = false, scanMode = scanMode)
59-
scanRepository.insert(scan)
58+
val scanId = scanRepository.insert(Scan(startDate = LocalDateTime.now(), isManual = false, scanMode = scanMode))
6059

6160
if (!Util.checkBluetoothPermission()) {
6261
Timber.d("Permission to perform bluetooth scan missing")
@@ -116,15 +115,18 @@ class ScanBluetoothWorker @AssistedInject constructor(
116115
)
117116
}
118117

119-
Timber.d("Scheduling tracking detector worker")
120-
backgroundWorkScheduler.scheduleTrackingDetector()
121-
122118
SharedPrefs.lastScanDate = LocalDateTime.now()
123119
SharedPrefs.isScanningInBackground = false
124-
scan.endDate = LocalDateTime.now()
125-
scan.duration = scanDuration.toInt() / 1000
126-
scan.noDevicesFound = scanResultDictionary.size
127-
scanRepository.update(scan)
120+
val scan = scanRepository.scanWithId(scanId.toInt())
121+
if (scan != null) {
122+
scan.endDate = LocalDateTime.now()
123+
scan.duration = scanDuration.toInt() / 1000
124+
scan.noDevicesFound = scanResultDictionary.size
125+
scanRepository.update(scan)
126+
}
127+
128+
Timber.d("Scheduling tracking detector worker")
129+
backgroundWorkScheduler.scheduleTrackingDetector()
128130

129131
return Result.success(
130132
Data.Builder()

app/src/main/java/de/seemoo/at_tracking_detection/ui/debug/DebugScanViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class DebugScanViewModel @Inject constructor(
1818
val scans: List<Scan>
1919

2020
init {
21-
scansLive = scanRepository.flowRelevantScans.asLiveData()
22-
scans = scanRepository.relevantScans
21+
scansLive = scanRepository.flowDebugScans.asLiveData()
22+
scans = scanRepository.relevantDebugScans
2323
}
2424
}

0 commit comments

Comments
 (0)