-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathBackgroundLocatorPlugin.m
251 lines (203 loc) · 8.99 KB
/
BackgroundLocatorPlugin.m
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
#import "BackgroundLocatorPlugin.h"
#import "Globals.h"
#import "Utils/Util.h"
#import "Preferences/PreferencesManager.h"
#import "InitPluggable.h"
#import "DisposePluggable.h"
@implementation BackgroundLocatorPlugin {
FlutterEngine *_headlessRunner;
FlutterMethodChannel *_callbackChannel;
FlutterMethodChannel *_mainChannel;
NSObject<FlutterPluginRegistrar> *_registrar;
CLLocationManager *_locationManager;
CLLocation* _lastLocation;
}
static FlutterPluginRegistrantCallback registerPlugins = nil;
static BackgroundLocatorPlugin *instance = nil;
#pragma mark FlutterPlugin Methods
+ (void)registerWithRegistrar:(nonnull NSObject<FlutterPluginRegistrar> *)registrar {
@synchronized(self) {
if (instance == nil) {
instance = [[BackgroundLocatorPlugin alloc] init:registrar];
[registrar addApplicationDelegate:instance];
}
}
}
+ (void)setPluginRegistrantCallback:(FlutterPluginRegistrantCallback)callback {
registerPlugins = callback;
}
+ (BackgroundLocatorPlugin *) getInstance {
return instance;
}
- (void)invokeMethod:(NSString*_Nonnull)method arguments:(id _Nullable)arguments {
// Return if flutter engine is not ready
NSString *isolateId = [_headlessRunner isolateId];
if (_callbackChannel == nil || isolateId == nil) {
return;
}
[_callbackChannel invokeMethod:method arguments:arguments];
}
- (void)handleMethodCall:(FlutterMethodCall *)call
result:(FlutterResult)result {
MethodCallHelper *callHelper = [[MethodCallHelper alloc] init];
[callHelper handleMethodCall:call result:result delegate:self];
}
//https://medium.com/@calvinlin_96474/ios-11-continuous-background-location-update-by-swift-4-12ce3ac603e3
// iOS will launch the app when new location received
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Check to see if we're being launched due to a location event.
if (launchOptions[UIApplicationLaunchOptionsLocationKey] != nil) {
// Restart the headless service.
[self startLocatorService:[PreferencesManager getCallbackDispatcherHandle]];
[PreferencesManager setObservingRegion:YES];
} else if([PreferencesManager isObservingRegion]) {
[self prepareLocationManager];
[self removeLocator];
[PreferencesManager setObservingRegion:NO];
[_locationManager startUpdatingLocation];
}
// Note: if we return NO, this vetos the launch of the application.
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
if ([PreferencesManager isServiceRunning]) {
[_locationManager startMonitoringSignificantLocationChanges];
}
}
-(void)applicationWillTerminate:(UIApplication *)application {
[self observeRegionForLocation:_lastLocation];
}
- (void) observeRegionForLocation:(CLLocation *)location {
double distanceFilter = [PreferencesManager getDistanceFilter];
CLRegion* region = [[CLCircularRegion alloc] initWithCenter:location.coordinate
radius:distanceFilter
identifier:@"region"];
region.notifyOnEntry = false;
region.notifyOnExit = true;
[_locationManager startMonitoringForRegion:region];
}
- (void) prepareLocationMap:(CLLocation*) location {
_lastLocation = location;
NSDictionary<NSString*,NSNumber*>* locationMap = [Util getLocationMap:location];
[self sendLocationEvent:locationMap];
}
#pragma mark LocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations {
if (locations.count > 0) {
CLLocation* location = [locations objectAtIndex:0];
[self prepareLocationMap: location];
if([PreferencesManager isObservingRegion]) {
[self observeRegionForLocation: location];
[_locationManager stopUpdatingLocation];
}
}
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
[_locationManager stopMonitoringForRegion:region];
[_locationManager startUpdatingLocation];
}
#pragma mark LocatorPlugin Methods
- (void) sendLocationEvent: (NSDictionary<NSString*,NSNumber*>*)location {
NSString *isolateId = [_headlessRunner isolateId];
if (_callbackChannel == nil || isolateId == nil) {
return;
}
NSDictionary *map = @{
kArgCallback : @([PreferencesManager getCallbackHandle:kCallbackKey]),
kArgLocation: location
};
[_callbackChannel invokeMethod:kBCMSendLocation arguments:map];
}
- (instancetype)init:(NSObject<FlutterPluginRegistrar> *)registrar {
self = [super init];
_headlessRunner = [[FlutterEngine alloc] initWithName:@"LocatorIsolate" project:nil allowHeadlessExecution:YES];
_registrar = registrar;
[self prepareLocationManager];
_mainChannel = [FlutterMethodChannel methodChannelWithName:kChannelId
binaryMessenger:[registrar messenger]];
[registrar addMethodCallDelegate:self channel:_mainChannel];
_callbackChannel =
[FlutterMethodChannel methodChannelWithName:kBackgroundChannelId
binaryMessenger:[_headlessRunner binaryMessenger] ];
return self;
}
- (void) prepareLocationManager {
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
_locationManager.pausesLocationUpdatesAutomatically = NO;
}
#pragma mark MethodCallHelperDelegate
- (void)startLocatorService:(int64_t)handle {
[PreferencesManager setCallbackDispatcherHandle:handle];
FlutterCallbackInformation *info = [FlutterCallbackCache lookupCallbackInformation:handle];
NSAssert(info != nil, @"failed to find callback");
NSString *entrypoint = info.callbackName;
NSString *uri = info.callbackLibraryPath;
[_headlessRunner runWithEntrypoint:entrypoint libraryURI:uri];
NSAssert(registerPlugins != nil, @"failed to set registerPlugins");
// Once our headless runner has been started, we need to register the application's plugins
// with the runner in order for them to work on the background isolate. `registerPlugins` is
// a callback set from AppDelegate.m in the main application. This callback should register
// all relevant plugins (excluding those which require UI).
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
registerPlugins(_headlessRunner);
});
[_registrar addMethodCallDelegate:self channel:_callbackChannel];
}
- (void)registerLocator:(int64_t)callback
initCallback:(int64_t)initCallback
initialDataDictionary:(NSDictionary*)initialDataDictionary
disposeCallback:(int64_t)disposeCallback
settings: (NSDictionary*)settings {
[self->_locationManager requestAlwaysAuthorization];
long accuracyKey = [[settings objectForKey:kSettingsAccuracy] longValue];
CLLocationAccuracy accuracy = [Util getAccuracy:accuracyKey];
double distanceFilter= [[settings objectForKey:kSettingsDistanceFilter] doubleValue];
bool showsBackgroundLocationIndicator=[[settings objectForKey:kSettingsShowsBackgroundLocationIndicator] boolValue];
_locationManager.desiredAccuracy = accuracy;
_locationManager.distanceFilter = distanceFilter;
if (@available(iOS 11.0, *)) {
_locationManager.showsBackgroundLocationIndicator = showsBackgroundLocationIndicator;
}
if (@available(iOS 9.0, *)) {
_locationManager.allowsBackgroundLocationUpdates = YES;
}
[PreferencesManager saveDistanceFilter:distanceFilter];
[PreferencesManager setCallbackHandle:callback key:kCallbackKey];
InitPluggable *initPluggable = [[InitPluggable alloc] init];
[initPluggable setCallback:initCallback];
[initPluggable onServiceStart:initialDataDictionary];
DisposePluggable *disposePluggable = [[DisposePluggable alloc] init];
[disposePluggable setCallback:disposeCallback];
[_locationManager startUpdatingLocation];
[_locationManager startMonitoringSignificantLocationChanges];
}
- (void)removeLocator {
if (_locationManager == nil) {
return;
}
@synchronized (self) {
[_locationManager stopUpdatingLocation];
if (@available(iOS 9.0, *)) {
_locationManager.allowsBackgroundLocationUpdates = NO;
}
[_locationManager stopMonitoringSignificantLocationChanges];
for (CLRegion* region in [_locationManager monitoredRegions]) {
[_locationManager stopMonitoringForRegion:region];
}
}
DisposePluggable *disposePluggable = [[DisposePluggable alloc] init];
[disposePluggable onServiceDispose];
}
- (void) setServiceRunning:(BOOL) value {
@synchronized(self) {
[PreferencesManager setServiceRunning:value];
}
}
- (BOOL)isServiceRunning{
return [PreferencesManager isServiceRunning];
}
@end