|
| 1 | +////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// B L I N K |
| 4 | +// |
| 5 | +// Copyright (C) 2016-2018 Blink Mobile Shell Project |
| 6 | +// |
| 7 | +// This file is part of Blink. |
| 8 | +// |
| 9 | +// Blink is free software: you can redistribute it and/or modify |
| 10 | +// it under the terms of the GNU General Public License as published by |
| 11 | +// the Free Software Foundation, either version 3 of the License, or |
| 12 | +// (at your option) any later version. |
| 13 | +// |
| 14 | +// Blink is distributed in the hope that it will be useful, |
| 15 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +// GNU General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License |
| 20 | +// along with Blink. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +// |
| 22 | +// In addition, Blink is also subject to certain additional terms under |
| 23 | +// GNU GPL version 3 section 7. |
| 24 | +// |
| 25 | +// You should have received a copy of these additional terms immediately |
| 26 | +// following the terms and conditions of the GNU General Public License |
| 27 | +// which accompanied the Blink Source Code. If not, see |
| 28 | +// <http://www.github.com/blinksh/blink>. |
| 29 | +// |
| 30 | +//////////////////////////////////////////////////////////////////////////////// |
| 31 | + |
| 32 | + |
| 33 | +#import "GeoManager.h" |
| 34 | +#import <CoreLocation/CoreLocation.h> |
| 35 | +#import <ImageIO/ImageIO.h> |
| 36 | +#import <UIKit/UIKit.h> |
| 37 | +#import <UserNotifications/UserNotifications.h> |
| 38 | + |
| 39 | +NSString * BLGeoLockNotification = @"BLGeoLockNotification"; |
| 40 | + |
| 41 | +@interface GeoManager() <CLLocationManagerDelegate> |
| 42 | + |
| 43 | +@end |
| 44 | + |
| 45 | +// https://gist.github.com/rsattar/b06060df7ea293b398d1 |
| 46 | +NSDictionary *__locationToJson(CLLocation * location) { |
| 47 | + NSMutableDictionary *gps = [NSMutableDictionary dictionary]; |
| 48 | + |
| 49 | + // Example: |
| 50 | + /* |
| 51 | + "{GPS}" = { |
| 52 | + Altitude = "41.28771929824561"; |
| 53 | + AltitudeRef = 0; |
| 54 | + DateStamp = "2014:07:21"; |
| 55 | + ImgDirection = "68.2140221402214"; |
| 56 | + ImgDirectionRef = T; |
| 57 | + Latitude = "37.74252"; |
| 58 | + LatitudeRef = N; |
| 59 | + Longitude = "122.42035"; |
| 60 | + LongitudeRef = W; |
| 61 | + TimeStamp = "15:53:24"; |
| 62 | + }; |
| 63 | + */ |
| 64 | + |
| 65 | + // GPS tag version |
| 66 | + // According to http://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf, |
| 67 | + // this value is 2.3.0.0 |
| 68 | + [gps setObject:@"2.3.0.0" forKey:(NSString *)kCGImagePropertyGPSVersion]; |
| 69 | + |
| 70 | + // Time and date must be provided as strings, not as an NSDate object |
| 71 | + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; |
| 72 | + formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; |
| 73 | + formatter.dateFormat = @"HH:mm:ss.SSSSSS"; |
| 74 | + gps[(NSString *)kCGImagePropertyGPSTimeStamp] = [formatter stringFromDate:location.timestamp]; |
| 75 | + formatter.dateFormat = @"yyyy:MM:dd"; |
| 76 | + gps[(NSString *)kCGImagePropertyGPSDateStamp] = [formatter stringFromDate:location.timestamp]; |
| 77 | + |
| 78 | + // Latitude |
| 79 | + CLLocationDegrees latitude = location.coordinate.latitude; |
| 80 | + gps[(NSString *)kCGImagePropertyGPSLatitudeRef] = (latitude < 0) ? @"S" : @"N"; |
| 81 | + gps[(NSString *)kCGImagePropertyGPSLatitude] = @(fabs(latitude)); |
| 82 | + |
| 83 | + // Longitude |
| 84 | + CLLocationDegrees longitude = location.coordinate.longitude; |
| 85 | + gps[(NSString *)kCGImagePropertyGPSLongitudeRef] = (longitude < 0) ? @"W" : @"E"; |
| 86 | + gps[(NSString *)kCGImagePropertyGPSLongitude] = @(fabs(longitude)); |
| 87 | + |
| 88 | + // Degree of Precision |
| 89 | + gps[(NSString *)kCGImagePropertyGPSDOP] = @(location.horizontalAccuracy); |
| 90 | + |
| 91 | + // Altitude |
| 92 | + CLLocationDistance altitude = location.altitude; |
| 93 | + if (!isnan(altitude)) { |
| 94 | + gps[(NSString *)kCGImagePropertyGPSAltitudeRef] = (altitude < 0) ? @(1) : @(0); |
| 95 | + gps[(NSString *)kCGImagePropertyGPSAltitude] = @(fabs(altitude)); |
| 96 | + } |
| 97 | + |
| 98 | + // Speed, must be converted from m/s to km/h |
| 99 | + if (location.speed >= 0) { |
| 100 | + gps[(NSString *)kCGImagePropertyGPSSpeedRef] = @"K"; |
| 101 | + gps[(NSString *)kCGImagePropertyGPSSpeed] = @(location.speed * (3600.0/1000.0)); |
| 102 | + } |
| 103 | + |
| 104 | + // Direction of movement |
| 105 | + if (location.course >= 0) { |
| 106 | + gps[(NSString *)kCGImagePropertyGPSTrackRef] = @"T"; |
| 107 | + gps[(NSString *)kCGImagePropertyGPSTrack] = @(location.course); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + return gps; |
| 112 | +} |
| 113 | + |
| 114 | +@implementation GeoManager { |
| 115 | + CLLocationManager * _locManager; |
| 116 | + NSMutableArray<CLLocation *> *_recentLocations; |
| 117 | + NSNumber *_lockDistanceInMeters; |
| 118 | + CLLocation *_lockCenter; |
| 119 | +} |
| 120 | + |
| 121 | +- (instancetype)init { |
| 122 | + if (self = [super init]) { |
| 123 | + _recentLocations = [[NSMutableArray alloc] init]; |
| 124 | + _locManager = [[CLLocationManager alloc] init]; |
| 125 | + _locManager.delegate = self; |
| 126 | + _traking = NO; |
| 127 | + } |
| 128 | + return self; |
| 129 | +} |
| 130 | + |
| 131 | ++ (GeoManager *)shared { |
| 132 | + static GeoManager *manager = nil; |
| 133 | + static dispatch_once_t onceToken; |
| 134 | + dispatch_once(&onceToken, ^{ |
| 135 | + manager = [[self alloc] init]; |
| 136 | + }); |
| 137 | + return manager; |
| 138 | +} |
| 139 | + |
| 140 | +- (void)authorize { |
| 141 | + [_locManager requestWhenInUseAuthorization]; |
| 142 | +} |
| 143 | + |
| 144 | +- (void)start { |
| 145 | + if (_traking) { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + _locManager.desiredAccuracy = 100000; |
| 150 | + _locManager.allowsBackgroundLocationUpdates = YES; |
| 151 | + _locManager.pausesLocationUpdatesAutomatically = NO; |
| 152 | + [_locManager startUpdatingLocation]; |
| 153 | + |
| 154 | + _traking = YES; |
| 155 | +} |
| 156 | + |
| 157 | +- (void)lockInDistance:(NSNumber *)meters { |
| 158 | + if (_traking) { |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + _lockDistanceInMeters = meters; |
| 163 | + _locManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; |
| 164 | + _locManager.allowsBackgroundLocationUpdates = YES; |
| 165 | + _locManager.pausesLocationUpdatesAutomatically = NO; |
| 166 | + [_locManager startUpdatingLocation]; |
| 167 | + |
| 168 | + _traking = YES; |
| 169 | +} |
| 170 | + |
| 171 | +- (void)_postLockNotifications { |
| 172 | + [[NSNotificationCenter defaultCenter] postNotificationName:BLGeoLockNotification object:nil]; |
| 173 | + UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter]; |
| 174 | + [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { |
| 175 | + if (settings.authorizationStatus != UNAuthorizationStatusAuthorized) { |
| 176 | + return; |
| 177 | + } |
| 178 | + if (settings.alertSetting != UNNotificationSettingEnabled) { |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; |
| 183 | + content.title = @"Geo lock"; |
| 184 | + content.body = @"Your device get out of geo range lock.\nAll session are terminated."; |
| 185 | + UNNotificationRequest *req = [UNNotificationRequest requestWithIdentifier:@"geo.lock" content:content trigger:nil]; |
| 186 | + |
| 187 | + [center addNotificationRequest:req withCompletionHandler:^(NSError * _Nullable error) { |
| 188 | + |
| 189 | + }]; |
| 190 | + }]; |
| 191 | +} |
| 192 | + |
| 193 | +- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations { |
| 194 | + [_recentLocations addObjectsFromArray:locations]; |
| 195 | + |
| 196 | + if (_lockDistanceInMeters) { |
| 197 | + |
| 198 | + if (_lockCenter) { |
| 199 | + CLLocationDistance distance = [_lockCenter distanceFromLocation:[_recentLocations lastObject]]; |
| 200 | + if (distance > _lockDistanceInMeters.doubleValue) { |
| 201 | + [self stop]; |
| 202 | + [self _postLockNotifications]; |
| 203 | + } |
| 204 | + } else if (_recentLocations.count > 10) { |
| 205 | + _lockCenter = [_recentLocations lastObject]; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + int maxHistory = 200; |
| 210 | + if (_recentLocations.count > maxHistory) { |
| 211 | + [_recentLocations removeObjectsInRange:NSMakeRange(0, _recentLocations.count - maxHistory)]; |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +- (CLLocation * __nullable)lastLocation { |
| 216 | + return [_recentLocations lastObject]; |
| 217 | +} |
| 218 | + |
| 219 | +- (NSString *)currentJSON { |
| 220 | + CLLocation * loc = [_recentLocations lastObject]; |
| 221 | + if (!loc) { |
| 222 | + return @"{}"; |
| 223 | + } |
| 224 | + |
| 225 | + NSData *data = [NSJSONSerialization dataWithJSONObject:__locationToJson(loc) options:NSJSONWritingPrettyPrinted error:nil]; |
| 226 | + return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; |
| 227 | +} |
| 228 | + |
| 229 | +- (NSString *)lastJSONN:(int)n { |
| 230 | + if (n <= 0) { |
| 231 | + n = 1; |
| 232 | + } |
| 233 | + NSMutableArray *result = [[NSMutableArray alloc] init]; |
| 234 | + NSArray *locs = [_recentLocations copy]; |
| 235 | + if (locs.count == 0) { |
| 236 | + return @"[]"; |
| 237 | + } |
| 238 | + int i = (int)locs.count; |
| 239 | + while (n > 0 && i > 0) { |
| 240 | + i--; |
| 241 | + n--; |
| 242 | + |
| 243 | + CLLocation *loc = locs[i]; |
| 244 | + NSDictionary *json = __locationToJson(loc); |
| 245 | + [result addObject: json]; |
| 246 | + } |
| 247 | + |
| 248 | + NSData *data = [NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingPrettyPrinted error:nil]; |
| 249 | + return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; |
| 250 | +} |
| 251 | + |
| 252 | +- (void)stop { |
| 253 | + if (_lockCenter) { |
| 254 | + [_recentLocations removeAllObjects]; |
| 255 | + } |
| 256 | + _lockDistanceInMeters = nil; |
| 257 | + _lockCenter = nil; |
| 258 | + [_locManager stopUpdatingLocation]; |
| 259 | + _traking = NO; |
| 260 | +} |
| 261 | + |
| 262 | +@end |
0 commit comments