Skip to content

Commit

Permalink
Prevent really small speed values so that they are not displayed in s…
Browse files Browse the repository at this point in the history
…cientific notation in JSON messages
  • Loading branch information
christianrowlands committed Oct 16, 2024
1 parent fd46869 commit 66085f2
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/android-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
api-level: [ 26, 34 ]
api-level: [ 26, 35 ]
target: [ default, google_apis ]

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/android.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:

strategy:
matrix:
api-level: [ 26, 34 ]
api-level: [ 26, 35 ]
target: [ default, google_apis ]

steps:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ buildscript {
classpath 'com.google.gms:google-services:4.4.2'
classpath 'com.google.firebase:firebase-crashlytics-gradle:3.0.2'
}
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.8.1"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.8.2"
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import com.craxiom.networksurvey.services.controller.CellularController;
import com.craxiom.networksurvey.services.controller.GnssController;
import com.craxiom.networksurvey.services.controller.WifiController;
import com.craxiom.networksurvey.util.FormatUtils;
import com.craxiom.networksurvey.util.MathUtils;
import com.craxiom.networksurvey.util.NsUtils;
import com.craxiom.networksurvey.util.PreferenceUtils;
Expand Down Expand Up @@ -1445,7 +1446,11 @@ private DeviceStatus generateDeviceStatus()
dataBuilder.setAccuracy(MathUtils.roundAccuracy(lastKnownLocation.getAccuracy()));
if (lastKnownLocation.hasSpeed())
{
dataBuilder.setSpeed(lastKnownLocation.getSpeed());
float speed = FormatUtils.formatSpeed(lastKnownLocation.getSpeed());
if (speed != 0f)
{
dataBuilder.setSpeed(speed);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
import com.craxiom.networksurvey.model.NrRecordWrapper;
import com.craxiom.networksurvey.model.WifiRecordWrapper;
import com.craxiom.networksurvey.services.controller.CellularController;
import com.craxiom.networksurvey.util.FormatUtils;
import com.craxiom.networksurvey.util.LocationUtils;
import com.craxiom.networksurvey.util.MathUtils;
import com.craxiom.networksurvey.util.NsUtils;
Expand Down Expand Up @@ -657,7 +658,11 @@ private PhoneState createPhoneStateMessage(TelephonyManager telephonyManager, in
dataBuilder.setAccuracy(MathUtils.roundAccuracy(lastKnownLocation.getAccuracy()));
if (lastKnownLocation.hasSpeed())
{
dataBuilder.setSpeed(lastKnownLocation.getSpeed());
float speed = FormatUtils.formatSpeed(lastKnownLocation.getSpeed());
if (speed != 0f)
{
dataBuilder.setSpeed(speed);
}
}
}
}
Expand Down Expand Up @@ -973,7 +978,11 @@ private GsmRecord generateGsmSurveyRecord(CellInfoGsm cellInfoGsm, int subscript
dataBuilder.setAccuracy(MathUtils.roundAccuracy(lastKnownLocation.getAccuracy()));
if (lastKnownLocation.hasSpeed())
{
dataBuilder.setSpeed(lastKnownLocation.getSpeed());
float speed = FormatUtils.formatSpeed(lastKnownLocation.getSpeed());
if (speed != 0f)
{
dataBuilder.setSpeed(speed);
}
}
}
}
Expand Down Expand Up @@ -1073,7 +1082,11 @@ private CdmaRecord generateCdmaSurveyRecord(CellInfoCdma cellInfoCdma, int subsc
dataBuilder.setAccuracy(MathUtils.roundAccuracy(lastKnownLocation.getAccuracy()));
if (lastKnownLocation.hasSpeed())
{
dataBuilder.setSpeed(lastKnownLocation.getSpeed());
float speed = FormatUtils.formatSpeed(lastKnownLocation.getSpeed());
if (speed != 0f)
{
dataBuilder.setSpeed(speed);
}
}
}
}
Expand Down Expand Up @@ -1160,7 +1173,11 @@ private UmtsRecord generateUmtsSurveyRecord(CellInfoWcdma cellInfoWcdma, int sub
dataBuilder.setAccuracy(MathUtils.roundAccuracy(lastKnownLocation.getAccuracy()));
if (lastKnownLocation.hasSpeed())
{
dataBuilder.setSpeed(lastKnownLocation.getSpeed());
float speed = FormatUtils.formatSpeed(lastKnownLocation.getSpeed());
if (speed != 0f)
{
dataBuilder.setSpeed(speed);
}
}
}
}
Expand Down Expand Up @@ -1276,7 +1293,11 @@ private LteRecord generateLteSurveyRecord(CellInfoLte cellInfoLte, int subscript
dataBuilder.setAccuracy(MathUtils.roundAccuracy(lastKnownLocation.getAccuracy()));
if (lastKnownLocation.hasSpeed())
{
dataBuilder.setSpeed(lastKnownLocation.getSpeed());
float speed = FormatUtils.formatSpeed(lastKnownLocation.getSpeed());
if (speed != 0f)
{
dataBuilder.setSpeed(speed);
}
}
}
}
Expand Down Expand Up @@ -1469,7 +1490,11 @@ private NrRecordWrapper generateNrSurveyRecord(CellInfoNr cellInfoNr, int subscr
dataBuilder.setAccuracy(MathUtils.roundAccuracy(lastKnownLocation.getAccuracy()));
if (lastKnownLocation.hasSpeed())
{
dataBuilder.setSpeed(lastKnownLocation.getSpeed());
float speed = FormatUtils.formatSpeed(lastKnownLocation.getSpeed());
if (speed != 0f)
{
dataBuilder.setSpeed(speed);
}
}
}
}
Expand Down Expand Up @@ -1579,7 +1604,11 @@ private WifiRecordWrapper generateWiFiBeaconSurveyRecord(ScanResult apScanResult
dataBuilder.setAccuracy(MathUtils.roundAccuracy(lastKnownLocation.getAccuracy()));
if (lastKnownLocation.hasSpeed())
{
dataBuilder.setSpeed(lastKnownLocation.getSpeed());
float speed = FormatUtils.formatSpeed(lastKnownLocation.getSpeed());
if (speed != 0f)
{
dataBuilder.setSpeed(speed);
}
}
}
}
Expand Down Expand Up @@ -1680,7 +1709,11 @@ private BluetoothRecord generateBluetoothSurveyRecord(BluetoothDevice device, in
dataBuilder.setAccuracy(MathUtils.roundAccuracy(lastKnownLocation.getAccuracy()));
if (lastKnownLocation.hasSpeed())
{
dataBuilder.setSpeed(lastKnownLocation.getSpeed());
float speed = FormatUtils.formatSpeed(lastKnownLocation.getSpeed());
if (speed != 0f)
{
dataBuilder.setSpeed(speed);
}
}
}
}
Expand Down Expand Up @@ -1767,7 +1800,11 @@ private GnssRecord generateGnssSurveyRecord(GnssMeasurement gnss, Map<Constellat

if (lastKnownLocation.hasSpeed())
{
dataBuilder.setSpeed(lastKnownLocation.getSpeed());
float speed = FormatUtils.formatSpeed(lastKnownLocation.getSpeed());
if (speed != 0f)
{
dataBuilder.setSpeed(speed);
}
}
}
}
Expand Down Expand Up @@ -1853,7 +1890,11 @@ private GnssRecord generateEmptyGnssSurveyRecord()

