Skip to content

Commit

Permalink
Merge pull request #68 from seemoo-lab/development
Browse files Browse the repository at this point in the history
Fixing that the scan would only be saved with the startDate
  • Loading branch information
Sn0wfreezeDev authored May 2, 2022
2 parents 7160ed5 + 7aa9836 commit 303841d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ android {
applicationId "de.seemoo.at_tracking_detection"
minSdkVersion 21
targetSdkVersion 31
versionCode 33
versionCode 34
versionName "1.3.4"

buildConfigField "String", "API_KEY", apiProperties["API_KEY"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,15 @@ import androidx.room.Room
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import de.seemoo.at_tracking_detection.database.AppDatabase
import de.seemoo.at_tracking_detection.database.daos.ScanDao
import de.seemoo.at_tracking_detection.database.models.Scan
import de.seemoo.at_tracking_detection.database.repository.ScanRepository
import de.seemoo.at_tracking_detection.util.risk.RiskLevelEvaluator
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.io.IOException
import java.time.LocalDate
import java.time.LocalDateTime
import javax.inject.Inject

@RunWith(AndroidJUnit4::class)
class ScanDBTests() {
Expand Down Expand Up @@ -99,6 +91,32 @@ class ScanDBTests() {
assert(scan == null)
}

@Test
fun updateScan() = runTest {
val scan = Scan(
startDate = LocalDateTime.now(),
isManual = true,
scanMode = ScanSettings.SCAN_MODE_LOW_LATENCY
)
val scanId = scanRepository.insert(scan)

val scanDB = scanRepository.scanWithId(scanId.toInt())
assert(scanDB != null)

scanDB?.endDate = LocalDateTime.now()
scanDB?.duration = 5
scanDB?.noDevicesFound = 10

scanRepository.update(scanDB!!)

val updatedScan = scanRepository.scanWithId(scanId.toInt())
assert(updatedScan != null)

assert(scanDB?.endDate == updatedScan?.endDate)
assert(updatedScan?.noDevicesFound == 10)
assert(updatedScan?.duration == 5)
}

@Test
fun getUnfinishedScans() = runTest {
val unfinished = scanRepository.relevantUnfinishedScans
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ interface ScanDao {
@Query("SELECT * FROM scan WHERE endDate >= :since ORDER by endDate DESC")
fun getScansSince(since: LocalDateTime): List<Scan>

@Query("SELECT * FROM scan WHERE startDate >= :since ORDER by startDate DESC")
fun getDebugScansSince(since: LocalDateTime): List<Scan>

@Query("SELECT * FROM scan WHERE endDate >= :since AND isManual = :manual ORDER by endDate DESC LIMIT :limit")
fun getScansSince(since: LocalDateTime, manual: Boolean, limit: Int): List<Scan>

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

@Query("SELECT * FROM scan WHERE startDate >= :since ORDER by startDate DESC")
fun getFlowDebugRelevantScans(since: LocalDateTime): Flow<List<Scan>>

@Query("SELECT COUNT(*) FROM scan WHERE endDate >= :since ORDER by endDate DESC")
fun getNumberOfScansSince(since: LocalDateTime): Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ class ScanRepository @Inject constructor(

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

val relevantDebugScans = scanDao.getDebugScansSince(RiskLevelEvaluator.relevantTrackingDate)

var flowRelevantScans =
scanDao.getFlowScansSince(RiskLevelEvaluator.relevantTrackingDate)

val flowDebugScans = scanDao.getFlowDebugRelevantScans(RiskLevelEvaluator.relevantTrackingDate)

var allScans: List<Scan> = scanDao.getAllScans()

var flowAllScans: Flow<List<Scan>> = scanDao.getFlowAllScans()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ class ScanBluetoothWorker @AssistedInject constructor(
override suspend fun doWork(): Result {
Timber.d("Bluetooth scanning worker started!")
val scanMode = getScanMode()
val scan = Scan(startDate = LocalDateTime.now(), isManual = false, scanMode = scanMode)
scanRepository.insert(scan)
val scanId = scanRepository.insert(Scan(startDate = LocalDateTime.now(), isManual = false, scanMode = scanMode))

if (!Util.checkBluetoothPermission()) {
Timber.d("Permission to perform bluetooth scan missing")
Expand Down Expand Up @@ -116,15 +115,18 @@ class ScanBluetoothWorker @AssistedInject constructor(
)
}

Timber.d("Scheduling tracking detector worker")
backgroundWorkScheduler.scheduleTrackingDetector()

SharedPrefs.lastScanDate = LocalDateTime.now()
SharedPrefs.isScanningInBackground = false
scan.endDate = LocalDateTime.now()
scan.duration = scanDuration.toInt() / 1000
scan.noDevicesFound = scanResultDictionary.size
scanRepository.update(scan)
val scan = scanRepository.scanWithId(scanId.toInt())
if (scan != null) {
scan.endDate = LocalDateTime.now()
scan.duration = scanDuration.toInt() / 1000
scan.noDevicesFound = scanResultDictionary.size
scanRepository.update(scan)
}

Timber.d("Scheduling tracking detector worker")
backgroundWorkScheduler.scheduleTrackingDetector()

return Result.success(
Data.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DebugScanViewModel @Inject constructor(
val scans: List<Scan>

init {
scansLive = scanRepository.flowRelevantScans.asLiveData()
scans = scanRepository.relevantScans
scansLive = scanRepository.flowDebugScans.asLiveData()
scans = scanRepository.relevantDebugScans
}
}

0 comments on commit 303841d

Please sign in to comment.