Skip to content

Commit

Permalink
fixed listener, tracking, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
sahil-roam committed Aug 4, 2022
1 parent 0acd415 commit 7548efe
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 49 deletions.
41 changes: 32 additions & 9 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@
<js-module name="roam" src="www/roam.js">
<clobbers target="cordova.plugins.roam" />
</js-module>
<!-- Android Configuration -->


<!-- Android Configuration -->
<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="roam">
<param name="android-package" value="com.roam.cordova.CDVRoam" />
</feature>
</config-file>
<config-file target="res/xml/config.xml" parent="/*">
<feature name="roam">
<param name="android-package" value="com.roam.cordova.CDVRoam"/>
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<service android:name="com.roam.cordova.RoamCDVService" android:foregroundServiceType="location"/>
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<receiver
android:name="com.roam.cordova.CDVRoam$RoamCordovaReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.roam.android.RECEIVED" />
</intent-filter>
</receiver>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml">
<!--Network-->
<uses-permission android:name="android.permission.INTERNET" />
Expand All @@ -23,6 +38,8 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<!-- Service -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<!--Restart-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!--Battery-->
Expand All @@ -32,10 +49,16 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.location.gps" />
</config-file>
<source-file src="src/android/CDVRoam.java" target-dir="src/com/roam/cordova/" />
<framework custom="true" src="src/android/roam-sdk.gradle" type="gradleReference" />
<source-file src="src/android/src/main/java/com/roam/cordova/CDVRoam.java" target-dir="src/com/roam/cordova/" />
<source-file src="src/android/src/main/java/com/roam/cordova/RoamCDVService.java" target-dir="src/com/roam/cordova"/>
<framework custom="true" src="src/android/src/main/java/com/roam/cordova/roam-sdk.gradle" type="gradleReference" />
</platform>
</plugin>




<!-- Android Configuration -->
</plugin>



Expand Down
15 changes: 15 additions & 0 deletions src/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.roam.cordova">

<application>
<receiver
android:name="com.roam.cordova.CDVRoam$RoamCordovaReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.roam.android.RECEIVED" />
</intent-filter>
</receiver>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.roam.cordova;

import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.NonNull;
import android.util.Log;

import com.google.gson.GsonBuilder;
import com.roam.sdk.Roam;
Expand All @@ -26,21 +22,16 @@
import com.roam.sdk.service.RoamReceiver;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class CDVRoam extends CordovaPlugin {
private static CallbackContext locationCallbackContext;
private static CallbackContext eventsCallbackContext;
private static CallbackContext errorCallbackContext;
private static CallbackContext permissionCallbackContext;
private Activity context;

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
Expand All @@ -58,23 +49,23 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
return true;

case "requestLocationPermission":
this.requestLocationPermission(callbackContext);
this.requestLocationPermission();
return true;

case "checkLocationServices":
this.checkLocationServices(callbackContext);
return true;

case "requestLocationServices":
this.requestLocationServices(callbackContext);
this.requestLocationServices();
return true;

case "checkBackgroundLocationPermission":
this.checkBackgroundLocationPermission(callbackContext);
return true;

case "requestBackgroundLocationPermission":
this.requestBackgroundLocationPermission(callbackContext);
this.requestBackgroundLocationPermission();
return true;

case "getDeviceToken":
Expand Down Expand Up @@ -123,6 +114,15 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
this.toggleEvents(geofenceEvents, tripEvents, locationEvents, movingGeofenceEvents, callbackContext);
return true;

case "setForegroundNotification":
boolean enabled = args.getBoolean(0);
String notificationTitle = args.getString(1);
String notificationDescription = args.getString(2);
String notificationIcon = args.getString(3);
String notificationActivity = args.getString(4);
this.setForegroundNotification(enabled, notificationTitle, notificationDescription, notificationIcon, notificationActivity);
return true;

case "getEventsStatus":
this.getEventsStatus(callbackContext);
return true;
Expand Down Expand Up @@ -161,7 +161,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
case "startTrackingDistanceInterval":
int distance = args.getInt(0);
int stationary = args.getInt(1);
String dDesiredAccuracy = args.getString(3);
String dDesiredAccuracy = args.getString(2);
this.startTrackingDistanceInterval(distance, stationary, dDesiredAccuracy);

case "publishAndSave":
Expand Down Expand Up @@ -232,43 +232,52 @@ private void checkLocationPermission(CallbackContext callbackContext) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, status));
}

private void requestLocationPermission(CallbackContext callbackContext) {
permissionCallbackContext = callbackContext;
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};
cordova.requestPermissions(this, Roam.REQUEST_CODE_LOCATION_PERMISSION, permissions);
private void requestLocationPermission() {
Roam.requestLocationPermission(cordova.getActivity());
}

private void checkLocationServices(CallbackContext callbackContext) {
String status = enabledStatus(Roam.checkLocationServices());
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, status));
}

private void requestLocationServices(CallbackContext callbackContext) {
permissionCallbackContext = callbackContext;
cordova.startActivityForResult(this, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), Roam.REQUEST_CODE_LOCATION_ENABLED);
private void requestLocationServices() {
Roam.requestLocationServices(cordova.getActivity());
}

private void checkBackgroundLocationPermission(CallbackContext callbackContext) {
String status = permissionsStatus(Roam.checkBackgroundLocationPermission());
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, status));
}

private void requestBackgroundLocationPermission(CallbackContext callbackContext) {
permissionCallbackContext = callbackContext;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
String[] permissions = {Manifest.permission.ACCESS_BACKGROUND_LOCATION};
cordova.requestPermissions(this, Roam.REQUEST_CODE_BACKGROUND_LOCATION_PERMISSION, permissions);
} else {
permissionCallbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, "UNKNOWN"));
}
private void requestBackgroundLocationPermission() {
Roam.requestBackgroundLocationPermission(cordova.getActivity());
}

private void getDeviceToken(CallbackContext callbackContext) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, Roam.getDeviceToken()));
}

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];
String secondSubString = split[1];
int resId = cordova.getActivity().getResources()
.getIdentifier(
secondSubString,
firstSubString,
cordova.getActivity().getPackageName()
);
Roam.setForegroundNotification(enabled, title, description, resId, activity);
} catch (Exception e){
e.printStackTrace();
}
}

private void createUser(String description, final CallbackContext callbackContext) {
Roam.createUser(description, new RoamCallback() {
Roam.createUser(description,null, new RoamCallback() {
@Override
public void onSuccess(RoamUser roamUser) {
String serializedUser = new GsonBuilder().create().toJson(roamUser);
Expand Down Expand Up @@ -387,13 +396,13 @@ public void onFailure(RoamError roamError) {
private void updateCurrentLocation(int accuracy, String desiredAccuracy) {
switch (desiredAccuracy) {
case "MEDIUM":
Roam.updateCurrentLocation(RoamTrackingMode.DesiredAccuracy.MEDIUM, accuracy);
Roam.updateCurrentLocation(RoamTrackingMode.DesiredAccuracy.MEDIUM, accuracy, null);
break;
case "LOW":
Roam.updateCurrentLocation(RoamTrackingMode.DesiredAccuracy.LOW, accuracy);
Roam.updateCurrentLocation(RoamTrackingMode.DesiredAccuracy.LOW, accuracy, null);
break;
default:
Roam.updateCurrentLocation(RoamTrackingMode.DesiredAccuracy.HIGH, accuracy);
Roam.updateCurrentLocation(RoamTrackingMode.DesiredAccuracy.HIGH, accuracy, null);
break;
}
}
Expand Down Expand Up @@ -458,16 +467,29 @@ 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);
switch (desiredAccuracy) {
Expand All @@ -482,6 +504,7 @@ private void startTrackingTimeInterval(int timeInterval, String desiredAccuracy)
break;
}
Roam.startTracking(builder.build());
startReceiverService();
}

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

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


public void publishAndSave() {
RoamPublish roamPublish = new RoamPublish.Builder().build();
Roam.publishAndSave(roamPublish);
Expand Down
56 changes: 56 additions & 0 deletions src/android/src/main/java/com/roam/cordova/RoamCDVService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.roam.cordova;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class RoamCDVService extends Service {

private CDVRoam.RoamCordovaReceiver roamCordovaReceiver;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
register();
}

private void register() {
roamCordovaReceiver = new CDVRoam.RoamCordovaReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.roam.android.RECEIVED");
registerReceiver(roamCordovaReceiver, intentFilter);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}

private void unRegister() {
if (roamCordovaReceiver != null) {
unregisterReceiver(roamCordovaReceiver);
}
}

@Override
public void onDestroy() {
unRegister();
super.onDestroy();
}
}
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.0.3'
implementation 'com.roam.sdk:roam-android:0.1.1'
}
Loading

0 comments on commit 7548efe

Please sign in to comment.