-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathDP3TTracing.swift
232 lines (204 loc) · 8.17 KB
/
DP3TTracing.swift
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
/*
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch>
*
* 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
*/
import Foundation
import UIKit.UIApplication
/// A delegate for the DP3T tracing
public protocol DP3TTracingDelegate: AnyObject {
/// The state has changed
/// - Parameter state: The new state
func DP3TTracingStateChanged(_ state: TracingState)
}
public protocol DP3TBackgroundHandler: AnyObject {
func performBackgroundTasks(completionHandler: @escaping (_ success: Bool) -> Void)
func didScheduleBackgrounTask()
}
@available(iOS 12.5, *)
private var instance: DP3TSDK!
/// DP3TTracing
public enum DP3TTracing {
/// The current version of the SDK
public static let frameworkVersion: String = "2.3.0"
/// sets global parameter values which are used throughout the sdk
public static var parameters: DP3TParameters {
get {
return Default.shared.parameters
}
set {
Default.shared.parameters = newValue
}
}
/// Determines if the OS is compatible with the DP3T SDK
/// only in the case that the OS is compatible the Instance can be initialized
public static var isOSCompatible: Bool {
guard NSClassFromString("ENManager") != nil else {
// between 13.0 and 13.5 where no Exposure Notification framework is available
return false
}
if #available(iOS 13.7, *) {
return true
} else if #available(iOS 13.5, *) {
// Not supportet between iOS 13.5 and 13.7
return false
}
return true
}
/// initialize the SDK
/// - Parameters:
/// - config: configuration describing the backend to use
/// - enviroment: enviroment to use
/// - urlSession: the url session to use for networking (can used to enable certificate pinning)
/// - backgroundHandler: a delegate to perform background tasks
/// - federationGateway: specifies whether keys should be shared with other countries
@available(iOS 12.5, *)
public static func initialize(with applicationDescriptor: ApplicationDescriptor,
urlSession: URLSession = .shared,
backgroundHandler: DP3TBackgroundHandler? = nil,
federationGateway: FederationGateway = .unspecified) {
precondition(Self.isOSCompatible, "Operating System is not compatible")
precondition(instance == nil, "DP3TSDK already initialized")
instance = DP3TSDK(applicationDescriptor: applicationDescriptor,
urlSession: urlSession,
backgroundHandler: backgroundHandler,
federationGateway: federationGateway)
}
@available(iOS 12.5, *)
private static func instancePrecondition(){
precondition(instance != nil, "DP3TSDK not initialized call `initialize(with:delegate:)`")
}
/// The delegate
@available(iOS 12.5, *)
public static var delegate: DP3TTracingDelegate? {
set {
instancePrecondition()
instance.delegate = newValue
}
get {
instance.delegate
}
}
/// Starts tracing
@available(iOS 12.5, *)
public static func startTracing(completionHandler: ((TracingEnableResult) -> Void)? = nil) {
instancePrecondition()
instance.startTracing(completionHandler: completionHandler)
}
/// Stops tracing
@available(iOS 12.5, *)
public static func stopTracing(completionHandler: ((TracingEnableResult) -> Void)? = nil) {
instancePrecondition()
instance.stopTracing(completionHandler: completionHandler)
}
/// Triggers sync with the backend to refresh the exposed list
/// - Parameter callback: callback
@available(iOS 12.5, *)
public static func sync(callback: ((SyncResult) -> Void)?) {
instancePrecondition()
instance.sync() { result in
DispatchQueue.main.async {
callback?(result)
}
}
}
/// Cancel any ongoing snyc
@available(iOS 12.5, *)
public static func cancelSync() {
instancePrecondition()
instance.cancelSync()
}
/// get the current status of the SDK
@available(iOS 12.5, *)
public static var status: TracingState {
instancePrecondition()
return instance.status
}
public struct IWasExposedResult {
public let oldestKeyDate: Date?
}
/// Request the user permission to obtain all TEK's
/// This results with a IWasExposedState object which later can be used to transmit the filtered TEK's to the backend by calling sendTEKs
/// - Parameter callback: a handler which receives the state object or an error
@available(iOS 12.5, *)
public static func requestTEKPermission(callback: @escaping (Result<IWasExposedState, DP3TTracingError>) -> Void) {
instancePrecondition()
instance.requestTEKPermission(callback: callback)
}
/// Send the obtained keys to the backend. First a valid state has to be obtained by calling requestTEKPermission
/// - Parameters:
/// - onset: Start date of the exposure
/// - iWasExposedState: state object obtained by calling requestTEKPermission
/// - authentication: Authentication method
/// - callback: callback
@available(iOS 12.5, *)
public static func sendTEKs(onset: Date,
iWasExposedState: IWasExposedState,
authentication: ExposeeAuthMethod,
callback: @escaping (Result<IWasExposedResult, DP3TTracingError>) -> Void) {
instancePrecondition()
instance.sendTEKs(onset: onset, iWasExposedState: iWasExposedState, authentication: authentication, callback: callback)
}
/// tell the SDK that the user was exposed
/// This is a convenience method that for not fake requests internally first calls requestTEKPermission and then sendTEKs
/// This will stop tracing
/// - Parameters:
/// - onset: Start date of the exposure
/// - authentication: Authentication method
/// - isFakeRequest: indicates if the request should be a fake one. This method should be called regulary so people sniffing the networking traffic can no figure out if somebody is marking themself actually as exposed
/// - callback: callback
@available(iOS 12.5, *)
public static func iWasExposed(onset: Date,
authentication: ExposeeAuthMethod,
isFakeRequest: Bool = false,
callback: @escaping (Result<IWasExposedResult, DP3TTracingError>) -> Void) {
instancePrecondition()
instance.iWasExposed(onset: onset,
authentication: authentication,
isFakeRequest: isFakeRequest,
callback: callback)
}
/// reset exposure days
@available(iOS 12.5, *)
public static func resetExposureDays() {
instancePrecondition()
instance.resetExposureDays()
}
/// reset the infection status
@available(iOS 12.5, *)
public static func resetInfectionStatus() {
instancePrecondition()
instance.resetInfectionStatus()
}
/// reset the SDK
@available(iOS 12.5, *)
public static func reset() {
instancePrecondition()
instance.reset()
instance = nil
}
@available(iOS 12.5, *)
public static func setBackgroundTasksEnabled(_ enabled: Bool) {
instancePrecondition()
instance.setBackgroundTasksEnabled(enabled)
}
public static var loggingEnabled: Bool {
set {
Logger.loggingEnabled = newValue
}
get {
Logger.loggingEnabled
}
}
public static var loggingDelegate: LoggingDelegate? {
set {
Logger.delegate = newValue
}
get { nil }
}
public static weak var activityDelegate: ActivityDelegate?
}