mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +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.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,27 +47,29 @@ 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;
|
||||
private NearbyActivityMode viewMode;
|
||||
private Disposable placesDisposable;
|
||||
private boolean lockNearbyView; //Determines if the nearby places needs to be refreshed
|
||||
|
||||
@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 +102,8 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
// Handle item selection
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_refresh:
|
||||
refreshView();
|
||||
lockNearbyView = false;
|
||||
refreshView(true);
|
||||
return true;
|
||||
case R.id.action_toggle_view:
|
||||
viewMode = viewMode.toggle();
|
||||
|
|
@ -107,16 +115,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 +160,7 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
startLookingForNearby();
|
||||
refreshView(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -166,13 +169,10 @@ 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) {
|
||||
progressBar.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
hideProgressBar();
|
||||
showLocationPermissionDeniedErrorDialog();
|
||||
}
|
||||
}
|
||||
|
|
@ -222,7 +222,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,30 +239,51 @@ 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();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
placesDisposable.dispose();
|
||||
if (placesDisposable != null) {
|
||||
placesDisposable.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
lockNearbyView = false;
|
||||
checkGps();
|
||||
}
|
||||
|
||||
private void refreshView() {
|
||||
curLatLang = locationManager.getLatestLocation();
|
||||
private void refreshView(boolean isHardRefresh) {
|
||||
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);
|
||||
setupPlaceList(getBaseContext());
|
||||
setupPlaceList(this);
|
||||
}
|
||||
|
||||
private void setupPlaceList(Context context) {
|
||||
|
|
@ -292,6 +313,7 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
bundle.putString("PlaceList", gsonPlaceList);
|
||||
bundle.putString("CurLatLng", gsonCurLatLng);
|
||||
|
||||
lockNearbyView = true;
|
||||
// Begin the transaction
|
||||
if (viewMode.isMap()) {
|
||||
setMapFragment();
|
||||
|
|
@ -299,6 +321,10 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
setListFragment();
|
||||
}
|
||||
|
||||
hideProgressBar();
|
||||
}
|
||||
|
||||
private void hideProgressBar() {
|
||||
if (progressBar != null) {
|
||||
progressBar.setVisibility(View.GONE);
|
||||
}
|
||||
|
|
@ -325,4 +351,14 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
fragmentTransaction.replace(R.id.container, fragment);
|
||||
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_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>
|
||||
<string name="nearby_location_not_available">Location not available.</string>
|
||||
</resources>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue