This commit is contained in:
Adam Domagalski 2018-11-04 12:39:57 +01:00
parent cad9f466c2
commit 40101ef7d0

View File

@ -1,15 +1,19 @@
package com.uam.wmi.findmytutor.activity; package com.uam.wmi.findmytutor.activity;
import android.Manifest;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.animation.ValueAnimator; import android.animation.ValueAnimator;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Location; import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.util.Log; import android.util.Log;
@ -75,6 +79,8 @@ public class MapActivity extends BaseActivity
private PermissionsManager permissionsManager; private PermissionsManager permissionsManager;
private LocationComponent locationComponent; private LocationComponent locationComponent;
LocationManager mLocationManager;
private CoordinateService coordinateService; private CoordinateService coordinateService;
private CompositeDisposable disposable = new CompositeDisposable(); private CompositeDisposable disposable = new CompositeDisposable();
@ -190,10 +196,6 @@ public class MapActivity extends BaseActivity
// TODO PUT MANUAL CORD // TODO PUT MANUAL CORD
try { try {
Coordinate coordinate = new Coordinate( Coordinate coordinate = new Coordinate(
latLng.getLatitude(), latLng.getLatitude(),
@ -212,18 +214,18 @@ public class MapActivity extends BaseActivity
@SuppressLint("LongLogTag") @SuppressLint("LongLogTag")
@Override @Override
public void onSuccess(Coordinate coord) { public void onSuccess(Coordinate coord) {
Log.e(tag+"POST", String.valueOf(coord)); Log.e(tag + "POST", String.valueOf(coord));
} }
@SuppressLint("LongLogTag") @SuppressLint("LongLogTag")
@Override @Override
public void onError(Throwable e) { public void onError(Throwable e) {
Log.e(tag+"onError", e.getMessage()); Log.e(tag + "onError", e.getMessage());
if (e instanceof HttpException) { if (e instanceof HttpException) {
ResponseBody responseBody = ((HttpException) e).response().errorBody(); ResponseBody responseBody = ((HttpException) e).response().errorBody();
Log.e(tag+"onError", RestApiHelper.getErrorMessage(responseBody)); Log.e(tag + "onError", RestApiHelper.getErrorMessage(responseBody));
} }
} }
@ -233,15 +235,6 @@ public class MapActivity extends BaseActivity
} }
Toast.makeText( Toast.makeText(
MapActivity.this, MapActivity.this,
"Manual Locations selected!" + latLng, "Manual Locations selected!" + latLng,
@ -478,17 +471,18 @@ public class MapActivity extends BaseActivity
private void enableLocationPlugin() { private void enableLocationPlugin() {
// Check if permissions are enabled and if not request // Check if permissions are enabled and if not request
if (PermissionsManager.areLocationPermissionsGranted(this)) { if (PermissionsManager.areLocationPermissionsGranted(this)) {
LocationComponentOptions options = LocationComponentOptions.builder(this) LocationComponentOptions options = LocationComponentOptions.builder(this)
.trackingGesturesManagement(true) .trackingGesturesManagement(true)
.accuracyColor(ContextCompat.getColor(this, R.color.mapboxGray)) .accuracyColor(ContextCompat.getColor(this, R.color.mapboxGray))
.build(); .build();
// Get an instance of the component // Get an instance of the component
locationComponent = mapboxMap.getLocationComponent(); locationComponent = mapboxMap.getLocationComponent();
// TODO mieszko tu jest lepsza lokalizacja // TODO tu jest lepsza lokalizacja(chyba)
// locationComponent.getLastKnownLocation(); // locationComponent.getLastKnownLocation();
// Log.e(tag + "Last", getLastKnownLocation() + ""); //Log.e(tag + "Last", locationComponent.getLastKnownLocation() + "");
// Activate with options // Activate with options
locationComponent.activateLocationComponent(this, options); locationComponent.activateLocationComponent(this, options);
@ -530,12 +524,13 @@ public class MapActivity extends BaseActivity
@Override @Override
public void onRequestPermissionsResult(int requestCode, @android.support.annotation.NonNull String[] permissions, @android.support.annotation.NonNull int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @android.support.annotation.NonNull String[] permissions, @android.support.annotation.NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults); permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
} }
@Override @Override
public void onExplanationNeeded(List<String> permissionsToExplain) { public void onExplanationNeeded(List<String> permissionsToExplain) {
Toast.makeText(this, R.string.user_location_permission_explanation, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.user_location_permission_explanation, Toast.LENGTH_LONG).show();
} }
@Override @Override
@ -547,4 +542,26 @@ public class MapActivity extends BaseActivity
finish(); finish();
} }
} }
private Location getLastKnownLocation() {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = l;
}
}
return bestLocation;
}
} }