summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2015-05-09 10:33:08 -0700
committerMinh Nguyễn <mxn@1ec5.org>2015-05-12 01:38:04 -0700
commit54a703bcb21e567559caf691759bb207c611d776 (patch)
treef63e10e8b8d6c7c5bfd2dbfb7b66046630d75202
parent58d695f6d31769437007e918fceb41a49b377001 (diff)
downloadqtlocation-mapboxgl-54a703bcb21e567559caf691759bb207c611d776.tar.gz
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.
-rw-r--r--include/mbgl/ios/MGLMapboxEvents.h3
-rw-r--r--platform/ios/MGLMapView.mm2
-rw-r--r--platform/ios/MGLMapboxEvents.m63
3 files changed, 48 insertions, 20 deletions
diff --git a/include/mbgl/ios/MGLMapboxEvents.h b/include/mbgl/ios/MGLMapboxEvents.h
index abbd3b4b44..5c5d8a924c 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 80ef4a5258..c928dd6aec 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 e1c84efe61..89a13f7b56 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