objc2-exposure-notification 0.3.2

Bindings to the ExposureNotification framework
Documentation
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! This file has been automatically generated by `objc2`'s `header-translator`.
//! DO NOT EDIT
use core::ffi::*;
use core::ptr::NonNull;
#[cfg(feature = "dispatch2")]
use dispatch2::*;
use objc2::__framework_prelude::*;
use objc2_foundation::*;

use crate::*;

/// Overall status of Exposure Notification on the system.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/exposurenotification/enstatus?language=objc)
// NS_ENUM
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ENStatus(pub NSInteger);
impl ENStatus {
    /// Status of Exposure Notification is unknown. This is the status before ENManager has activated successfully.
    #[doc(alias = "ENStatusUnknown")]
    pub const Unknown: Self = Self(0);
    /// Exposure Notification is active on the system.
    #[doc(alias = "ENStatusActive")]
    pub const Active: Self = Self(1);
    /// Exposure Notification is disabled. setExposureNotificationEnabled:completionHandler can be used to enable it.
    #[doc(alias = "ENStatusDisabled")]
    pub const Disabled: Self = Self(2);
    /// Bluetooth has been turned off on the system. Bluetooth is required for Exposure Notification.
    /// Note: this may not match the state of Bluetooth as reported by CoreBluetooth.
    /// Exposure Notification is a system service and can use Bluetooth in situations when apps cannot.
    /// So for the purposes of Exposure Notification, it's better to use this API instead of CoreBluetooth.
    #[doc(alias = "ENStatusBluetoothOff")]
    pub const BluetoothOff: Self = Self(3);
    /// Exposure Notification is not active due to system restrictions, such as parental controls.
    /// When in this state, the app cannot enable Exposure Notification.
    #[doc(alias = "ENStatusRestricted")]
    pub const Restricted: Self = Self(4);
    /// For future use. Not returned by any APIs yet.
    #[doc(alias = "ENStatusPaused")]
    pub const Paused: Self = Self(5);
    /// Exposure Notification is not available due to insufficient authorization.
    #[doc(alias = "ENStatusUnauthorized")]
    pub const Unauthorized: Self = Self(6);
}

unsafe impl Encode for ENStatus {
    const ENCODING: Encoding = NSInteger::ENCODING;
}

