-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*[FakeReport] Implemented random fake infection reports
- Loading branch information
Showing
11 changed files
with
190 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
app/src/main/java/es/gob/radarcovid/datamanager/repository/DomainRepositoryImpl.kt
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
app/src/main/java/es/gob/radarcovid/datamanager/repository/ExampleRepository.kt
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
app/src/main/java/es/gob/radarcovid/datamanager/repository/ExampleRepositoryImpl.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...c/main/java/es/gob/radarcovid/datamanager/repository/FakeInfectionReportRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) 2020 Gobierno de España | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
package es.gob.radarcovid.datamanager.repository | ||
|
||
import android.content.Context | ||
import es.gob.radarcovid.BuildConfig | ||
import io.reactivex.rxjava3.core.Completable | ||
import org.dpppt.android.sdk.DP3T | ||
import org.dpppt.android.sdk.models.ExposeeAuthMethodAuthorization | ||
import javax.inject.Inject | ||
import javax.inject.Named | ||
|
||
class FakeInfectionReportRepositoryImpl @Inject constructor( | ||
@Named("applicationContext") private val context: Context | ||
) : FakeInfectionReportRepository { | ||
|
||
override fun notifyFakeInfected(authCode: String): Completable = | ||
Completable.create { | ||
if (BuildConfig.isMock) { | ||
it.onComplete() | ||
} else { | ||
DP3T.sendFakeInfectedRequest(context, | ||
ExposeeAuthMethodAuthorization("Bearer $authCode"), | ||
{ it.onComplete() }, | ||
{ it.onError(Exception("Error notifying fake infection")) } | ||
) | ||
} | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/es/gob/radarcovid/datamanager/usecase/ReportFakeInfectionUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2020 Gobierno de España | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
package es.gob.radarcovid.datamanager.usecase | ||
|
||
import es.gob.radarcovid.datamanager.repository.ApiRepository | ||
import es.gob.radarcovid.datamanager.repository.FakeInfectionReportRepository | ||
import es.gob.radarcovid.models.request.RequestVerifyCode | ||
import es.gob.radarcovid.models.response.ResponseToken | ||
import io.reactivex.rxjava3.core.Completable | ||
import io.reactivex.rxjava3.core.Observable | ||
import javax.inject.Inject | ||
|
||
class ReportFakeInfectionUseCase @Inject constructor( | ||
private val fakeInfectionReportRepository: FakeInfectionReportRepository, | ||
private val apiRepository: ApiRepository | ||
) { | ||
|
||
companion object { | ||
|
||
const val FAKE_REPORT_CODE: String = "112358132134" | ||
|
||
} | ||
|
||
|
||
fun reportFakeInfection(): Completable = | ||
getFakeVerifyToken().flatMapCompletable { | ||
fakeInfectionReportRepository.notifyFakeInfected(it.token) | ||
}.concatWith { | ||
it.onComplete() | ||
} | ||
|
||
private fun getFakeVerifyToken(): Observable<ResponseToken> { | ||
return Observable.create { emitter -> | ||
val result = apiRepository.verifyCode(RequestVerifyCode(null, FAKE_REPORT_CODE)) | ||
|
||
if (result.isLeft()) { | ||
emitter.onError(result.left().get()) | ||
} else { | ||
emitter.onNext(result.right().get()) | ||
emitter.onComplete() | ||
} | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
app/src/main/java/es/gob/radarcovid/features/worker/FakeInfectionReportWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (c) 2020 Gobierno de España | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
package es.gob.radarcovid.features.worker | ||
|
||
import android.content.Context | ||
import androidx.work.* | ||
import dagger.android.HasAndroidInjector | ||
import es.gob.radarcovid.datamanager.usecase.ReportFakeInfectionUseCase | ||
import org.dpppt.android.sdk.BuildConfig | ||
import org.dpppt.android.sdk.DP3T | ||
import java.util.concurrent.TimeUnit | ||
import javax.inject.Inject | ||
import kotlin.random.Random | ||
|
||
class FakeInfectionReportWorker(context: Context, workerParams: WorkerParameters) : | ||
Worker(context, workerParams) { | ||
|
||
init { | ||
(context.applicationContext as? HasAndroidInjector) | ||
?.androidInjector() | ||
?.inject(this) | ||
} | ||
|
||
companion object { | ||
|
||
private const val TAG = "FakeInfectionReportWorker" | ||
|
||
fun start(context: Context) { | ||
start(context, getRandomDelay(), ExistingWorkPolicy.KEEP) | ||
} | ||
|
||
private fun start( | ||
context: Context, | ||
delayInMinutes: Long, | ||
existingWorkPolicy: ExistingWorkPolicy | ||
) { | ||
val constraints = Constraints.Builder() | ||
.setRequiredNetworkType(NetworkType.CONNECTED) | ||
.build() | ||
val work = | ||
OneTimeWorkRequest | ||
.Builder(FakeInfectionReportWorker::class.java) | ||
.setInitialDelay(delayInMinutes, TimeUnit.MINUTES) | ||
.setConstraints(constraints) | ||
.build() | ||
WorkManager.getInstance(context) | ||
.enqueueUniqueWork(TAG, existingWorkPolicy, work) | ||
} | ||
|
||
private fun getRandomDelay(): Long = Random.nextLong(180, 360) | ||
|
||
} | ||
|
||
@Inject | ||
lateinit var reportFakeInfectionUseCase: ReportFakeInfectionUseCase | ||
|
||
override fun doWork(): Result { | ||
if (BuildConfig.DEBUG) | ||
DP3T.addWorkerStartedToHistory(applicationContext, TAG) | ||
reportFakeInfectionUseCase.reportFakeInfection().subscribe() | ||
start(applicationContext, getRandomDelay(), ExistingWorkPolicy.APPEND) | ||
return Result.success() | ||
} | ||
|
||
} |