diff --git a/app/src/main/java/com/app/simplitend/locationupdates/DefaultLocationClient.java b/app/src/main/java/com/app/simplitend/locationupdates/DefaultLocationClient.java index a3c639e..25ab0d1 100644 --- a/app/src/main/java/com/app/simplitend/locationupdates/DefaultLocationClient.java +++ b/app/src/main/java/com/app/simplitend/locationupdates/DefaultLocationClient.java @@ -51,7 +51,7 @@ public class DefaultLocationClient implements LocationClient{ } locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, interval) - .setMinUpdateDistanceMeters(100) // receive updates only if user has moved over 100 meters + .setMinUpdateDistanceMeters(50) // receive updates only if user has moved over 100 meters .build(); locationCallback = new LocationCallback() { 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 33b96da..831c4b3 100644 --- a/app/src/main/java/com/app/simplitend/locationupdates/LocationService.java +++ b/app/src/main/java/com/app/simplitend/locationupdates/LocationService.java @@ -10,7 +10,6 @@ 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; @@ -40,7 +39,7 @@ 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"; public static final String ACTION_STOP_LOCATION_UPDATES = "com.simplitent.action.stop_lu"; public static final String LOCATION_NOTIFICATION_CHANNEL_ID = "location"; @@ -64,14 +63,11 @@ public class LocationService extends Service implements LocationClient.DefaultLo @Override public int onStartCommand(Intent intent, int flags, int startId) { - if (intent != null && intent.getAction() != null){ - switch (intent.getAction()){ + if (intent != null && intent.getAction() != null) { + switch (intent.getAction()) { case ACTION_START_LOCATION_UPDATES: - int minInterval = intent.getIntExtra(LOCATION_UPDATE_MIN_INTERVAL, 0); - if (minInterval != 0){ - removeLocationUpdates(); - startLocationUpdates(minInterval); - } + removeLocationUpdates(); + startLocationUpdates(10_000); // 10 seconds interval break; case ACTION_STOP_LOCATION_UPDATES: stopLocationUpdates(); @@ -91,7 +87,7 @@ public class LocationService extends Service implements LocationClient.DefaultLo SocketHelper.getInstance().closeConnection(); } - private void startLocationUpdates(int minInterval){ + private void startLocationUpdates(int minInterval) { SocketHelper.getInstance().establishConnection(null); Notification notification = new NotificationCompat.Builder(this, LOCATION_NOTIFICATION_CHANNEL_ID) @@ -122,9 +118,9 @@ public class LocationService extends Service implements LocationClient.DefaultLo removeLocationUpdates(); SocketHelper.getInstance().closeConnection(); } - - public void removeLocationUpdates(){ - if (locationClient != null){ + + public void removeLocationUpdates() { + if (locationClient != null) { locationClient.removeLocationUpdates(); } } @@ -132,7 +128,7 @@ public class LocationService extends Service implements LocationClient.DefaultLo @Nullable @Override public IBinder onBind(Intent intent) { - return null; + return null; } @Override @@ -147,9 +143,9 @@ public class LocationService extends Service implements LocationClient.DefaultLo private void checkGeofence(Location currentLocation) { if (currentLocation != null) { String[] geofenceDetails = AppUtil.getPatientLatLng(this); - if (geofenceDetails[0] != null && geofenceDetails[1] != null){ + if (geofenceDetails[0] != null && geofenceDetails[1] != null) { String geofenceRadius_str = AppUtil.getPatientGeofenceRadius(this); - if (geofenceRadius_str != null){ + if (geofenceRadius_str != null) { double geofenceRadius; try { @@ -158,44 +154,39 @@ public class LocationService extends Service implements LocationClient.DefaultLo geofenceRadius = 0; } - if (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){ + } catch (Exception e) { homeLocation = null; } - if (homeLocation != null){ + if (homeLocation != null) { double distance = homeLocation.distanceTo(currentLocation); - if (distance > geofenceRadius){ + 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)); + if (!alreadyOutOfGeofence) { + distance = distance / 1609; // converting to miles + 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{ + } 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 + if (alreadyOutOfGeofence) { // updating out of geofence AppUtil.updateSeniorOutOfGeofence(this, false); @@ -244,7 +235,7 @@ public class LocationService extends Service implements LocationClient.DefaultLo try { if (args.length >= 4) { String received_sender_id = (String) args[1]; - if (received_sender_id.equals(cg_id+"")){ + if (received_sender_id.equals(cg_id + "")) { // message was sent successfully Log.d(GEOFENCE_TAG, "GEOFENCE MESSAGE WAS SENT SUCCESSFULLY"); @@ -303,15 +294,5 @@ public class LocationService extends Service implements LocationClient.DefaultLo 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); - } } }