summaryrefslogtreecommitdiff
path: root/platform/ios/src/MGLMapboxEvents.m
blob: 273af5b3bc6f09a1c487a57da6f112ffac26e977 (plain)
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
#import "MGLMapboxEvents.h"
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MGLAccountManager.h"
#import "NSProcessInfo+MGLAdditions.h"
#import "NSException+MGLAdditions.h"
#import "MGLAPIClient.h"
#import "MGLLocationManager.h"
#import "MGLTelemetryConfig.h"

#include <mbgl/storage/reachability.h>
#include <sys/sysctl.h>

// Event types
NSString *const MGLEventTypeAppUserTurnstile = @"appUserTurnstile";
NSString *const MGLEventTypeMapLoad = @"map.load";
NSString *const MGLEventTypeMapTap = @"map.click";
NSString *const MGLEventTypeMapDragEnd = @"map.dragend";
NSString *const MGLEventTypeLocation = @"location";
NSString *const MGLEventTypeLocalDebug = @"debug";

// Gestures
NSString *const MGLEventGestureSingleTap = @"SingleTap";
NSString *const MGLEventGestureDoubleTap = @"DoubleTap";
NSString *const MGLEventGestureTwoFingerSingleTap = @"TwoFingerTap";
NSString *const MGLEventGestureQuickZoom = @"QuickZoom";
NSString *const MGLEventGesturePanStart = @"Pan";
NSString *const MGLEventGesturePinchStart = @"Pinch";
NSString *const MGLEventGestureRotateStart = @"Rotation";
NSString *const MGLEventGesturePitchStart = @"Pitch";

// Event keys
NSString *const MGLEventKeyLatitude = @"lat";
NSString *const MGLEventKeyLongitude = @"lng";
NSString *const MGLEventKeyZoomLevel = @"zoom";
NSString *const MGLEventKeySpeed = @"speed";
NSString *const MGLEventKeyCourse = @"course";
NSString *const MGLEventKeyGestureID = @"gesture";
NSString *const MGLEventHorizontalAccuracy = @"horizontalAccuracy";
NSString *const MGLEventKeyLocalDebugDescription = @"debug.description";

static NSString *const MGLEventKeyEvent = @"event";
static NSString *const MGLEventKeyCreated = @"created";
static NSString *const MGLEventKeyVendorID = @"userId";
static NSString *const MGLEventKeyModel = @"model";
static NSString *const MGLEventKeyEnabledTelemetry = @"enabled.telemetry";
static NSString *const MGLEventKeyOperatingSystem = @"operatingSystem";
static NSString *const MGLEventKeyResolution = @"resolution";
static NSString *const MGLEventKeyAccessibilityFontScale = @"accessibilityFontScale";
static NSString *const MGLEventKeyOrientation = @"orientation";
static NSString *const MGLEventKeyPluggedIn = @"pluggedIn";
static NSString *const MGLEventKeyWifi = @"wifi";
static NSString *const MGLEventKeySource = @"source";
static NSString *const MGLEventKeySessionId = @"sessionId";
static NSString *const MGLEventKeyApplicationState = @"applicationState";
static NSString *const MGLEventKeyAltitude = @"altitude";

static NSString *const MGLMapboxAccountType = @"MGLMapboxAccountType";
static NSString *const MGLMapboxMetricsEnabled = @"MGLMapboxMetricsEnabled";

// SDK event source
static NSString *const MGLEventSource = @"mapbox";

// Event application state
static NSString *const MGLApplicationStateForeground = @"Foreground";
static NSString *const MGLApplicationStateBackground = @"Background";
static NSString *const MGLApplicationStateInactive = @"Inactive";
static NSString *const MGLApplicationStateUnknown = @"Unknown";

const NSUInteger MGLMaximumEventsPerFlush = 180;
const NSTimeInterval MGLFlushInterval = 180;

@interface MGLMapboxEventsData : NSObject

@property (nonatomic) NSString *vendorId;
@property (nonatomic) NSString *model;
@property (nonatomic) NSString *iOSVersion;
@property (nonatomic) CGFloat scale;

@end

@implementation MGLMapboxEventsData

