diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 14d193b..03d9c98 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -1,18 +1,6 @@ - - - - - - - - - - - - diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e2cae98..1cdfcf6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,6 +9,7 @@ + + + + + + = Build.VERSION_CODES.O) { + context.startForegroundService(intent); + }else { + context.startService(intent); + } } public static void removeGeofence(Context context) { diff --git a/app/src/main/java/com/app/simplitend/apputils/BootCompleteReceiver.java b/app/src/main/java/com/app/simplitend/apputils/BootCompleteReceiver.java new file mode 100644 index 0000000..f253648 --- /dev/null +++ b/app/src/main/java/com/app/simplitend/apputils/BootCompleteReceiver.java @@ -0,0 +1,106 @@ +package com.app.simplitend.apputils; + +import static com.app.simplitend.locationupdates.LocationService.LOCATION_INTERVAL_BASE_TIME; +import static com.app.simplitend.locationupdates.LocationService.LOCATION_UPDATE_MIN_INTERVAL; +import static com.app.simplitend.patientgeofencing.GeoFenceHelper.GEOFENCE_ID; +import static com.app.simplitend.patientgeofencing.GeoFenceHelper.GEOFENCE_TAG; + +import android.Manifest; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Build; +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.annotation.RequiresPermission; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; + +import com.app.simplitend.locationupdates.LocationService; +import com.app.simplitend.patientgeofencing.GeoFenceHelper; +import com.google.android.gms.location.Geofence; +import com.google.android.gms.location.GeofencingClient; +import com.google.android.gms.location.GeofencingRequest; +import com.google.android.gms.location.LocationServices; +import com.google.android.gms.maps.model.LatLng; + +public class BootCompleteReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + + if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { + // starting over the Location updates service again + Intent locationUpdateIntent = new Intent(context, LocationService.class); + locationUpdateIntent.setAction(LocationService.ACTION_START_LOCATION_UPDATES); + locationUpdateIntent.putExtra(LOCATION_UPDATE_MIN_INTERVAL, LOCATION_INTERVAL_BASE_TIME); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(locationUpdateIntent); + } else { + context.startService(locationUpdateIntent); + } + + // setting up geofence if available + String radius_str = AppUtil.getPatientGeofenceRadius(context); + if (radius_str != null) { + float radius_f; + try { + radius_f = Float.parseFloat(radius_str); + } catch (NumberFormatException e) { + radius_f = 0; + } + + if (radius_f != 0) { + String[] latLngStr = AppUtil.getPatientLatLng(context); + if (latLngStr[0] != null && latLngStr[1] != null) { + LatLng latLng; + try { + latLng = new LatLng(Double.parseDouble(latLngStr[0]), + Double.parseDouble(latLngStr[1])); + } catch (Exception e) { + latLng = null; + } + + if (latLng != null) { + if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + return; + } + addGeoFence(latLng, radius_f, context); + } + } + } + } + } + } + + @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION) + private void addGeoFence(@NonNull LatLng latLng, float GEOFENCING_RADIUS , Context context) { + + GeoFenceHelper geoFenceHelper = new GeoFenceHelper(context); + GeofencingClient geofencingClient = LocationServices.getGeofencingClient(context); + + // checking for background location updates for API level 29 and above + if (Build.VERSION.SDK_INT >= 29) { + if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) { + Log.d(GEOFENCE_TAG, "setGeofence: NO BACKGROUND PERMISSION" ); + return; + } + } + + Geofence geofence = geoFenceHelper.getGeoFence(GEOFENCE_ID, latLng, GEOFENCING_RADIUS, + Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT); + GeofencingRequest geofencingRequest = geoFenceHelper.getGeoFencingRequest(geofence); + PendingIntent pendingIntent = geoFenceHelper.getPendingIntent(); + + geofencingClient.addGeofences(geofencingRequest, pendingIntent) + .addOnSuccessListener(aVoid -> { + Log.d(GEOFENCE_TAG, "Geofence added successfully. " + latLng + " Radius: " + GEOFENCING_RADIUS + " meters"); + }) + .addOnFailureListener(e -> { + Log.d(GEOFENCE_TAG, "onFailure: Geofence couldn't be added: " + e.getLocalizedMessage() + " " + latLng + " Radius: " + GEOFENCING_RADIUS); + }); + + } +} diff --git a/app/src/main/java/com/app/simplitend/apputils/NotificationService.java b/app/src/main/java/com/app/simplitend/apputils/NotificationService.java index a9bb4b8..bb17428 100644 --- a/app/src/main/java/com/app/simplitend/apputils/NotificationService.java +++ b/app/src/main/java/com/app/simplitend/apputils/NotificationService.java @@ -26,15 +26,14 @@ import com.google.android.gms.location.GeofencingClient; import com.google.android.gms.location.GeofencingRequest; import com.google.android.gms.location.LocationServices; import com.google.android.gms.maps.model.LatLng; +import com.onesignal.OneSignal; import com.onesignal.notifications.INotificationReceivedEvent; import com.onesignal.notifications.INotificationServiceExtension; import org.json.JSONException; import org.json.JSONObject; -import java.lang.reflect.Array; import java.util.Arrays; -import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -49,24 +48,26 @@ public class NotificationService implements INotificationServiceExtension { @Override public void onNotificationReceived(@NonNull INotificationReceivedEvent iNotificationReceivedEvent) { +// Log.d("aditya testing", "onNotificationReceived: " + iNotificationReceivedEvent.getNotification()); + // showing maximum 7 notifications due to the limitations enforced by manufacturers // Thus, removing the oldest notification to show this new notification NotificationManager notificationManager = (NotificationManager) iNotificationReceivedEvent.getContext().getSystemService(NOTIFICATION_SERVICE); try { int active_notifications_count = notificationManager.getActiveNotifications().length; - if (active_notifications_count == 8) // already number is incremented when notification is arrived + if (active_notifications_count >= 7) // already number is incremented when notification is arrived { List active_notifications = Arrays.asList(notificationManager.getActiveNotifications()); active_notifications.sort(Comparator.comparingInt(n -> (int) n.getPostTime())); // removing last item StatusBarNotification notification = active_notifications.get(active_notifications.size() - 1); - notificationManager.cancel(notification.getId()); + OneSignal.getNotifications().removeNotification(notification.getId()); } } catch (Exception e) { // clearing all notifications - notificationManager.cancelAll(); + OneSignal.getNotifications().clearAllNotifications(); } JSONObject extras = iNotificationReceivedEvent.getNotification().getAdditionalData(); diff --git a/app/src/main/java/com/app/simplitend/locationupdates/LocationService.java b/app/src/main/java/com/app/simplitend/locationupdates/LocationService.java index e753d0c..551313e 100644 --- a/app/src/main/java/com/app/simplitend/locationupdates/LocationService.java +++ b/app/src/main/java/com/app/simplitend/locationupdates/LocationService.java @@ -58,7 +58,7 @@ public class LocationService extends Service implements LocationClient.DefaultLo break; } } - return super.onStartCommand(intent, flags, startId); + return START_REDELIVER_INTENT; } private void stopLocationUpdates() { diff --git a/app/src/main/java/com/app/simplitend/patient_dashboard/PatientMainViewModel.java b/app/src/main/java/com/app/simplitend/patient_dashboard/PatientMainViewModel.java index c64c5bf..e18ba28 100644 --- a/app/src/main/java/com/app/simplitend/patient_dashboard/PatientMainViewModel.java +++ b/app/src/main/java/com/app/simplitend/patient_dashboard/PatientMainViewModel.java @@ -112,7 +112,11 @@ public class PatientMainViewModel extends ViewModel { Intent intent = new Intent(activity, LocationService.class); intent.setAction(LocationService.ACTION_START_LOCATION_UPDATES); intent.putExtra(LOCATION_UPDATE_MIN_INTERVAL, LOCATION_INTERVAL_BASE_TIME); - activity.startService(intent); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + activity.startForegroundService(intent); + }else { + activity.startService(intent); + } } } diff --git a/app/src/main/java/com/app/simplitend/patientgeofencing/GeoFenceBroadcastReceiver.java b/app/src/main/java/com/app/simplitend/patientgeofencing/GeoFenceBroadcastReceiver.java index 896b77b..d7f27b3 100644 --- a/app/src/main/java/com/app/simplitend/patientgeofencing/GeoFenceBroadcastReceiver.java +++ b/app/src/main/java/com/app/simplitend/patientgeofencing/GeoFenceBroadcastReceiver.java @@ -8,7 +8,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.location.Location; -import android.os.AsyncTask; +import android.os.Build; import android.util.Log; import androidx.annotation.NonNull; @@ -16,8 +16,8 @@ import androidx.annotation.NonNull; import com.app.simplitend.apputils.AppUtil; import com.app.simplitend.apputils.RetrofitHelper; import com.app.simplitend.caregiverdashboard.mvvm.NotificationApiService; -import com.app.simplitend.locationupdates.LocationService; import com.app.simplitend.chats.SocketHelper; +import com.app.simplitend.locationupdates.LocationService; import com.app.simplitend.welcome.welcomepatient.mvvm.models.CallResponse; import com.google.android.gms.location.Geofence; import com.google.android.gms.location.GeofencingEvent; @@ -78,7 +78,11 @@ public class GeoFenceBroadcastReceiver extends BroadcastReceiver { Intent locationServiceIntent = new Intent(context, LocationService.class); locationServiceIntent.setAction(LocationService.ACTION_START_LOCATION_UPDATES); locationServiceIntent.putExtra(LOCATION_UPDATE_MIN_INTERVAL, LOCATION_INTERVAL_BASE_TIME); - context.startService(locationServiceIntent); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(intent); + }else { + context.startService(intent); + } break; case Geofence.GEOFENCE_TRANSITION_EXIT: Log.d(GEOFENCE_TAG, "onReceive: EXIT"); @@ -105,49 +109,32 @@ public class GeoFenceBroadcastReceiver extends BroadcastReceiver { Log.d(GEOFENCE_TAG, "notifyPatient: " + chatsCred[2]); // sending message - new SendMessageTask(message, chatsCred[0], chatsCred[1], chatsCred[2], chatsCred[3]).execute(); + sendMessage(message, chatsCred[0], chatsCred[1], chatsCred[2], chatsCred[3]); } - public static class SendMessageTask extends AsyncTask { - @NonNull - private final String message; - private final int patientId, cg_id, channel_id, patient_principal_id; + private void sendMessage(@NonNull String message, int patientId, int cg_id, int channel_id, int patient_principal_id) { + SocketHelper socketHelper = SocketHelper.getInstance(); - public SendMessageTask(@NonNull String message, int patientId, int cg_id, int channel_id, int patient_principal_id) { - this.patientId = patientId; - this.cg_id = cg_id; - this.channel_id = channel_id; - this.message = message; - this.patient_principal_id = patient_principal_id; - } + Log.d(GEOFENCE_TAG, "doInBackground: CONNECTING TO SOCKET"); - @Override - protected Void doInBackground(Void... voids) { - SocketHelper socketHelper = SocketHelper.getInstance(); + socketHelper.establishConnection(new SocketHelper.SockCallBack() { + @Override + public void onSocketConnected() { + Log.d(GEOFENCE_TAG, "doInBackground: SOCKET CONNECTED"); + socketHelper.sendMessage(message, cg_id + "", patientId + "", channel_id + "", + patient_principal_id < 0?null:patient_principal_id+""); + } - Log.d(GEOFENCE_TAG, "doInBackground: CONNECTING TO SOCKET"); + @Override + public void onConnectionError(Exception e) { + Log.d(GEOFENCE_TAG, "doInBackground: SOCKET CONNECTION ERROR " + e); + } - socketHelper.establishConnection(new SocketHelper.SockCallBack() { - @Override - public void onSocketConnected() { - Log.d(GEOFENCE_TAG, "doInBackground: SOCKET CONNECTED"); - socketHelper.sendMessage(message, cg_id + "", patientId + "", channel_id + "", - patient_principal_id < 0?null:patient_principal_id+""); - } - - @Override - public void onConnectionError(Exception e) { - Log.d(GEOFENCE_TAG, "doInBackground: SOCKET CONNECTION ERROR " + e); - } - - @Override - public void onDisconnected() { - Log.d(GEOFENCE_TAG, "doInBackground: SOCKET DISCONNECTED"); - } - }); - - return null; - } + @Override + public void onDisconnected() { + Log.d(GEOFENCE_TAG, "doInBackground: SOCKET DISCONNECTED"); + } + }); } private void notifyOutOfGeofence(Context context, String senior_address) { @@ -179,7 +166,10 @@ public class GeoFenceBroadcastReceiver extends BroadcastReceiver { Intent intent = new Intent(context, LocationService.class); intent.setAction(LocationService.ACTION_START_LOCATION_UPDATES); intent.putExtra(LOCATION_UPDATE_MIN_INTERVAL, 5 * 1000); // every 5 seconds - context.startService(intent); - + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(intent); + }else { + context.startService(intent); + } } } \ No newline at end of file