Added getLastKnownLocation method and hooked it up to refreshView

This commit is contained in:
misaochan 2018-04-14 20:24:40 +10:00
parent c4a00e9641
commit cce7ac6770
2 changed files with 25 additions and 5 deletions

View file

@ -79,6 +79,16 @@ public class LocationServiceManager implements LocationListener {
Manifest.permission.ACCESS_FINE_LOCATION);
}
public LatLng getLKN() {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Location lastKN = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng lastLatLng = LatLng.from(lastKN);
return lastLatLng;
} else {
return null;
}
}
public LatLng getLastLocation() {
if (lastLocation == null) {
return null;
@ -251,6 +261,7 @@ public class LocationServiceManager implements LocationListener {
public enum LocationChangeType{
LOCATION_SIGNIFICANTLY_CHANGED, //Went out of borders of nearby markers
LOCATION_SLIGHTLY_CHANGED, //User might be walking or driving
LOCATION_NOT_CHANGED
LOCATION_NOT_CHANGED,
PERMISSION_JUST_GRANTED
}
}

View file

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.location.Location;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
@ -81,6 +82,7 @@ public class NearbyActivity extends NavigationBaseActivity implements LocationUp
private final String NETWORK_INTENT_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
private BroadcastReceiver broadcastReceiver;
private LatLng lastKN;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -162,7 +164,8 @@ public class NearbyActivity extends NavigationBaseActivity implements LocationUp
lockNearbyView = false;
checkGps();
addNetworkBroadcastReceiver();
refreshView(LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED);
lastKN = locationManager.getLKN();
refreshView(LocationServiceManager.LocationChangeType.PERMISSION_JUST_GRANTED);
} else {
//If permission not granted, go to page that says Nearby Places cannot be displayed
hideProgressBar();
@ -346,6 +349,11 @@ public class NearbyActivity extends NavigationBaseActivity implements LocationUp
locationManager.registerLocationManager();
LatLng lastLocation = locationManager.getLastLocation();
if (locationChangeType.equals(LocationServiceManager.LocationChangeType.PERMISSION_JUST_GRANTED)) {
lastLocation = lastKN;
Timber.d("Permission was just granted, lastKnownLocation is " + lastKN.toString());
}
if (curLatLang != null && curLatLang.equals(lastLocation)) { //refresh view only if location has changed
return;
}
@ -356,15 +364,16 @@ public class NearbyActivity extends NavigationBaseActivity implements LocationUp
return;
}
if (locationChangeType
.equals(LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED)) {
if (locationChangeType.equals(LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED)
|| locationChangeType.equals(LocationServiceManager.LocationChangeType.PERMISSION_JUST_GRANTED)) {
progressBar.setVisibility(View.VISIBLE);
placesDisposable = Observable.fromCallable(() -> nearbyController
.loadAttractionsFromLocation(curLatLang))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::populatePlaces);
} else if (locationChangeType
}
else if (locationChangeType
.equals(LocationServiceManager.LocationChangeType.LOCATION_SLIGHTLY_CHANGED)) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriSerializer())