Compare commits
9 commits
1a64788590
...
1b7e725026
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1b7e725026 | ||
|
|
678cce4536 | ||
|
|
c840e621e2 | ||
|
|
1bd6b6833f | ||
|
|
8eeb20e1ad | ||
|
|
3b30dd585f | ||
|
|
33ccb30f8a | ||
|
|
e38d9b7170 | ||
|
|
412a4666c3 |
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.proculite.logmylocation;
|
||||||
|
|
||||||
|
import androidx.room.Dao;
|
||||||
|
import androidx.room.Insert;
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
public interface LocationDao {
|
||||||
|
@Insert
|
||||||
|
void insert(LocationEntity location);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.proculite.logmylocation;
|
||||||
|
|
||||||
|
import androidx.room.Database;
|
||||||
|
import androidx.room.RoomDatabase;
|
||||||
|
|
||||||
|
@Database(entities = {LocationEntity.class}, version = 2)
|
||||||
|
public abstract class LocationDatabase extends RoomDatabase {
|
||||||
|
public abstract LocationDao locationDao();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.proculite.logmylocation;
|
||||||
|
|
||||||
|
import androidx.room.ColumnInfo;
|
||||||
|
import androidx.room.Entity;
|
||||||
|
import androidx.room.PrimaryKey;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class LocationEntity {
|
||||||
|
@PrimaryKey(autoGenerate = true)
|
||||||
|
public long id;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "accuracy")
|
||||||
|
public Float accuracy;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "altitude")
|
||||||
|
public Double altitude;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "altitude_accuracy")
|
||||||
|
public Float altitudeAccuracy;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "msl_altitude")
|
||||||
|
public Double mslAltitude;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "msl_altitude_accuracy")
|
||||||
|
public Float mslAltitudeAccuracy;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "bearing")
|
||||||
|
public Float bearing;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "latitude")
|
||||||
|
public double latitude;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "longitude")
|
||||||
|
public double longitude;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "speed")
|
||||||
|
public Float speed;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "speed_accuracy")
|
||||||
|
public Float speedAccuracy;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "unix_time")
|
||||||
|
public long unixTime;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "is_mock")
|
||||||
|
public boolean isMock;
|
||||||
|
}
|
||||||
|
|
@ -7,16 +7,19 @@ 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;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.core.location.LocationManagerCompat;
|
import androidx.core.location.LocationManagerCompat;
|
||||||
|
import androidx.room.Room;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class LocationLoggingService extends Service implements LocationListener {
|
public class LocationLoggingService extends Service implements LocationListener {
|
||||||
private final String TAG = LocationLoggingService.class.getName();
|
private final String TAG = LocationLoggingService.class.getName();
|
||||||
|
private LocationDao locationDao;
|
||||||
|
|
||||||
public LocationLoggingService() {
|
public LocationLoggingService() {
|
||||||
}
|
}
|
||||||
|
|
@ -30,6 +33,13 @@ public class LocationLoggingService extends Service implements LocationListener
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
Log.d(TAG, "Service started.");
|
Log.d(TAG, "Service started.");
|
||||||
|
|
||||||
|
LocationDatabase database = Room
|
||||||
|
.databaseBuilder(getApplicationContext(), LocationDatabase.class, "location")
|
||||||
|
.fallbackToDestructiveMigration()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
locationDao = database.locationDao();
|
||||||
|
|
||||||
subscribeToLocationUpdates(this);
|
subscribeToLocationUpdates(this);
|
||||||
|
|
||||||
return super.onStartCommand(intent, flags, startId);
|
return super.onStartCommand(intent, flags, startId);
|
||||||
|
|
@ -56,12 +66,72 @@ public class LocationLoggingService extends Service implements LocationListener
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLocationChanged(@NonNull Location location) {
|
public void onLocationChanged(@NonNull Location location) {
|
||||||
Log.d(TAG, String.format(
|
new Thread(()->{
|
||||||
"New location. Latitude: %s Longitude: %s Altitude: %s",
|
Log.d(TAG, String.format(
|
||||||
location.getLatitude(),
|
"New location. Latitude: %s Longitude: %s Altitude: %s",
|
||||||
location.getLongitude(),
|
location.getLatitude(),
|
||||||
location.getAltitude())
|
location.getLongitude(),
|
||||||
);
|
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