- (instancetype)init {
    if (self = [super init]) {
        _vendorId = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        _model = [self sysInfoByName:"hw.machine"];
        _iOSVersion = [NSString stringWithFormat:@"%@ %@", [UIDevice currentDevice].systemName, [UIDevice currentDevice].systemVersion];
        if ([UIScreen instancesRespondToSelector:@selector(nativeScale)]) {
            _scale = [UIScreen mainScreen].nativeScale;
        } else {
            _scale = [UIScreen mainScreen].scale;
        }
    }
    return self;
}

- (NSString *)sysInfoByName:(char *)typeSpecifier {
    size_t size;
    sysctlbyname(typeSpecifier, NULL, &size, NULL, 0);

    char *answer = malloc(size);
    sysctlbyname(typeSpecifier, answer, &size, NULL, 0);

    NSString *results = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];

    free(answer);
    return results;
}

@end

@interface MGLMapboxEvents () <MGLLocationManagerDelegate>

@property (nonatomic) MGLMapboxEventsData *data;
@property (nonatomic, copy) NSString *appBundleId;
@property (nonatomic, readonly) NSString *instanceID;
@property (nonatomic, copy) NSString *dateForDebugLogFile;
@property (nonatomic) NSDateFormatter *rfc3339DateFormatter;
@property (nonatomic) MGLAPIClient *apiClient;
@property (nonatomic) BOOL usesTestServer;
@property (nonatomic) BOOL canEnableDebugLogging;
@property (nonatomic, getter=isPaused) BOOL paused;
@property (nonatomic) NS_MUTABLE_ARRAY_OF(MGLMapboxEventAttributes *) *eventQueue;
@property (nonatomic) dispatch_queue_t serialQueue;
@property (nonatomic) dispatch_queue_t debugLogSerialQueue;
@property (nonatomic) MGLLocationManager *locationManager;
@property (nonatomic) NSTimer *timer;
@property (nonatomic) NSDate *instanceIDRotationDate;
@property (nonatomic) NSDate *nextTurnstileSendDate;
@property (nonatomic) NSNumber *currentAccountTypeValue;
@property (nonatomic) BOOL currentMetricsEnabledValue;

@end

@implementation MGLMapboxEvents {
    NSString *_instanceID;
    UIBackgroundTaskIdentifier _backgroundTaskIdentifier;
}

+ (void)initialize {
    if (self == [MGLMapboxEvents class]) {
        NSBundle *bundle = [NSBundle mainBundle];
        NSNumber *accountTypeNumber = [bundle objectForInfoDictionaryKey:MGLMapboxAccountType];
        [[NSUserDefaults standardUserDefaults] registerDefaults:@{
             MGLMapboxAccountType: accountTypeNumber ?: @0,
             MGLMapboxMetricsEnabled: @YES,
             @"MGLMapboxMetricsDebugLoggingEnabled": @NO,
         }];
    }
}

+ (BOOL)isEnabled {
#if TARGET_OS_SIMULATOR
    return NO;
#else
    BOOL isLowPowerModeEnabled = NO;
    if ([NSProcessInfo instancesRespondToSelector:@selector(isLowPowerModeEnabled)]) {
        isLowPowerModeEnabled = [[NSProcessInfo processInfo] isLowPowerModeEnabled];
    }
    return ([[NSUserDefaults standardUserDefaults] boolForKey:MGLMapboxMetricsEnabled] &&
            [[NSUserDefaults standardUserDefaults] integerForKey:MGLMapboxAccountType] == 0 &&
            !isLowPowerModeEnabled);
#endif
}


