Fix: Location update in nearby activity

This commit is contained in:
maskara 2017-11-22 01:11:02 +05:30
parent 21f08ae986
commit a61124a580
7 changed files with 92 additions and 33 deletions

View file

@ -3,10 +3,14 @@ package fr.free.nrw.commons.di;
import dagger.Module; import dagger.Module;
import dagger.android.ContributesAndroidInjector; import dagger.android.ContributesAndroidInjector;
import fr.free.nrw.commons.contributions.ContributionsActivity; import fr.free.nrw.commons.contributions.ContributionsActivity;
import fr.free.nrw.commons.nearby.NearbyActivity;
@Module @Module
public abstract class ActivityBuilder { public abstract class ActivityBuilder {
@ContributesAndroidInjector() @ContributesAndroidInjector()
abstract ContributionsActivity bindSplashScreenActivity(); abstract ContributionsActivity bindSplashScreenActivity();
@ContributesAndroidInjector()
abstract NearbyActivity bindNearbyActivity();
} }

View file

@ -8,6 +8,7 @@ import javax.inject.Singleton;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import fr.free.nrw.commons.BuildConfig; import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.location.LocationServiceManager;
import fr.free.nrw.commons.mwapi.ApacheHttpClientMediaWikiApi; import fr.free.nrw.commons.mwapi.ApacheHttpClientMediaWikiApi;
import fr.free.nrw.commons.mwapi.MediaWikiApi; import fr.free.nrw.commons.mwapi.MediaWikiApi;

View file

