Skip to content

Commit d18fd57

Browse files
committed
Error handling in GPS location save
1 parent 5e0d99e commit d18fd57

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

mage/src/main/java/mil/nga/giat/mage/location/LocationSaveTask.kt

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,49 +41,51 @@ class LocationSaveTask(val context: Context, private val listener: LocationDatab
4141
return location
4242
}
4343

44-
private fun saveLocation(location: Location) {
45-
Log.v(LOG_NAME, "Saving MAGE location to database.")
44+
private fun saveLocation(gpsLocation: Location) {
45+
Log.v(LOG_NAME, "Saving GPS location to database.")
4646

47-
if (location.time > 0) {
47+
if (gpsLocation.time > 0) {
4848
val locationProperties = ArrayList<LocationProperty>()
4949

5050
val locationHelper = LocationHelper.getInstance(context)
5151

5252
// build properties
53-
locationProperties.add(LocationProperty("accuracy", location.accuracy))
54-
locationProperties.add(LocationProperty("bearing", location.bearing))
55-
locationProperties.add(LocationProperty("speed", location.speed))
56-
locationProperties.add(LocationProperty("provider", location.provider))
57-
locationProperties.add(LocationProperty("altitude", location.altitude))
53+
locationProperties.add(LocationProperty("accuracy", gpsLocation.accuracy))
54+
locationProperties.add(LocationProperty("bearing", gpsLocation.bearing))
55+
locationProperties.add(LocationProperty("speed", gpsLocation.speed))
56+
locationProperties.add(LocationProperty("provider", gpsLocation.provider))
57+
locationProperties.add(LocationProperty("altitude", gpsLocation.altitude))
5858

5959
val level = batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
6060
level?.let {
6161
locationProperties.add(LocationProperty("battery_level", it))
6262
}
6363

64-
var currentUser: User? = null
64+
var user: User? = null
6565
try {
66-
currentUser = UserHelper.getInstance(context).readCurrentUser()
66+
user = UserHelper.getInstance(context).readCurrentUser()
6767
} catch (e: UserException) {
68-
Log.e(LOG_NAME, "Could not get current User!")
68+
Log.e(LOG_NAME, "Error reading current user from database", e)
6969
}
7070

71-
// build location
72-
val loc = mil.nga.giat.mage.sdk.datastore.location.Location(
73-
"Feature",
74-
currentUser,
75-
locationProperties,
76-
Point(location.longitude, location.latitude),
77-
Date(location.time),
78-
currentUser!!.currentEvent)
71+
if (user == null || user.currentEvent == null) {
72+
Log.e(LOG_NAME, "Not saving location for user: $user in event: ${user?.currentEvent}")
73+
return
74+
}
7975

80-
// save the location
8176
try {
82-
locationHelper.create(loc)
83-
} catch (le: LocationException) {
84-
Log.e(LOG_NAME, "Unable to save current location locally!", le)
77+
val location = mil.nga.giat.mage.sdk.datastore.location.Location(
78+
"Feature",
79+
user,
80+
locationProperties,
81+
Point(gpsLocation.longitude, gpsLocation.latitude),
82+
Date(gpsLocation.time),
83+
user.currentEvent)
84+
85+
locationHelper.create(location)
86+
} catch (e: LocationException) {
87+
Log.e(LOG_NAME, "Error saving GPS location", e)
8588
}
86-
8789
}
8890
}
8991

0 commit comments

Comments
 (0)