- (BOOL)debugLoggingEnabled {
    return (self.canEnableDebugLogging &&
            [[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsDebugLoggingEnabled"]);
}

- (instancetype) init {
    self = [super init];
    if (self) {
        [MGLTelemetryConfig.sharedConfig configurationFromKey:[[NSUserDefaults standardUserDefaults] objectForKey:MGLMapboxMetricsProfile]];
        
        _currentAccountTypeValue = @0;
        _currentMetricsEnabledValue = YES;
        
        _appBundleId = [[NSBundle mainBundle] bundleIdentifier];
        _apiClient = [[MGLAPIClient alloc] init];

        NSString *uniqueID = [[NSProcessInfo processInfo] globallyUniqueString];
        _serialQueue = dispatch_queue_create([[NSString stringWithFormat:@"%@.%@.events.serial", _appBundleId, uniqueID] UTF8String], DISPATCH_QUEUE_SERIAL);

        _locationManager = [[MGLLocationManager alloc] init];
        _locationManager.delegate = self;
        _paused = YES;
        [self resumeMetricsCollection];

        // Events Control
        _eventQueue = [[NSMutableArray alloc] init];

        // Setup Date Format
        _rfc3339DateFormatter = [[NSDateFormatter alloc] init];
        NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

        [_rfc3339DateFormatter setLocale:enUSPOSIXLocale];
        [_rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"];
        // Clear Any System TimeZone Cache
        [NSTimeZone resetSystemTimeZone];
        [_rfc3339DateFormatter setTimeZone:[NSTimeZone systemTimeZone]];

        // Configure logging
        if ([self isProbablyAppStoreBuild]) {
            self.canEnableDebugLogging = NO;

            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsDebugLoggingEnabled"]) {
                NSLog(@"Telemetry logging is only enabled in non-app store builds.");
            }
        } else {
            self.canEnableDebugLogging = YES;
        }

        // Watch for changes to telemetry settings by the user
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDefaultsDidChange:) name:NSUserDefaultsDidChangeNotification object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseOrResumeMetricsCollectionIfRequired) name:UIApplicationDidEnterBackgroundNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseOrResumeMetricsCollectionIfRequired) name:UIApplicationDidBecomeActiveNotification object:nil];

        // Watch for Low Power Mode change events
        if (&NSProcessInfoPowerStateDidChangeNotification != NULL) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseOrResumeMetricsCollectionIfRequired) name:NSProcessInfoPowerStateDidChangeNotification object:nil];
        }
    }
    return self;
}

// Called implicitly from any public class convenience methods.
// May return nil if this feature is disabled.
//
+ (nullable instancetype)sharedManager {
    if (NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) {
        return nil;
    }
    static dispatch_once_t onceToken;
    static MGLMapboxEvents *_sharedManager;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[self alloc] init];
    });
    return _sharedManager;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self pauseMetricsCollection];
}

- (NSString *)instanceID {
    if (self.instanceIDRotationDate && [[NSDate date] timeIntervalSinceDate:self.instanceIDRotationDate] >= 0) {
        _instanceID = nil;
    }
    if (!_instanceID) {
        _instanceID = [[NSUUID UUID] UUIDString];
        NSTimeInterval twentyFourHourTimeInterval = 24 * 3600;
        self.instanceIDRotationDate = [[NSDate date] dateByAddingTimeInterval:twentyFourHourTimeInterval];
    }
    return _instanceID;
}

- (void)userDefaultsDidChange:(NSNotification *)notification {
    
    // Guard against over calling pause / resume if the values this implementation actually
    // cares about have not changed
    
    if ([[notification object] respondsToSelector:@selector(objectForKey:)]) {
        NSUserDefaults *userDefaults = [notification object];
        
        NSNumber *accountType = [userDefaults objectForKey:MGLMapboxAccountType];
        BOOL metricsEnabled = [[userDefaults objectForKey:MGLMapboxMetricsEnabled] boolValue];
        
        if (![accountType isEqualToNumber:self.currentAccountTypeValue] || metricsEnabled != self.currentMetricsEnabledValue) {
            [self pauseOrResumeMetricsCollectionIfRequired];
            self.currentAccountTypeValue = accountType;
            self.currentMetricsEnabledValue = metricsEnabled;
        }        
    }
    
}

- (void)pauseOrResumeMetricsCollectionIfRequired {
    
    // [CLLocationManager authorizationStatus] has been found to block in some cases so
    // dispatch the call to a non-UI thread
    dispatch_async(self.serialQueue, ^{
        CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
        
        // Checking application state must be done on the main thread for safety and
        // to avoid a thread sanitizer error
        dispatch_async(dispatch_get_main_queue(), ^{
            UIApplication *application = [UIApplication sharedApplication];
            UIApplicationState state = application.applicationState;
            
            // Prevent blue status bar when host app has `when in use` permission only and it is not in foreground
            if (status == kCLAuthorizationStatusAuthorizedWhenInUse && state == UIApplicationStateBackground) {
                if (_backgroundTaskIdentifier == UIBackgroundTaskInvalid) {
                    _backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
                        [application endBackgroundTask:_backgroundTaskIdentifier];
                        _backgroundTaskIdentifier = UIBackgroundTaskInvalid;
                    }];
                    [self flush];
                }
                [self pauseMetricsCollection];
                return;
            }
            
            // Toggle pause based on current pause state, user opt-out state, and low-power state.
            BOOL enabled = [[self class] isEnabled];
            if (self.paused && enabled) {
                [self resumeMetricsCollection];
            } else if (!self.paused && !enabled) {
                [self flush];
                [self pauseMetricsCollection];
            }
        });
    });
}

