Skip to content

Commit

Permalink
Fix missing Gson dependency issue (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akr-Pandey authored Aug 1, 2022
1 parent 4f0870e commit e4919d7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.6.3] - 2022-07-29
### Changed
- Fix issue with missing com.google.gson dependency

## [0.6.2] - 2022-07-08
### Changed
- Updated Hypertrack iOS SDK to [4.12.3](https://github.com/hypertrack/sdk-ios/blob/master/CHANGELOG.md#4123---2022-06-13)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.6.2",
"version": "0.6.3",
"name": "cordova-plugin-hypertrack-v3",
"cordova_name": "Cordova HyperTrack Plugin",
"description": "Cordova Plugin for native HyperTrack SDKs",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-hypertrack-v3"
version="0.6.2">
version="0.6.3">
<name>HyperTrackPlugin</name>
<description>Cordova HyperTrack Plugin</description>
<license>MIT</license>
Expand Down
86 changes: 62 additions & 24 deletions src/android/HyperTrackPlugin.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.hypertrack.sdk.cordova.plugin;

import android.location.Location;
import android.location.LocationManager;
import android.util.Log;

import androidx.annotation.NonNull;

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;
Expand All @@ -26,6 +24,10 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand All @@ -35,8 +37,6 @@ public class HyperTrackPlugin extends CordovaPlugin implements TrackingStateObse

private HyperTrack sdkInstance;
private CallbackContext statusUpdateCallback;

private final Gson mGson = new Gson();

@Override
public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) {
Expand Down Expand Up @@ -88,7 +88,8 @@ public boolean execute(final String action, final JSONArray args, final Callback
case "setDeviceMetadata":
throwIfNotInitialized();
String deviceMetaJson = args.getString(0);
Map<String, Object> meta = mGson.fromJson(deviceMetaJson, new TypeToken<Map<String, Object>>() {}.getType());
JSONObject deviceMetaJsonObject = new JSONObject(deviceMetaJson);
Map<String, Object> meta = jsonToMap(deviceMetaJsonObject);
sdkInstance.setDeviceMetadata(meta);
callbackContext.success();
return true;
Expand All @@ -108,7 +109,8 @@ public boolean execute(final String action, final JSONArray args, final Callback
throwIfNotInitialized();
String tagMetaJson = args.getString(0);
Location expectedLocation = getExpectedLocation(args);
Map<String, Object> payload = mGson.fromJson(tagMetaJson, new TypeToken<Map<String, Object>>() {}.getType());
JSONObject tagMetaJsonObject = new JSONObject(tagMetaJson);
Map<String, Object> payload = jsonToMap(tagMetaJsonObject);
GeotagResult result = sdkInstance.addGeotag(payload, expectedLocation);
if (result instanceof GeotagResult.Success) {
HTLogger.d(TAG, "Geotag created successfully " + result);
Expand Down Expand Up @@ -210,12 +212,17 @@ private void createTrackingStateChannel(CallbackContext callbackContext) {
sdkInstance.addTrackingListener(this);
}

private Location getExpectedLocation(JSONArray args) {
private Location getExpectedLocation(JSONArray args) throws JSONException {
if (args.length() < 2) return null;
Log.i(TAG, "expected location argument " + args.optString(1));
SerializedLocation serializedLocation = mGson.fromJson(args.optString(1), SerializedLocation.class);
Log.i(TAG, "Serializedlocation " + serializedLocation);
return serializedLocation.asLocation();
String coordinates = args.optString(1);
JSONObject coordinate = new JSONObject(coordinates);
Double latitude = coordinate.getDouble("latitude");
Double longitude = coordinate.getDouble("longitude");
Location expectedLocation = new Location(LocationManager.GPS_PROVIDER);
expectedLocation.setLatitude(latitude);
expectedLocation.setLongitude(longitude);
return expectedLocation;
}

private void throwIfNotInitialized() throws IllegalStateException {
Expand Down Expand Up @@ -322,24 +329,55 @@ private JSONObject getLocationJson(Location location) {
return json;
}

@Override public void onError(TrackingError trackingError) { sendUpdate(trackingError.message); }
private Map<String, Object> jsonToMap(JSONObject json) throws JSONException {
Map<String, Object> retMap = new HashMap<String, Object>();

@Override public void onTrackingStart() { sendUpdate("start"); }
if (json != JSONObject.NULL) {
retMap = toMap(json);
}
return retMap;
}

@Override public void onTrackingStop() { sendUpdate("stop"); }
private Map<String, Object> toMap(JSONObject object) throws JSONException {
Map<String, Object> map = new HashMap<String, Object>();

static class SerializedLocation{
@SerializedName("latitude") public double latitude = Double.NaN;
@SerializedName("longitude") public double lognitude = Double.NaN;
@SerializedName("deviation") public Integer deviation = null;
@SerializedName("isRestricted") public Boolean isRestricted = false;
Iterator<String> keysItr = object.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Object value = object.get(key);

Location asLocation() {
Location result = new Location("any");
result.setLatitude(latitude);
result.setLongitude(lognitude);
return result;
if (value instanceof JSONArray) {
value = toList((JSONArray) value);
}

else if (value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
map.put(key, value);
}
return map;
}

private List<Object> toList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<Object>();
for(int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if (value instanceof JSONArray) {
value = toList((JSONArray) value);
}

else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
list.add(value);
}
return list;
}

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

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

@Override public void onTrackingStop() { sendUpdate("stop"); }

}

0 comments on commit e4919d7

Please sign in to comment.