Skip to content

Commit 22a2b08

Browse files
Implement getLatestLocation and getCurrentLocation SDK methods
1 parent 98f0336 commit 22a2b08

File tree

3 files changed

+124
-2
lines changed

3 files changed

+124
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.4.2",
2+
"version": "0.5.0",
33
"name": "cordova-plugin-hypertrack-v3",
44
"cordova_name": "Cordova HyperTrack Plugin",
55
"description": "Cordova Plugin for native HyperTrack SDKs",

src/android/HyperTrackPlugin.java

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
import com.google.gson.Gson;
99
import com.google.gson.annotations.SerializedName;
1010
import com.google.gson.reflect.TypeToken;
11+
import com.hypertrack.sdk.AsyncResultReceiver;
1112
import com.hypertrack.sdk.Blocker;
1213
import com.hypertrack.sdk.GeotagResult;
1314
import com.hypertrack.sdk.HyperTrack;
1415
import com.hypertrack.sdk.ServiceNotificationConfig;
1516
import com.hypertrack.sdk.TrackingError;
1617
import com.hypertrack.sdk.TrackingStateObserver;
1718
import com.hypertrack.sdk.logger.HTLogger;
19+
import com.hypertrack.sdk.Result;
20+
import com.hypertrack.sdk.OutageReason;
1821

1922
import org.apache.cordova.CallbackContext;
2023
import org.apache.cordova.CordovaPlugin;
@@ -149,7 +152,19 @@ public boolean execute(final String action, final JSONArray args, final Callback
149152
unsubscribeResult.setKeepCallback(true);
150153
callbackContext.sendPluginResult(unsubscribeResult);
151154
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;
153168
default:
154169
callbackContext.error("Method not found");
155170
return false;
@@ -233,6 +248,50 @@ private JSONArray serializeBlockers(Set<Blocker> blockers) {
233248
}
234249
return result;
235250
}
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+
}
236295

237296
private JSONObject getLocationJson(GeotagResult result) {
238297
assert result instanceof GeotagResult.Success;
@@ -252,6 +311,17 @@ private JSONObject getLocationJson(GeotagResult result) {
252311
return json;
253312
}
254313

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+
255325
@Override public void onError(TrackingError trackingError) { sendUpdate(trackingError.message); }
256326

257327
@Override public void onTrackingStart() { sendUpdate("start"); }

www/HyperTrackPlugin.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,58 @@ sdkHandle.stop = function(success, error) {
143143
exec(success, error, "HyperTrackPlugin", 'stop', []);
144144
}
145145

146+
/**
147+
* Get the latest device location that was sent by the SDK.
148+
* This method is available only for Android platform.
149+
*
150+
* Result object:
151+
* Either location
152+
* {
153+
* "type": "location",
154+
* "latitiude": number,
155+
* "longitude": number
156+
* }
157+
* Or outage:
158+
* {
159+
* "type": "outage",
160+
* "code": number,
161+
* "name": string
162+
* }
163+
*
164+
* @param {function(object)} success - callback that recieves the location JSON.
165+
* @param {function(error)} errror - error callback.
166+
*/
167+
sdkHandle.getLatestLocation = function(success, error) {
168+
console.log("HyperTrack:getLatestLocation");
169+
exec(success, error, "HyperTrackPlugin", 'getLatestLocation', []);
170+
}
171+
172+
/**
173+
* Get the current device location from system location provider.
174+
* This method is available only for Android platform.
175+
*
176+
* Result object:
177+
* Either location
178+
* {
179+
* "type": "location",
180+
* "latitiude": number,
181+
* "longitude": number
182+
* }
183+
* Or outage:
184+
* {
185+
* "type": "outage",
186+
* "code": number,
187+
* "name": string
188+
* }
189+
*
190+
* @param {function(object)} success - callback that recieves the location JSON.
191+
* @param {function(error)} errror - error callback.
192+
*/
193+
sdkHandle.getCurrentLocation = function(success, error) {
194+
console.log("HyperTrack:getCurrentLocation");
195+
exec(success, error, "HyperTrackPlugin", 'getCurrentLocation', []);
196+
}
197+
146198
/* ------------ */
147199
/* Internal API */
148200
/* ------------ */

0 commit comments

Comments
 (0)