-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile13.tsx
399 lines (353 loc) · 10.3 KB
/
file13.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import React, {
useEffect,
useState,
createContext,
useCallback,
useContext,
SetStateAction
} from 'react';
import {
NativeEventEmitter,
Alert,
Platform,
AppState,
AppStateStatus
} from 'react-native';
import ExposureNotification, {
AuthorisedStatus,
StatusState,
Status,
CloseContact,
StatusType,
KeyServerType
} from './exposure-notification-module';
import {getPermissions, requestPermissions} from './utils/permissions';
import {
ExposurePermissions,
PermissionStatus,
TraceConfiguration
} from './types';
const emitter = new NativeEventEmitter(ExposureNotification);
interface State {
status: Status;
supported: boolean;
canSupport: boolean;
isAuthorised: AuthorisedStatus;
enabled: boolean;
contacts?: CloseContact[];
initialised: boolean;
permissions: ExposurePermissions;
}
export interface ExposureContextValue extends State {
start: () => void;
stop: () => void;
configure: () => void;
checkExposure: (readDetails: boolean, skipTimeCheck: boolean) => void;
simulateExposure: (timeDelay: number) => void;
getDiagnosisKeys: () => Promise<any[]>;
exposureEnabled: () => Promise<boolean>;
authoriseExposure: () => Promise<boolean>;
deleteAllData: () => Promise<void>;
supportsExposureApi: () => Promise<void>;
getCloseContacts: () => Promise<CloseContact[] | null>;
getLogData: () => Promise<{[key: string]: any}>;
triggerUpdate: () => Promise<string | undefined>;
deleteExposureData: () => Promise<void>;
readPermissions: () => Promise<void>;
askPermissions: () => Promise<void>;
setExposureState: (setStateAction: SetStateAction<State>) => void;
}
const initialState = {
status: {
state: StatusState.unavailable,
type: [StatusType.starting]
},
supported: false,
canSupport: false,
isAuthorised: 'unknown' as AuthorisedStatus,
enabled: false,
contacts: [] as CloseContact[],
initialised: false,
permissions: {
exposure: {status: PermissionStatus.Unknown},
notifications: {status: PermissionStatus.Unknown}
}
};
export const ExposureContext = createContext<ExposureContextValue>({
...initialState,
start: () => {},
stop: () => {},
configure: () => {},
checkExposure: () => {},
simulateExposure: () => {},
getDiagnosisKeys: () => Promise.resolve([]),
exposureEnabled: () => Promise.resolve(false),
authoriseExposure: () => Promise.resolve(false),
deleteAllData: () => Promise.resolve(),
supportsExposureApi: () => Promise.resolve(),
getCloseContacts: () => Promise.resolve([]),
getLogData: () => Promise.resolve({}),
triggerUpdate: () => Promise.resolve(undefined),
deleteExposureData: () => Promise.resolve(),
readPermissions: () => Promise.resolve(),
askPermissions: () => Promise.resolve(),
setExposureState: () => {}
});
export interface ExposureProviderProps {
isReady: boolean;
traceConfiguration: TraceConfiguration;
appVersion: string;
serverUrl: string;
keyServerUrl: string;
keyServerType: KeyServerType;
authToken: string;
refreshToken: string;
notificationTitle: string;
notificationDescription: string;
callbackNumber?: string;
analyticsOptin?: boolean;
}
export const ExposureProvider: React.FC<ExposureProviderProps> = ({
children,
isReady = false,
traceConfiguration,
appVersion,
serverUrl,
keyServerUrl,
keyServerType = KeyServerType.nearform,
authToken = "Qk32EZw55f2I00ECpfoem6xiNuUXL5yNUneUHvNy5qFxPWr0kKEbY4GQG1mwAoTFjd9tBm9kdirvhNI",
refreshToken = '',
notificationTitle,
notificationDescription,
callbackNumber = '',
analyticsOptin = false
}) => {
const [state, setState] = useState<State>(initialState);
useEffect(() => {
function handleEvent(
ev: {onStatusChanged?: Status; status?: any; scheduledTask?: any} = {}
) {
console.log(`exposureEvent: ${JSON.stringify(ev)}`);
if (ev.onStatusChanged) {
return validateStatus(ev.onStatusChanged);
}
}
let subscription = emitter.addListener('exposureEvent', handleEvent);
const listener = (type: AppStateStatus) => {
if (type === 'active') {
validateStatus();
getCloseContacts();
}
};
AppState.addEventListener('change', listener);
return () => {
subscription.remove();
emitter.removeListener('exposureEvent', handleEvent);
AppState.removeEventListener('change', listener);
};
}, []);
useEffect(() => {
async function checkSupportAndStart() {
await supportsExposureApi();
// Start as soon as we're able to
if (
isReady &&
state.permissions.exposure.status === PermissionStatus.Allowed
) {
await configure();
start();
}
}
checkSupportAndStart();
}, [state.permissions, isReady]);
const supportsExposureApi = async function () {
const can = await ExposureNotification.canSupport();
const is = await ExposureNotification.isSupported();
const status = await ExposureNotification.status();
const enabled = await ExposureNotification.exposureEnabled();
const isAuthorised = await ExposureNotification.isAuthorised();
setState((s) => ({
...s,
status,
enabled,
canSupport: can,
supported: is,
isAuthorised
}));
await validateStatus(status);
if (enabled) {
getCloseContacts();
}
};
const validateStatus = async (status?: Status) => {
let newStatus = status || ((await ExposureNotification.status()) as Status);
const enabled = await ExposureNotification.exposureEnabled();
const isAuthorised = await ExposureNotification.isAuthorised();
const canSupport = await ExposureNotification.canSupport();
const isStarting =
(isAuthorised === AuthorisedStatus.unknown ||
isAuthorised === AuthorisedStatus.granted) &&
newStatus.state === StatusState.unavailable &&
newStatus.type?.includes(StatusType.starting);
const initialised = !isStarting || !canSupport;
setState((s) => ({
...s,
status: newStatus,
enabled,
isAuthorised,
canSupport,
initialised
}));
};
const start = async () => {
try {
await ExposureNotification.start();
await validateStatus();
await getCloseContacts();
} catch (err) {
console.log('start err', err);
}
};
const stop = async () => {
try {
await ExposureNotification.stop();
await validateStatus();
} catch (err) {
console.log('stop err', err);
}
};
const configure = async () => {
try {
const iosLimit =
traceConfiguration.fileLimitiOS > 0
? traceConfiguration.fileLimitiOS
: traceConfiguration.fileLimit;
const config = {
exposureCheckFrequency: traceConfiguration.exposureCheckInterval,
serverURL: serverUrl,
keyServerUrl,
keyServerType,
authToken,
refreshToken,
storeExposuresFor: traceConfiguration.storeExposuresFor,
fileLimit:
Platform.OS === 'ios' ? iosLimit : traceConfiguration.fileLimit,
version: appVersion,
notificationTitle,
notificationDesc: notificationDescription,
callbackNumber,
analyticsOptin
};
await ExposureNotification.configure(config);
return true;
} catch (err) {
console.log('configure err', err);
return false;
}
};
const checkExposure = (readDetails: boolean, skipTimeCheck: boolean) => {
ExposureNotification.checkExposure(readDetails, skipTimeCheck);
};
const simulateExposure = (timeDelay: number) => {
ExposureNotification.simulateExposure(timeDelay);
};
const getDiagnosisKeys = () => {
return ExposureNotification.getDiagnosisKeys();
};
const exposureEnabled = async () => {
return ExposureNotification.exposureEnabled();
};
const authoriseExposure = async () => {
return ExposureNotification.authoriseExposure();
};
const deleteAllData = async () => {
await ExposureNotification.deleteAllData();
await validateStatus();
};
const getCloseContacts = async () => {
try {
if (state.permissions.exposure.status === PermissionStatus.Allowed) {
const contacts = await ExposureNotification.getCloseContacts();
setState((s) => ({...s, contacts}));
return contacts;
}
return [];
} catch (err) {
console.log('getCloseContacts err', err);
return null;
}
};
const getLogData = async () => {
try {
const data = await ExposureNotification.getLogData();
return data;
} catch (err) {
console.log('getLogData err', err);
return null;
}
};
const triggerUpdate = async () => {
try {
const result = await ExposureNotification.triggerUpdate();
console.log('trigger update: ', result);
// this will not occur after play services update available to public
if (result === 'api_not_available') {
Alert.alert(
'API Not Available',
'Google Exposure Notifications API not available on this device yet'
);
}
return result;
} catch (e) {
console.log('trigger update error', e);
}
};
const deleteExposureData = async () => {
try {
await ExposureNotification.deleteExposureData();
setState((s) => ({...s, contacts: []}));
} catch (e) {
console.log('delete exposure data error', e);
}
};
const readPermissions = useCallback(async () => {
console.log('Read permissions...');
const perms = await getPermissions();
console.log('perms: ', JSON.stringify(perms, null, 2));
setState((s) => ({...s, permissions: perms}));
}, []);
const askPermissions = useCallback(async () => {
console.log('Requesting permissions...', state.permissions);
await requestPermissions();
await readPermissions();
}, []);
useEffect(() => {
readPermissions();
}, [readPermissions]);
const value: ExposureContextValue = {
...state,
start,
stop,
configure,
checkExposure,
simulateExposure,
getDiagnosisKeys,
exposureEnabled,
authoriseExposure,
deleteAllData,
supportsExposureApi,
getCloseContacts,
getLogData,
triggerUpdate,
deleteExposureData,
readPermissions,
askPermissions,
setExposureState: setState
};
return (
<ExposureContext.Provider value={value}>
{children}
</ExposureContext.Provider>
);
};
export const useExposure = () => useContext(ExposureContext);