- (void)pauseMetricsCollection {
    if (self.paused) {
        return;
    }

    self.paused = YES;
    [self.timer invalidate];
    self.timer = nil;
    [self.eventQueue removeAllObjects];
    self.data = nil;

    [self.locationManager stopUpdatingLocation];
}

- (void)resumeMetricsCollection {
    if (!self.paused || ![[self class] isEnabled]) {
        return;
    }

    self.paused = NO;
    self.data = [[MGLMapboxEventsData alloc] init];

    [self.locationManager startUpdatingLocation];
}

+ (void)flush {
    [[MGLMapboxEvents sharedManager] flush];
}

- (void)flush {
    if ([MGLAccountManager accessToken] == nil) {
        return;
    }

    NSArray *events = [NSArray arrayWithArray:self.eventQueue];
    [self.eventQueue removeAllObjects];

    [self postEvents:events];

    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }

    [self pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription:@"flush"}];
}

- (void)pushTurnstileEvent {
    if (self.nextTurnstileSendDate && [[NSDate date] timeIntervalSinceDate:self.nextTurnstileSendDate] < 0) {
        return;
    }

    NSString *vendorID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    if (!vendorID) {
        return;
    }

    NSDictionary *turnstileEventAttributes = @{MGLEventKeyEvent: MGLEventTypeAppUserTurnstile,
                                               MGLEventKeyCreated: [self.rfc3339DateFormatter stringFromDate:[NSDate date]],
                                               MGLEventKeyVendorID: vendorID,
                                               MGLEventKeyEnabledTelemetry: @([[self class] isEnabled])};

    if ([MGLAccountManager accessToken] == nil) {
        return;
    }

    __weak __typeof__(self) weakSelf = self;
    [self.apiClient postEvent:turnstileEventAttributes completionHandler:^(NSError * _Nullable error) {
        __strong __typeof__(weakSelf) strongSelf = weakSelf;
        if (error) {
            [strongSelf pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription: @"Network error",
                                                                         @"error": error}];
            return;
        }
        [strongSelf writeEventToLocalDebugLog:turnstileEventAttributes];
        [strongSelf updateNextTurnstileSendDate];
    }];
}

- (void)updateNextTurnstileSendDate {
    // Find the time a day from now (sometime tomorrow)
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
    dayComponent.day = 1;
    NSDate *sometimeTomorrow = [calendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];

    // Find the start of tomorrow and use that as the next turnstile send date. The effect of this is that
    // turnstile events can be sent as much as once per calendar day and always at the start of a session
    // when a map load happens.
    NSDate *startOfTomorrow = nil;
    [calendar rangeOfUnit:NSCalendarUnitDay startDate:&startOfTomorrow interval:nil forDate:sometimeTomorrow];
    self.nextTurnstileSendDate = startOfTomorrow;
}

+ (void)pushEvent:(NSString *)event withAttributes:(MGLMapboxEventAttributes *)attributeDictionary {
    [[MGLMapboxEvents sharedManager] pushEvent:event withAttributes:attributeDictionary];
}

- (void)pushEvent:(NSString *)event withAttributes:(MGLMapboxEventAttributes *)attributeDictionary {
    if (!event) {
        return;
    }

    if ([event isEqualToString:MGLEventTypeMapLoad]) {
        [self pushTurnstileEvent];
    }

    if (self.paused) {
        return;
    }

    MGLMapboxEventAttributes *fullyFormedEvent = [self fullyFormedEventForEvent:event withAttributes:attributeDictionary];
    if (fullyFormedEvent) {
        [self.eventQueue addObject:fullyFormedEvent];
        [self writeEventToLocalDebugLog:fullyFormedEvent];
        // Has Flush Limit Been Reached?
        if (self.eventQueue.count >= MGLMaximumEventsPerFlush) {
            [self flush];
        } else if (self.eventQueue.count ==  1) {
            // If this is first new event on queue start timer,
            [self startTimer];
        }
    } else {
        [self pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription: @"Unknown event",
                                                                     @"eventName": event,
                                                                     @"event.attributes": attributeDictionary}];
    }
}

