Allowing destructive migrations and writing locations to dabase in a new thread.
This commit is contained in:
parent
678cce4536
commit
1b7e725026
|
|
@ -7,6 +7,7 @@ import android.location.Location;
|
||||||
import android.location.LocationListener;
|
import android.location.LocationListener;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.location.LocationProvider;
|
import android.location.LocationProvider;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
|
@ -34,6 +35,7 @@ public class LocationLoggingService extends Service implements LocationListener
|
||||||
|
|
||||||
LocationDatabase database = Room
|
LocationDatabase database = Room
|
||||||
.databaseBuilder(getApplicationContext(), LocationDatabase.class, "location")
|
.databaseBuilder(getApplicationContext(), LocationDatabase.class, "location")
|
||||||
|
.fallbackToDestructiveMigration()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
locationDao = database.locationDao();
|
locationDao = database.locationDao();
|
||||||
|
|
@ -64,12 +66,72 @@ public class LocationLoggingService extends Service implements LocationListener
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLocationChanged(@NonNull Location location) {
|
public void onLocationChanged(@NonNull Location location) {
|
||||||
|
new Thread(()->{
|
||||||
Log.d(TAG, String.format(
|
Log.d(TAG, String.format(
|
||||||
"New location. Latitude: %s Longitude: %s Altitude: %s",
|
"New location. Latitude: %s Longitude: %s Altitude: %s",
|
||||||
location.getLatitude(),
|
location.getLatitude(),
|
||||||
location.getLongitude(),
|
location.getLongitude(),
|
||||||
location.getAltitude())
|
location.getAltitude())
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if(locationDao == null)
|
||||||
|
{
|
||||||
|
Log.w(TAG, "Location DAO is null, could not write new location.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
LocationEntity locationEntity = new LocationEntity();
|
||||||
|
|
||||||
|
locationEntity.latitude = location.getLatitude();
|
||||||
|
locationEntity.longitude = location.getLongitude();
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
|
locationEntity.isMock = location.isMock();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(location.hasAccuracy())
|
||||||
|
{
|
||||||
|
locationEntity.accuracy = location.getAccuracy();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(location.hasAltitude())
|
||||||
|
{
|
||||||
|
locationEntity.altitude = location.getAltitude();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(location.hasBearing())
|
||||||
|
{
|
||||||
|
locationEntity.bearing = location.getBearing();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(location.hasSpeed())
|
||||||
|
{
|
||||||
|
locationEntity.speed = location.getSpeed();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(location.hasSpeedAccuracy())
|
||||||
|
{
|
||||||
|
locationEntity.speedAccuracy = location.getSpeedAccuracyMetersPerSecond();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(location.hasVerticalAccuracy())
|
||||||
|
{
|
||||||
|
locationEntity.altitudeAccuracy = location.getVerticalAccuracyMeters();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
||||||
|
if(location.hasMslAltitude())
|
||||||
|
{
|
||||||
|
locationEntity.mslAltitude = location.getMslAltitudeMeters();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(location.hasMslAltitudeAccuracy())
|
||||||
|
{
|
||||||
|
locationEntity.mslAltitudeAccuracy = location.getMslAltitudeAccuracyMeters();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
locationDao.insert(locationEntity);
|
||||||
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue