From 427fda5f8149b53843710a9359ab94160fdcd69a Mon Sep 17 00:00:00 2001 From: Alexander Marchuk Date: Fri, 1 Apr 2016 12:33:59 +0300 Subject: [PATCH 1/3] [new downloader][android] fix: Obtaining last known location regardless of "My position" button state. --- .../maps/downloader/MigrationController.java | 2 +- .../maps/downloader/MigrationFragment.java | 2 +- .../maps/location/AndroidNativeProvider.java | 11 +++++----- .../maps/location/LocationHelper.java | 20 +++++++++++++++++++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/android/src/com/mapswithme/maps/downloader/MigrationController.java b/android/src/com/mapswithme/maps/downloader/MigrationController.java index 581047772a..23e07a823b 100644 --- a/android/src/com/mapswithme/maps/downloader/MigrationController.java +++ b/android/src/com/mapswithme/maps/downloader/MigrationController.java @@ -149,7 +149,7 @@ final class MigrationController if (mState == State.PROGRESS) return; - Location loc = LocationHelper.INSTANCE.getLastLocation(); + Location loc = LocationHelper.INSTANCE.getLastKnownLocation(); double lat = (loc == null ? 0.0 : loc.getLatitude()); double lon = (loc == null ? 0.0 : loc.getLongitude()); diff --git a/android/src/com/mapswithme/maps/downloader/MigrationFragment.java b/android/src/com/mapswithme/maps/downloader/MigrationFragment.java index df90d12407..3cde2c0988 100644 --- a/android/src/com/mapswithme/maps/downloader/MigrationFragment.java +++ b/android/src/com/mapswithme/maps/downloader/MigrationFragment.java @@ -153,7 +153,7 @@ public class MigrationFragment extends BaseMwmFragment UiUtils.show(mButtonPrimary); UiUtils.hide(mPrepare, mProgress, mError); - Location loc = LocationHelper.INSTANCE.getLastLocation(); + Location loc = LocationHelper.INSTANCE.getLastKnownLocation(); UiUtils.showIf(loc != null, mButtonSecondary); } diff --git a/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java b/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java index 0466e30fd8..42310c7885 100644 --- a/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java +++ b/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java @@ -4,6 +4,7 @@ package com.mapswithme.maps.location; import android.content.Context; import android.location.Location; import android.location.LocationManager; +import android.support.annotation.Nullable; import java.util.ArrayList; import java.util.List; @@ -29,7 +30,7 @@ public class AndroidNativeProvider extends BaseLocationProvider final List providers = getFilteredProviders(); - if (providers.size() == 0) + if (providers.isEmpty()) LocationHelper.INSTANCE.notifyLocationError(LocationHelper.ERROR_DENIED); else { @@ -39,7 +40,7 @@ public class AndroidNativeProvider extends BaseLocationProvider LocationHelper.INSTANCE.registerSensorListeners(); - final Location newLocation = findBestNotExpiredLocation(providers); + final Location newLocation = findBestNotExpiredLocation(providers, LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_SHORT); if (isLocationBetterThanLast(newLocation)) LocationHelper.INSTANCE.setLastLocation(newLocation); else @@ -59,13 +60,13 @@ public class AndroidNativeProvider extends BaseLocationProvider mIsActive = false; } - private Location findBestNotExpiredLocation(List providers) + @Nullable Location findBestNotExpiredLocation(List providers, long expiration) { Location res = null; for (final String pr : providers) { final Location l = mLocationManager.getLastKnownLocation(pr); - if (l != null && !LocationUtils.isExpired(l, l.getTime(), LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_SHORT)) + if (l != null && !LocationUtils.isExpired(l, l.getTime(), expiration)) { if (res == null || res.getAccuracy() > l.getAccuracy()) res = l; @@ -74,7 +75,7 @@ public class AndroidNativeProvider extends BaseLocationProvider return res; } - private List getFilteredProviders() + List getFilteredProviders() { final List allProviders = mLocationManager.getProviders(false); final List acceptedProviders = new ArrayList<>(allProviders.size()); diff --git a/android/src/com/mapswithme/maps/location/LocationHelper.java b/android/src/com/mapswithme/maps/location/LocationHelper.java index cb7e846d22..6ed23d1691 100644 --- a/android/src/com/mapswithme/maps/location/LocationHelper.java +++ b/android/src/com/mapswithme/maps/location/LocationHelper.java @@ -15,6 +15,8 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.text.TextUtils; +import java.util.List; + import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; import com.mapswithme.maps.LocationState; @@ -338,6 +340,24 @@ public enum LocationHelper implements SensorEventListener location.getBearing()); } + /** + * Obtains last known location regardless of "My position" button state. + * @return {@code null} on failure. + */ + public @Nullable Location getLastKnownLocation() + { + if (mLastLocation != null) + return mLastLocation; + + AndroidNativeProvider provider = new AndroidNativeProvider(); + List providers = provider.getFilteredProviders(); + + if (providers.isEmpty()) + return null; + + return provider.findBestNotExpiredLocation(providers, LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_LONG); + } + public static native void nativeOnLocationError(int errorCode); private static native void nativeLocationUpdated(long time, double lat, double lon, float accuracy, double altitude, float speed, float bearing); private static native float[] nativeUpdateCompassSensor(int ind, float[] arr); From d94064716ae1d5ffc8dcaf589eeb8b39ae33b850 Mon Sep 17 00:00:00 2001 From: Alexander Marchuk Date: Fri, 1 Apr 2016 13:19:56 +0300 Subject: [PATCH 2/3] [new downloader][android] fix: Location retrieving methods renamed, javadoc added. fix: Review fixes. --- .../maps/DownloadResourcesActivity.java | 2 +- .../src/com/mapswithme/maps/MwmActivity.java | 2 +- .../maps/bookmarks/BookmarkListAdapter.java | 2 +- .../downloader/CountrySuggestFragment.java | 2 +- .../maps/downloader/DownloaderAdapter.java | 2 +- .../maps/downloader/OnmapDownloader.java | 2 +- .../maps/location/AndroidNativeProvider.java | 12 +++---- .../maps/location/BaseLocationProvider.java | 4 +-- .../location/GoogleFusedLocationProvider.java | 2 +- .../maps/location/LocationHelper.java | 31 +++++++++++-------- .../maps/routing/NavigationController.java | 2 +- .../widget/placepage/DirectionFragment.java | 2 +- .../maps/widget/placepage/PlacePageView.java | 4 +-- .../com/mapswithme/util/LocationUtils.java | 2 +- .../com/mapswithme/util/ThemeSwitcher.java | 2 +- android/src/com/mapswithme/util/Yota.java | 2 +- 16 files changed, 40 insertions(+), 35 deletions(-) diff --git a/android/src/com/mapswithme/maps/DownloadResourcesActivity.java b/android/src/com/mapswithme/maps/DownloadResourcesActivity.java index 1f2c0e1298..553c9c011c 100644 --- a/android/src/com/mapswithme/maps/DownloadResourcesActivity.java +++ b/android/src/com/mapswithme/maps/DownloadResourcesActivity.java @@ -624,7 +624,7 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity mMapTaskToForward = new MwmActivity.ShowCountryTask(countryId, autoDownload); org.alohalytics.Statistics.logEvent("OpenCountryTaskProcessor::process", new String[] { "autoDownload", String.valueOf(autoDownload) }, - LocationHelper.INSTANCE.getLastLocation()); + LocationHelper.INSTANCE.getSavedLocation()); return true; } } diff --git a/android/src/com/mapswithme/maps/MwmActivity.java b/android/src/com/mapswithme/maps/MwmActivity.java index 4c722da5dc..bb91b5502c 100644 --- a/android/src/com/mapswithme/maps/MwmActivity.java +++ b/android/src/com/mapswithme/maps/MwmActivity.java @@ -293,7 +293,7 @@ public class MwmActivity extends BaseMwmFragmentActivity private void shareMyLocation() { - final Location loc = LocationHelper.INSTANCE.getLastLocation(); + final Location loc = LocationHelper.INSTANCE.getSavedLocation(); if (loc != null) { final String geoUrl = Framework.nativeGetGe0Url(loc.getLatitude(), loc.getLongitude(), Framework.nativeGetDrawScale(), ""); diff --git a/android/src/com/mapswithme/maps/bookmarks/BookmarkListAdapter.java b/android/src/com/mapswithme/maps/bookmarks/BookmarkListAdapter.java index d1f39f3b3f..6294837d81 100644 --- a/android/src/com/mapswithme/maps/bookmarks/BookmarkListAdapter.java +++ b/android/src/com/mapswithme/maps/bookmarks/BookmarkListAdapter.java @@ -173,7 +173,7 @@ public class BookmarkListAdapter extends BaseAdapter void setDistance(Bookmark bmk) { - final Location loc = LocationHelper.INSTANCE.getLastLocation(); + final Location loc = LocationHelper.INSTANCE.getSavedLocation(); if (loc != null) { final DistanceAndAzimut daa = bmk.getDistanceAndAzimuth(loc.getLatitude(), loc.getLongitude(), 0.0); diff --git a/android/src/com/mapswithme/maps/downloader/CountrySuggestFragment.java b/android/src/com/mapswithme/maps/downloader/CountrySuggestFragment.java index efc459f8ad..384cc5da9c 100644 --- a/android/src/com/mapswithme/maps/downloader/CountrySuggestFragment.java +++ b/android/src/com/mapswithme/maps/downloader/CountrySuggestFragment.java @@ -114,7 +114,7 @@ public class CountrySuggestFragment extends BaseMwmFragment implements View.OnCl { super.onResume(); - Location loc = LocationHelper.INSTANCE.getLastLocation(); + Location loc = LocationHelper.INSTANCE.getSavedLocation(); if (loc != null) { String id = MapManager.nativeFindCountry(loc.getLatitude(), loc.getLongitude()); diff --git a/android/src/com/mapswithme/maps/downloader/DownloaderAdapter.java b/android/src/com/mapswithme/maps/downloader/DownloaderAdapter.java index 4801b5cef9..36553db256 100644 --- a/android/src/com/mapswithme/maps/downloader/DownloaderAdapter.java +++ b/android/src/com/mapswithme/maps/downloader/DownloaderAdapter.java @@ -618,7 +618,7 @@ class DownloaderAdapter extends RecyclerView.Adapter providers, long expiration) + @Nullable Location findBestNotExpiredLocation(List providers, long expirationMs) { Location res = null; for (final String pr : providers) { final Location l = mLocationManager.getLastKnownLocation(pr); - if (l != null && !LocationUtils.isExpired(l, l.getTime(), expiration)) + if (l != null && !LocationUtils.isExpired(l, l.getTime(), expirationMs)) { if (res == null || res.getAccuracy() > l.getAccuracy()) res = l; diff --git a/android/src/com/mapswithme/maps/location/BaseLocationProvider.java b/android/src/com/mapswithme/maps/location/BaseLocationProvider.java index 6cd6a6b4d1..5b325e8870 100644 --- a/android/src/com/mapswithme/maps/location/BaseLocationProvider.java +++ b/android/src/com/mapswithme/maps/location/BaseLocationProvider.java @@ -32,7 +32,7 @@ abstract class BaseLocationProvider implements LocationListener if (newLocation == null) return false; - final Location lastLocation = LocationHelper.INSTANCE.getLastLocation(); + final Location lastLocation = LocationHelper.INSTANCE.getSavedLocation(); return (lastLocation == null || isLocationBetterThanLast(newLocation, lastLocation)); } @@ -46,7 +46,7 @@ abstract class BaseLocationProvider implements LocationListener if (isLocationBetterThanLast(location)) { LocationHelper.INSTANCE.initMagneticField(location); - LocationHelper.INSTANCE.setLastLocation(location); + LocationHelper.INSTANCE.saveLocation(location); } } diff --git a/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java b/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java index 0772736aa0..8fcdc1f99c 100644 --- a/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java +++ b/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java @@ -71,7 +71,7 @@ public class GoogleFusedLocationProvider extends BaseLocationProvider LocationHelper.INSTANCE.registerSensorListeners(); final Location l = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if (l != null) - LocationHelper.INSTANCE.setLastLocation(l); + LocationHelper.INSTANCE.saveLocation(l); } @Override diff --git a/android/src/com/mapswithme/maps/location/LocationHelper.java b/android/src/com/mapswithme/maps/location/LocationHelper.java index 6ed23d1691..4571b55f94 100644 --- a/android/src/com/mapswithme/maps/location/LocationHelper.java +++ b/android/src/com/mapswithme/maps/location/LocationHelper.java @@ -69,7 +69,7 @@ public enum LocationHelper implements SensorEventListener private boolean mActive; - private Location mLastLocation; + private Location mSavedLocation; private MapObject mMyPosition; private long mLastLocationTime; @@ -175,22 +175,27 @@ public enum LocationHelper implements SensorEventListener return null; } - if (mLastLocation == null) + if (mSavedLocation == null) return null; if (mMyPosition == null) - mMyPosition = new MapObject(MapObject.MY_POSITION, "", "", "", mLastLocation.getLatitude(), mLastLocation.getLongitude(), ""); + mMyPosition = new MapObject(MapObject.MY_POSITION, "", "", "", mSavedLocation.getLatitude(), mSavedLocation.getLongitude(), ""); return mMyPosition; } - public Location getLastLocation() { return mLastLocation; } + /** + *

Obtains last known saved location. It depends on "My position" button state and is cleared on "No position" one. + *

If you need the location regardless of the button's state, use {@link #getLastKnownLocation()}. + * @return {@code null} if no location is saved or "My position" button is in "No position" state. + */ + public Location getSavedLocation() { return mSavedLocation; } - public long getLastLocationTime() { return mLastLocationTime; } + public long getSavedLocationTime() { return mLastLocationTime; } - public void setLastLocation(@NonNull Location loc) + public void saveLocation(@NonNull Location loc) { - mLastLocation = loc; + mSavedLocation = loc; mMyPosition = null; mLastLocationTime = System.currentTimeMillis(); notifyLocationUpdated(); @@ -198,11 +203,11 @@ public enum LocationHelper implements SensorEventListener protected void notifyLocationUpdated() { - if (mLastLocation == null) + if (mSavedLocation == null) return; for (LocationListener listener : mListeners) - listener.onLocationUpdated(mLastLocation); + listener.onLocationUpdated(mSavedLocation); mListeners.finishIterate(); } @@ -312,8 +317,8 @@ public enum LocationHelper implements SensorEventListener if (mSensorManager != null) { // Recreate magneticField if location has changed significantly - if (mMagneticField == null || mLastLocation == null || - newLocation.distanceTo(mLastLocation) > DISTANCE_TO_RECREATE_MAGNETIC_FIELD_M) + if (mMagneticField == null || mSavedLocation == null || + newLocation.distanceTo(mSavedLocation) > DISTANCE_TO_RECREATE_MAGNETIC_FIELD_M) mMagneticField = new GeomagneticField((float) newLocation.getLatitude(), (float) newLocation.getLongitude(), (float) newLocation.getAltitude(), newLocation.getTime()); } @@ -346,8 +351,8 @@ public enum LocationHelper implements SensorEventListener */ public @Nullable Location getLastKnownLocation() { - if (mLastLocation != null) - return mLastLocation; + if (mSavedLocation != null) + return mSavedLocation; AndroidNativeProvider provider = new AndroidNativeProvider(); List providers = provider.getFilteredProviders(); diff --git a/android/src/com/mapswithme/maps/routing/NavigationController.java b/android/src/com/mapswithme/maps/routing/NavigationController.java index 8aefb1d478..9d7691a183 100644 --- a/android/src/com/mapswithme/maps/routing/NavigationController.java +++ b/android/src/com/mapswithme/maps/routing/NavigationController.java @@ -86,7 +86,7 @@ public class NavigationController private void updatePedestrian(RoutingInfo info) { Location next = info.pedestrianNextDirection; - Location location = LocationHelper.INSTANCE.getLastLocation(); + Location location = LocationHelper.INSTANCE.getSavedLocation(); DistanceAndAzimut da = Framework.nativeGetDistanceAndAzimuthFromLatLon(next.getLatitude(), next.getLongitude(), location.getLatitude(), location.getLongitude(), mNorth); diff --git a/android/src/com/mapswithme/maps/widget/placepage/DirectionFragment.java b/android/src/com/mapswithme/maps/widget/placepage/DirectionFragment.java index 8059e7caee..39ce5ba9bd 100644 --- a/android/src/com/mapswithme/maps/widget/placepage/DirectionFragment.java +++ b/android/src/com/mapswithme/maps/widget/placepage/DirectionFragment.java @@ -132,7 +132,7 @@ public class DirectionFragment extends BaseMwmDialogFragment implements Location @Override public void onCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy) { - final Location last = LocationHelper.INSTANCE.getLastLocation(); + final Location last = LocationHelper.INSTANCE.getSavedLocation(); if (last == null || mMapObject == null) return; diff --git a/android/src/com/mapswithme/maps/widget/placepage/PlacePageView.java b/android/src/com/mapswithme/maps/widget/placepage/PlacePageView.java index aebd343d8d..ae85e5ea6b 100644 --- a/android/src/com/mapswithme/maps/widget/placepage/PlacePageView.java +++ b/android/src/com/mapswithme/maps/widget/placepage/PlacePageView.java @@ -378,7 +378,7 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene refreshPreview(); refreshDetails(); - final Location loc = LocationHelper.INSTANCE.getLastLocation(); + final Location loc = LocationHelper.INSTANCE.getSavedLocation(); switch (mMapObject.getMapObjectType()) { @@ -610,7 +610,7 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene MapObject.isOfType(MapObject.MY_POSITION, mMapObject)) return; - final Location location = LocationHelper.INSTANCE.getLastLocation(); + final Location location = LocationHelper.INSTANCE.getSavedLocation(); if (location == null) return; diff --git a/android/src/com/mapswithme/util/LocationUtils.java b/android/src/com/mapswithme/util/LocationUtils.java index 9996173317..a3066689ad 100644 --- a/android/src/com/mapswithme/util/LocationUtils.java +++ b/android/src/com/mapswithme/util/LocationUtils.java @@ -85,7 +85,7 @@ public class LocationUtils // Do compare current and previous system times in case when // we have incorrect time settings on a device. time = System.currentTimeMillis(); - lastTime = LocationHelper.INSTANCE.getLastLocationTime(); + lastTime = LocationHelper.INSTANCE.getSavedLocationTime(); } return (time - lastTime) * 1.0E-3; diff --git a/android/src/com/mapswithme/util/ThemeSwitcher.java b/android/src/com/mapswithme/util/ThemeSwitcher.java index d0e5be9168..38c7d79327 100644 --- a/android/src/com/mapswithme/util/ThemeSwitcher.java +++ b/android/src/com/mapswithme/util/ThemeSwitcher.java @@ -38,7 +38,7 @@ public final class ThemeSwitcher if (RoutingController.get().isNavigating()) { - Location last = LocationHelper.INSTANCE.getLastLocation(); + Location last = LocationHelper.INSTANCE.getSavedLocation(); if (last == null) { LocationHelper.INSTANCE.addLocationListener(mLocationListener, true); diff --git a/android/src/com/mapswithme/util/Yota.java b/android/src/com/mapswithme/util/Yota.java index 906e9b77e1..dfd5f320c8 100644 --- a/android/src/com/mapswithme/util/Yota.java +++ b/android/src/com/mapswithme/util/Yota.java @@ -59,7 +59,7 @@ public class Yota if (addLastKnow) { - final Location lastLocation = LocationHelper.INSTANCE.getLastLocation(); + final Location lastLocation = LocationHelper.INSTANCE.getSavedLocation(); if (lastLocation != null) { i.putExtra(EXTRA_HAS_LOCATION, true) From 2f24c644b26cd7ab91af2b3813b1db5b8df561d9 Mon Sep 17 00:00:00 2001 From: Alexander Marchuk Date: Fri, 1 Apr 2016 14:24:41 +0300 Subject: [PATCH 3/3] [new downloader][android] fix: Added expiration timeout for last location retrieving method. --- .../src/com/mapswithme/maps/location/LocationHelper.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/android/src/com/mapswithme/maps/location/LocationHelper.java b/android/src/com/mapswithme/maps/location/LocationHelper.java index 4571b55f94..bc7f43689c 100644 --- a/android/src/com/mapswithme/maps/location/LocationHelper.java +++ b/android/src/com/mapswithme/maps/location/LocationHelper.java @@ -349,7 +349,7 @@ public enum LocationHelper implements SensorEventListener * Obtains last known location regardless of "My position" button state. * @return {@code null} on failure. */ - public @Nullable Location getLastKnownLocation() + public @Nullable Location getLastKnownLocation(long expirationMs) { if (mSavedLocation != null) return mSavedLocation; @@ -360,7 +360,12 @@ public enum LocationHelper implements SensorEventListener if (providers.isEmpty()) return null; - return provider.findBestNotExpiredLocation(providers, LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_LONG); + return provider.findBestNotExpiredLocation(providers, expirationMs); + } + + public @Nullable Location getLastKnownLocation() + { + return getLastKnownLocation(LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_LONG); } public static native void nativeOnLocationError(int errorCode);