#pragma mark Events

- (MGLMapboxEventAttributes *)fullyFormedEventForEvent:(NSString *)event withAttributes:(MGLMapboxEventAttributes *)attributeDictionary {
    if ([event isEqualToString:MGLEventTypeMapLoad]) {
        return  [self mapLoadEventWithAttributes:attributeDictionary];
    } else if ([event isEqualToString:MGLEventTypeMapTap]) {
        return [self mapClickEventWithAttributes:attributeDictionary];
    } else if ([event isEqualToString:MGLEventTypeMapDragEnd]) {
        return [self mapDragEndEventWithAttributes:attributeDictionary];
    } else if ([event isEqualToString:MGLEventTypeLocation]) {
        return [self locationEventWithAttributes:attributeDictionary];
    }
    return nil;
}

- (MGLMapboxEventAttributes *)locationEventWithAttributes:(MGLMapboxEventAttributes *)attributeDictionary {
    MGLMutableMapboxEventAttributes *attributes = [NSMutableDictionary dictionary];
    attributes[MGLEventKeyEvent] = MGLEventTypeLocation;
    attributes[MGLEventKeySource] = MGLEventSource;
    attributes[MGLEventKeySessionId] = self.instanceID;
    attributes[MGLEventKeyOperatingSystem] = self.data.iOSVersion;
    NSString *currentApplicationState = [self applicationState];
    if (![currentApplicationState isEqualToString:MGLApplicationStateUnknown]) {
        attributes[MGLEventKeyApplicationState] = currentApplicationState;
    }

    return [self eventForAttributes:attributes attributeDictionary:attributeDictionary];
}

- (MGLMapboxEventAttributes *)mapLoadEventWithAttributes:(MGLMapboxEventAttributes *)attributeDictionary {
    MGLMutableMapboxEventAttributes *attributes = [NSMutableDictionary dictionary];
    attributes[MGLEventKeyEvent] = MGLEventTypeMapLoad;
    attributes[MGLEventKeyCreated] = [self.rfc3339DateFormatter stringFromDate:[NSDate date]];
    attributes[MGLEventKeyVendorID] = self.data.vendorId;
    attributes[MGLEventKeyModel] = self.data.model;
    attributes[MGLEventKeyOperatingSystem] = self.data.iOSVersion;
    attributes[MGLEventKeyResolution] = @(self.data.scale);
    attributes[MGLEventKeyAccessibilityFontScale] = @([self contentSizeScale]);
    attributes[MGLEventKeyOrientation] = [self deviceOrientation];
    attributes[MGLEventKeyWifi] = @([[MGLReachability reachabilityForLocalWiFi] isReachableViaWiFi]);

    return [self eventForAttributes:attributes attributeDictionary:attributeDictionary];
}

- (MGLMapboxEventAttributes *)mapClickEventWithAttributes:(MGLMapboxEventAttributes *)attributeDictionary {
    MGLMutableMapboxEventAttributes *attributes = [self interactionEvent];
    attributes[MGLEventKeyEvent] = MGLEventTypeMapTap;
    return [self eventForAttributes:attributes attributeDictionary:attributeDictionary];
}

- (MGLMapboxEventAttributes *)mapDragEndEventWithAttributes:(MGLMapboxEventAttributes *)attributeDictionary {
    MGLMutableMapboxEventAttributes *attributes = [self interactionEvent];
    attributes[MGLEventKeyEvent] = MGLEventTypeMapDragEnd;

    return [self eventForAttributes:attributes attributeDictionary:attributeDictionary];
}

- (MGLMutableMapboxEventAttributes *)interactionEvent {
    MGLMutableMapboxEventAttributes *attributes = [NSMutableDictionary dictionary];
    attributes[MGLEventKeyCreated] = [self.rfc3339DateFormatter stringFromDate:[NSDate date]];
    attributes[MGLEventKeyOrientation] = [self deviceOrientation];
    attributes[MGLEventKeyWifi] = @([[MGLReachability reachabilityForLocalWiFi] isReachableViaWiFi]);

    return attributes;
}