unsafe impl RefEncode for ENStatus {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

/// Activities that occurred while the app might not be running.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/exposurenotification/enactivityflags?language=objc)
// NS_OPTIONS
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ENActivityFlags(pub u32);
bitflags::bitflags! {
    impl ENActivityFlags: u32 {
/// Reserved field
        #[doc(alias = "ENActivityFlagsReserved1")]
        const Reserved1 = 1<<0;
/// Reserved field
        #[doc(alias = "ENActivityFlagsReserved2")]
        const Reserved2 = 1<<1;
/// The app launched in the background to perform periodic operations on iOS 12.5.
        #[doc(alias = "ENActivityFlagsPeriodicRun")]
        const PeriodicRun = 1<<2;
/// The app launched in the foreground to display information about the pre-authorized key release
        #[doc(alias = "ENActivityFlagsPreAuthorizedKeyReleaseNotificationTapped")]
        const PreAuthorizedKeyReleaseNotificationTapped = 1<<3;
    }
}

unsafe impl Encode for ENActivityFlags {
    const ENCODING: Encoding = u32::ENCODING;
}

unsafe impl RefEncode for ENActivityFlags {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

/// Invoked after the app is launched to report activities that occurred while the app might not be running.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/exposurenotification/enactivityhandler?language=objc)
#[cfg(feature = "block2")]
pub type ENActivityHandler = *mut block2::DynBlock<dyn Fn(ENActivityFlags)>;

/// Invoked when getDiagnosisKeysWithCompletionHandler completes.
/// If it completes successfully, keys will contain the Diagnosis Keys for this device and error will be nil.
/// If it fails, keys will be nil and error indicates the reason it failed.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/exposurenotification/engetdiagnosiskeyshandler?language=objc)
#[cfg(all(feature = "ENCommon", feature = "block2"))]
pub type ENGetDiagnosisKeysHandler =
    *mut block2::DynBlock<dyn Fn(*mut NSArray<ENTemporaryExposureKey>, *mut NSError)>;

/// Invoked when detecting exposures completes. It provides a summary of exposures.
/// If it completes successfully, summary will contain a summary of exposures and error will be nil.
/// If it fails, summary will be nil and error indicates the reason it failed.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/exposurenotification/endetectexposureshandler?language=objc)
#[cfg(all(feature = "ENCommon", feature = "block2"))]
pub type ENDetectExposuresHandler =
    *mut block2::DynBlock<dyn Fn(*mut ENExposureDetectionSummary, *mut NSError)>;

/// [Apple's documentation](https://developer.apple.com/documentation/exposurenotification/endiagnosiskeysavailablehandler?language=objc)
#[cfg(all(feature = "ENCommon", feature = "block2"))]
pub type ENDiagnosisKeysAvailableHandler =
    *mut block2::DynBlock<dyn Fn(NonNull<NSArray<ENTemporaryExposureKey>>)>;

/// Invoked when getting exposures completes. It provides info about each exposure.
/// If it completes successfully, exposures will contain info about each exposure and error will be nil.
/// If it fails, exposures will be nil and error indicates the reason it failed.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/exposurenotification/engetexposureinfohandler?language=objc)
#[cfg(all(feature = "ENCommon", feature = "block2"))]
pub type ENGetExposureInfoHandler =
    *mut block2::DynBlock<dyn Fn(*mut NSArray<ENExposureInfo>, *mut NSError)>;

/// Invoked when getExposureWindows completes. It provides info about each exposure window.
/// If it completes successfully, exposureWindows will non-nil and error will be nil.
/// If it fails, exposureWindows will be nil and error indicates the reason it failed.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/exposurenotification/engetexposurewindowshandler?language=objc)
#[cfg(all(feature = "ENCommon", feature = "block2"))]
pub type ENGetExposureWindowsHandler =
    *mut block2::DynBlock<dyn Fn(*mut NSArray<ENExposureWindow>, *mut NSError)>;

/// Invoked when getUserTraveled completes.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/exposurenotification/engetusertraveledhandler?language=objc)
#[cfg(feature = "block2")]
pub type ENGetUserTraveledHandler = *mut block2::DynBlock<dyn Fn(Bool, *mut NSError)>;

extern_class!(
    /// Manages Exposure Notification functionality.
    ///
    /// See also [Apple's documentation](https://developer.apple.com/documentation/exposurenotification/enmanager?language=objc)
    #[unsafe(super(NSObject))]
    #[derive(Debug, PartialEq, Eq, Hash)]
    pub struct ENManager;
);

extern_conformance!(
    unsafe impl NSObjectProtocol for ENManager {}
);

impl ENManager {
    extern_methods!(
        #[cfg(feature = "block2")]
        /// Invoked when the app is launched for an activity while it might not be running.
        /// When the app is launched, it should create an ENManager instance, set this handler, and then activate the manager.
        #[unsafe(method(activityHandler))]
        #[unsafe(method_family = none)]
        pub unsafe fn activityHandler(&self) -> ENActivityHandler;

        #[cfg(feature = "block2")]
        /// Setter for [`activityHandler`][Self::activityHandler].
        ///
        /// This is [copied][objc2_foundation::NSCopying::copy] when set.
        ///
        /// # Safety
        ///
        /// `activity_handler` must be a valid pointer or null.
        #[unsafe(method(setActivityHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn setActivityHandler(&self, activity_handler: ENActivityHandler);

        #[cfg(feature = "dispatch2")]
        #[unsafe(method(dispatchQueue))]
        #[unsafe(method_family = none)]
        pub unsafe fn dispatchQueue(&self) -> Retained<DispatchQueue>;

        #[cfg(feature = "dispatch2")]
        /// Setter for [`dispatchQueue`][Self::dispatchQueue].
        ///
        /// # Safety
        ///
        /// `dispatch_queue` possibly has additional threading requirements.
        #[unsafe(method(setDispatchQueue:))]
        #[unsafe(method_family = none)]
        pub unsafe fn setDispatchQueue(&self, dispatch_queue: &DispatchQueue);

        /// Overall status of Exposure Notification. KVO may be used to monitor for changes.
        #[unsafe(method(exposureNotificationStatus))]
        #[unsafe(method_family = none)]
        pub unsafe fn exposureNotificationStatus(&self) -> ENStatus;

        #[cfg(feature = "dispatch2")]
        /// Invoked exactly once when invalidation completes. This property is cleared before it's invoked to break retain cycles.
        #[unsafe(method(invalidationHandler))]
        #[unsafe(method_family = none)]
        pub unsafe fn invalidationHandler(&self) -> dispatch_block_t;

        #[cfg(feature = "dispatch2")]
        /// Setter for [`invalidationHandler`][Self::invalidationHandler].
        ///
        /// This is [copied][objc2_foundation::NSCopying::copy] when set.
        ///
        /// # Safety
        ///
        /// `invalidation_handler` must be a valid pointer or null.
        #[unsafe(method(setInvalidationHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn setInvalidationHandler(&self, invalidation_handler: dispatch_block_t);

        #[cfg(all(feature = "ENCommon", feature = "block2"))]
        /// Activates the object to prepare it for use. Properties may not be usable until the completion handler reports success.
        ///
        /// # Safety
        ///
        /// `completion_handler` must be a valid pointer.
        #[unsafe(method(activateWithCompletionHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn activateWithCompletionHandler(&self, completion_handler: ENErrorHandler);

        /// Stops any outstanding operations and invalidates this object. Once this is called, the object can no longer be used.
        /// To start using ENManager again, a new instance of the class must be created and activated.
        #[unsafe(method(invalidate))]
        #[unsafe(method_family = none)]
        pub unsafe fn invalidate(&self);

        #[cfg(feature = "block2")]
        /// Reports if the user traveled within an exposure period (e.g. 14 days).
        ///
        /// # Safety
        ///
        /// `completion_handler` must be a valid pointer.
        #[unsafe(method(getUserTraveledWithCompletionHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn getUserTraveledWithCompletionHandler(
            &self,
            completion_handler: ENGetUserTraveledHandler,
        );

