Skip to content

Commit

Permalink
feat: add support altitude precision on Android (#176)
Browse files Browse the repository at this point in the history
* feat: add support altitude precision on Android

* fix: copy paste error

cette version ajoute le support de la précision de l'altitude sur Android

* fix: add default values for vertical accuracy

cette version ajoute le support de la précision de l'altitude sur Android

---------

Co-authored-by: Pierre-Dominique CHARRON <[email protected]>
  • Loading branch information
Athorcis and Pierre-Dominique CHARRON authored Jan 18, 2024
1 parent ec4c1e8 commit 7e93917
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ public class BackgroundLocation implements Parcelable {
private long time = 0;
private long elapsedRealtimeNanos = 0;
private float accuracy = 0.0f;
private float verticalAccuracy = 0.0f;
private float speed = 0.0f;
private float bearing = 0.0f;
private double altitude = 0.0f;
private float radius = 0.0f;
private boolean hasAccuracy = false;
private boolean hasVerticalAccuracy = false;
private boolean hasAltitude = false;
private boolean hasSpeed = false;
private boolean hasBearing = false;
Expand Down Expand Up @@ -90,11 +92,13 @@ public BackgroundLocation(BackgroundLocation l) {
time = l.time;
elapsedRealtimeNanos = l.elapsedRealtimeNanos;
accuracy = l.accuracy;
verticalAccuracy = l.verticalAccuracy;
speed = l.speed;
bearing = l.bearing;
altitude = l.altitude;
radius = l.radius;
hasAccuracy = l.hasAccuracy;
hasVerticalAccuracy = l.hasVerticalAccuracy;
hasAltitude = l.hasAltitude;
hasSpeed = l.hasSpeed;
hasBearing = l.hasBearing;
Expand All @@ -116,11 +120,13 @@ private static BackgroundLocation fromParcel(Parcel in) {
l.time = in.readLong();
l.elapsedRealtimeNanos = in.readLong();
l.accuracy = in.readFloat();
l.verticalAccuracy = in.readFloat();
l.speed = in.readFloat();
l.bearing = in.readFloat();
l.altitude = in.readDouble();
l.radius = in.readFloat();
l.hasAccuracy = in.readInt() != 0;
l.hasVerticalAccuracy = in.readInt() != 0;
l.hasAltitude = in.readInt() != 0;
l.hasSpeed = in.readInt() != 0;
l.hasBearing = in.readInt() != 0;
Expand Down Expand Up @@ -155,6 +161,11 @@ public static BackgroundLocation fromLocation(Location location) {
l.setIsFromMockProvider(location.isFromMockProvider());
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
l.verticalAccuracy = location.getVerticalAccuracyMeters();
l.hasVerticalAccuracy = location.hasVerticalAccuracy();
}

return l;
}

Expand All @@ -172,6 +183,9 @@ public static BackgroundLocation fromCursor(Cursor c) {
if (c.getInt(c.getColumnIndex(LocationEntry.COLUMN_NAME_HAS_ACCURACY)) == 1) {
l.setAccuracy(c.getFloat(c.getColumnIndex(LocationEntry.COLUMN_NAME_ACCURACY)));
}
if (c.getInt(c.getColumnIndex(LocationEntry.COLUMN_NAME_HAS_VERTICAL_ACCURACY)) == 1) {
l.setVerticalAccuracy(c.getFloat(c.getColumnIndex(LocationEntry.COLUMN_NAME_VERTICAL_ACCURACY)));
}
if (c.getInt(c.getColumnIndex(LocationEntry.COLUMN_NAME_HAS_SPEED)) == 1) {
l.setSpeed(c.getFloat(c.getColumnIndex(LocationEntry.COLUMN_NAME_SPEED)));
}
Expand Down Expand Up @@ -211,11 +225,13 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(time);
dest.writeLong(elapsedRealtimeNanos);
dest.writeFloat(accuracy);
dest.writeFloat(verticalAccuracy);
dest.writeFloat(speed);
dest.writeFloat(bearing);
dest.writeDouble(altitude);
dest.writeFloat(radius);
dest.writeInt(hasAccuracy ? 1 : 0);
dest.writeInt(hasVerticalAccuracy ? 1 : 0);
dest.writeInt(hasAltitude ? 1 : 0);
dest.writeInt(hasSpeed ? 1 : 0);
dest.writeInt(hasBearing ? 1 : 0);
Expand Down Expand Up @@ -438,6 +454,15 @@ public void setAccuracy(float accuracy) {
this.hasAccuracy = true;
}

public float getVerticalAccuracy() {
return verticalAccuracy;
}

public void setVerticalAccuracy(float accuracy) {
this.verticalAccuracy = accuracy;
this.hasVerticalAccuracy = true;
}

/**
* Get the speed if it is available, in meters/second over ground.
*
Expand Down Expand Up @@ -528,6 +553,10 @@ public boolean hasAccuracy() {
return hasAccuracy;
}

public boolean hasVerticalAccuracy() {
return hasVerticalAccuracy;
}

/**
* True if this location has an altitude.
*/
Expand Down Expand Up @@ -697,6 +726,12 @@ public Location getLocation() {
l.setLongitude(longitude);
l.setTime(time);
if (hasAccuracy) l.setAccuracy(accuracy);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (hasVerticalAccuracy)
l.setVerticalAccuracyMeters(verticalAccuracy);
}

if (hasAltitude) l.setAltitude(altitude);
if (hasSpeed) l.setSpeed(speed);
if (hasBearing) l.setBearing(bearing);
Expand Down Expand Up @@ -811,6 +846,14 @@ public String toString () {
} else {
s.append(" acc=???");
}

if (hasVerticalAccuracy) {
s.append(String.format(" altAcc=%.0f", verticalAccuracy));
}
else {
s.append(" altAcc=???");
}

if (time == 0) {
s.append(" t=?!?");
} else {
Expand Down Expand Up @@ -849,6 +892,7 @@ public JSONObject toJSONObject() throws JSONException {
json.put("latitude", latitude);
json.put("longitude", longitude);
if (hasAccuracy) json.put("accuracy", accuracy);
if (hasVerticalAccuracy) json.put("altitudeAccuracy", verticalAccuracy);
if (hasSpeed) json.put("speed", speed);
if (hasAltitude) json.put("altitude", altitude);
if (hasBearing) json.put("bearing", bearing);
Expand Down Expand Up @@ -880,13 +924,15 @@ public ContentValues toContentValues() {
//values.put(LocationEntry._ID, locationId);
values.put(LocationEntry.COLUMN_NAME_TIME, time);
values.put(LocationEntry.COLUMN_NAME_ACCURACY, accuracy);
values.put(LocationEntry.COLUMN_NAME_VERTICAL_ACCURACY, verticalAccuracy);
values.put(LocationEntry.COLUMN_NAME_SPEED, speed);
values.put(LocationEntry.COLUMN_NAME_BEARING, bearing);
values.put(LocationEntry.COLUMN_NAME_ALTITUDE, altitude);
values.put(LocationEntry.COLUMN_NAME_LATITUDE, latitude);
values.put(LocationEntry.COLUMN_NAME_LONGITUDE, longitude);
values.put(LocationEntry.COLUMN_NAME_RADIUS, radius);
values.put(LocationEntry.COLUMN_NAME_HAS_ACCURACY, hasAccuracy);
values.put(LocationEntry.COLUMN_NAME_HAS_VERTICAL_ACCURACY, hasVerticalAccuracy);
values.put(LocationEntry.COLUMN_NAME_HAS_SPEED, hasSpeed);
values.put(LocationEntry.COLUMN_NAME_HAS_BEARING, hasBearing);
values.put(LocationEntry.COLUMN_NAME_HAS_ALTITUDE, hasAltitude);
Expand Down Expand Up @@ -921,6 +967,9 @@ public Object getValueForKey(String key) {
if ("@accuracy".equals(key)) {
return hasAccuracy ? accuracy : JSONObject.NULL;
}
if ("@altitudeAccuracy".equals(key)) {
return hasVerticalAccuracy ? verticalAccuracy : JSONObject.NULL;
}
if ("@speed".equals(key)) {
return hasSpeed ? speed : JSONObject.NULL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static LocationTemplate getDefault() {
attrs.put("latitude", "@latitude");
attrs.put("longitude", "@longitude");
attrs.put("accuracy", "@accuracy");
attrs.put("altitudeAccuracy", "@altitudeAccuracy");
attrs.put("speed", "@speed");
attrs.put("altitude", "@altitude");
attrs.put("bearing", "@bearing");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ public static abstract class LocationEntry implements BaseColumns {
public static final String COLUMN_NAME_NULLABLE = "NULLHACK";
public static final String COLUMN_NAME_TIME = "time";
public static final String COLUMN_NAME_ACCURACY = "accuracy";
public static final String COLUMN_NAME_VERTICAL_ACCURACY = "vertical_accuracy";
public static final String COLUMN_NAME_SPEED = "speed";
public static final String COLUMN_NAME_BEARING = "bearing";
public static final String COLUMN_NAME_ALTITUDE = "altitude";
public static final String COLUMN_NAME_LATITUDE = "latitude";
public static final String COLUMN_NAME_LONGITUDE = "longitude";
public static final String COLUMN_NAME_RADIUS = "radius";
public static final String COLUMN_NAME_HAS_ACCURACY = "has_accuracy";
public static final String COLUMN_NAME_HAS_VERTICAL_ACCURACY = "has_vertical_accuracy";
public static final String COLUMN_NAME_HAS_SPEED = "has_speed";
public static final String COLUMN_NAME_HAS_BEARING = "has_bearing";
public static final String COLUMN_NAME_HAS_ALTITUDE = "has_altitude";
Expand All @@ -41,13 +43,15 @@ public static abstract class LocationEntry implements BaseColumns {
LocationEntry._ID + " INTEGER PRIMARY KEY," +
LocationEntry.COLUMN_NAME_TIME + INTEGER_TYPE + COMMA_SEP +
LocationEntry.COLUMN_NAME_ACCURACY + REAL_TYPE + COMMA_SEP +
LocationEntry.COLUMN_NAME_VERTICAL_ACCURACY + REAL_TYPE + COMMA_SEP +
LocationEntry.COLUMN_NAME_SPEED + REAL_TYPE + COMMA_SEP +
LocationEntry.COLUMN_NAME_BEARING + REAL_TYPE + COMMA_SEP +
LocationEntry.COLUMN_NAME_ALTITUDE + REAL_TYPE + COMMA_SEP +
LocationEntry.COLUMN_NAME_LATITUDE + REAL_TYPE + COMMA_SEP +
LocationEntry.COLUMN_NAME_LONGITUDE + REAL_TYPE + COMMA_SEP +
LocationEntry.COLUMN_NAME_RADIUS + REAL_TYPE + COMMA_SEP +
LocationEntry.COLUMN_NAME_HAS_ACCURACY + INTEGER_TYPE + COMMA_SEP +
LocationEntry.COLUMN_NAME_HAS_VERTICAL_ACCURACY + INTEGER_TYPE + COMMA_SEP +
LocationEntry.COLUMN_NAME_HAS_SPEED + INTEGER_TYPE + COMMA_SEP +
LocationEntry.COLUMN_NAME_HAS_BEARING + INTEGER_TYPE + COMMA_SEP +
LocationEntry.COLUMN_NAME_HAS_ALTITUDE + INTEGER_TYPE + COMMA_SEP +
Expand Down Expand Up @@ -85,13 +89,15 @@ public static abstract class LocationEntry implements BaseColumns {
_ID,
COLUMN_NAME_TIME,
COLUMN_NAME_ACCURACY,
COLUMN_NAME_VERTICAL_ACCURACY,
COLUMN_NAME_SPEED,
COLUMN_NAME_BEARING,
COLUMN_NAME_ALTITUDE,
COLUMN_NAME_LATITUDE,
COLUMN_NAME_LONGITUDE,
COLUMN_NAME_RADIUS,
COLUMN_NAME_HAS_ACCURACY,
COLUMN_NAME_HAS_VERTICAL_ACCURACY,
COLUMN_NAME_HAS_SPEED,
COLUMN_NAME_HAS_BEARING,
COLUMN_NAME_HAS_ALTITUDE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,15 @@ public long persistLocation(BackgroundLocation location, int maxRows) {
.append(LocationEntry.COLUMN_NAME_PROVIDER).append("= ?,")
.append(LocationEntry.COLUMN_NAME_TIME).append("= ?,")
.append(LocationEntry.COLUMN_NAME_ACCURACY).append("= ?,")
.append(LocationEntry.COLUMN_NAME_VERTICAL_ACCURACY).append("= ?,")
.append(LocationEntry.COLUMN_NAME_SPEED).append("= ?,")
.append(LocationEntry.COLUMN_NAME_BEARING).append("= ?,")
.append(LocationEntry.COLUMN_NAME_ALTITUDE).append("= ?,")
.append(LocationEntry.COLUMN_NAME_RADIUS).append("= ?,")
.append(LocationEntry.COLUMN_NAME_LATITUDE).append("= ?,")
.append(LocationEntry.COLUMN_NAME_LONGITUDE).append("= ?,")
.append(LocationEntry.COLUMN_NAME_HAS_ACCURACY).append("= ?,")
.append(LocationEntry.COLUMN_NAME_HAS_VERTICAL_ACCURACY).append("= ?,")
.append(LocationEntry.COLUMN_NAME_HAS_SPEED).append("= ?,")
.append(LocationEntry.COLUMN_NAME_HAS_BEARING).append("= ?,")
.append(LocationEntry.COLUMN_NAME_HAS_ALTITUDE).append("= ?,")
Expand All @@ -306,13 +308,15 @@ public long persistLocation(BackgroundLocation location, int maxRows) {
location.getProvider(),
location.getTime(),
location.getAccuracy(),
location.getVerticalAccuracy(),
location.getSpeed(),
location.getBearing(),
location.getAltitude(),
location.getRadius(),
location.getLatitude(),
location.getLongitude(),
location.hasAccuracy() ? 1 : 0,
location.hasVerticalAccuracy() ? 1 : 0,
location.hasSpeed() ? 1 : 0,
location.hasBearing() ? 1 : 0,
location.hasAltitude() ? 1 : 0,
Expand Down Expand Up @@ -420,6 +424,9 @@ private BackgroundLocation hydrate(Cursor c) {
if (c.getInt(c.getColumnIndex(LocationEntry.COLUMN_NAME_HAS_ACCURACY)) == 1) {
l.setAccuracy(c.getFloat(c.getColumnIndex(LocationEntry.COLUMN_NAME_ACCURACY)));
}
if (c.getInt(c.getColumnIndex(LocationEntry.COLUMN_NAME_HAS_VERTICAL_ACCURACY)) == 1) {
l.setVerticalAccuracy(c.getFloat(c.getColumnIndex(LocationEntry.COLUMN_NAME_VERTICAL_ACCURACY)));
}
if (c.getInt(c.getColumnIndex(LocationEntry.COLUMN_NAME_HAS_SPEED)) == 1) {
l.setSpeed(c.getFloat(c.getColumnIndex(LocationEntry.COLUMN_NAME_SPEED)));
}
Expand Down Expand Up @@ -448,13 +455,15 @@ private ContentValues getContentValues(BackgroundLocation l) {
values.put(LocationEntry.COLUMN_NAME_PROVIDER, l.getProvider());
values.put(LocationEntry.COLUMN_NAME_TIME, l.getTime());
values.put(LocationEntry.COLUMN_NAME_ACCURACY, l.getAccuracy());
values.put(LocationEntry.COLUMN_NAME_VERTICAL_ACCURACY, l.getVerticalAccuracy());
values.put(LocationEntry.COLUMN_NAME_SPEED, l.getSpeed());
values.put(LocationEntry.COLUMN_NAME_BEARING, l.getBearing());
values.put(LocationEntry.COLUMN_NAME_ALTITUDE, l.getAltitude());
values.put(LocationEntry.COLUMN_NAME_RADIUS, l.getRadius());
values.put(LocationEntry.COLUMN_NAME_LATITUDE, l.getLatitude());
values.put(LocationEntry.COLUMN_NAME_LONGITUDE, l.getLongitude());
values.put(LocationEntry.COLUMN_NAME_HAS_ACCURACY, l.hasAccuracy() ? 1 : 0);
values.put(LocationEntry.COLUMN_NAME_HAS_VERTICAL_ACCURACY, l.hasVerticalAccuracy() ? 1 : 0);
values.put(LocationEntry.COLUMN_NAME_HAS_SPEED, l.hasSpeed() ? 1 : 0);
values.put(LocationEntry.COLUMN_NAME_HAS_BEARING, l.hasBearing() ? 1 : 0);
values.put(LocationEntry.COLUMN_NAME_HAS_ALTITUDE, l.hasAltitude() ? 1 : 0);
Expand All @@ -473,13 +482,15 @@ private String[] queryColumns() {
LocationEntry.COLUMN_NAME_PROVIDER,
LocationEntry.COLUMN_NAME_TIME,
LocationEntry.COLUMN_NAME_ACCURACY,
LocationEntry.COLUMN_NAME_VERTICAL_ACCURACY,
LocationEntry.COLUMN_NAME_SPEED,
LocationEntry.COLUMN_NAME_BEARING,
LocationEntry.COLUMN_NAME_ALTITUDE,
LocationEntry.COLUMN_NAME_RADIUS,
LocationEntry.COLUMN_NAME_LATITUDE,
LocationEntry.COLUMN_NAME_LONGITUDE,
LocationEntry.COLUMN_NAME_HAS_ACCURACY,
LocationEntry.COLUMN_NAME_HAS_VERTICAL_ACCURACY,
LocationEntry.COLUMN_NAME_HAS_SPEED,
LocationEntry.COLUMN_NAME_HAS_BEARING,
LocationEntry.COLUMN_NAME_HAS_ALTITUDE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class SQLiteOpenHelper extends android.database.sqlite.SQLiteOpenHelper {
private static final String TAG = SQLiteOpenHelper.class.getName();
public static final String SQLITE_DATABASE_NAME = "cordova_bg_geolocation.db";
public static final int DATABASE_VERSION = 15;
public static final int DATABASE_VERSION = 16;

public static final String TEXT_TYPE = " TEXT";
public static final String INTEGER_TYPE = " INTEGER";
Expand Down Expand Up @@ -112,6 +112,14 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
case 14:
alterSql.add("ALTER TABLE " + ConfigurationEntry.TABLE_NAME +
" ADD COLUMN " + ConfigurationEntry.COLUMN_NAME_NOTIFICATIONS_ENABLED + INTEGER_TYPE);
case 15:
alterSql.add("ALTER TABLE " + LocationEntry.TABLE_NAME +
" ADD COLUMN " + LocationEntry.COLUMN_NAME_VERTICAL_ACCURACY + REAL_TYPE);
alterSql.add("ALTER TABLE " + LocationEntry.TABLE_NAME +
" ADD COLUMN " + LocationEntry.COLUMN_NAME_HAS_VERTICAL_ACCURACY + INTEGER_TYPE);
alterSql.add("UPDATE " + LocationEntry.TABLE_NAME +
" SET " + LocationEntry.COLUMN_NAME_VERTICAL_ACCURACY + "= -1,"
LocationEntry.COLUMN_NAME_HAS_VERTICAL_ACCURACY + "= 0"

break; // DO NOT FORGET TO MOVE DOWN BREAK ON DB UPGRADE!!!
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public Bundle getBundle() {
params.putDouble("latitude", mLocation.getLatitude());
params.putDouble("longitude", mLocation.getLongitude());
if (mLocation.hasAccuracy()) params.putFloat("accuracy", mLocation.getAccuracy());
if (mLocation.hasVerticalAccuracy()) params.putFloat("accuracy", mLocation.getVerticalAccuracy());
if (mLocation.hasSpeed()) params.putFloat("speed", mLocation.getSpeed());
if (mLocation.hasAltitude()) params.putDouble("altitude", mLocation.getAltitude());
if (mLocation.hasBearing()) params.putFloat("bearing", mLocation.getBearing());
Expand Down

0 comments on commit 7e93917

Please sign in to comment.