8
8
import com .google .gson .Gson ;
9
9
import com .google .gson .annotations .SerializedName ;
10
10
import com .google .gson .reflect .TypeToken ;
11
+ import com .hypertrack .sdk .AsyncResultReceiver ;
11
12
import com .hypertrack .sdk .Blocker ;
12
13
import com .hypertrack .sdk .GeotagResult ;
13
14
import com .hypertrack .sdk .HyperTrack ;
14
15
import com .hypertrack .sdk .ServiceNotificationConfig ;
15
16
import com .hypertrack .sdk .TrackingError ;
16
17
import com .hypertrack .sdk .TrackingStateObserver ;
17
18
import com .hypertrack .sdk .logger .HTLogger ;
19
+ import com .hypertrack .sdk .Result ;
20
+ import com .hypertrack .sdk .OutageReason ;
18
21
19
22
import org .apache .cordova .CallbackContext ;
20
23
import org .apache .cordova .CordovaPlugin ;
@@ -149,7 +152,19 @@ public boolean execute(final String action, final JSONArray args, final Callback
149
152
unsubscribeResult .setKeepCallback (true );
150
153
callbackContext .sendPluginResult (unsubscribeResult );
151
154
return true ;
152
-
155
+ case "getLatestLocation" :
156
+ throwIfNotInitialized ();
157
+ callbackContext .success (createLocationResult (sdkInstance .getLatestLocation ()));
158
+ return true ;
159
+ case "getCurrentLocation" :
160
+ throwIfNotInitialized ();
161
+ sdkInstance .getCurrentLocation (new AsyncResultReceiver <Location , OutageReason >() {
162
+ @ Override
163
+ public void onResult (Result <Location , OutageReason > result ) {
164
+ callbackContext .success (createLocationResult (result ));
165
+ }
166
+ });
167
+ return true ;
153
168
default :
154
169
callbackContext .error ("Method not found" );
155
170
return false ;
@@ -233,6 +248,50 @@ private JSONArray serializeBlockers(Set<Blocker> blockers) {
233
248
}
234
249
return result ;
235
250
}
251
+
252
+ private JSONObject createLocationResult (Result <Location , OutageReason > locationResult ) {
253
+ if (locationResult .isSuccess ()) {
254
+ return createLocationSuccessResult (locationResult .getValue ());
255
+ } else {
256
+ return createOutageLocationResult (locationResult .getError ());
257
+ }
258
+ }
259
+
260
+ private JSONObject createLocationSuccessResult (Location location ) {
261
+ JSONObject serializedResult = new JSONObject ();
262
+ try {
263
+ serializedResult .put ("type" , "location" );
264
+ serializedResult .put (
265
+ "location" ,
266
+ getLocationJson (location )
267
+ );
268
+ } catch (JSONException e ) {
269
+ HTLogger .w (TAG , "Can't serialize Json" , e );
270
+ }
271
+ return serializedResult ;
272
+ }
273
+
274
+ private JSONObject createOutageLocationResult (OutageReason outage ) {
275
+ JSONObject serializedResult = new JSONObject ();
276
+ try {
277
+ serializedResult .put ("type" , "outage" );
278
+ serializedResult .put ("outage" , getOutageJson (outage ));
279
+ } catch (JSONException e ) {
280
+ HTLogger .w (TAG , "Can't serialize Json" , e );
281
+ }
282
+ return serializedResult ;
283
+ }
284
+
285
+ private JSONObject getOutageJson (OutageReason outage ) {
286
+ JSONObject json = new JSONObject ();
287
+ try {
288
+ json .put ("code" , outage .ordinal ());
289
+ json .put ("name" , outage .name ());
290
+ } catch (JSONException e ) {
291
+ HTLogger .w (TAG , "Can't serialize Json" , e );
292
+ }
293
+ return json ;
294
+ }
236
295
237
296
private JSONObject getLocationJson (GeotagResult result ) {
238
297
assert result instanceof GeotagResult .Success ;
@@ -252,6 +311,17 @@ private JSONObject getLocationJson(GeotagResult result) {
252
311
return json ;
253
312
}
254
313
314
+ private JSONObject getLocationJson (Location location ) {
315
+ JSONObject json = new JSONObject ();
316
+ try {
317
+ json .put ("latitude" , location .getLatitude ());
318
+ json .put ("longitude" , location .getLongitude ());
319
+ } catch (JSONException e ) {
320
+ HTLogger .w (TAG , "Can't serialize Json" , e );
321
+ }
322
+ return json ;
323
+ }
324
+
255
325
@ Override public void onError (TrackingError trackingError ) { sendUpdate (trackingError .message ); }
256
326
257
327
@ Override public void onTrackingStart () { sendUpdate ("start" ); }
0 commit comments