        #[cfg(feature = "ENCommon")]
        /// Reports the current authorization status of the app.
        /// Apps can use this property to preflight authorization in order to determine if the user will be prompted.
        #[unsafe(method(authorizationStatus))]
        #[unsafe(method_family = none)]
        pub unsafe fn authorizationStatus() -> ENAuthorizationStatus;

        /// Indicates if Exposure Notification is enabled on the system. KVO may be used to monitor for changes.
        /// This will be NO until activateWithCompletionHandler has completed successfully.
        /// Note that even if it's enabled, it may be inactive for other reasons, such as Bluetooth being turned off.
        /// The exposureNotificationStatus property can be monitored for the overall status of Exposure Notification.
        #[unsafe(method(exposureNotificationEnabled))]
        #[unsafe(method_family = none)]
        pub unsafe fn exposureNotificationEnabled(&self) -> bool;

        #[cfg(all(feature = "ENCommon", feature = "block2"))]
        /// Enables or disables Exposure Notification.
        /// If not previously authorized, this shows a user dialog for consent to enable Exposure Notification.
        /// Note: Disabling stops Bluetooth advertising and scanning related to Exposure Notification, but the
        /// Diagnosis Keys and data will remain.
        ///
        /// # Safety
        ///
        /// `completion_handler` must be a valid pointer.
        #[unsafe(method(setExposureNotificationEnabled:completionHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn setExposureNotificationEnabled_completionHandler(
            &self,
            enabled: bool,
            completion_handler: ENErrorHandler,
        );

        #[cfg(all(feature = "ENCommon", feature = "block2"))]
        /// Detects exposures using the specified configuration to control the scoring algorithm.
        /// This uses the diagnosis keys already known to the system.
        /// Only available to apps with ENAPIVersion 2 or higher.
        ///
        /// # Safety
        ///
        /// `completion_handler` must be a valid pointer.
        #[unsafe(method(detectExposuresWithConfiguration:completionHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn detectExposuresWithConfiguration_completionHandler(
            &self,
            configuration: &ENExposureConfiguration,
            completion_handler: ENDetectExposuresHandler,
        ) -> Retained<NSProgress>;

