Adjust logic and add addition params to location (in addition to latitute & longitude)

This commit is contained in:
cepprice 2021-01-20 22:55:51 +05:00
parent a980e4016b
commit 319ed41700
6 changed files with 264 additions and 73 deletions

View file

@ -0,0 +1,3 @@
package net.osmand.aidlapi.map;
parcelable ALocation;

View file

@ -0,0 +1,209 @@
package net.osmand.aidlapi.map;
import android.location.Location;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import net.osmand.aidlapi.AidlParams;
public class ALocation extends AidlParams {
private double latitude = 0.0;
private double longitude = 0.0;
private long time = 0;
private boolean hasAltitude = false;
private double altitude = 0.0f;
private boolean hasSpeed = false;
private float speed = 0.0f;
private boolean hasBearing = false;
private float bearing = 0.0f;
private boolean hasAccuracy = false;
private float accuracy = 0.0f;
private boolean hasVerticalAccuracy = false;
private float verticalAccuracy = 0.0f;
private ALocation() {
}
public ALocation(Parcel in) {
readFromParcel(in);
}
public static final Parcelable.Creator<ALocation> CREATOR = new Parcelable.Creator<ALocation>() {
@Override
public ALocation createFromParcel(Parcel in) {
return new ALocation(in);
}
@Override
public ALocation[] newArray(int size) {
return new ALocation[size];
}
};
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public long getTime() {
return time;
}
public boolean hasAltitude() {
return hasAltitude;
}
public double getAltitude() {
return altitude;
}
public boolean hasSpeed() {
return hasSpeed;
}
public float getSpeed() {
return speed;
}
public boolean hasBearing() {
return hasBearing;
}
public float getBearing() {
return bearing;
}
public boolean hasAccuracy() {
return hasAccuracy;
}
public float getAccuracy() {
return accuracy;
}
public boolean hasVerticalAccuracy() {
return hasVerticalAccuracy;
}
public float getVerticalAccuracy() {
return verticalAccuracy;
}
@Override
protected void writeToBundle(Bundle bundle) {
bundle.putDouble("latitude", latitude);
bundle.putDouble("longitude", longitude);
bundle.putLong("time", time);
bundle.putBoolean("hasAltitude", hasAltitude);
bundle.putDouble("altitude", altitude);
bundle.putBoolean("hasSpeed", hasSpeed);
bundle.putFloat("speed", speed);
bundle.putBoolean("hasBearing", hasBearing);
bundle.putFloat("bearing", bearing);
bundle.putBoolean("hasAccuracy", hasAccuracy);
bundle.putFloat("accuracy", accuracy);
bundle.putBoolean("hasVerticalAccuracy", hasVerticalAccuracy);
bundle.putFloat("verticalAccuracy", verticalAccuracy);
}
@Override
protected void readFromBundle(Bundle bundle) {
latitude = bundle.getDouble("latitude");
longitude = bundle.getDouble("longitude");
time = bundle.getLong("time");
hasAltitude = bundle.getBoolean("hasAltitude");
altitude = bundle.getDouble("altitude");
hasSpeed = bundle.getBoolean("hasSpeed");
speed = bundle.getFloat("speed");
hasBearing = bundle.getBoolean("hasBearing");
bearing = bundle.getFloat("bearing");
hasAccuracy = bundle.getBoolean("hasAccuracy");
accuracy = bundle.getFloat("accuracy");
hasVerticalAccuracy = bundle.getBoolean("hasVerticalAccuracy");
verticalAccuracy = bundle.getFloat("verticalAccuracy");
}
public static Builder builder() {
return new ALocation().new Builder();
}
public class Builder {
private Builder() {
}
public Builder setLatitude(double latitude) {
ALocation.this.latitude = latitude;
return this;
}
public Builder setLongitude(double longitude) {
ALocation.this.longitude = longitude;
return this;
}
public Builder setTime(long time) {
ALocation.this.time = time;
return this;
}
public Builder hasAltitude(boolean hasAltitude) {
ALocation.this.hasAltitude = hasAltitude;
return this;
}
public Builder setAltitude(float altitude) {
ALocation.this.altitude = altitude;
return this;
}
public Builder hasSpeed(boolean hasSpeed) {
ALocation.this.hasSpeed = hasSpeed;
return this;
}
public Builder setSpeed(float speed) {
ALocation.this.speed = speed;
return this;
}
public Builder hasBearing(boolean hasBearing) {
ALocation.this.hasBearing = hasBearing;
return this;
}
public Builder setBearing(float bearing) {
ALocation.this.bearing = bearing;
return this;
}
public Builder hasAccuracy(boolean hasAccuracy) {
ALocation.this.hasAccuracy = hasAccuracy;
return this;
}
public Builder setAccuracy(float accuracy) {
ALocation.this.accuracy = accuracy;
return this;
}
public Builder hasVerticalAccuracy(boolean hasVerticalAccuracy) {
ALocation.this.hasVerticalAccuracy = hasVerticalAccuracy;
return this;
}
public Builder setVerticalAccuracy(float verticalAccuracy) {
ALocation.this.verticalAccuracy = verticalAccuracy;
return this;
}
public ALocation build() {
return ALocation.this;
}
}
}