- (MGLMapboxEventAttributes *)eventForAttributes:(MGLMutableMapboxEventAttributes *)attributes attributeDictionary:(MGLMapboxEventAttributes *)attributeDictionary {
    [attributes addEntriesFromDictionary:attributeDictionary];

    return [attributes copy];
}

// Called implicitly from public use of +flush.
//
- (void)postEvents:(NS_ARRAY_OF(MGLMapboxEventAttributes *) *)events {
    if (self.paused) {
        return;
    }

    __weak __typeof__(self) weakSelf = self;
    dispatch_async(self.serialQueue, ^{
        __strong __typeof__(weakSelf) strongSelf = weakSelf;
        [self.apiClient postEvents:events completionHandler:^(NSError * _Nullable error) {
            if (error) {
                [strongSelf pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription: @"Network error",
                                                                                        @"error": error}];
            } else {
                [strongSelf pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription: @"post",
                                                                                   @"debug.eventsCount": @(events.count)}];
            }
            [[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskIdentifier];
            _backgroundTaskIdentifier = UIBackgroundTaskInvalid;
        }];
    });
}

- (void)startTimer {
    [self.timer invalidate];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:MGLFlushInterval
                                                  target:self
                                                selector:@selector(flush)
                                                userInfo:nil
                                                 repeats:YES];
}

- (NSString *)deviceOrientation {
    NSString *result;

    switch ([UIDevice currentDevice].orientation) {
        case UIDeviceOrientationUnknown:
            result = @"Unknown";
            break;
        case UIDeviceOrientationPortrait:
            result = @"Portrait";
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            result = @"PortraitUpsideDown";
            break;
        case UIDeviceOrientationLandscapeLeft:
            result = @"LandscapeLeft";
            break;
        case UIDeviceOrientationLandscapeRight:
            result = @"LandscapeRight";
            break;
        case UIDeviceOrientationFaceUp:
            result = @"FaceUp";
            break;
        case UIDeviceOrientationFaceDown:
            result = @"FaceDown";
            break;
        default:
            result = @"Default - Unknown";
            break;
    }

    return result;
}

- (NSString *)applicationState {
    switch ([UIApplication sharedApplication].applicationState) {
        case UIApplicationStateActive:
            return MGLApplicationStateForeground;
        case UIApplicationStateInactive:
            return MGLApplicationStateInactive;
        case UIApplicationStateBackground:
            return MGLApplicationStateBackground;
        default:
            return MGLApplicationStateUnknown;
    }
}

- (NSInteger)contentSizeScale {
    NSInteger result = -9999;

    NSString *sc = [UIApplication sharedApplication].preferredContentSizeCategory;

    if ([sc isEqualToString:UIContentSizeCategoryExtraSmall]) {
        result = -3;
    } else if ([sc isEqualToString:UIContentSizeCategorySmall]) {
        result = -2;
    } else if ([sc isEqualToString:UIContentSizeCategoryMedium]) {
        result = -1;
    } else if ([sc isEqualToString:UIContentSizeCategoryLarge]) {
        result = 0;
    } else if ([sc isEqualToString:UIContentSizeCategoryExtraLarge]) {
        result = 1;
    } else if ([sc isEqualToString:UIContentSizeCategoryExtraExtraLarge]) {
        result = 2;
    } else if ([sc isEqualToString:UIContentSizeCategoryExtraExtraExtraLarge]) {
        result = 3;
    } else if ([sc isEqualToString:UIContentSizeCategoryAccessibilityMedium]) {
        result = -11;
    } else if ([sc isEqualToString:UIContentSizeCategoryAccessibilityLarge]) {
        result = 10;
    } else if ([sc isEqualToString:UIContentSizeCategoryAccessibilityExtraLarge]) {
        result = 11;
    } else if ([sc isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraLarge]) {
        result = 12;
    } else if ([sc isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraExtraLarge]) {
        result = 13;
    }

    return result;
}