@ -7,37 +7,35 @@ import android.location.LocationListener;
import android.location.LocationManager; import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import fr.free.nrw.commons.CommonsApplication; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.inject.Inject;
import javax.inject.Singleton;
import timber.log.Timber; import timber.log.Timber;
@Singleton
public class LocationServiceManager implements LocationListener { public class LocationServiceManager implements LocationListener {
private String provider; private String provider;
private LocationManager locationManager; private LocationManager locationManager;
private LatLng latestLocation; private LatLng lastLocation;
private Float latestLocationAccuracy; private Float latestLocationAccuracy;
private final List<LocationUpdateListener> locationListeners = new CopyOnWriteArrayList<>();
private static LocationServiceManager locationServiceManager; @Inject
public LocationServiceManager(Context context) {
private LocationServiceManager() { this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Context applicationContext = CommonsApplication.getInstance().getApplicationContext();
this.locationManager = (LocationManager) applicationContext.getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), true); provider = locationManager.getBestProvider(new Criteria(), true);
} }
public static LocationServiceManager getInstance() {
if (locationServiceManager == null) {
locationServiceManager = new LocationServiceManager();
}
return locationServiceManager;
}
public boolean isProviderEnabled() { public boolean isProviderEnabled() {
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} }
public LatLng getLatestLocation() { public LatLng getLastLocation() {
return latestLocation; return lastLocation;
} }
/** /**
@ -79,6 +77,16 @@ public class LocationServiceManager implements LocationListener {
} }
} }
public void addLocationListener(LocationUpdateListener listener) {
if (!locationListeners.contains(listener)) {
locationListeners.add(listener);
}
}
public void removeLocationListener(LocationUpdateListener listener) {
locationListeners.remove(listener);
}
@Override @Override
public void onLocationChanged(Location location) { public void onLocationChanged(Location location) {
double currentLatitude = location.getLatitude(); double currentLatitude = location.getLatitude();
@ -86,8 +94,11 @@ public class LocationServiceManager implements LocationListener {
latestLocationAccuracy = location.getAccuracy(); latestLocationAccuracy = location.getAccuracy();
Timber.d("Latitude: %f Longitude: %f Accuracy %f", Timber.d("Latitude: %f Longitude: %f Accuracy %f",
currentLatitude, currentLongitude, latestLocationAccuracy); currentLatitude, currentLongitude, latestLocationAccuracy);
lastLocation = new LatLng(currentLatitude, currentLongitude, latestLocationAccuracy);
latestLocation = new LatLng(currentLatitude, currentLongitude, latestLocationAccuracy); for (LocationUpdateListener listener : locationListeners) {
listener.onLocationChanged(lastLocation);
}
} }
@Override @Override

View file

@ -0,0 +1,5 @@
package fr.free.nrw.commons.location;
public interface LocationUpdateListener {
void onLocationChanged(LatLng latLng);
}

View file

@ -27,14 +27,19 @@ import com.google.gson.GsonBuilder;
import java.util.List; import java.util.List;
import javax.inject.Inject;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import dagger.android.AndroidInjection;
import fr.free.nrw.commons.CommonsApplication; import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
import fr.free.nrw.commons.location.LatLng; import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.location.LocationServiceManager; import fr.free.nrw.commons.location.LocationServiceManager;
import fr.free.nrw.commons.location.LocationUpdateListener;
import fr.free.nrw.commons.theme.NavigationBaseActivity; import fr.free.nrw.commons.theme.NavigationBaseActivity;
import fr.free.nrw.commons.utils.UriSerializer; import fr.free.nrw.commons.utils.UriSerializer;
import fr.free.nrw.commons.utils.ViewUtil;
import io.reactivex.Observable; import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
@ -42,14 +47,15 @@ import io.reactivex.schedulers.Schedulers;
import timber.log.Timber; import timber.log.Timber;
public class NearbyActivity extends NavigationBaseActivity { public class NearbyActivity extends NavigationBaseActivity implements LocationUpdateListener {
@BindView(R.id.progressBar) @BindView(R.id.progressBar)
ProgressBar progressBar; ProgressBar progressBar;
private static final int LOCATION_REQUEST = 1; private static final int LOCATION_REQUEST = 1;
private static final String MAP_LAST_USED_PREFERENCE = "mapLastUsed"; private static final String MAP_LAST_USED_PREFERENCE = "mapLastUsed";
private LocationServiceManager locationManager; @Inject
LocationServiceManager locationManager;
private LatLng curLatLang; private LatLng curLatLang;
private Bundle bundle; private Bundle bundle;
private SharedPreferences sharedPreferences; private SharedPreferences sharedPreferences;
@ -59,10 +65,10 @@ public class NearbyActivity extends NavigationBaseActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
AndroidInjection.inject(this);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
setContentView(R.layout.activity_nearby); setContentView(R.layout.activity_nearby);
ButterKnife.bind(this); ButterKnife.bind(this);
locationManager = LocationServiceManager.getInstance();
checkLocationPermission(); checkLocationPermission();
bundle = new Bundle(); bundle = new Bundle();
initDrawer(); initDrawer();
@ -95,7 +101,7 @@ public class NearbyActivity extends NavigationBaseActivity {
// Handle item selection // Handle item selection
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_refresh: case R.id.action_refresh:
refreshView(); refreshView(true);
return true; return true;
case R.id.action_toggle_view: case R.id.action_toggle_view:
viewMode = viewMode.toggle(); viewMode = viewMode.toggle();
@ -107,16 +113,11 @@ public class NearbyActivity extends NavigationBaseActivity {
} }
} }
private void startLookingForNearby() {
curLatLang = locationManager.getLatestLocation();
setupPlaceList(getBaseContext());
}
private void checkLocationPermission() { private void checkLocationPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
startLookingForNearby(); refreshView(false);
} else { } else {
if (ContextCompat.checkSelfPermission(this, if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) Manifest.permission.ACCESS_FINE_LOCATION)
@ -157,7 +158,7 @@ public class NearbyActivity extends NavigationBaseActivity {
} }
} }
} else { } else {
startLookingForNearby(); refreshView(false);
} }
} }
@ -166,7 +167,7 @@ public class NearbyActivity extends NavigationBaseActivity {
switch (requestCode) { switch (requestCode) {
case LOCATION_REQUEST: { case LOCATION_REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startLookingForNearby(); refreshView(false);
} 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
if (progressBar != null) { if (progressBar != null) {
@ -222,7 +223,7 @@ public class NearbyActivity extends NavigationBaseActivity {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) { if (requestCode == 1) {
Timber.d("User is back from Settings page"); Timber.d("User is back from Settings page");
refreshView(); refreshView(false);
} }
} }
@ -239,11 +240,13 @@ public class NearbyActivity extends NavigationBaseActivity {
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
locationManager.registerLocationManager(); locationManager.registerLocationManager();
locationManager.addLocationListener(this);
} }
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
locationManager.removeLocationListener(this);
locationManager.unregisterLocationManager(); locationManager.unregisterLocationManager();
} }
@ -259,13 +262,24 @@ public class NearbyActivity extends NavigationBaseActivity {
checkGps(); checkGps();
} }
private void refreshView() { private void refreshView(boolean isToastRequired) {
curLatLang = locationManager.getLatestLocation(); LatLng lastLocation = locationManager.getLastLocation();
if (curLatLang != null && curLatLang.equals(lastLocation)) { //refresh view only if location has changed
if (isToastRequired) {
ViewUtil.showLongToast(this, R.string.nearby_location_has_not_changed);
}
return;
}
curLatLang = lastLocation;
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);
setupPlaceList(getBaseContext()); setupPlaceList(this);
} }
private void setupPlaceList(Context context) { private void setupPlaceList(Context context) {
if (curLatLang == null) {
Timber.d("Skipping update of nearby places as location is unavailable");
return;
}
placesDisposable = Observable.fromCallable(() -> NearbyController placesDisposable = Observable.fromCallable(() -> NearbyController
.loadAttractionsFromLocation(curLatLang, CommonsApplication.getInstance())) .loadAttractionsFromLocation(curLatLang, CommonsApplication.getInstance()))
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
@ -330,4 +344,9 @@ public class NearbyActivity extends NavigationBaseActivity {
Intent settingsIntent = new Intent(context, NearbyActivity.class); Intent settingsIntent = new Intent(context, NearbyActivity.class);
context.startActivity(settingsIntent); context.startActivity(settingsIntent);
} }
@Override
public void onLocationChanged(LatLng latLng) {
refreshView(false);
}
} }

View file

@ -0,0 +1,17 @@
package fr.free.nrw.commons.utils;
import android.content.Context;
import android.support.annotation.StringRes;
import android.widget.Toast;
public class ViewUtil {
public static void showLongToast(final Context context, @StringRes final int stringResId) {
ExecutorUtils.uiExecutor().execute(new Runnable() {
@Override
public void run() {
Toast.makeText(context, context.getString(stringResId), Toast.LENGTH_LONG).show();
}
});
}
}

View file

@ -210,4 +210,6 @@ Tap this message (or hit back) to skip this step.</string>
<string name="send_log_file">Send log file</string> <string name="send_log_file">Send log file</string>
<string name="send_log_file_description">Send log file to developers via email</string> <string name="send_log_file_description">Send log file to developers via email</string>
<string name="login_to_your_account">Login to your account</string> <string name="login_to_your_account">Login to your account</string>
<string name="nearby_location_has_not_changed">Location has not changed.</string>
</resources> </resources>