From 4d3067ae49c5c17e77730ec7e68cd141d650a567 Mon Sep 17 00:00:00 2001 From: 14Sandee Date: Thu, 14 Dec 2023 21:48:41 +0530 Subject: [PATCH] , --- .idea/deploymentTargetDropDown.xml | 12 -- app/src/main/AndroidManifest.xml | 2 - .../com/app/simplitend/apputils/AppUtil.java | 18 +- .../apputils/BootCompleteReceiver.java | 59 +----- .../apputils/NotificationService.java | 46 +--- .../locationupdates/LocationService.java | 196 ++++++++++++++++++ .../patient_dashboard/DashBoardActivity.java | 41 ++-- .../PatientMainViewModel.java | 67 ------ .../GeoFenceBroadcastReceiver.java | 9 - 9 files changed, 232 insertions(+), 218 deletions(-) diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 0dd5b1f..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 1cdfcf6..9304821 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,7 +7,6 @@ - @@ -228,7 +227,6 @@ android:name=".locationupdates.LocationService" android:foregroundServiceType="location" /> - diff --git a/app/src/main/java/com/app/simplitend/apputils/AppUtil.java b/app/src/main/java/com/app/simplitend/apputils/AppUtil.java index e045046..c66f546 100644 --- a/app/src/main/java/com/app/simplitend/apputils/AppUtil.java +++ b/app/src/main/java/com/app/simplitend/apputils/AppUtil.java @@ -566,6 +566,22 @@ public abstract class AppUtil { private static final String PATIENT_GEOFENCE_CHANNEL_ID = "pg_channel_uid"; private static final String PATIENT_GEOFENCE_PATIENT_PRINCIPLE_ID = "pg_patient_geofence_uid"; + public static final String IS_SENIOR_OUT_OF_GEOFENCE = "is_senior_out_of_geofence"; + + public static void updateSeniorOutOfGeofence(Context context, boolean isOutOfGeofence){ + SharedPreferences sp = context.getSharedPreferences(PATIENT_DETAILS, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sp.edit(); + + editor.putBoolean(IS_SENIOR_OUT_OF_GEOFENCE, isOutOfGeofence); + + editor.apply(); + } + + public static boolean isSeniorOutOfGeofence(Context context){ + return context.getSharedPreferences(PATIENT_DETAILS, Context.MODE_PRIVATE). + getBoolean(IS_SENIOR_OUT_OF_GEOFENCE, false); + } + public static void updatePatientGeofenceChatsCred(Context context, int cg_id, int channel_id, int patient_principal_id){ @@ -610,7 +626,7 @@ public abstract class AppUtil { editor.putString(PATIENT_GEOFENCE_MESSAGE, message); editor.apply(); - Log.d(GEOFENCE_TAG, "updatePatientGeofence: UPDATED"); + Log.d(GEOFENCE_TAG, "updatePatientGeofence: GEOFENCE DATA UPDATED"); } public static String[] getPatientLatLng(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 index 9faf7ae..23af608 100644 --- a/app/src/main/java/com/app/simplitend/apputils/BootCompleteReceiver.java +++ b/app/src/main/java/com/app/simplitend/apputils/BootCompleteReceiver.java @@ -32,6 +32,7 @@ public class BootCompleteReceiver extends BroadcastReceiver { 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); @@ -42,65 +43,7 @@ public class BootCompleteReceiver extends BroadcastReceiver { 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.getApplicationContext()); - 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 08cb259..c18e986 100644 --- a/app/src/main/java/com/app/simplitend/apputils/NotificationService.java +++ b/app/src/main/java/com/app/simplitend/apputils/NotificationService.java @@ -127,11 +127,7 @@ public class NotificationService implements INotificationServiceExtension { if (AppUtil.isPatientLoggedIn(iNotificationReceivedEvent.getContext())){ // Either Geofence radius or Patient's location has changed. - Log.d(GEOFENCE_TAG, "Adding geo fence..."); - if (ActivityCompat.checkSelfPermission(iNotificationReceivedEvent.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { - Log.d(GEOFENCE_TAG, "No location permission"); - return; - } + Log.d(GEOFENCE_TAG, "UPDATING GEOFENCE DETAILS"); try { double lat = extras.getDouble("lat"); @@ -144,49 +140,19 @@ public class NotificationService implements INotificationServiceExtension { message = null; } - Log.d(GEOFENCE_TAG, "DATA RECEIVED WITH NOTIFICATION : Lat/Lng: " + lat + "," + lng + " Radius: " + radius); + Log.d(GEOFENCE_TAG, "DATA RECEIVED WITH NOTIFICATION : Lat/Lng: " + lat + "," + lng + " Radius: " + radius + " message: " + message); if (radius >= 0){ - addGeoFence(new LatLng(lat, lng), - radius, message, iNotificationReceivedEvent.getContext()); + AppUtil.updatePatientGeofence(iNotificationReceivedEvent.getContext(), + lat+"", lng+"", + radius+"", "kms", message); } }catch (Exception e){ - Log.e(GEOFENCE_TAG, "COULDN'T CREATE GEOFENCE: " + e); + Log.e(GEOFENCE_TAG, "COULDN'T UPDATE GEOFENCE DETAILS FROM THE NOTIFICATION: " + e); Log.e(GEOFENCE_TAG, "EXTRAS FROM NOTIFICATION: " + extras); } - }else{ - Log.d(GEOFENCE_TAG, "onNotificationReceived of PATIENT GEOFENCE CHANGED. BUT PATIENT IS LOGGED OUT."); } } } - @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION) - private void addGeoFence(@NonNull LatLng latLng, double GEOFENCING_RADIUS, String message, Context context) { - AppUtil.removeGeofence(context); - - GeoFenceHelper geoFenceHelper = new GeoFenceHelper(context.getApplicationContext()); - 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, "addGeoFence: No background location permission"); - return; - } - } - - Geofence geofence = geoFenceHelper.getGeoFence(GEOFENCE_ID, latLng, (float) 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."); - AppUtil.updatePatientGeofence(context, latLng.latitude+"", latLng.longitude+"", - GEOFENCING_RADIUS+"", "kms", message); - }) - .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/locationupdates/LocationService.java b/app/src/main/java/com/app/simplitend/locationupdates/LocationService.java index 551313e..33b96da 100644 --- a/app/src/main/java/com/app/simplitend/locationupdates/LocationService.java +++ b/app/src/main/java/com/app/simplitend/locationupdates/LocationService.java @@ -1,24 +1,44 @@ package com.app.simplitend.locationupdates; +import static com.app.simplitend.patientgeofencing.GeoFenceHelper.GEOFENCE_TAG; import static com.app.simplitend.patientgeofencing.PatientLocationUpdatesReceiver.LOCATION_EXTRA_KEY; import static com.app.simplitend.patientgeofencing.PatientLocationUpdatesReceiver.LOCATION_REQUEST_TAG; import android.app.Notification; import android.app.NotificationManager; import android.app.Service; +import android.content.Context; import android.content.Intent; import android.location.Location; +import android.os.Build; import android.os.IBinder; import android.util.Log; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; +import com.app.simplitend.BuildConfig; import com.app.simplitend.R; +import com.app.simplitend.apputils.AppUtil; +import com.app.simplitend.apputils.RetrofitHelper; +import com.app.simplitend.caregiverdashboard.mvvm.NotificationApiService; import com.app.simplitend.chats.SocketHelper; import com.app.simplitend.patientgeofencing.PatientLocationUpdatesReceiver; +import com.app.simplitend.welcome.welcomepatient.mvvm.models.CallResponse; import com.google.android.gms.location.LocationServices; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import io.socket.client.IO; +import io.socket.client.Socket; +import io.socket.emitter.Emitter; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + public class LocationService extends Service implements LocationClient.DefaultLocationUpdates { public static final String ACTION_START_LOCATION_UPDATES = "com.simplitent.action_start_lu"; @@ -55,6 +75,9 @@ public class LocationService extends Service implements LocationClient.DefaultLo break; case ACTION_STOP_LOCATION_UPDATES: stopLocationUpdates(); + + // setting that senior is at home for start + AppUtil.updateSeniorOutOfGeofence(this, false); break; } } @@ -117,5 +140,178 @@ public class LocationService extends Service implements LocationClient.DefaultLo Intent intent = new Intent(this, PatientLocationUpdatesReceiver.class); intent.putExtra(LOCATION_EXTRA_KEY, location); sendBroadcast(intent); + + checkGeofence(location); + } + + private void checkGeofence(Location currentLocation) { + if (currentLocation != null) { + String[] geofenceDetails = AppUtil.getPatientLatLng(this); + if (geofenceDetails[0] != null && geofenceDetails[1] != null){ + String geofenceRadius_str = AppUtil.getPatientGeofenceRadius(this); + if (geofenceRadius_str != null){ + double geofenceRadius; + + try { + geofenceRadius = Double.parseDouble(geofenceRadius_str); + } catch (Exception e) { + geofenceRadius = 0; + } + + if (geofenceRadius > 0){ + Location homeLocation; + try { + homeLocation = new Location("homeLocation"); + homeLocation.setLatitude(Double.parseDouble(geofenceDetails[0])); + homeLocation.setLongitude(Double.parseDouble(geofenceDetails[1])); + }catch (Exception e){ + homeLocation = null; + } + + if (homeLocation != null){ + double distance = homeLocation.distanceTo(currentLocation); + if (distance > geofenceRadius){ + // senior is gone out of geofence + Log.d(GEOFENCE_TAG, "EXITED GEOFENCE"); + boolean alreadyOutOfGeofence = AppUtil.isSeniorOutOfGeofence(this); + Log.d(GEOFENCE_TAG, "IS SENIOR ALREADY OUT OF GEOFENCE " + alreadyOutOfGeofence); + + if (!alreadyOutOfGeofence){ + notifyOutOfGeofence(this, String.format(Locale.getDefault(), "%.2f", distance)); + + notifyPatient(this); + // getting faster location updates as patient is out of geofence + removeLocationUpdates(); + startLocationUpdates(5*1000); // 5 seconds + + // updating out of geofence + AppUtil.updateSeniorOutOfGeofence(this, true); + } + }else{ + // senior is at home + Log.d(GEOFENCE_TAG, "ENTERED GEOFENCE"); + boolean alreadyOutOfGeofence = AppUtil.isSeniorOutOfGeofence(this); + + if (alreadyOutOfGeofence){ + // getting slower location updates as patient is inside of geofence + removeLocationUpdates(); + startLocationUpdates(25*1000); // 5 seconds + + // updating out of geofence + AppUtil.updateSeniorOutOfGeofence(this, false); + } + } + } + } + } + } + } + } + + private void notifyPatient(Context context) { + Log.d(GEOFENCE_TAG, "notifyPatient: SENDING MESSAGE TO SENIOR PHONE FROM CAREGIVER PHONE"); + + String message = AppUtil.getPatientGeofenceMessage(context); + if (message == null || message.trim().isEmpty()) { + message = "You are too far from home"; + } + + int[] chatsCred = AppUtil.getPatientGeofenceChatCred(context); + if (chatsCred[0] == -1 || chatsCred[1] == -1 || chatsCred[2] == -1) { + Log.d(GEOFENCE_TAG, "notifyPatient: CANNOT SEND MESSAGE AS EITHER PAT_ID, CG_ID OR CHANNEL_ID IS NOT AVAILABLE"); + return; + } + + Log.d(GEOFENCE_TAG, "notifyPatient: " + chatsCred[2]); + // sending message + sendMessage(message, chatsCred[0], chatsCred[1], chatsCred[2], chatsCred[3]); + } + + private void sendMessage(@NonNull String message, int patientId, int cg_id, int channel_id, int patient_principal_id) { + SocketHelper socketHelper = SocketHelper.getInstance(); + + Log.d(GEOFENCE_TAG, "doInBackground: CONNECTING TO SOCKET"); + + try { + IO.Options options = new IO.Options(); + options.forceNew = true; + options.reconnection = true; + Socket mSocket = IO.socket(BuildConfig.SIMPLITEND_SOKCET_HOST, options); + + mSocket.on(channel_id + "", new Emitter.Listener() { + @Override + public void call(Object... args) { + try { + if (args.length >= 4) { + String received_sender_id = (String) args[1]; + if (received_sender_id.equals(cg_id+"")){ + // message was sent successfully + Log.d(GEOFENCE_TAG, "GEOFENCE MESSAGE WAS SENT SUCCESSFULLY"); + + mSocket.disconnect(); + } + } + } catch (Exception e) { + mSocket.disconnect(); + } + } + }); + + mSocket.on(Socket.EVENT_CONNECT, args -> { + Log.d(GEOFENCE_TAG, "Socket connected "); + socketHelper.sendMessage(message, cg_id + "", patientId + "", channel_id + "", + patient_principal_id < 0 ? null : patient_principal_id + ""); + }); + + mSocket.on(Socket.EVENT_DISCONNECT, args -> { + Log.d(GEOFENCE_TAG, "Socket disconnected "); + }); + + mSocket.on(Socket.EVENT_CONNECT_ERROR, args -> { + Exception e = (Exception) args[0]; + Log.e(GEOFENCE_TAG, "call: ", e); + }); + + mSocket.connect(); + } catch (Exception e) { + e.printStackTrace(); + Log.e(GEOFENCE_TAG, "SocketHelper: ", e); + } + } + + private void notifyOutOfGeofence(Context context, String senior_address) { + Log.d(GEOFENCE_TAG, "Sending notification to patient"); + Log.d(GEOFENCE_TAG, "Current location: " + senior_address); + NotificationApiService apiService = RetrofitHelper.getRetrofit().create(NotificationApiService.class); + + Map body = new HashMap<>(); + body.put("patient_id", AppUtil.getPatientUid(context) + ""); + body.put("address", senior_address); + + apiService.notifyOutOfGeoFence(body, "Bearer " + AppUtil.getPatientToken(context)).enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if (response.code() == 200) { + Log.d(GEOFENCE_TAG, "OUT OF GEOFENCE NOTIFICATION SENT SUCCESSFULLY."); + } else { + Log.d(GEOFENCE_TAG, "Couldn't notify patient " + response.message()); + } + } + + @Override + public void onFailure(Call> call, Throwable t) { + Log.d(GEOFENCE_TAG, "Couldn't notify patient due to " + t); + } + }); + + // getting faster location updates as patient is out of geofence + 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 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(intent); + } else { + context.startService(intent); + } } } diff --git a/app/src/main/java/com/app/simplitend/patient_dashboard/DashBoardActivity.java b/app/src/main/java/com/app/simplitend/patient_dashboard/DashBoardActivity.java index 3221cc2..cc38035 100644 --- a/app/src/main/java/com/app/simplitend/patient_dashboard/DashBoardActivity.java +++ b/app/src/main/java/com/app/simplitend/patient_dashboard/DashBoardActivity.java @@ -18,6 +18,7 @@ import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; import androidx.lifecycle.ViewModelProvider; import com.app.simplitend.R; @@ -32,7 +33,6 @@ public class DashBoardActivity extends AppCompatActivity implements CgHomeContra protected PatientMainViewModel viewModel; protected ActivityResultLauncher finePermissionLauncher; - protected ActivityResultLauncher backgroundPermissionLauncher; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,29 +41,16 @@ public class DashBoardActivity extends AppCompatActivity implements CgHomeContra viewModel = new ViewModelProvider(this).get(PatientMainViewModel.class); + updateGeofenceDetails(); + finePermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> { if (isGranted) { - // fine location permission is granted - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - if (checkSelfPermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED){ - AppUtil.showSOSDecision(this, - "To enable caregivers to receive notifications even when the app is closed, go to the settings menu and select allow all the time", - "Settings", "No thanks", view -> { - // Settings click - backgroundPermissionLauncher.launch(Manifest.permission.ACCESS_BACKGROUND_LOCATION); - }, view -> { - // No thanks click - Toast.makeText(this, "Geofencing is off.", Toast.LENGTH_SHORT).show(); - }); - }else{ - setGeofenceAndLocationUpdates(); - } - } else { - setGeofenceAndLocationUpdates(); + // getting location updates + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + return; } - // getting location updates viewModel.addLocationUpdates(this); } else { // permission denied @@ -77,13 +64,6 @@ public class DashBoardActivity extends AppCompatActivity implements CgHomeContra } }); - backgroundPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), - isGranted -> { - if (isGranted) { - setGeofenceAndLocationUpdates(); - } - }); - registerForActivityResult(new ActivityResultContracts.RequestPermission(), result -> { @@ -114,7 +94,8 @@ public class DashBoardActivity extends AppCompatActivity implements CgHomeContra super.attachBaseContext(newBase); } - private void setGeofenceAndLocationUpdates() { + // saves the geofence details + private void updateGeofenceDetails() { // retrieving geofence viewModel.getGeoFenceDetails(AppUtil.getPatientUid(this) + "", "", "Bearer " + AppUtil.getPatientToken(this), @@ -128,9 +109,10 @@ public class DashBoardActivity extends AppCompatActivity implements CgHomeContra if (AppUtil.shouldAddPatientGeofence(this, geoFenceDetails.radius, geoFenceDetails.type, patientData)) { // should add a geofence - viewModel.setGeofence(this, geoFenceDetails, patientData); + AppUtil.updatePatientGeofence(this, patientData.lat, patientData.lng, + geoFenceDetails.radius, geoFenceDetails.type, geoFenceDetails.message); } else { - Log.d(GEOFENCE_TAG, "onGeofenceDetailsFetched: should not add patient geofence because GEOFENCE DETAILS: " + geoFenceDetails + " PATIENT DETAILS: " + patientData); + Log.d(GEOFENCE_TAG, "onGeofenceDetailsFetched: GEOFENCE DATA UP TO DATE"); } } }), false); @@ -142,6 +124,7 @@ public class DashBoardActivity extends AppCompatActivity implements CgHomeContra public void onGeofenceDetailsFetched(@NonNull GeoFenceDetails geoFenceDetails) { validateAndAddGeofence(geoFenceDetails); } + @Override public void onGeofenceDetailsFetchFailed(Throwable throwable, String message) { Log.d(GEOFENCE_TAG, "onGeofenceDetailsFetchFailed: " + message); 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 eccdbd5..3f353d4 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 @@ -3,16 +3,12 @@ package com.app.simplitend.patient_dashboard; import static android.content.Context.ACTIVITY_SERVICE; 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 static com.app.simplitend.patientgeofencing.PatientLocationUpdatesReceiver.LOCATION_REQUEST_TAG; import android.Manifest; import android.app.Activity; import android.app.ActivityManager; -import android.app.PendingIntent; import android.content.Intent; -import android.content.pm.PackageManager; import android.os.Build; import android.os.Handler; import android.os.Looper; @@ -21,30 +17,20 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.RequiresPermission; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; import androidx.lifecycle.ViewModel; -import com.app.simplitend.apputils.AppUtil; import com.app.simplitend.apputils.RetrofitHelper; import com.app.simplitend.caregiverdashboard.mvvm.CaregiverMainViewModel; import com.app.simplitend.caregiverdashboard.mvvm.CgHomeContracts; import com.app.simplitend.caregiverdashboard.mvvm.CgHomeRepository; import com.app.simplitend.caregiverdashboard.mvvm.NotificationApiService; -import com.app.simplitend.caregiverdashboard.mvvm.models.GeoFenceDetails; import com.app.simplitend.locationupdates.LocationService; -import com.app.simplitend.patientgeofencing.GeoFenceHelper; import com.app.simplitend.patientprofile.PatientProfileAPIService; import com.app.simplitend.patientprofile.medreminder.mvvm.models.NearestActivity; import com.app.simplitend.patientprofile.medreminder.mvvm.models.NearestReminder; import com.app.simplitend.patientprofile.medreminder.mvvm.models.ReminderResult; import com.app.simplitend.patientprofile.setuproutine.mvvm.RoutineDetails; import com.app.simplitend.welcome.welcomepatient.mvvm.models.CallResponse; -import com.app.simplitend.welcome.welcomepatient.mvvm.models.PatientData; -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; import java.text.SimpleDateFormat; @@ -83,27 +69,6 @@ public class PatientMainViewModel extends ViewModel { this.upcomingReminderText = this.dailyReminderText = "Loading..."; } - public void setGeofence(Activity activity, GeoFenceDetails geoFenceDetails, PatientData patientData) { - LatLng latLng; - float radius; - try { - latLng = new LatLng(Double.parseDouble(patientData.lat), Double.parseDouble(patientData.lng)); - radius = Float.parseFloat(geoFenceDetails.radius); - } catch (Exception e) { - latLng = null; - radius = 0; - } - - if (latLng != null && radius != 0) { - if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { - Log.d(GEOFENCE_TAG, "setGeofence: NO FINE LOCATION PERMISSION" ); - return; - } - - addGeoFence(latLng, radius, activity, geoFenceDetails.type, geoFenceDetails.message); - } - } - @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION) public void addLocationUpdates(Activity activity) { if (!isLocationServiceRunning(activity)) { @@ -137,38 +102,6 @@ public class PatientMainViewModel extends ViewModel { cgHomeRepository.getGeoFenceDetails(p_id, cg_xid, token, getGeoFenceCallback); } - @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION) - private void addGeoFence(@NonNull LatLng latLng, float GEOFENCING_RADIUS , Activity activity, String unit, String message) { - AppUtil.removeGeofence(activity); - - GeoFenceHelper geoFenceHelper = new GeoFenceHelper(activity.getApplicationContext()); - GeofencingClient geofencingClient = LocationServices.getGeofencingClient(activity); - - // checking for background location updates for API level 29 and above - if (Build.VERSION.SDK_INT >= 29) { - if (ContextCompat.checkSelfPermission(activity, 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"); - AppUtil.updatePatientGeofence(activity, latLng.latitude+"", latLng.longitude+"", - GEOFENCING_RADIUS+"", unit, message); - }) - .addOnFailureListener(e -> { - Log.d(GEOFENCE_TAG, "onFailure: Geofence couldn't be added: " + e.getLocalizedMessage() + " " + latLng + " Radius: " + GEOFENCING_RADIUS); - }); - - } - public void notifyRequestedSOS(String patient_Id, String token){ Map body = new HashMap<>(); body.put("patient_id", patient_Id); 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 580bdef..e9613b0 100644 --- a/app/src/main/java/com/app/simplitend/patientgeofencing/GeoFenceBroadcastReceiver.java +++ b/app/src/main/java/com/app/simplitend/patientgeofencing/GeoFenceBroadcastReceiver.java @@ -57,15 +57,6 @@ public class GeoFenceBroadcastReceiver extends BroadcastReceiver { int transition_type = geofencingEvent.getGeofenceTransition(); Location location = geofencingEvent.getTriggeringLocation(); -// String senior_address = null; -// if (location != null){ -// senior_address = AppUtil.getCompleteAddress(context, location.getLatitude(), location.getLongitude()); -// } -// -// if (senior_address == null) { -// senior_address = "Unable to locate"; -// } - double distance; try { String[] homeLatLng = AppUtil.getPatientLatLng(context);