        #[cfg(all(feature = "ENCommon", feature = "block2"))]
        /// Detects exposures using the specified configuration to control the scoring algorithm and URLs to specify the
        /// files containing diagnosis keys the app has downloaded. The diagnosis key files must be signed appropriately.
        /// When the app's ENAPIVersion is 2 or higher, keys already known to the system are included in the analysis.
        ///
        /// # Safety
        ///
        /// `completion_handler` must be a valid pointer.
        #[unsafe(method(detectExposuresWithConfiguration:diagnosisKeyURLs:completionHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn detectExposuresWithConfiguration_diagnosisKeyURLs_completionHandler(
            &self,
            configuration: &ENExposureConfiguration,
            diagnosis_key_ur_ls: &NSArray<NSURL>,
            completion_handler: ENDetectExposuresHandler,
        ) -> Retained<NSProgress>;

        #[cfg(all(feature = "ENCommon", feature = "block2"))]
        /// Gets info about each exposure from the summary provided when exposure detection completes.
        /// Using this API will inform the user that their exposure details have been revealed to the app.
        /// The user explanation string will be displayed as part of the UI to inform the user of using this API.
        ///
        /// # Safety
        ///
        /// `completion_handler` must be a valid pointer.
        #[deprecated = "Use getExposureWindowsFromSummary, if needed."]
        #[unsafe(method(getExposureInfoFromSummary:userExplanation:completionHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn getExposureInfoFromSummary_userExplanation_completionHandler(
            &self,
            summary: &ENExposureDetectionSummary,
            user_explanation: &NSString,
            completion_handler: ENGetExposureInfoHandler,
        ) -> Retained<NSProgress>;

        #[cfg(all(feature = "ENCommon", feature = "block2"))]
        /// Gets info about each exposure window from the summary provided when exposure detection completes.
        ///
        /// # Safety
        ///
        /// `completion_handler` must be a valid pointer.
        #[unsafe(method(getExposureWindowsFromSummary:completionHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn getExposureWindowsFromSummary_completionHandler(
            &self,
            summary: &ENExposureDetectionSummary,
            completion_handler: ENGetExposureWindowsHandler,
        ) -> Retained<NSProgress>;

        #[cfg(all(feature = "ENCommon", feature = "block2"))]
        /// Requests the temporary exposure keys used by this device to share with a server.
        /// Each use of this API will present the user with system UI to authorize it.
        ///
        /// # Safety
        ///
        /// `completion_handler` must be a valid pointer.
        #[unsafe(method(getDiagnosisKeysWithCompletionHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn getDiagnosisKeysWithCompletionHandler(
            &self,
            completion_handler: ENGetDiagnosisKeysHandler,
        );

        #[cfg(all(feature = "ENCommon", feature = "block2"))]
        /// Requests the temporary exposure keys, including the current key, used by this device for testing.
        /// Each use of this API will present the user with system UI to authorize it.
        /// WARNING: This API is only for use by developers. It requires a special entitlement that is not allowed in the app store.
        /// It's only intended to allow developers to test without needing to wait 24 hours for a key to be released.
        ///
        /// # Safety
        ///
        /// `completion_handler` must be a valid pointer.
        #[unsafe(method(getTestDiagnosisKeysWithCompletionHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn getTestDiagnosisKeysWithCompletionHandler(
            &self,
            completion_handler: ENGetDiagnosisKeysHandler,
        );

        #[cfg(all(feature = "ENCommon", feature = "block2"))]
        /// Invoked after requestPreAuthorizedDiagnosisKeys if user authorization is still valid.
        ///
        /// # Safety
        ///
        /// The returned block's argument must be a valid pointer.
        #[unsafe(method(diagnosisKeysAvailableHandler))]
        #[unsafe(method_family = none)]
        pub unsafe fn diagnosisKeysAvailableHandler(&self) -> ENDiagnosisKeysAvailableHandler;

        #[cfg(all(feature = "ENCommon", feature = "block2"))]
        /// Setter for [`diagnosisKeysAvailableHandler`][Self::diagnosisKeysAvailableHandler].
        ///
        /// This is [copied][objc2_foundation::NSCopying::copy] when set.
        ///
        /// # Safety
        ///
        /// `diagnosis_keys_available_handler` must be a valid pointer or null.
        #[unsafe(method(setDiagnosisKeysAvailableHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn setDiagnosisKeysAvailableHandler(
            &self,
            diagnosis_keys_available_handler: ENDiagnosisKeysAvailableHandler,
        );

        #[cfg(all(feature = "ENCommon", feature = "block2"))]
        /// Authorizes a one-time, future release of diagnosis keys without a user prompt at the time of release.
        /// This allows the user to authorize ahead of time in case they are unable to approve at the time of positive diagnosis.
        /// WARNING: Application should be in foreground to request the authorization
        ///
        /// # Safety
        ///
        /// `completion_handler` must be a valid pointer.
        #[unsafe(method(preAuthorizeDiagnosisKeysWithCompletionHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn preAuthorizeDiagnosisKeysWithCompletionHandler(
            &self,
            completion_handler: ENErrorHandler,
        );

        #[cfg(all(feature = "ENCommon", feature = "block2"))]
        /// Requests diagnosis keys after previously using preAuthorizeDiagnosisKeys successfully.
        /// This will display a notification to the user for the user to know the keys will be returned.
        /// Keys are returned by invoking diagnosisKeysAvailable, which must be set before calling this.
        ///
        /// # Safety
        ///
        /// `completion_handler` must be a valid pointer.
        #[unsafe(method(requestPreAuthorizedDiagnosisKeysWithCompletionHandler:))]
        #[unsafe(method_family = none)]
        pub unsafe fn requestPreAuthorizedDiagnosisKeysWithCompletionHandler(
            &self,
            completion_handler: ENErrorHandler,
        );
    );
}

/// Methods declared on superclass `NSObject`.
impl ENManager {
    extern_methods!(
        #[unsafe(method(init))]
        #[unsafe(method_family = init)]
        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;

        #[unsafe(method(new))]
        #[unsafe(method_family = new)]
        pub unsafe fn new() -> Retained<Self>;
    );
}