+ (void)ensureMetricsOptoutExists {
    NSNumber *shownInAppNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLMapboxMetricsEnabledSettingShownInApp"];
    BOOL metricsEnabledSettingShownInAppFlag = [shownInAppNumber boolValue];

    if (!metricsEnabledSettingShownInAppFlag &&
        [[NSUserDefaults standardUserDefaults] integerForKey:MGLMapboxAccountType] == 0) {
        // Opt-out is not configured in UI, so check for Settings.bundle
        id defaultEnabledValue;
        NSString *appSettingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];

        if (appSettingsBundle) {
            // Dynamic Settings.bundle loading based on http://stackoverflow.com/a/510329/2094275
            NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[appSettingsBundle stringByAppendingPathComponent:@"Root.plist"]];
            NSArray *preferences = settings[@"PreferenceSpecifiers"];
            for (NSDictionary *prefSpecification in preferences) {
                if ([prefSpecification[@"Key"] isEqualToString:MGLMapboxMetricsEnabled]) {
                    defaultEnabledValue = prefSpecification[@"DefaultValue"];
                }
            }
        }

        if (!defaultEnabledValue) {
            [NSException raise:@"Telemetry opt-out missing" format:
             @"End users must be able to opt out of Mapbox Telemetry in your app, either inside Settings (via Settings.bundle) or inside this app. "
             @"By default, this opt-out control is included as a menu item in the attribution action sheet. "
             @"If you reimplement the opt-out control inside this app, disable this assertion by setting MGLMapboxMetricsEnabledSettingShownInApp to YES in Info.plist."
             @"\n\nSee https://www.mapbox.com/ios-sdk/#telemetry_opt_out for more information."
             @"\n\nAdditionally, by hiding this attribution control you agree to display the required attribution elsewhere in this app."];
        }
    }
}

#pragma mark CLLocationManagerUtilityDelegate

- (void)locationManager:(MGLLocationManager *)locationManager didUpdateLocations:(NSArray *)locations {
    for (CLLocation *loc in locations) {
        double accuracy = 10000000;
        double lat = floor(loc.coordinate.latitude * accuracy) / accuracy;
        double lng = floor(loc.coordinate.longitude * accuracy) / accuracy;
        double horizontalAccuracy = round(loc.horizontalAccuracy);
        NSString *formattedDate = [self.rfc3339DateFormatter stringFromDate:loc.timestamp];
        [MGLMapboxEvents pushEvent:MGLEventTypeLocation withAttributes:@{MGLEventKeyCreated: formattedDate,
                                                                         MGLEventKeyLatitude: @(lat),
                                                                         MGLEventKeyLongitude: @(lng),
                                                                         MGLEventKeyAltitude: @(round(loc.altitude)),
                                                                         MGLEventHorizontalAccuracy: @(horizontalAccuracy)}];
    }
}

- (void)locationManagerBackgroundLocationUpdatesDidAutomaticallyPause:(MGLLocationManager *)locationManager {
    [self pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription:@"locationManager.locationManagerAutoPause"}];
}

- (void)locationManagerBackgroundLocationUpdatesDidTimeout:(MGLLocationManager *)locationManager {
    [self pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription:@"locationManager.locationManagerTimeout"}];
}

- (void)locationManagerDidStartLocationUpdates:(MGLLocationManager *)locationManager {
    [self pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription:@"locationManager.locationManagerStartUpdates"}];
}

- (void)locationManagerDidStopLocationUpdates:(MGLLocationManager *)locationManager {
    [self pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription: @"locationManager.locationManagerStopUpdates"}];
}

#pragma mark MGLMapboxEvents Debug

- (void)pushDebugEvent:(NSString *)event withAttributes:(MGLMapboxEventAttributes *)attributeDictionary {
    if (![self debugLoggingEnabled]) {
        return;
    }

    if (!event) {
        return;
    }

    MGLMutableMapboxEventAttributes *evt = [MGLMutableMapboxEventAttributes dictionaryWithDictionary:attributeDictionary];
    [evt setObject:event forKey:@"event"];
    [evt setObject:[self.rfc3339DateFormatter stringFromDate:[NSDate date]] forKey:@"created"];
    [evt setValue:[self applicationState] forKey:@"applicationState"];
    [evt setValue:@([[self class] isEnabled]) forKey:@"telemetryEnabled"];
    [evt setObject:self.instanceID forKey:@"instance"];

    MGLMapboxEventAttributes *finalEvent = [NSDictionary dictionaryWithDictionary:evt];
    [self writeEventToLocalDebugLog:finalEvent];
}

