Skip to content

Commit

Permalink
Implement getLatestLocation and getCurrentLocation SDK methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-kuznetsov-hypertrack authored Apr 4, 2022
1 parent 98f0336 commit 22a2b08
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.4.2",
"version": "0.5.0",
"name": "cordova-plugin-hypertrack-v3",
"cordova_name": "Cordova HyperTrack Plugin",
"description": "Cordova Plugin for native HyperTrack SDKs",
Expand Down
72 changes: 71 additions & 1 deletion src/android/HyperTrackPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.hypertrack.sdk.AsyncResultReceiver;
import com.hypertrack.sdk.Blocker;
import com.hypertrack.sdk.GeotagResult;
import com.hypertrack.sdk.HyperTrack;
import com.hypertrack.sdk.ServiceNotificationConfig;
import com.hypertrack.sdk.TrackingError;
import com.hypertrack.sdk.TrackingStateObserver;
import com.hypertrack.sdk.logger.HTLogger;
import com.hypertrack.sdk.Result;
import com.hypertrack.sdk.OutageReason;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
Expand Down Expand Up @@ -149,7 +152,19 @@ public boolean execute(final String action, final JSONArray args, final Callback
unsubscribeResult.setKeepCallback(true);
callbackContext.sendPluginResult(unsubscribeResult);
return true;

case "getLatestLocation":
throwIfNotInitialized();
callbackContext.success(createLocationResult(sdkInstance.getLatestLocation()));
return true;
case "getCurrentLocation":
throwIfNotInitialized();
sdkInstance.getCurrentLocation(new AsyncResultReceiver<Location, OutageReason>() {
@Override
public void onResult(Result<Location, OutageReason> result) {
callbackContext.success(createLocationResult(result));
}
});
return true;
default:
callbackContext.error("Method not found");
return false;
Expand Down Expand Up @@ -233,6 +248,50 @@ private JSONArray serializeBlockers(Set<Blocker> blockers) {
}
return result;
}

private JSONObject createLocationResult(Result<Location, OutageReason> locationResult) {
if(locationResult.isSuccess()) {
return createLocationSuccessResult(locationResult.getValue());
} else {
return createOutageLocationResult(locationResult.getError());
}
}

private JSONObject createLocationSuccessResult(Location location) {
JSONObject serializedResult = new JSONObject();
try {
serializedResult.put("type", "location");
serializedResult.put(
"location",
getLocationJson(location)
);
} catch (JSONException e) {
HTLogger.w(TAG, "Can't serialize Json", e);
}
return serializedResult;
}

private JSONObject createOutageLocationResult(OutageReason outage) {
JSONObject serializedResult = new JSONObject();
try {
serializedResult.put("type", "outage");
serializedResult.put("outage", getOutageJson(outage));
} catch (JSONException e) {
HTLogger.w(TAG, "Can't serialize Json", e);
}
return serializedResult;
}

private JSONObject getOutageJson(OutageReason outage) {
JSONObject json = new JSONObject();
try {
json.put("code", outage.ordinal());
json.put("name", outage.name());
} catch (JSONException e) {
HTLogger.w(TAG, "Can't serialize Json", e);
}
return json;
}

private JSONObject getLocationJson(GeotagResult result) {
assert result instanceof GeotagResult.Success;
Expand All @@ -252,6 +311,17 @@ private JSONObject getLocationJson(GeotagResult result) {
return json;
}

private JSONObject getLocationJson(Location location) {
JSONObject json = new JSONObject();
try {
json.put("latitude", location.getLatitude());
json.put("longitude", location.getLongitude());
} catch (JSONException e) {
HTLogger.w(TAG, "Can't serialize Json", e);
}
return json;
}

@Override public void onError(TrackingError trackingError) { sendUpdate(trackingError.message); }

@Override public void onTrackingStart() { sendUpdate("start"); }
Expand Down
52 changes: 52 additions & 0 deletions www/HyperTrackPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,58 @@ sdkHandle.stop = function(success, error) {
exec(success, error, "HyperTrackPlugin", 'stop', []);
}

/**
* Get the latest device location that was sent by the SDK.
* This method is available only for Android platform.
*
* Result object:
* Either location
* {
* "type": "location",
* "latitiude": number,
* "longitude": number
* }
* Or outage:
* {
* "type": "outage",
* "code": number,
* "name": string
* }
*
* @param {function(object)} success - callback that recieves the location JSON.
* @param {function(error)} errror - error callback.
*/
sdkHandle.getLatestLocation = function(success, error) {
console.log("HyperTrack:getLatestLocation");
exec(success, error, "HyperTrackPlugin", 'getLatestLocation', []);
}

/**
* Get the current device location from system location provider.
* This method is available only for Android platform.
*
* Result object:
* Either location
* {
* "type": "location",
* "latitiude": number,
* "longitude": number
* }
* Or outage:
* {
* "type": "outage",
* "code": number,
* "name": string
* }
*
* @param {function(object)} success - callback that recieves the location JSON.
* @param {function(error)} errror - error callback.
*/
sdkHandle.getCurrentLocation = function(success, error) {
console.log("HyperTrack:getCurrentLocation");
exec(success, error, "HyperTrackPlugin", 'getCurrentLocation', []);
}

/* ------------ */
/* Internal API */
/* ------------ */
Expand Down

0 comments on commit 22a2b08

Please sign in to comment.