Compare commits

...

1 Commits

Author SHA1 Message Date
451ea60bbb A little refactor 2018-12-25 20:58:54 +01:00
2 changed files with 91 additions and 19 deletions

View File

@ -72,4 +72,5 @@ dependencies {
// FloatingBarMenu // FloatingBarMenu
implementation 'com.getbase:floatingactionbutton:1.10.1' implementation 'com.getbase:floatingactionbutton:1.10.1'
implementation 'org.apache.commons:commons-collections4:4.0' implementation 'org.apache.commons:commons-collections4:4.0'
implementation 'com.patloew.rxlocation:rxlocation:1.0.5'
} }

View File

@ -17,6 +17,7 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.RequiresApi; import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
@ -24,8 +25,10 @@ import android.util.Log;
import com.annimon.stream.Stream; import com.annimon.stream.Stream;
import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices; import com.google.android.gms.location.LocationServices;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.mapbox.geojson.Point; import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.geometry.LatLng;
import com.uam.wmi.findmytutor.model.Coordinate; import com.uam.wmi.findmytutor.model.Coordinate;
@ -33,7 +36,6 @@ import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.utils.ApproximatedLocalization; import com.uam.wmi.findmytutor.utils.ApproximatedLocalization;
import com.uam.wmi.findmytutor.utils.MapUtils; import com.uam.wmi.findmytutor.utils.MapUtils;
import com.uam.wmi.findmytutor.utils.PrefUtils; import com.uam.wmi.findmytutor.utils.PrefUtils;
import com.uam.wmi.findmytutor.utils.RestApiHelper;
import com.uam.wmi.findmytutor.utils.SharingLevel; import com.uam.wmi.findmytutor.utils.SharingLevel;
import org.apache.commons.collections4.queue.CircularFifoQueue; import org.apache.commons.collections4.queue.CircularFifoQueue;
@ -45,9 +47,7 @@ import java.util.concurrent.atomic.AtomicReference;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableCompletableObserver; import io.reactivex.observers.DisposableCompletableObserver;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import static com.uam.wmi.findmytutor.utils.Const.offlineBackgroundLocationInterval; import static com.uam.wmi.findmytutor.utils.Const.offlineBackgroundLocationInterval;
import static com.uam.wmi.findmytutor.utils.Const.onlineBackgroundLocationInterval; import static com.uam.wmi.findmytutor.utils.Const.onlineBackgroundLocationInterval;
@ -62,10 +62,10 @@ public class BackgroundLocalizationService extends Service {
private static final String TAG = "MyLocationService"; private static final String TAG = "MyLocationService";
private static final float LOCATION_DISTANCE = 1f; private static final float LOCATION_DISTANCE = 1f;
private static long notify_interval = onlineBackgroundLocationInterval; private static long notify_interval = onlineBackgroundLocationInterval;
private static final Long LOCATION_INTERVAL = notify_interval;
private static long notify_interval_inside_building = onlineBackgroundLocationInterval; private static long notify_interval_inside_building = onlineBackgroundLocationInterval;
private static long notify_interval_outside_building = offlineBackgroundLocationInterval; private static long notify_interval_outside_building = offlineBackgroundLocationInterval;
private static int coordinatesHistoryLength = 5; private static int coordinatesHistoryLength = 5;
private static final Long LOCATION_INTERVAL = notify_interval;
private Location mLastLocation; private Location mLastLocation;
private Boolean stopService = false; private Boolean stopService = false;
private ArrayList<String> providers = new ArrayList<String>(); private ArrayList<String> providers = new ArrayList<String>();
@ -75,6 +75,8 @@ public class BackgroundLocalizationService extends Service {
private Handler mHandler = new Handler(); private Handler mHandler = new Handler();
private Runnable mStatusChecker; private Runnable mStatusChecker;
private FusedLocationProviderClient mFusedLocationClient; private FusedLocationProviderClient mFusedLocationClient;
private LocationCallback mLocationCallback;
private LocationRequest locationRequest;
public BackgroundLocalizationService() { public BackgroundLocalizationService() {
providers.add(LocationManager.GPS_PROVIDER); providers.add(LocationManager.GPS_PROVIDER);
@ -111,6 +113,7 @@ public class BackgroundLocalizationService extends Service {
return START_STICKY; return START_STICKY;
} }
@SuppressLint("CheckResult")
@Override @Override
public void onCreate() { public void onCreate() {
Log.e(TAG, "onCreate"); Log.e(TAG, "onCreate");
@ -128,13 +131,13 @@ public class BackgroundLocalizationService extends Service {
if (!stopService) { if (!stopService) {
mStatusChecker = () -> { mStatusChecker = () -> {
try { try {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); mFusedLocationClient = new FusedLocationProviderClient(this);
initializeLocationManager(); initializeLocationManager();
getLocalizationFromListeners(); getLocalizationFromListeners();
changeBackgroundMode(); changeBackgroundMode();
} finally { } finally {
mFusedLocationClient = null; //mFusedLocationClient = null;
destroyLocationListeners(); //destroyLocationListeners();
mHandler.postDelayed(mStatusChecker, notify_interval); mHandler.postDelayed(mStatusChecker, notify_interval);
} }
}; };
@ -196,18 +199,50 @@ public class BackgroundLocalizationService extends Service {
Location bestLocation = null; Location bestLocation = null;
AtomicReference<Boolean> triggerAnotherLocationListener = new AtomicReference<>(false); AtomicReference<Boolean> triggerAnotherLocationListener = new AtomicReference<>(false);
/* mLocationCallback = new LocationCallback() {
for (Location location : locationResult.getLocations()) {
mLastLocation = location;
coordinatesHistory.add(location);
sendCoordinateToBackend(location);
}
};
};
*/
/* @Override
public void onLo(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
mLastLocation = location;
coordinatesHistory.add(location);
sendCoordinateToBackend(location);
Log.i("MainActivity", "Location: " + location.getLatitude() + " " + location.getLongitude());
}
};*/
mFusedLocationClient.getLastLocation().addOnSuccessListener( mFusedLocationClient.getLastLocation().addOnSuccessListener(
location -> { location -> {
Log.e(TAG,"Getting loc from fused");
if (location != null) { if (location != null) {
mLastLocation = location; mLastLocation = location;
coordinatesHistory.add(location); coordinatesHistory.add(location);
sendCoordinateToBackend(location); sendCoordinateToBackend(location);
} }
//triggerAnotherLocationListener.set(true);
triggerAnotherLocationListener.set(true); })
.addOnFailureListener(e -> {
Log.d(TAG, "Error trying to get last GPS location");
e.printStackTrace();
}); });
if (triggerAnotherLocationListener.get()) {
for (String provider : providers1) { for (String provider : providers1) {
Location location = mLocationManager.getLastKnownLocation(provider); Location location = mLocationManager.getLastKnownLocation(provider);
@ -218,9 +253,9 @@ public class BackgroundLocalizationService extends Service {
bestLocation = location; bestLocation = location;
} }
} }
Log.e(TAG, bestLocation.getLongitude() + " " + bestLocation.getLatitude());
coordinatesHistory.add(bestLocation); coordinatesHistory.add(bestLocation);
} sendCoordinateToBackend(bestLocation);
} }
@ -231,13 +266,14 @@ public class BackgroundLocalizationService extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
Log.e(TAG, "onDestroy"); Log.e(TAG, "onDestroy");
super.onDestroy(); super.onDestroy();
mHandler.removeCallbacks(mStatusChecker); mHandler.removeCallbacks(mStatusChecker);
destroyLocationListeners(); destroyLocationListeners();
} }
private void destroyLocationListeners(){ private void destroyLocationListeners() {
//mFusedLocationClient.removeLocationUpdates(mLocationCallback);
if (mLocationManager != null) { if (mLocationManager != null) {
for (LocationListener listener : mLocationListeners) { for (LocationListener listener : mLocationListeners) {
try { try {
@ -337,7 +373,7 @@ public class BackgroundLocalizationService extends Service {
if (locationLevel.equals(SharingLevel.PRESENCE.toString())) { if (locationLevel.equals(SharingLevel.PRESENCE.toString())) {
if(!MapUtils.checkIfCoordinateIsValid(latitude,longitude)){ if (!MapUtils.checkIfCoordinateIsValid(latitude, longitude)) {
return null; return null;
} }
@ -381,7 +417,7 @@ public class BackgroundLocalizationService extends Service {
@Override @Override
public void onError(Throwable e) { public void onError(Throwable e) {
Log.e(TAG,"onErr" + valueOf(e)); Log.e(TAG, "onErr" + valueOf(e));
} }
})); }));
@ -393,3 +429,38 @@ public class BackgroundLocalizationService extends Service {
} }
} }
} }
/*locationRequest = new LocationRequest();
locationRequest.setInterval(notify_interval);
locationRequest.setFastestInterval(notify_interval);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Log.e(TAG, "ELO");
for (Location location : locationResult.getLocations()) {
mLastLocation = location;
coordinatesHistory.add(location);
sendCoordinateToBackend(location);
Log.i(TAG, "Location: " + location.getLatitude() + " " + location.getLongitude());
}
}
};
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, Looper.getMainLooper());
*/