-
Notifications
You must be signed in to change notification settings - Fork 207
/
RNLocationModule.java
109 lines (91 loc) · 3.92 KB
/
RNLocationModule.java
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
package com.github.reactnativecommunity.location;
import android.app.Activity;
import android.content.Intent;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.BaseActivityEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import javax.annotation.Nonnull;
public class RNLocationModule extends ReactContextBaseJavaModule {
private RNLocationProvider locationProvider;
public RNLocationModule(ReactApplicationContext reactContext) {
super(reactContext);
reactContext.addActivityEventListener(activityEventListener);
}
@Override
public String getName() {
return "RNLocation";
}
// React interface
@ReactMethod
@SuppressWarnings("unused")
public void configure(ReadableMap options, final Promise promise) {
// Update the location provider if we are given one
if (options.hasKey("androidProvider")) {
String providerName = options.getString("androidProvider");
switch (providerName) {
case "auto":
locationProvider = createDefaultLocationProvider();
break;
case "playServices":
locationProvider = createPlayServicesLocationProvider();
break;
case "standard":
locationProvider = createStandardLocationProvider();
break;
default:
Utils.emitWarning(getReactApplicationContext(), "androidProvider was passed an unknown value: " + providerName, "401");
}
} else if (locationProvider == null) {
// Otherwise ensure we have a provider and create a default if not
locationProvider = createDefaultLocationProvider();
}
// Pass the options to the location provider
locationProvider.configure(getCurrentActivity(), options, promise);
}
@ReactMethod
@SuppressWarnings("unused")
public void startUpdatingLocation() {
// Ensure we have a provider
if (locationProvider == null) {
locationProvider = createDefaultLocationProvider();
}
// Call the provider
locationProvider.startUpdatingLocation();
}
@ReactMethod
@SuppressWarnings("unused")
public void stopUpdatingLocation() {
// Ensure we have a provider
if (locationProvider == null) {
locationProvider = createDefaultLocationProvider();
}
// Call the provider
locationProvider.stopUpdatingLocation();
}
// Helpers
private ActivityEventListener activityEventListener = new BaseActivityEventListener() {
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (locationProvider instanceof RNPlayServicesLocationProvider) {
((RNPlayServicesLocationProvider) locationProvider).onActivityResult(requestCode, resultCode, data);
}
}
};
private RNLocationProvider createDefaultLocationProvider() {
// If we have the correct classes for the fused location provider, we default to that. Otherwise, we default to the built-in methods
if (Utils.hasFusedLocationProvider()) {
return createPlayServicesLocationProvider();
} else {
return createStandardLocationProvider();
}
}
private RNPlayServicesLocationProvider createPlayServicesLocationProvider() {
return new RNPlayServicesLocationProvider(getCurrentActivity(), getReactApplicationContext());
}
private RNStandardLocationProvider createStandardLocationProvider() {
return new RNStandardLocationProvider(getReactApplicationContext());
}
}