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