Skip to content

Commit

Permalink
fixed location receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
sahil-roam committed Dec 24, 2022
1 parent ff33e93 commit a9dcea1
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 40 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@
## 0.0.4

- Fixed location object in the location receiver.

## 0.0.5

- Updated Android SDK v0.1.7
- Fixed location receiver output
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "roam-cordova",
"version": "0.0.4",
"version": "0.0.5",
"description": "This plugin allows to use the Roam.ai SDK in your React Native mobile application on iOS and Android.",
"homepage": "https://roam.ai",
"license": "MIT",
Expand Down
3 changes: 3 additions & 0 deletions src/android/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/android/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/android/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/android/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/android/android.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
83 changes: 45 additions & 38 deletions src/android/src/main/java/com/roam/cordova/CDVRoam.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Intent;
import android.location.Location;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.GsonBuilder;
Expand All @@ -29,6 +30,8 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class CDVRoam extends CordovaPlugin {
private static CallbackContext locationCallbackContext;
private static CallbackContext eventsCallbackContext;
Expand Down Expand Up @@ -260,7 +263,6 @@ private void getDeviceToken(CallbackContext callbackContext) {
}

private void setForegroundNotification(boolean enabled, String title, String description, String image, String activity){
Log.e("TAG", "setForegroundNotification: " + enabled + title + description + image + activity);
try{
String[] split = image.split("/");
String firstSubString = split[0];
Expand All @@ -271,7 +273,7 @@ private void setForegroundNotification(boolean enabled, String title, String des
firstSubString,
cordova.getActivity().getPackageName()
);
Roam.setForegroundNotification(enabled, title, description, resId, activity);
Roam.setForegroundNotification(enabled, title, description, resId, activity, "com.roam.cordova.RoamCDVService");
} catch (Exception e){
e.printStackTrace();
}
Expand All @@ -294,7 +296,7 @@ public void onFailure(RoamError roamError) {
}

private void setDescription(String description) {
Roam.setDescription(description);
Roam.setDescription(description, null);
}

private void getUser(String userId, final CallbackContext callbackContext) {
Expand Down Expand Up @@ -423,7 +425,7 @@ public void getCurrentLocation(int accuracy, String desired_Accuracy, final Call
}
Roam.getCurrentLocation(desiredAccuracy, accuracy, new RoamLocationCallback() {
@Override
public void location(Location location) {
public void location(Location location, float direction) {
String serializedLocation = new GsonBuilder().create().toJson(location);
callbackContext.success(serializedLocation);
}
Expand Down Expand Up @@ -468,28 +470,16 @@ private void startTracking(String trackingMode){
switch (trackingMode) {
case "ACTIVE":
Roam.startTracking(RoamTrackingMode.ACTIVE);
startReceiverService();
break;
case "BALANCED":
Roam.startTracking(RoamTrackingMode.BALANCED);
startReceiverService();
break;
case "PASSIVE":
Roam.startTracking(RoamTrackingMode.PASSIVE);
startReceiverService();
break;
}
}

private void startReceiverService(){
Activity activity = cordova.getActivity();
activity.startService(new Intent(activity, RoamCDVService.class));
}

private void stopReceiverService(){
Activity activity = cordova.getActivity();
activity.stopService(new Intent(activity, RoamCDVService.class));
}

private void startTrackingTimeInterval(int timeInterval, String desiredAccuracy) {
RoamTrackingMode.Builder builder = new RoamTrackingMode.Builder(timeInterval);
Expand All @@ -505,7 +495,6 @@ private void startTrackingTimeInterval(int timeInterval, String desiredAccuracy)
break;
}
Roam.startTracking(builder.build());
startReceiverService();
}

private void startTrackingDistanceInterval(int distance, int stationary, String desiredAccuracy) {
Expand All @@ -522,12 +511,10 @@ private void startTrackingDistanceInterval(int distance, int stationary, String
break;
}
Roam.startTracking(builder.build());
startReceiverService();
}

private void stopTracking() {
Roam.stopTracking();
stopReceiverService();
}


Expand Down Expand Up @@ -598,29 +585,14 @@ public static void safeRemoveCallback(){

public static class RoamCordovaReceiver extends RoamReceiver{
@Override
public void onLocationUpdated(Context context, RoamLocation roamLocation) {
public void onLocationUpdated(Context context, List<RoamLocation> roamLocation) {
super.onLocationUpdated(context, roamLocation);
JSONObject jsonObject = new JSONObject();
String serializedLocation = "";
try {
jsonObject.put("userId", roamLocation.getUserId());
jsonObject.put("activity", roamLocation.getActivity());
jsonObject.put("recordedAt", roamLocation.getRecordedAt());
jsonObject.put("timezoneOffset", roamLocation.getTimezoneOffset());
jsonObject.put("metadata", (roamLocation.getMetadata() != null) ? roamLocation.getMetadata().toString() : "");
jsonObject.put("batteryStatus", roamLocation.getBatteryStatus());
jsonObject.put("networkStatus", roamLocation.getNetworkStatus());
jsonObject.put("provider", roamLocation.getLocation().getProvider());
jsonObject.put("time", roamLocation.getLocation().getTime());
jsonObject.put("latitude", roamLocation.getLocation().getLatitude());
jsonObject.put("longitude", roamLocation.getLocation().getLongitude());
jsonObject.put("altitude", roamLocation.getLocation().getAltitude());
jsonObject.put("speed", roamLocation.getLocation().getSpeed());
jsonObject.put("bearing", roamLocation.getLocation().getBearing());
jsonObject.put("accuracy", roamLocation.getLocation().getAccuracy());
}catch (Exception e){
serializedLocation = mapForLocationList(roamLocation).toString();
} catch (JSONException e) {
e.printStackTrace();
}
String serializedLocation = jsonObject.toString();
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, serializedLocation);
pluginResult.setKeepCallback(true);
if (CDVRoam.locationCallbackContext != null) {
Expand Down Expand Up @@ -652,5 +624,40 @@ public void onError(Context context, RoamError roamError) {
String serializedLocation = new GsonBuilder().create().toJson(roamError);
errorCallbackContext.success(serializedLocation);
}

private JSONArray mapForLocationList(List<RoamLocation> locationList) throws JSONException {
JSONArray array = new JSONArray();
for (RoamLocation roamLocation: locationList){
JSONObject map = new JSONObject();
map.put("userId", (roamLocation.getUserId() != null) ? roamLocation.getUserId() : "");
map.put("location", mapForLocation(roamLocation.getLocation()));
if (TextUtils.isEmpty(roamLocation.getActivity())) {
map.put("activity", " ");
} else {
map.put("activity", roamLocation.getActivity());
}
map.put("recordedAt", roamLocation.getRecordedAt());
map.put("timezone", roamLocation.getTimezoneOffset());
array.put(map);
}
return array;
}

private JSONObject mapForLocation(Location location){
JSONObject map = new JSONObject();
if (location == null) {
return map;
}
try {
map.put("latitude", location.getLatitude());
map.put("longitude", location.getLongitude());
map.put("accuracy", location.getAccuracy());
map.put("altitude", location.getAltitude());
map.put("speed", location.getSpeed());
} catch (JSONException e) {
}
return map;
}

}
}
2 changes: 1 addition & 1 deletion src/android/src/main/java/com/roam/cordova/roam-sdk.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ repositories {
}
}
dependencies {
implementation 'com.roam.sdk:roam-android:0.1.1'
implementation 'com.roam.sdk:roam-android:0.1.7'
}

0 comments on commit a9dcea1

Please sign in to comment.