- (void)writeEventToLocalDebugLog:(MGLMapboxEventAttributes *)event {
    if (![self debugLoggingEnabled]) {
        return;
    }

    NSLog(@"%@", [self stringForDebugEvent:event]);

    if (!self.dateForDebugLogFile) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd"];
        [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
        self.dateForDebugLogFile = [dateFormatter stringFromDate:[NSDate date]];
    }

    if (!self.debugLogSerialQueue) {
        NSString *uniqueID = [[NSProcessInfo processInfo] globallyUniqueString];
        self.debugLogSerialQueue = dispatch_queue_create([[NSString stringWithFormat:@"%@.%@.events.debugLog", _appBundleId, uniqueID] UTF8String], DISPATCH_QUEUE_SERIAL);
    }

    dispatch_async(self.debugLogSerialQueue, ^{
        if ([NSJSONSerialization isValidJSONObject:event]) {
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:event options:NSJSONWritingPrettyPrinted error:nil];

            NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            jsonString = [jsonString stringByAppendingString:@",\n"];

            NSString *logFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"telemetry_log-%@.json", self.dateForDebugLogFile]];

            NSFileManager *fileManager = [[NSFileManager alloc] init];
            if ([fileManager fileExistsAtPath:logFilePath]) {
                NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:logFilePath];
                [fileHandle seekToEndOfFile];
                [fileHandle writeData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
            } else {
                [fileManager createFileAtPath:logFilePath contents:[jsonString dataUsingEncoding:NSUTF8StringEncoding] attributes:@{ NSFileProtectionKey: NSFileProtectionCompleteUntilFirstUserAuthentication }];
            }
        }
    });
}

- (NSString *)stringForDebugEvent:(MGLMapboxEventAttributes *)event {
    // redact potentially sensitive location details from system console log
    if ([event[@"event"] isEqualToString:MGLEventTypeLocation]) {
        MGLMutableMapboxEventAttributes *evt = [MGLMutableMapboxEventAttributes dictionaryWithDictionary:event];
        [evt setObject:@"<redacted>" forKey:@"lat"];
        [evt setObject:@"<redacted>" forKey:@"lng"];
        event = evt;
    }

    return [NSString stringWithFormat:@"Mapbox Telemetry event %@", event];
}

- (BOOL)isProbablyAppStoreBuild {
#if TARGET_IPHONE_SIMULATOR
    return NO;
#else
    // BugshotKit by Marco Arment https://github.com/marcoarment/BugshotKit/
    // Adapted from https://github.com/blindsightcorp/BSMobileProvision

    NSString *binaryMobileProvision = [NSString stringWithContentsOfFile:[NSBundle.mainBundle pathForResource:@"embedded" ofType:@"mobileprovision"] encoding:NSISOLatin1StringEncoding error:NULL];
    if (!binaryMobileProvision) {
        return YES; // no provision
    }

    NSScanner *scanner = [NSScanner scannerWithString:binaryMobileProvision];
    NSString *plistString;
    if (![scanner scanUpToString:@"<plist" intoString:nil] || ! [scanner scanUpToString:@"</plist>" intoString:&plistString]) {
        return YES; // no XML plist found in provision
    }
    plistString = [plistString stringByAppendingString:@"</plist>"];

    NSData *plistdata_latin1 = [plistString dataUsingEncoding:NSISOLatin1StringEncoding];
    NSError *error = nil;
    NSDictionary *mobileProvision = [NSPropertyListSerialization propertyListWithData:plistdata_latin1 options:NSPropertyListImmutable format:NULL error:&error];
    if (error) {
        return YES; // unknown plist format
    }

    if (!mobileProvision || ! mobileProvision.count) {
        return YES; // no entitlements
    }

    if (mobileProvision[@"ProvisionsAllDevices"]) {
        return NO; // enterprise provisioning
    }

    if (mobileProvision[@"ProvisionedDevices"] && [mobileProvision[@"ProvisionedDevices"] count]) {
        return NO; // development or ad-hoc
    }

    return YES; // expected development/enterprise/ad-hoc entitlements not found
#endif
}

@end