From 54a703bcb21e567559caf691759bb207c611d776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Sat, 9 May 2015 10:33:08 -0700 Subject: [PATCH] Consolidated logic for pausing/resuming MGLMapboxEvents `MGLMapboxEvents` automatically recreates its location manager when the host app is reauthorized for Location Services. Added some redundancy to ensure that there is no location manager when either Location Services or Mapbox Metrics is off. --- include/mbgl/ios/MGLMapboxEvents.h | 3 ++ platform/ios/MGLMapView.mm | 2 + platform/ios/MGLMapboxEvents.m | 63 ++++++++++++++++++++---------- 3 files changed, 48 insertions(+), 20 deletions(-) diff --git a/include/mbgl/ios/MGLMapboxEvents.h b/include/mbgl/ios/MGLMapboxEvents.h index abbd3b4b4..5c5d8a924 100644 --- a/include/mbgl/ios/MGLMapboxEvents.h +++ b/include/mbgl/ios/MGLMapboxEvents.h @@ -54,4 +54,7 @@ extern NSString *const MGLEventGestureRotateStart; // + (void) flush; +// Main thread only ++ (void)validate; + @end diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 80ef4a525..c928dd6ae 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -707,6 +707,8 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration) if (self.isDormant) { self.dormant = NO; + + [MGLMapboxEvents validate]; self.glSnapshotView.hidden = YES; diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index e1c84efe6..89a13f7b5 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -260,12 +260,7 @@ const NSTimeInterval MGLFlushInterval = 60; usingBlock: ^(NSNotification *notification) { MGLMapboxEvents *strongSelf = weakSelf; - BOOL enabledInSettings = [[strongSelf class] isEnabled]; - if (strongSelf.paused && enabledInSettings) { - [strongSelf resumeMetricsCollection]; - } else if (!strongSelf.paused && !enabledInSettings) { - [strongSelf pauseMetricsCollection]; - } + [strongSelf validate]; }]; } return self; @@ -302,6 +297,38 @@ const NSTimeInterval MGLFlushInterval = 60; [self pauseMetricsCollection]; } ++ (void)validate { + [[MGLMapboxEvents sharedManager] validate]; +} + +- (void)validate { + MGLAssertIsMainThread(); + BOOL enabledInSettings = [[self class] isEnabled]; + if (self.paused && enabledInSettings) { + [self resumeMetricsCollection]; + } else if (!self.paused && !enabledInSettings) { + [self pauseMetricsCollection]; + } + + [self validateUpdatingLocation]; +} + +- (void)validateUpdatingLocation { + MGLAssertIsMainThread(); + if (self.paused) { + [self stopUpdatingLocation]; + } else { + CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus]; + if (authStatus == kCLAuthorizationStatusDenied || + authStatus == kCLAuthorizationStatusRestricted) { + [self stopUpdatingLocation]; + } else if (authStatus == kCLAuthorizationStatusAuthorized || + authStatus == kCLAuthorizationStatusAuthorizedWhenInUse) { + [self startUpdatingLocation]; + } + } +} + + (void)pauseMetricsCollection { [[MGLMapboxEvents sharedManager] pauseMetricsCollection]; } @@ -321,7 +348,7 @@ const NSTimeInterval MGLFlushInterval = 60; [_session invalidateAndCancel]; _session = nil; - [self stopUpdatingLocation]; + [self validateUpdatingLocation]; } - (void)stopUpdatingLocation { @@ -343,17 +370,23 @@ const NSTimeInterval MGLFlushInterval = 60; // - (void)resumeMetricsCollection { MGLAssertIsMainThread(); - if (!self.isPaused || ![[self class] isEnabled]) { + if (!self.paused || ![[self class] isEnabled]) { return; } self.paused = NO; _data = [[MGLMapboxEventsData alloc] init]; _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil]; - [self startUpdatingLocation]; + [self validateUpdatingLocation]; } - (void)startUpdatingLocation { + MGLAssertIsMainThread(); + if (_locationManager || _paused) { + NSAssert(!(_locationManager && _paused), + @"MGLMapboxEvents should not have a CLLocationManager while paused."); + return; + } _locationManager = [[CLLocationManager alloc] init]; _locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; _locationManager.distanceFilter = 10; @@ -742,17 +775,7 @@ const NSTimeInterval MGLFlushInterval = 60; } - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { - switch (status) { - case kCLAuthorizationStatusDenied: - [self stopUpdatingLocation]; - break; - case kCLAuthorizationStatusAuthorized: - case kCLAuthorizationStatusAuthorizedWhenInUse: - [self startUpdatingLocation]; - break; - default: - break; - } + [self validateUpdatingLocation]; } #pragma mark NSURLSessionDelegate