mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Fix: Location update in nearby activity
This commit is contained in:
parent
21f08ae986
commit
a61124a580
7 changed files with 92 additions and 33 deletions
|
|
@ -3,10 +3,14 @@ package fr.free.nrw.commons.di;
|
|||
import dagger.Module;
|
||||
import dagger.android.ContributesAndroidInjector;
|
||||
import fr.free.nrw.commons.contributions.ContributionsActivity;
|
||||
import fr.free.nrw.commons.nearby.NearbyActivity;
|
||||
|
||||
@Module
|
||||
public abstract class ActivityBuilder {
|
||||
|
||||
@ContributesAndroidInjector()
|
||||
abstract ContributionsActivity bindSplashScreenActivity();
|
||||
|
||||
@ContributesAndroidInjector()
|
||||
abstract NearbyActivity bindNearbyActivity();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import javax.inject.Singleton;
|
|||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
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.MediaWikiApi;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,37 +7,35 @@ import android.location.LocationListener;
|
|||
import android.location.LocationManager;
|
||||
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;
|
||||
|
||||
@Singleton
|
||||
public class LocationServiceManager implements LocationListener {
|
||||
|
||||
private String provider;
|
||||
private LocationManager locationManager;
|
||||
private LatLng latestLocation;
|
||||
private LatLng lastLocation;
|
||||
private Float latestLocationAccuracy;
|
||||
private final List<LocationUpdateListener> locationListeners = new CopyOnWriteArrayList<>();
|
||||
|
||||
private static LocationServiceManager locationServiceManager;
|
||||
|
||||
private LocationServiceManager() {
|
||||
Context applicationContext = CommonsApplication.getInstance().getApplicationContext();
|
||||
this.locationManager = (LocationManager) applicationContext.getSystemService(Context.LOCATION_SERVICE);
|
||||
@Inject
|
||||
public LocationServiceManager(Context context) {
|
||||
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
||||
provider = locationManager.getBestProvider(new Criteria(), true);
|
||||
}
|
||||
|
||||
public static LocationServiceManager getInstance() {
|
||||
if (locationServiceManager == null) {
|
||||
locationServiceManager = new LocationServiceManager();
|
||||
}
|
||||
return locationServiceManager;
|
||||
}
|
||||
|
||||
public boolean isProviderEnabled() {
|
||||
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
|
||||
}
|
||||
|
||||
public LatLng getLatestLocation() {
|
||||
return latestLocation;
|
||||
public LatLng getLastLocation() {
|
||||
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
|
||||
public void onLocationChanged(Location location) {
|
||||
double currentLatitude = location.getLatitude();
|
||||
|
|
@ -86,8 +94,11 @@ public class LocationServiceManager implements LocationListener {
|
|||
latestLocationAccuracy = location.getAccuracy();
|
||||
Timber.d("Latitude: %f Longitude: %f Accuracy %f",
|
||||
currentLatitude, currentLongitude, latestLocationAccuracy);
|
||||
lastLocation = new LatLng(currentLatitude, currentLongitude, latestLocationAccuracy);
|
||||
|
||||
latestLocation = new LatLng(currentLatitude, currentLongitude, latestLocationAccuracy);
|
||||
for (LocationUpdateListener listener : locationListeners) {
|
||||
listener.onLocationChanged(lastLocation);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
package fr.free.nrw.commons.location;
|
||||
|
||||
public interface LocationUpdateListener {
|
||||
void onLocationChanged(LatLng latLng);
|
||||
}
|
||||
|
|
@ -27,14 +27,19 @@ import com.google.gson.GsonBuilder;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import dagger.android.AndroidInjection;
|
||||
import fr.free.nrw.commons.CommonsApplication;
|
||||
import fr.free.nrw.commons.R;
|
||||
import fr.free.nrw.commons.location.LatLng;
|
||||
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.utils.UriSerializer;
|
||||
import fr.free.nrw.commons.utils.ViewUtil;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
|
|
@ -42,14 +47,15 @@ import io.reactivex.schedulers.Schedulers;
|
|||
import timber.log.Timber;
|
||||
|
||||
|
||||
public class NearbyActivity extends NavigationBaseActivity {
|
||||
public class NearbyActivity extends NavigationBaseActivity implements LocationUpdateListener {
|
||||
|
||||
@BindView(R.id.progressBar)
|
||||
ProgressBar progressBar;
|
||||
private static final int LOCATION_REQUEST = 1;
|
||||
private static final String MAP_LAST_USED_PREFERENCE = "mapLastUsed";
|
||||
|
||||
private LocationServiceManager locationManager;
|
||||
@Inject
|
||||
LocationServiceManager locationManager;
|
||||
private LatLng curLatLang;
|
||||
private Bundle bundle;
|
||||
private SharedPreferences sharedPreferences;
|
||||
|
|
@ -59,10 +65,10 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
AndroidInjection.inject(this);
|
||||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
||||
setContentView(R.layout.activity_nearby);
|
||||
ButterKnife.bind(this);
|
||||
locationManager = LocationServiceManager.getInstance();
|
||||
checkLocationPermission();
|
||||
bundle = new Bundle();
|
||||
initDrawer();
|
||||
|
|
@ -95,7 +101,7 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
// Handle item selection
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_refresh:
|
||||
refreshView();
|
||||
refreshView(true);
|
||||
return true;
|
||||
case R.id.action_toggle_view:
|
||||
viewMode = viewMode.toggle();
|
||||
|
|
@ -107,16 +113,11 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
}
|
||||
}
|
||||
|
||||
private void startLookingForNearby() {
|
||||
curLatLang = locationManager.getLatestLocation();
|
||||
setupPlaceList(getBaseContext());
|
||||
}
|
||||
|
||||
private void checkLocationPermission() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
if (ContextCompat.checkSelfPermission(this,
|
||||
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||
startLookingForNearby();
|
||||
refreshView(false);
|
||||
} else {
|
||||
if (ContextCompat.checkSelfPermission(this,
|
||||
Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
|
|
@ -157,7 +158,7 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
startLookingForNearby();
|
||||
refreshView(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -166,7 +167,7 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
switch (requestCode) {
|
||||
case LOCATION_REQUEST: {
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
startLookingForNearby();
|
||||
refreshView(false);
|
||||
} else {
|
||||
//If permission not granted, go to page that says Nearby Places cannot be displayed
|
||||
if (progressBar != null) {
|
||||
|
|
@ -222,7 +223,7 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == 1) {
|
||||
Timber.d("User is back from Settings page");
|
||||
refreshView();
|
||||
refreshView(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -239,11 +240,13 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
protected void onStart() {
|
||||
super.onStart();
|
||||
locationManager.registerLocationManager();
|
||||
locationManager.addLocationListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
locationManager.removeLocationListener(this);
|
||||
locationManager.unregisterLocationManager();
|
||||
}
|
||||
|
||||
|
|
@ -259,13 +262,24 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
checkGps();
|
||||
}
|
||||
|
||||
private void refreshView() {
|
||||
curLatLang = locationManager.getLatestLocation();
|
||||
private void refreshView(boolean isToastRequired) {
|
||||
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);
|
||||
setupPlaceList(getBaseContext());
|
||||
setupPlaceList(this);
|
||||
}
|
||||
|
||||
private void setupPlaceList(Context context) {
|
||||
if (curLatLang == null) {
|
||||
Timber.d("Skipping update of nearby places as location is unavailable");
|
||||
return;
|
||||
}
|
||||
placesDisposable = Observable.fromCallable(() -> NearbyController
|
||||
.loadAttractionsFromLocation(curLatLang, CommonsApplication.getInstance()))
|
||||
.subscribeOn(Schedulers.io())
|
||||
|
|
@ -330,4 +344,9 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
Intent settingsIntent = new Intent(context, NearbyActivity.class);
|
||||
context.startActivity(settingsIntent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(LatLng latLng) {
|
||||
refreshView(false);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
17
app/src/main/java/fr/free/nrw/commons/utils/ViewUtil.java
Normal file
17
app/src/main/java/fr/free/nrw/commons/utils/ViewUtil.java
Normal 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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -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_description">Send log file to developers via email</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>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue