logmylocation/app/src/main/java/com/proculite/logmylocation/LocationLoggingService.java

154 lines
4.6 KiB
Java

package com.proculite.logmylocation;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.location.LocationManagerCompat;
import androidx.room.Room;
import java.util.List;
public class LocationLoggingService extends Service implements LocationListener {
private final String TAG = LocationLoggingService.class.getName();
private LocationDao locationDao;
public LocationLoggingService() {
}
@Override
public IBinder onBind(Intent intent) {
return new LocationLoggingServiceBinder(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service started.");
LocationDatabase database = Room
.databaseBuilder(getApplicationContext(), LocationDatabase.class, "location")
.fallbackToDestructiveMigration()
.build();
locationDao = database.locationDao();
subscribeToLocationUpdates(this);
return super.onStartCommand(intent, flags, startId);
}
@SuppressLint("MissingPermission")
public void subscribeToLocationUpdates(LocationListener locationListener)
{
LocationManager locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
@Override
public void onDestroy() {
Log.d(TAG, "Service has been destroyed.");
}
@Override
public void onCreate() {
Log.d(TAG, "Service created.");
super.onCreate();
}
@Override
public void onLocationChanged(@NonNull Location location) {
new Thread(()->{
Log.d(TAG, String.format(
"New location. Latitude: %s Longitude: %s Altitude: %s",
location.getLatitude(),
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
public void onFlushComplete(int requestCode) {
Log.d(TAG, "Flush complete.");
LocationListener.super.onFlushComplete(requestCode);
}
@Override
public void onProviderEnabled(@NonNull String provider) {
Log.d(TAG, "GPS provider became enabled.");
LocationListener.super.onProviderEnabled(provider);
}
@Override
public void onProviderDisabled(@NonNull String provider) {
Log.d(TAG, "GPS provider became disabled.");
LocationListener.super.onProviderDisabled(provider);
}
}