View file

@ -7,13 +7,11 @@ import net.osmand.aidlapi.AidlParams;
public class SetLocationParams extends AidlParams { public class SetLocationParams extends AidlParams {
private double latitude; private ALocation location;
private double longitude;
private long timeToNotUseOtherGPS; private long timeToNotUseOtherGPS;
public SetLocationParams(double latitude, double longitude, long timeToNotUseOtherGPS) { public SetLocationParams(ALocation location, long timeToNotUseOtherGPS) {
this.latitude = latitude; this.location = location;
this.longitude = longitude;
this.timeToNotUseOtherGPS = timeToNotUseOtherGPS; this.timeToNotUseOtherGPS = timeToNotUseOtherGPS;
} }
@ -33,12 +31,8 @@ public class SetLocationParams extends AidlParams {
} }
}; };
public double getLatitude() { public ALocation getLocation() {
return latitude; return location;
}
public double getLongitude() {
return longitude;
} }
public long getTimeToNotUseOtherGPS() { public long getTimeToNotUseOtherGPS() {
@ -47,15 +41,14 @@ public class SetLocationParams extends AidlParams {
@Override @Override
public void writeToBundle(Bundle bundle) { public void writeToBundle(Bundle bundle) {
bundle.putDouble("latitude", latitude); bundle.putParcelable("location", location);
bundle.putDouble("longitude", longitude);
bundle.putLong("aidl_time_to_not_use_other_gps", timeToNotUseOtherGPS); bundle.putLong("aidl_time_to_not_use_other_gps", timeToNotUseOtherGPS);
} }
@Override @Override
protected void readFromBundle(Bundle bundle) { protected void readFromBundle(Bundle bundle) {
latitude = bundle.getDouble("latitude"); bundle.setClassLoader(ALocation.class.getClassLoader());
longitude = bundle.getDouble("longitude"); location = bundle.getParcelable("location");
timeToNotUseOtherGPS = bundle.getLong("aidl_time_to_not_use_other_gps"); timeToNotUseOtherGPS = bundle.getLong("aidl_time_to_not_use_other_gps");
} }
} }

View file

@ -43,6 +43,7 @@ import net.osmand.aidl.tiles.ASqliteDbFile;
import net.osmand.aidlapi.customization.AProfile; import net.osmand.aidlapi.customization.AProfile;
import net.osmand.aidlapi.info.AppInfoParams; import net.osmand.aidlapi.info.AppInfoParams;
import net.osmand.aidlapi.map.ALatLon; import net.osmand.aidlapi.map.ALatLon;
import net.osmand.aidlapi.map.ALocation;
import net.osmand.aidlapi.navigation.ABlockedRoad; import net.osmand.aidlapi.navigation.ABlockedRoad;
import net.osmand.data.FavouritePoint; import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
@ -176,6 +177,7 @@ public class OsmandAidlApi {
private static final String AIDL_ZOOM = "aidl_zoom"; private static final String AIDL_ZOOM = "aidl_zoom";
private static final String AIDL_ROTATION = "aidl_rotation"; private static final String AIDL_ROTATION = "aidl_rotation";
private static final String AIDL_ANIMATED = "aidl_animated"; private static final String AIDL_ANIMATED = "aidl_animated";
private static final String AIDL_LOCATION = "aidl_location";
private static final String AIDL_TIME_TO_NOT_USE_OTHER_GPS = "aidl_time_to_not_use_other_gps"; private static final String AIDL_TIME_TO_NOT_USE_OTHER_GPS = "aidl_time_to_not_use_other_gps";
private static final String AIDL_START_NAME = "aidl_start_name"; private static final String AIDL_START_NAME = "aidl_start_name";
@ -237,7 +239,6 @@ public class OsmandAidlApi {
private MapActivity mapActivity; private MapActivity mapActivity;
private boolean mapActivityActive = false; private boolean mapActivityActive = false;
private boolean hasCustomLocation = false;
public OsmandAidlApi(OsmandApplication app) { public OsmandAidlApi(OsmandApplication app) {
this.app = app; this.app = app;
@ -911,44 +912,41 @@ public class OsmandAidlApi {
} }
private void registerSetLocationReceiver(MapActivity mapActivity) { private void registerSetLocationReceiver(MapActivity mapActivity) {
final WeakReference<MapActivity> mapActivityRef = new WeakReference<>(mapActivity);
BroadcastReceiver setLocationReceiver = new BroadcastReceiver() { BroadcastReceiver setLocationReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
final MapActivity mapActivity = mapActivityRef.get(); ALocation aLocation = intent.getParcelableExtra(AIDL_LOCATION);
if (mapActivity == null) {
return;
}
double lat = intent.getDoubleExtra(AIDL_LATITUDE, Double.NaN);
double lon = intent.getDoubleExtra(AIDL_LONGITUDE, Double.NaN);
long timeToNotUseOtherGPS = intent.getLongExtra(AIDL_TIME_TO_NOT_USE_OTHER_GPS, 0); long timeToNotUseOtherGPS = intent.getLongExtra(AIDL_TIME_TO_NOT_USE_OTHER_GPS, 0);
if (!Double.isNaN(lat) && !Double.isNaN(lon)) { if (aLocation != null && !Double.isNaN(aLocation.getLatitude()) && !Double.isNaN(aLocation.getLongitude())) {
mapActivity.setMapLocation(lat, lon); Location location = new Location(app.getPackageName());
mapActivity.refreshMap(); location.setLatitude(aLocation.getLatitude());
hasCustomLocation = true; location.setLongitude(aLocation.getLongitude());
net.osmand.Location location = new net.osmand.Location("OsmAnd", lat, lon); location.setTime(aLocation.getTime());
if (aLocation.hasAltitude()) {
location.setAltitude(aLocation.getAltitude());
}
if (aLocation.hasSpeed()) {
location.setSpeed(aLocation.getSpeed());
}
if (aLocation.hasBearing()) {
location.setBearing(aLocation.getBearing());
}
if (aLocation.hasAccuracy()) {
location.setAccuracy(aLocation.getAccuracy());
}
if (aLocation.hasVerticalAccuracy()) {
location.setVerticalAccuracy(aLocation.getVerticalAccuracy());
}
app.getLocationProvider().setCustomLocation(location); app.getLocationProvider().setCustomLocation(location);
}
final OsmAndLocationProvider.OsmAndLocationListener listener = new OsmAndLocationProvider.OsmAndLocationListener() { app.runInUIThread(new Runnable() {
@Override @Override
public void updateLocation(Location location) { public void run() {
mapActivity.setMapLocation(location.getLatitude(), location.getLongitude()); app.getLocationProvider().setCustomLocation(null);
mapActivity.refreshMap();
}
};
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if (app.getLocationProvider() != null) {
hasCustomLocation = false;
app.getLocationProvider().addLocationListener(listener);
} }
} }, timeToNotUseOtherGPS);
}, timeToNotUseOtherGPS); }
} }
}; };
registerReceiver(setLocationReceiver, mapActivity, AIDL_SET_LOCATION); registerReceiver(setLocationReceiver, mapActivity, AIDL_SET_LOCATION);
@ -2456,20 +2454,15 @@ public class OsmandAidlApi {
return true; return true;
} }
public boolean setLocation(double latitude, double longitude, long timeToNotUseOtherGPS) { public boolean setLocation(ALocation location, long timeToNotUseOtherGPS) {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setAction(AIDL_SET_LOCATION); intent.setAction(AIDL_SET_LOCATION);
intent.putExtra(AIDL_LATITUDE, latitude); intent.putExtra(AIDL_LOCATION, location);
intent.putExtra(AIDL_LONGITUDE, longitude);
intent.putExtra(AIDL_TIME_TO_NOT_USE_OTHER_GPS, timeToNotUseOtherGPS); intent.putExtra(AIDL_TIME_TO_NOT_USE_OTHER_GPS, timeToNotUseOtherGPS);
app.sendBroadcast(intent); app.sendBroadcast(intent);
return true; return true;
} }
public boolean hasCustomLocation() {
return hasCustomLocation;
}
private static class FileCopyInfo { private static class FileCopyInfo {
long startTime; long startTime;
long lastAccessTime; long lastAccessTime;

View file

@ -1450,8 +1450,7 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener
try { try {
if (params != null) { if (params != null) {
OsmandAidlApi api = getApi("setLocation"); OsmandAidlApi api = getApi("setLocation");
return api != null && api.setLocation(params.getLatitude(), return api != null && api.setLocation(params.getLocation(), params.getTimeToNotUseOtherGPS());
params.getLongitude(), params.getTimeToNotUseOtherGPS());
} }
} catch (Exception e) { } catch (Exception e) {
handleException(e); handleException(e);

View file

@ -97,6 +97,7 @@ public class OsmAndLocationProvider implements SensorEventListener {
private long cachedLocationTimeFix = 0; private long cachedLocationTimeFix = 0;
private net.osmand.Location cachedLocation; private net.osmand.Location cachedLocation;
private net.osmand.Location customLocation;
private boolean sensorRegistered = false; private boolean sensorRegistered = false;
private float[] mGravs = new float[3]; private float[] mGravs = new float[3];
@ -796,7 +797,7 @@ public class OsmAndLocationProvider implements SensorEventListener {
if (locationSimulation.isRouteAnimating()) { if (locationSimulation.isRouteAnimating()) {
return; return;
} }
if (app.getAidlApi().hasCustomLocation() && isNotSimulatedLocation(location)) { if (hasCustomLocation() && isNotSimulatedLocation(location)) {
return; return;
} }
if (location != null) { if (location != null) {
@ -810,6 +811,15 @@ public class OsmAndLocationProvider implements SensorEventListener {
app.getRoutingHelper().updateLocation(location); app.getRoutingHelper().updateLocation(location);
app.getWaypointHelper().locationChanged(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) { public void setLocationFromSimulation(net.osmand.Location location) {
setLocation(location); setLocation(location);
@ -819,7 +829,7 @@ public class OsmAndLocationProvider implements SensorEventListener {
if (location == null) { if (location == null) {
updateGPSInfo(null); updateGPSInfo(null);
} }
if (app.getAidlApi().hasCustomLocation() && isNotSimulatedLocation(location)) { if (hasCustomLocation() && isNotSimulatedLocation(location)) {
return; return;
} }
if (location != null) { if (location != null) {
@ -856,22 +866,6 @@ public class OsmAndLocationProvider implements SensorEventListener {
updateLocation(this.location); updateLocation(this.location);
} }
public void setCustomLocation(net.osmand.Location location) {
if (locationSimulation.isRouteAnimating()) {
return;
}
if (location != null) {
notifyGpsLocationRecovered();
}
// notify about lost location
scheduleCheckIfGpsLost(location);
app.getSavingTrackHelper().updateLocation(location, heading);
OsmandPlugin.updateLocationPlugins(location);
app.getRoutingHelper().updateLocation(location);
app.getWaypointHelper().locationChanged(location);
}
private void notifyGpsLocationRecovered() { private void notifyGpsLocationRecovered() {
if (gpsSignalLost) { if (gpsSignalLost) {
gpsSignalLost = false; gpsSignalLost = false;