From 91a3098f8d42c193b2d63deb2a53c8e1c2aecc8e Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 22 Jan 2021 16:34:24 +0200 Subject: [PATCH] Minor fixes --- .../src/net/osmand/aidlapi/map/ALocation.java | 1 - .../osmand/aidlapi/map/SetLocationParams.java | 4 +-- OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 21 ++++------- .../net/osmand/aidl/OsmandAidlServiceV2.java | 3 +- .../osmand/plus/OsmAndLocationProvider.java | 36 ++++++++++--------- 5 files changed, 31 insertions(+), 34 deletions(-) diff --git a/OsmAnd-api/src/net/osmand/aidlapi/map/ALocation.java b/OsmAnd-api/src/net/osmand/aidlapi/map/ALocation.java index 243e8e89fa..03741138d5 100644 --- a/OsmAnd-api/src/net/osmand/aidlapi/map/ALocation.java +++ b/OsmAnd-api/src/net/osmand/aidlapi/map/ALocation.java @@ -1,6 +1,5 @@ package net.osmand.aidlapi.map; -import android.location.Location; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; diff --git a/OsmAnd-api/src/net/osmand/aidlapi/map/SetLocationParams.java b/OsmAnd-api/src/net/osmand/aidlapi/map/SetLocationParams.java index 61531125fe..ddbe492a3f 100644 --- a/OsmAnd-api/src/net/osmand/aidlapi/map/SetLocationParams.java +++ b/OsmAnd-api/src/net/osmand/aidlapi/map/SetLocationParams.java @@ -42,13 +42,13 @@ public class SetLocationParams extends AidlParams { @Override public void writeToBundle(Bundle bundle) { bundle.putParcelable("location", location); - bundle.putLong("aidl_time_to_not_use_other_gps", timeToNotUseOtherGPS); + bundle.putLong("timeToNotUseOtherGPS", timeToNotUseOtherGPS); } @Override protected void readFromBundle(Bundle bundle) { bundle.setClassLoader(ALocation.class.getClassLoader()); location = bundle.getParcelable("location"); - timeToNotUseOtherGPS = bundle.getLong("aidl_time_to_not_use_other_gps"); + timeToNotUseOtherGPS = bundle.getLong("timeToNotUseOtherGPS"); } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 108e52c558..1dc3e00d98 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -58,7 +58,6 @@ import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.GPXDatabase.GpxDataItem; import net.osmand.plus.GpxSelectionHelper; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; -import net.osmand.plus.OsmAndLocationProvider; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.SQLiteTileSource; @@ -124,8 +123,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.Timer; -import java.util.TimerTask; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; @@ -915,11 +912,13 @@ public class OsmandAidlApi { BroadcastReceiver setLocationReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { + String packName = intent.getStringExtra(AIDL_PACKAGE_NAME); ALocation aLocation = intent.getParcelableExtra(AIDL_LOCATION); long timeToNotUseOtherGPS = intent.getLongExtra(AIDL_TIME_TO_NOT_USE_OTHER_GPS, 0); - if (aLocation != null && !Double.isNaN(aLocation.getLatitude()) && !Double.isNaN(aLocation.getLongitude())) { - Location location = new Location(app.getPackageName()); + if (!Algorithms.isEmpty(packName) && aLocation != null + && !Double.isNaN(aLocation.getLatitude()) && !Double.isNaN(aLocation.getLongitude())) { + Location location = new Location(packName); location.setLatitude(aLocation.getLatitude()); location.setLongitude(aLocation.getLongitude()); location.setTime(aLocation.getTime()); @@ -938,14 +937,7 @@ public class OsmandAidlApi { if (aLocation.hasVerticalAccuracy()) { location.setVerticalAccuracy(aLocation.getVerticalAccuracy()); } - app.getLocationProvider().setCustomLocation(location); - - app.runInUIThread(new Runnable() { - @Override - public void run() { - app.getLocationProvider().setCustomLocation(null); - } - }, timeToNotUseOtherGPS); + app.getLocationProvider().setCustomLocation(location, timeToNotUseOtherGPS); } } }; @@ -2454,10 +2446,11 @@ public class OsmandAidlApi { return true; } - public boolean setLocation(ALocation location, long timeToNotUseOtherGPS) { + public boolean setLocation(String packName, ALocation location, long timeToNotUseOtherGPS) { Intent intent = new Intent(); intent.setAction(AIDL_SET_LOCATION); intent.putExtra(AIDL_LOCATION, location); + intent.putExtra(AIDL_PACKAGE_NAME, packName); intent.putExtra(AIDL_TIME_TO_NOT_USE_OTHER_GPS, timeToNotUseOtherGPS); app.sendBroadcast(intent); return true; diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java index 60a8d8acd6..ed7ab614f3 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java @@ -1450,7 +1450,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener try { if (params != null) { OsmandAidlApi api = getApi("setLocation"); - return api != null && api.setLocation(params.getLocation(), params.getTimeToNotUseOtherGPS()); + String packName = getCallingAppPackName(); + return api != null && api.setLocation(packName, params.getLocation(), params.getTimeToNotUseOtherGPS()); } } catch (Exception e) { handleException(e); diff --git a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java index 35f63b96bd..760b60599f 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java @@ -45,6 +45,7 @@ import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.router.RouteSegmentResult; +import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; import java.util.ArrayList; @@ -93,6 +94,7 @@ public class OsmAndLocationProvider implements SensorEventListener { private SimulationProvider simulatePosition = null; private long cachedLocationTimeFix = 0; + private long timeToNotUseOtherGPS = 0; private net.osmand.Location cachedLocation; private net.osmand.Location customLocation; @@ -727,11 +729,21 @@ public class OsmAndLocationProvider implements SensorEventListener { } } - public void setLocationFromService(net.osmand.Location location) { - if (locationSimulation.isRouteAnimating()) { - return; + public void setCustomLocation(net.osmand.Location location, long ignoreLocationsTime) { + timeToNotUseOtherGPS = System.currentTimeMillis() + ignoreLocationsTime; + customLocation = location; + setLocation(location); + } + + private boolean shouldIgnoreLocation(net.osmand.Location location) { + if (customLocation != null && timeToNotUseOtherGPS >= System.currentTimeMillis()) { + return location == null || !Algorithms.stringsEqual(customLocation.getProvider(), location.getProvider()); } - if (hasCustomLocation() && isNotSimulatedLocation(location)) { + return false; + } + + public void setLocationFromService(net.osmand.Location location) { + if (locationSimulation.isRouteAnimating() || shouldIgnoreLocation(location)) { return; } if (location != null) { @@ -745,27 +757,19 @@ public class OsmAndLocationProvider implements SensorEventListener { app.getRoutingHelper().updateLocation(location); app.getWaypointHelper().locationChanged(location); } - - public void setCustomLocation(net.osmand.Location location) { - customLocation = location; - setLocation(location); - } - - private boolean hasCustomLocation() { - return customLocation != null; - } public void setLocationFromSimulation(net.osmand.Location location) { setLocation(location); } private void setLocation(net.osmand.Location location) { + if (shouldIgnoreLocation(location)) { + return; + } if (location == null) { updateGPSInfo(null); } - if (hasCustomLocation() && isNotSimulatedLocation(location)) { - return; - } + if (location != null) { // // use because there is a bug on some devices with location.getTime() lastTimeLocationFixed = System.currentTimeMillis();