Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
Implement setDiagnosisKeysDataMapping on Android (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
smcmurtry authored Jan 29, 2021
1 parent 6962ee6 commit b1fa3be
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.nearby.Nearby
import com.google.android.gms.nearby.exposurenotification.DiagnosisKeyFileProvider
import com.google.android.gms.nearby.exposurenotification.*
import com.google.android.gms.nearby.exposurenotification.ExposureNotificationClient.ACTION_EXPOSURE_NOTIFICATION_SETTINGS
import com.google.android.gms.nearby.exposurenotification.ExposureNotificationStatusCodes
import com.google.android.gms.nearby.exposurenotification.ExposureWindow
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -218,6 +216,33 @@ class ExposureNotificationModule(context: ReactApplicationContext) : ReactContex
}
}

@ReactMethod
fun setDiagnosisKeysDataMapping(promise: Promise) {
promise.launch(this) {
try{
val diagnosisKeysDataMapping: DiagnosisKeysDataMapping by lazy {
val daysToInfectiousness = mutableMapOf<Int, Int>()
for (i in -14..14) {
daysToInfectiousness[i] = Infectiousness.STANDARD
}
DiagnosisKeysDataMapping.DiagnosisKeysDataMappingBuilder()
.setDaysSinceOnsetToInfectiousness(daysToInfectiousness)
.setInfectiousnessWhenDaysSinceOnsetMissing(Infectiousness.STANDARD)
.setReportTypeWhenMissing(ReportType.CONFIRMED_TEST)
.build()
}
val oldDiagnosisKeysDataMapping = exposureNotificationClient.diagnosisKeysDataMapping.await()
if (diagnosisKeysDataMapping != oldDiagnosisKeysDataMapping) {
exposureNotificationClient.setDiagnosisKeysDataMapping(diagnosisKeysDataMapping)
}
promise.resolve(null)
} catch (exception: Exception) {
promise.reject(exception)
}
}
}


@ReactMethod
fun getExposureWindows(promise: Promise) {
promise.launch(this) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import {log} from 'shared/logging/config';
import {captureMessage} from 'shared/log';

import {
CalibrationConfidence,
ExposureConfiguration,
ExposureNotificationAPI,
ExposureSummary,
Infectiousness,
ReportType,
} from './types';
import {ExposureConfiguration, ExposureNotificationAPI, ExposureSummary} from './types';
import {getLastExposureTimestamp} from './utils';

export default function ExposureNotificationAdapter(exposureNotificationAPI: ExposureNotificationAPI) {
Expand Down Expand Up @@ -45,21 +38,41 @@ export default function ExposureNotificationAdapter(exposureNotificationAPI: Exp
return [];
},
getExposureWindowsAndroid: async (diagnosisKeysURLs: string[]) => {
await exposureNotificationAPI.provideDiagnosisKeys(diagnosisKeysURLs);
const _exposureWindows = await exposureNotificationAPI.getExposureWindows();
const exposureWindows = _exposureWindows.map(window => {
window.day = Number(window.day);
window.calibrationConfidence = window.calibrationConfidence as CalibrationConfidence;
window.infectiousness = window.infectiousness as Infectiousness;
window.reportType = window.reportType as ReportType;
return window;
});
log.debug({
category: 'exposure-check',
message: 'exposureWindows',
payload: exposureWindows,
});
return exposureWindows;
try {
await exposureNotificationAPI.provideDiagnosisKeys(diagnosisKeysURLs);
} catch (error) {
log.error({category: 'exposure-check', message: 'exposureNotificationAPI.provideDiagnosisKeys failed', error});
// no-op
}
try {
const _exposureWindows = await exposureNotificationAPI.getExposureWindows();
const exposureWindows = _exposureWindows.map(window => {
return {...window, day: Number(window.day)};
});
log.debug({
category: 'exposure-check',
message: 'exposureWindows',
payload: {'exposureWindows.length': exposureWindows.length, exposureWindows},
});
return exposureWindows;
} catch (error) {
log.error({category: 'exposure-check', message: 'exposureNotificationAPI.getExposureWindows failed', error});
// no-op
}
return [];
},
setDiagnosisKeysDataMapping: async () => {
try {
await exposureNotificationAPI.setDiagnosisKeysDataMapping();
} catch (error) {
log.error({
category: 'exposure-check',
message: 'exposureNotificationAPI.setDiagnosisKeysDataMapping failed',
error,
});
// no-op
}
log.debug({category: 'exposure-check', message: 'setDiagnosisKeysDataMapping successful'});
},
};
}
2 changes: 2 additions & 0 deletions src/bridge/ExposureNotification/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export interface ExposureNotification {
provideDiagnosisKeys(diagnosisKeysURLs: string[]): Promise<undefined>;
getExposureWindows(): Promise<ExposureWindow[]>;
getExposureWindowsAndroid(diagnosisKeysURLs: string[]): Promise<ExposureWindow[]>;
setDiagnosisKeysDataMapping(): Promise<void>;
}

export interface ExposureNotificationAPI {
Expand All @@ -104,6 +105,7 @@ export interface ExposureNotificationAPI {
getPendingExposureSummary(): Promise<ExposureSummary | undefined> /* used only by Android */;
provideDiagnosisKeys(diagnosisKeysURLs: string[]): Promise<undefined> /* used only by Android */;
getExposureWindows(): Promise<ExposureWindow[]>;
setDiagnosisKeysDataMapping(): Promise<void>;
}

export interface ExposureWindow {
Expand Down
2 changes: 2 additions & 0 deletions src/screens/testScreen/MockExposureNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export class MockExposureNotification implements ExposureNotification {

provideDiagnosisKeys = async () => undefined;

setDiagnosisKeysDataMapping = async () => {};

getExposureWindows = async () => [];

getExposureWindowsAndroid = async () => [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ export class ExposureNotificationService {
try {
let exposureWindows: ExposureWindow[];
if (Platform.OS === 'android') {
await this.exposureNotification.setDiagnosisKeysDataMapping();
exposureWindows = await this.exposureNotification.getExposureWindowsAndroid(keysFileUrls);
} else {
exposureWindows = await this.exposureNotification.getExposureWindowsIos(exposureConfiguration, keysFileUrls);
Expand All @@ -610,7 +611,7 @@ export class ExposureNotificationService {
}
return this.finalize({}, lastCheckedPeriod);
} catch (error) {
log.error({category: 'exposure-check', error});
log.error({category: 'exposure-check', message: 'performExposureStatusUpdateV2', error});
}

return this.finalize();
Expand Down Expand Up @@ -682,7 +683,7 @@ export class ExposureNotificationService {

public getExposureDetectedAt(): Date[] {
const exposureStatus = this.exposureStatus.get();
log.info({message: 'getExposureDetectedAt', payload: {exposureStatus}});
log.info({category: 'exposure-check', message: 'getExposureDetectedAt', payload: {exposureStatus}});

if (exposureStatus.type !== ExposureStatusType.Exposed) {
return [];
Expand Down

0 comments on commit b1fa3be

Please sign in to comment.