Use fused and change loc mode

This commit is contained in:
Mieszko Wrzeszczyński 2019-01-03 17:39:34 +01:00
parent 6bdf402df8
commit 88f263217a
3 changed files with 100 additions and 26 deletions

View File

@ -29,7 +29,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8 (1)" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -11,11 +11,13 @@ import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat;
@ -23,12 +25,17 @@ import android.util.Log;
import com.annimon.stream.Stream;
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.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.uam.wmi.findmytutor.model.Coordinate;
import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.utils.ApproximatedLocalization;
import com.uam.wmi.findmytutor.utils.Const;
import com.uam.wmi.findmytutor.utils.MapUtils;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import com.uam.wmi.findmytutor.utils.SharingLevel;
@ -54,7 +61,8 @@ import static java.lang.String.valueOf;
public class BackgroundLocalizationService extends Service {
private static final String TAG = "MyLocationService";
private static long notify_interval = onlineBackgroundLocationInterval;
private static long notify_interval;
private Boolean highAccuracyMode;
private static long notify_interval_inside_building = onlineBackgroundLocationInterval;
private static long notify_interval_outside_building = offlineBackgroundLocationInterval;
private static int coordinatesHistoryLength = 5;
@ -66,6 +74,11 @@ public class BackgroundLocalizationService extends Service {
private Runnable mStatusChecker;
private FusedLocationProviderClient mFusedLocationClient;
private Location fakeLoc = null;
private LocationRequest mLocationRequest;
private Location mCurrentLocation;
private LocationCallback mLocationCallback;
private Looper myLooper;
private LocationListener mLocationListener;
public BackgroundLocalizationService() {
providers.add(LocationManager.GPS_PROVIDER);
@ -90,7 +103,9 @@ public class BackgroundLocalizationService extends Service {
if (intent != null) {
stopService = intent.getBooleanExtra("request_stop", false);
notify_interval = intent.getLongExtra("notify_interval", onlineBackgroundLocationInterval);
}
if (stopService) {
storeBackgroundLocationStatus(getApplication(), false);
stopForeground(true);
@ -101,6 +116,21 @@ public class BackgroundLocalizationService extends Service {
return START_STICKY;
}
private void createLocationCallback() {
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
if (locationResult != null) {
mCurrentLocation = locationResult.getLastLocation();
sendCoordinateToBackend(mCurrentLocation);
coordinatesHistory.add(mCurrentLocation);
changeBackgroundMode();
}
}
};
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
@ -114,30 +144,40 @@ public class BackgroundLocalizationService extends Service {
startForeground(1001, notification);
}
createFusedLocationClient();
}
private void createFusedLocationClient(){
Integer saveMode = Long.valueOf(notify_interval).compareTo(Long.valueOf(offlineBackgroundLocationInterval));
myLooper = Looper.myLooper();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationRequest = new LocationRequest();
if(saveMode != 0){
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}else{
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
mLocationRequest.setFastestInterval(notify_interval);
mLocationRequest.setInterval(notify_interval);
createLocationCallback();
if (!stopService) {
mStatusChecker = () -> {
try {
if (PrefUtils.getLocationLevel(getApplicationContext()).equals(SharingLevel.MANUAL.toString())) {
sendCoordinateToBackend(fakeLoc);
} else {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
getLocalizationFromListeners();
changeBackgroundMode();
}
} finally {
mFusedLocationClient = null;
destroyLocationListeners();
mHandler.postDelayed(mStatusChecker, notify_interval);
}
};
AsyncTask.execute(mStatusChecker);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback, myLooper);
}
}
private void changeBackgroundMode() {
if (coordinatesHistory.size() > 4) {
Long prevInterval = notify_interval;
Boolean shouldExtendTimeInterval = Stream.of(coordinatesHistory)
.map(MapUtils::checkIfCoordinateIsValid).takeWhile(s -> !s).toList().size() == coordinatesHistory.size();
@ -150,6 +190,11 @@ public class BackgroundLocalizationService extends Service {
notify_interval = notify_interval_inside_building;
}
Integer changedMode = Long.valueOf(prevInterval).compareTo(Long.valueOf(notify_interval));
if(changedMode != 0){
updateListeners();
}
}
}
@ -192,9 +237,7 @@ public class BackgroundLocalizationService extends Service {
coordinatesHistory.add(location);
sendCoordinateToBackend(location);
}
});
}
private void sendCoordinateToBackend(Location location) {
@ -211,7 +254,41 @@ public class BackgroundLocalizationService extends Service {
}
private void destroyLocationListeners() {
if (mFusedLocationClient != null) {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
mFusedLocationClient = null;
mLocationCallback = null;
mLocationRequest = null;
}
}
private void updateListeners() {
if (mFusedLocationClient != null) {
mFusedLocationClient.removeLocationUpdates(mLocationCallback)
.addOnCompleteListener(task -> {
restartService();
mFusedLocationClient = null;
mLocationCallback = null;
mLocationRequest = null;
});
}
}
private void restartService(){
Intent stopIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
stopIntent.putExtra("request_stop", true);
stopService(stopIntent);
Intent startIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
startIntent.putExtra("notify_interval", notify_interval);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
startForegroundService(startIntent);
} else {
startService(startIntent);
}
}
@ -277,7 +354,6 @@ public class BackgroundLocalizationService extends Service {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableCompletableObserver() {
@Override
public void onComplete() {
Log.e(TAG, "CoordinateSuccess");
@ -285,9 +361,7 @@ public class BackgroundLocalizationService extends Service {
@Override
public void onError(Throwable e) {
Log.e(TAG, "onErr" + valueOf(e));
}
}));
} catch (IllegalArgumentException e) {

View File

@ -7,8 +7,8 @@ import java.util.List;
public class Const {
public final static String BASE_URL = "https://s416084.projektstudencki.pl/master/";
public final static Integer onlineBackgroundLocationInterval = 7000;
public final static Integer offlineBackgroundLocationInterval = 36000;
public final static Integer onlineBackgroundLocationInterval = 7000;
public final static Integer offlineBackgroundLocationInterval = 14000;
public final static Integer defaultMapZoom = 17;
public final static Integer searchMapZoom = 13;
public final static Double presenceLatitude = 52.466365;