mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Merge pull request #975 from maskaravivek/locationUpdate
Fix: Location update in nearby activity
This commit is contained in:
commit
9888a07d90
7 changed files with 115 additions and 38 deletions
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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 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,27 +47,29 @@ 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;
|
||||||
private NearbyActivityMode viewMode;
|
private NearbyActivityMode viewMode;
|
||||||
private Disposable placesDisposable;
|
private Disposable placesDisposable;
|
||||||
|
private boolean lockNearbyView; //Determines if the nearby places needs to be refreshed
|
||||||
|
|
||||||
@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 +102,8 @@ 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();
|
lockNearbyView = false;
|
||||||
|
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 +115,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 +160,7 @@ public class NearbyActivity extends NavigationBaseActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
startLookingForNearby();
|
refreshView(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -166,13 +169,10 @@ 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) {
|
hideProgressBar();
|
||||||
progressBar.setVisibility(View.GONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
showLocationPermissionDeniedErrorDialog();
|
showLocationPermissionDeniedErrorDialog();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -222,7 +222,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,30 +239,51 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
placesDisposable.dispose();
|
if (placesDisposable != null) {
|
||||||
|
placesDisposable.dispose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
lockNearbyView = false;
|
||||||
checkGps();
|
checkGps();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void refreshView() {
|
private void refreshView(boolean isHardRefresh) {
|
||||||
curLatLang = locationManager.getLatestLocation();
|
if (lockNearbyView) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LatLng lastLocation = locationManager.getLastLocation();
|
||||||
|
if (curLatLang != null && curLatLang.equals(lastLocation)) { //refresh view only if location has changed
|
||||||
|
if (isHardRefresh) {
|
||||||
|
ViewUtil.showLongToast(this, R.string.nearby_location_has_not_changed);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
curLatLang = lastLocation;
|
||||||
|
|
||||||
|
if (curLatLang == null) {
|
||||||
|
Timber.d("Skipping update of nearby places as location is unavailable");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
progressBar.setVisibility(View.VISIBLE);
|
progressBar.setVisibility(View.VISIBLE);
|
||||||
setupPlaceList(getBaseContext());
|
setupPlaceList(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupPlaceList(Context context) {
|
private void setupPlaceList(Context context) {
|
||||||
|
|
@ -292,6 +313,7 @@ public class NearbyActivity extends NavigationBaseActivity {
|
||||||
bundle.putString("PlaceList", gsonPlaceList);
|
bundle.putString("PlaceList", gsonPlaceList);
|
||||||
bundle.putString("CurLatLng", gsonCurLatLng);
|
bundle.putString("CurLatLng", gsonCurLatLng);
|
||||||
|
|
||||||
|
lockNearbyView = true;
|
||||||
// Begin the transaction
|
// Begin the transaction
|
||||||
if (viewMode.isMap()) {
|
if (viewMode.isMap()) {
|
||||||
setMapFragment();
|
setMapFragment();
|
||||||
|
|
@ -299,6 +321,10 @@ public class NearbyActivity extends NavigationBaseActivity {
|
||||||
setListFragment();
|
setListFragment();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hideProgressBar();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void hideProgressBar() {
|
||||||
if (progressBar != null) {
|
if (progressBar != null) {
|
||||||
progressBar.setVisibility(View.GONE);
|
progressBar.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
|
@ -325,4 +351,14 @@ public class NearbyActivity extends NavigationBaseActivity {
|
||||||
fragmentTransaction.replace(R.id.container, fragment);
|
fragmentTransaction.replace(R.id.container, fragment);
|
||||||
fragmentTransaction.commitAllowingStateLoss();
|
fragmentTransaction.commitAllowingStateLoss();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void startYourself(Context context) {
|
||||||
|
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,7 @@ 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>
|
||||||
|
<string name="nearby_location_not_available">Location not available.</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue