From 7548efe8e1144047094834765e96288784d519d6 Mon Sep 17 00:00:00 2001 From: sahil-roam Date: Thu, 4 Aug 2022 17:57:17 +0530 Subject: [PATCH 1/2] fixed listener, tracking, etc --- plugin.xml | 41 +++++++-- src/android/src/main/AndroidManifest.xml | 15 +++ .../main/java/com/roam/cordova}/CDVRoam.java | 92 ++++++++++++------- .../java/com/roam/cordova/RoamCDVService.java | 56 +++++++++++ .../java/com/roam/cordova}/roam-sdk.gradle | 2 +- www/roam.js | 26 ++++-- 6 files changed, 183 insertions(+), 49 deletions(-) create mode 100644 src/android/src/main/AndroidManifest.xml rename src/android/{ => src/main/java/com/roam/cordova}/CDVRoam.java (88%) create mode 100644 src/android/src/main/java/com/roam/cordova/RoamCDVService.java rename src/android/{ => src/main/java/com/roam/cordova}/roam-sdk.gradle (70%) diff --git a/plugin.xml b/plugin.xml index e998bb2..46ff786 100644 --- a/plugin.xml +++ b/plugin.xml @@ -8,13 +8,28 @@ - + + + - - - - - + + + + + + + + + + + + + + + @@ -23,6 +38,8 @@ + + @@ -32,10 +49,16 @@ - - + + + - + + + + + + diff --git a/src/android/src/main/AndroidManifest.xml b/src/android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..f9572c8 --- /dev/null +++ b/src/android/src/main/AndroidManifest.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/android/CDVRoam.java b/src/android/src/main/java/com/roam/cordova/CDVRoam.java similarity index 88% rename from src/android/CDVRoam.java rename to src/android/src/main/java/com/roam/cordova/CDVRoam.java index 371b62e..b0149d9 100644 --- a/src/android/CDVRoam.java +++ b/src/android/src/main/java/com/roam/cordova/CDVRoam.java @@ -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; @@ -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 { @@ -58,7 +49,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo return true; case "requestLocationPermission": - this.requestLocationPermission(callbackContext); + this.requestLocationPermission(); return true; case "checkLocationServices": @@ -66,7 +57,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo return true; case "requestLocationServices": - this.requestLocationServices(callbackContext); + this.requestLocationServices(); return true; case "checkBackgroundLocationPermission": @@ -74,7 +65,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo return true; case "requestBackgroundLocationPermission": - this.requestBackgroundLocationPermission(callbackContext); + this.requestBackgroundLocationPermission(); return true; case "getDeviceToken": @@ -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; @@ -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": @@ -232,10 +232,8 @@ 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) { @@ -243,9 +241,8 @@ private void checkLocationServices(CallbackContext callbackContext) { 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) { @@ -253,22 +250,34 @@ private void checkBackgroundLocationPermission(CallbackContext callbackContext) 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); @@ -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; } } @@ -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) { @@ -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) { @@ -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); diff --git a/src/android/src/main/java/com/roam/cordova/RoamCDVService.java b/src/android/src/main/java/com/roam/cordova/RoamCDVService.java new file mode 100644 index 0000000..8399610 --- /dev/null +++ b/src/android/src/main/java/com/roam/cordova/RoamCDVService.java @@ -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(); + } +} diff --git a/src/android/roam-sdk.gradle b/src/android/src/main/java/com/roam/cordova/roam-sdk.gradle similarity index 70% rename from src/android/roam-sdk.gradle rename to src/android/src/main/java/com/roam/cordova/roam-sdk.gradle index 0dd6584..1cfbac9 100644 --- a/src/android/roam-sdk.gradle +++ b/src/android/src/main/java/com/roam/cordova/roam-sdk.gradle @@ -5,5 +5,5 @@ repositories { } } dependencies { - implementation 'com.roam.sdk:roam-android:0.0.3' + implementation 'com.roam.sdk:roam-android:0.1.1' } \ No newline at end of file diff --git a/www/roam.js b/www/roam.js index 08985a5..e45e00d 100644 --- a/www/roam.js +++ b/www/roam.js @@ -14,24 +14,24 @@ roam.checkLocationPermission = function(callback) { exec( callback, null, 'roam', "checkLocationPermission",[]); }; -roam.requestLocationPermission = function(callback) { - exec( callback, null, 'roam', "requestLocationPermission",[]); +roam.requestLocationPermission = function() { + exec( null, null, 'roam', "requestLocationPermission",[]); }; roam.checkLocationServices = function(callback) { exec( callback, null, 'roam', "checkLocationServices",[]); }; -roam.requestLocationServices = function(callback) { - exec( callback, null, 'roam', "requestLocationServices",[]); +roam.requestLocationServices = function() { + exec( null, null, 'roam', "requestLocationServices",[]); }; roam.checkBackgroundLocationPermission = function(callback) { exec( callback, null, 'roam', "checkBackgroundLocationPermission",[]); }; -roam.requestBackgroundLocationPermission = function(callback) { - exec( callback, null, 'roam', "requestBackgroundLocationPermission",[]); +roam.requestBackgroundLocationPermission = function() { + exec( null, null, 'roam', "requestBackgroundLocationPermission",[]); }; roam.getDeviceToken = function(callback) { @@ -42,6 +42,12 @@ roam.createUser = function(description, success, error) { exec( success, error, 'roam', "createUser",[description]); }; +roam.setForegroundNotification = function(enabled, title, description, icon, activity){ + exec( null, null, 'roam', "setForegroundNotification",[enabled, title, description, icon, activity]); +} + + + roam.setDescription = function(description, success, error) { exec( success, error, 'roam', "setDescription",[description]); }; @@ -102,6 +108,14 @@ roam.startTracking = function(trackingMode) { exec( null, null, 'roam', "startTracking",[trackingMode]); }; +roam.startTrackingTimeInterval = function(timeInterval, desiredAccuracy){ + exec( null, null, 'roam', "startTrackingTimeInterval",[timeInterval, desiredAccuracy]); +} + +roam.startTrackingDistanceInterval = function(distance, stationary, desiredAccuracy){ + exec( null, null, 'roam', "startTrackingDistanceInterval",[distance, stationary, desiredAccuracy]); +} + roam.publishAndSave = function() { exec( null, null, 'roam', "publishAndSave",[]); }; From 1aa2cb7906f4d8368f26dc5ce5ef616429c4e5e4 Mon Sep 17 00:00:00 2001 From: sahil-roam Date: Thu, 4 Aug 2022 18:14:14 +0530 Subject: [PATCH 2/2] updated docs --- CHANGELOG.md | 10 +++++++++- README.md | 10 +++++++++- package.json | 2 +- plugin.xml | 2 +- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a0e409..0855448 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ ## 0.0.1 -* First version of new Roam Cordova SDK +- First version of new Roam Cordova SDK + +## 0.0.2 + +- Updated Android SDK v0.1.1 +- Added `setForegroundNotification` method +- Fixed time and distance based tracking methods +- Fixed location listener +- Fixed `requestLocationPermission` and `requestBackgroundLocationPermission` methods diff --git a/README.md b/README.md index 8e9bd4f..509bd57 100644 --- a/README.md +++ b/README.md @@ -271,7 +271,7 @@ Now that the location tracking is set up, you can subscribe to locations and eve To do that, you need to set the location and event listener to `true` using the below method. By default the status will set to `false` and needs to be set to `true` in order to stream the location and events updates to the same device or other devices. ```javascript -cordova.plugins.roam.toggleListener(true, true,, function(success){ +cordova.plugins.roam.toggleListener(true, true, function(success){ // do something on success }, function(error){ // do something on error @@ -303,6 +303,14 @@ cordova.plugins.roam.offEvents(); cordova.plugins.roam.offError(); ``` +## Set Foreground Notification + +To set foreground notification, use the below method. + +```javascript +cordova.plugins.roam.setForegroundNotification(boolean, "title", "description", "icon", "activity_path") +``` + ## Documentation Please visit our [Developer Center](https://github.com/roam-ai/roam-cordova/wiki) for instructions on other SDK methods. diff --git a/package.json b/package.json index 72b0efb..8f212f4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "roam-cordova", - "version": "0.0.1", + "version": "0.0.2", "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", diff --git a/plugin.xml b/plugin.xml index 46ff786..692059c 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ roam