if (lastKnownLocation.hasSpeed())
{
dataBuilder.setSpeed(lastKnownLocation.getSpeed());
float speed = FormatUtils.formatSpeed(lastKnownLocation.getSpeed());
if (speed != 0f)
{
dataBuilder.setSpeed(speed);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ abstract class ASignalChartViewModel : ViewModel() {

// Remove any makers that have moved "off screen"
xValueQueue.firstOrNull()?.let { xValue ->
_markerList.value = markerList.value - xValue
_markerList.value -= xValue
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ object FormatUtils {
)
}

/**
* This function prevents really small speed values from being displayed in scientific notation
* when converted to a string (for JSON serialization). If the speed is less than 0.1, then
* the speed is set to 0.
*/
@JvmStatic
fun formatSpeed(speed: Float): Float {
// If less then 0.1 then don't set the value
return if (speed < 0.1) {
0f
} else {
speed
}
}

fun formatSpeedAccuracy(
context: Context,
location: Location
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.craxiom.networksurvey.util;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class FormatUtilsTest
{
@Test
public void formatSpeed_withSpeed()
{
float inputSpeed = 15.5f;
float formattedSpeed = FormatUtils.formatSpeed(inputSpeed);
assertEquals(15.5f, formattedSpeed, 0.0f);
}

@Test
public void formatSpeed_withLowSpeed()
{
float inputSpeed = 0.5f;
float formattedSpeed = FormatUtils.formatSpeed(inputSpeed);
assertEquals(0.5f, formattedSpeed, 0.0f);
}

@Test
public void formatSpeed_withoutSpeed()
{
float inputSpeed = 10000000f;
float formattedSpeed = FormatUtils.formatSpeed(inputSpeed);
assertEquals(10000000f, formattedSpeed, 0.0f);
}

@Test
public void formatSpeed_smallSpeed()
{
float inputSpeed = 0.000000000000082499864f;
float formattedSpeed = FormatUtils.formatSpeed(inputSpeed);
assertEquals(0.0f, formattedSpeed, 0.0f);

String floatString = Float.toString(formattedSpeed);
assertEquals("0.0", floatString);
}
}

0 comments on commit 66085f2

Please sign in to comment.