Merge pull request #2682 from trashkalmar/forced-last-location

[new downloader][android] fix: Obtaining last known location regardless of "My position" button state.
This commit is contained in:
Dmitry Yunitsky 2016-04-01 15:51:30 +03:00
commit e676c9dba8
18 changed files with 69 additions and 38 deletions

View file

@ -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;
}
}

View file

@ -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(), "");

View file

@ -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);

View file

@ -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());

View file

@ -618,7 +618,7 @@ class DownloaderAdapter extends RecyclerView.Adapter<DownloaderAdapter.ViewHolde
if (TextUtils.isEmpty(parent))
{
Location loc = LocationHelper.INSTANCE.getLastLocation();
Location loc = LocationHelper.INSTANCE.getSavedLocation();
hasLocation = (loc != null);
if (hasLocation)
{

View file

@ -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());

View file

@ -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);
}

View file

@ -136,7 +136,7 @@ public class OnmapDownloader implements MwmActivity.LeftAnimationTrackListener
!MapManager.nativeIsLegacyMode() &&
ConnectionState.isWifiConnected())
{
Location loc = LocationHelper.INSTANCE.getLastLocation();
Location loc = LocationHelper.INSTANCE.getSavedLocation();
if (loc != null)
{
String country = MapManager.nativeFindCountry(loc.getLatitude(), loc.getLongitude());

View file

@ -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<String> providers = getFilteredProviders();
if (providers.size() == 0)
if (providers.isEmpty())
LocationHelper.INSTANCE.notifyLocationError(LocationHelper.ERROR_DENIED);
else
{
@ -39,15 +40,15 @@ 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);
LocationHelper.INSTANCE.saveLocation(newLocation);
else
{
final Location lastLocation = LocationHelper.INSTANCE.getLastLocation();
if (lastLocation != null && !LocationUtils.isExpired(lastLocation, LocationHelper.INSTANCE.getLastLocationTime(),
final Location lastLocation = LocationHelper.INSTANCE.getSavedLocation();
if (lastLocation != null && !LocationUtils.isExpired(lastLocation, LocationHelper.INSTANCE.getSavedLocationTime(),
LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_SHORT))
LocationHelper.INSTANCE.setLastLocation(lastLocation);
LocationHelper.INSTANCE.saveLocation(lastLocation);
}
}
}
@ -59,13 +60,13 @@ public class AndroidNativeProvider extends BaseLocationProvider
mIsActive = false;
}
private Location findBestNotExpiredLocation(List<String> providers)
@Nullable Location findBestNotExpiredLocation(List<String> 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(), LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_SHORT))
if (l != null && !LocationUtils.isExpired(l, l.getTime(), expirationMs))
{
if (res == null || res.getAccuracy() > l.getAccuracy())
res = l;
@ -74,7 +75,7 @@ public class AndroidNativeProvider extends BaseLocationProvider
return res;
}
private List<String> getFilteredProviders()
List<String> getFilteredProviders()
{
final List<String> allProviders = mLocationManager.getProviders(false);
final List<String> acceptedProviders = new ArrayList<>(allProviders.size());

View file

@ -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);
}
}

View file

@ -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

View file

@ -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;
@ -67,7 +69,7 @@ public enum LocationHelper implements SensorEventListener
private boolean mActive;
private Location mLastLocation;
private Location mSavedLocation;
private MapObject mMyPosition;
private long mLastLocationTime;
@ -173,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; }
/**
* <p>Obtains last known saved location. It depends on "My position" button state and is cleared on "No position" one.
* <p>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();
@ -196,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();
}
@ -310,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());
}
@ -338,6 +345,29 @@ 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(long expirationMs)
{
if (mSavedLocation != null)
return mSavedLocation;
AndroidNativeProvider provider = new AndroidNativeProvider();
List<String> providers = provider.getFilteredProviders();
if (providers.isEmpty())
return null;
return provider.findBestNotExpiredLocation(providers, expirationMs);
}
public @Nullable Location getLastKnownLocation()
{
return getLastKnownLocation(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);

View file

@ -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);

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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);

View file

@ -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)