|
32 | 32 | import com.transistorsoft.locationmanager.adapter.BackgroundGeolocation;
|
33 | 33 | import com.transistorsoft.locationmanager.adapter.TSConfig;
|
34 | 34 | import com.transistorsoft.locationmanager.adapter.callback.*;
|
| 35 | +import com.transistorsoft.locationmanager.config.TransistorAuthorizationToken; |
| 36 | +import com.transistorsoft.locationmanager.config.TSAuthorization; |
35 | 37 | import com.transistorsoft.locationmanager.data.LocationModel;
|
36 | 38 | import com.transistorsoft.locationmanager.data.SQLQuery;
|
| 39 | +import com.transistorsoft.locationmanager.device.DeviceInfo; |
37 | 40 | import com.transistorsoft.locationmanager.event.ActivityChangeEvent;
|
| 41 | +import com.transistorsoft.locationmanager.event.AuthorizationEvent; |
38 | 42 | import com.transistorsoft.locationmanager.event.ConnectivityChangeEvent;
|
39 | 43 | import com.transistorsoft.locationmanager.event.GeofenceEvent;
|
40 | 44 | import com.transistorsoft.locationmanager.event.GeofencesChangeEvent;
|
41 | 45 | import com.transistorsoft.locationmanager.event.HeartbeatEvent;
|
42 | 46 | import com.transistorsoft.locationmanager.event.LocationProviderChangeEvent;
|
43 | 47 | import com.transistorsoft.locationmanager.geofence.TSGeofence;
|
44 | 48 | import com.transistorsoft.locationmanager.http.HttpResponse;
|
| 49 | +import com.transistorsoft.locationmanager.http.HttpService; |
45 | 50 | import com.transistorsoft.locationmanager.location.TSCurrentPositionRequest;
|
46 | 51 | import com.transistorsoft.locationmanager.location.TSLocation;
|
47 | 52 | import com.transistorsoft.locationmanager.location.TSWatchPositionRequest;
|
@@ -107,6 +112,7 @@ public RNBackgroundGeolocationModule(ReactApplicationContext reactContext) {
|
107 | 112 | mEvents.add(BackgroundGeolocation.EVENT_CONNECTIVITYCHANGE);
|
108 | 113 | mEvents.add(BackgroundGeolocation.EVENT_ENABLEDCHANGE);
|
109 | 114 | mEvents.add(BackgroundGeolocation.EVENT_NOTIFICATIONACTION);
|
| 115 | + mEvents.add(TSAuthorization.NAME); |
110 | 116 |
|
111 | 117 | reactContext.addLifecycleEventListener(this);
|
112 | 118 | }
|
@@ -287,6 +293,18 @@ private class PowerSaveChangeCallack implements TSPowerSaveChangeCallback {
|
287 | 293 | }
|
288 | 294 | }
|
289 | 295 |
|
| 296 | + /** |
| 297 | + * authorization event callback |
| 298 | + */ |
| 299 | + private class AuthorizationCallback implements TSAuthorizationCallback { |
| 300 | + @Override public void onResponse(AuthorizationEvent event) { |
| 301 | + try { |
| 302 | + sendEvent(TSAuthorization.NAME, jsonToMap(event.toJson())); |
| 303 | + } catch (JSONException e) { |
| 304 | + TSLog.logger.error(TSLog.error(e.getMessage()), e); |
| 305 | + } |
| 306 | + } |
| 307 | + } |
290 | 308 | @Override
|
291 | 309 | public void onHostResume() {
|
292 | 310 | if (!mInitialized) {
|
@@ -343,6 +361,17 @@ public void ready(ReadableMap params, final Callback success, final Callback fai
|
343 | 361 | if (reset) {
|
344 | 362 | config.reset();
|
345 | 363 | config.updateWithJSONObject(mapToJson(setHeadlessJobService(params)));
|
| 364 | + } else if (params.hasKey(TSAuthorization.NAME)) { |
| 365 | + ReadableMap readableMap = params.getMap(TSAuthorization.NAME); |
| 366 | + if (readableMap != null) { |
| 367 | + Map<String, Object> options = readableMap.toHashMap(); |
| 368 | + // Have to be careful with expires: ReadadbleMap#toHashMap converts it to Double. |
| 369 | + options.put(TSAuthorization.FIELD_EXPIRES, readableMap.getInt(TSAuthorization.FIELD_EXPIRES)); |
| 370 | + |
| 371 | + config.updateWithBuilder() |
| 372 | + .setAuthorization(new TSAuthorization(options)) |
| 373 | + .commit(); |
| 374 | + } |
346 | 375 | }
|
347 | 376 | }
|
348 | 377 | getAdapter().ready(new TSCallback() {
|
@@ -755,6 +784,34 @@ public void finish(int taskId, Callback success, Callback failure) {
|
755 | 784 | success.invoke(taskId);
|
756 | 785 | }
|
757 | 786 |
|
| 787 | + @ReactMethod |
| 788 | + public void getTransistorToken(String orgname, String username, String url, final Callback success, final Callback failure) { |
| 789 | + |
| 790 | + TransistorAuthorizationToken.findOrCreate(getReactApplicationContext(), orgname, username, url, new TransistorAuthorizationToken.Callback() { |
| 791 | + @Override public void onSuccess(TransistorAuthorizationToken token) { |
| 792 | + try { |
| 793 | + success.invoke(jsonToMap(token.toJson())); |
| 794 | + } catch (JSONException e) { |
| 795 | + failure.invoke(e.getMessage()); |
| 796 | + } |
| 797 | + } |
| 798 | + @Override public void onFailure(String error) { |
| 799 | + failure.invoke(error); |
| 800 | + } |
| 801 | + }); |
| 802 | + } |
| 803 | + |
| 804 | + @ReactMethod |
| 805 | + public void destroyTransistorToken(String url, final Callback success, final Callback failure) { |
| 806 | + TransistorAuthorizationToken.destroyTokenForUrl(getReactApplicationContext(), url, new TSCallback() { |
| 807 | + @Override public void onSuccess() { |
| 808 | + success.invoke(true); |
| 809 | + } |
| 810 | + @Override public void onFailure(String error) { |
| 811 | + failure.invoke(error); |
| 812 | + } |
| 813 | + }); |
| 814 | + } |
758 | 815 |
|
759 | 816 | @ReactMethod
|
760 | 817 | public void playSound(String soundId) {
|
@@ -839,6 +896,19 @@ public void getSensors(Callback success, Callback error) {
|
839 | 896 | success.invoke(params);
|
840 | 897 | }
|
841 | 898 |
|
| 899 | + @ReactMethod |
| 900 | + public void getDeviceInfo(Callback success, Callback error) { |
| 901 | + DeviceInfo deviceInfo = DeviceInfo.getInstance(getReactApplicationContext()); |
| 902 | + |
| 903 | + WritableMap params = new WritableNativeMap(); |
| 904 | + params.putString("manufacturer", deviceInfo.getManufacturer()); |
| 905 | + params.putString("model", deviceInfo.getModel()); |
| 906 | + params.putString("version", deviceInfo.getVersion()); |
| 907 | + params.putString("platform", deviceInfo.getPlatform()); |
| 908 | + params.putString("framework", "react-native"); |
| 909 | + success.invoke(params); |
| 910 | + } |
| 911 | + |
842 | 912 | @ReactMethod
|
843 | 913 | public void isPowerSaveMode(Callback success, Callback error) {
|
844 | 914 | success.invoke(getAdapter().isPowerSaveMode());
|
@@ -897,8 +967,9 @@ public void requestPermission(final Callback success, final Callback error) {
|
897 | 967 |
|
898 | 968 | @ReactMethod
|
899 | 969 | public void addEventListener(String event) {
|
| 970 | + |
900 | 971 | if (!mEvents.contains(event)) {
|
901 |
| - Log.e(TAG, "[RNBackgroundGeolocation addListener] Unknown event: " + event); |
| 972 | + TSLog.logger.warn(TSLog.warn("[RNBackgroundGeolocation addListener] Unknown event: " + event)); |
902 | 973 | return;
|
903 | 974 | }
|
904 | 975 | BackgroundGeolocation adapter = getAdapter();
|
@@ -941,6 +1012,8 @@ public void addEventListener(String event) {
|
941 | 1012 | adapter.onEnabledChange(new EnabledChangeCallback());
|
942 | 1013 | } else if (event.equalsIgnoreCase(BackgroundGeolocation.EVENT_NOTIFICATIONACTION)) {
|
943 | 1014 | adapter.onNotificationAction(new NotificationActionCallback());
|
| 1015 | + } else if (event.equalsIgnoreCase(TSAuthorization.NAME)) { |
| 1016 | + HttpService.getInstance(getReactApplicationContext()).onAuthorization(new AuthorizationCallback()); |
944 | 1017 | }
|
945 | 1018 | }
|
946 | 1019 | }
|
|
0 commit comments