Compare commits

..

1 commit

Author SHA1 Message Date
Filip Strajnar 0ae7622259 Preparations to ask for location permissions. 2024-10-20 14:47:34 +02:00
7 changed files with 28 additions and 192 deletions

View file

@ -26,8 +26,8 @@ android {
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
@ -50,9 +50,4 @@ dependencies {
// optional - Guava support for Room, including Optional and ListenableFuture
implementation("androidx.room:room-guava:$room_version")
implementation("io.jenetics:jpx:3.1.0")
// https://mvnrepository.com/artifact/org.codehaus.woodstox/woodstox-core-asl
implementation("org.codehaus.woodstox:woodstox-core-asl:4.4.1")
}

View file

@ -2,15 +2,9 @@ package com.proculite.logmylocation;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import java.util.List;
@Dao
public interface LocationDao {
@Insert
void insert(LocationEntity location);
@Query("SELECT * FROM locationentity")
List<LocationEntity> getAll();
}

View file

@ -65,10 +65,6 @@ public class LocationLoggingService extends Service implements LocationListener
return super.onStartCommand(intent, flags, startId);
}
public List<LocationEntity> allLocations(){
return locationDao.getAll();
}
@SuppressLint("MissingPermission")
public void subscribeToLocationUpdates(LocationListener locationListener)
{

View file

@ -3,8 +3,6 @@ package com.proculite.logmylocation;
import android.location.LocationListener;
import android.os.Binder;
import java.util.List;
public class LocationLoggingServiceBinder extends Binder {
private final LocationLoggingService service;
@ -19,8 +17,4 @@ public class LocationLoggingServiceBinder extends Binder {
public void subscribeToLocationUpdates(LocationListener locationListener){
this.service.subscribeToLocationUpdates(locationListener);
}
public List<LocationEntity> allLocations(){
return this.service.allLocations();
}
}

View file

@ -1,47 +1,35 @@
package com.proculite.logmylocation;
import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.Writer;
import io.jenetics.jpx.GPX;
import io.jenetics.jpx.WayPoint;
public class MainActivity extends AppCompatActivity implements LocationListener, ServiceConnection {
private final String TAG = MainActivity.class.getName();
private Button exportButton;
private WriteToFile writeToFile;
private static boolean includeComments = false;
private static Double accuracyThreshold = 2.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
writeToFile = new WriteToFile(this,"application/gpx");
Intent serviceIntent = new Intent(this, LocationLoggingService.class);
startForegroundService(serviceIntent);
@ -52,8 +40,6 @@ public class MainActivity extends AppCompatActivity implements LocationListener,
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
exportButton = findViewById(R.id.buttonExport);
}
@Override
@ -88,89 +74,6 @@ public class MainActivity extends AppCompatActivity implements LocationListener,
LocationLoggingServiceBinder binder = (LocationLoggingServiceBinder) iBinder;
binder.subscribeToLocationUpdates(this);
if(exportButton != null){
exportButton.setOnClickListener(v -> {
Log.d(TAG, "Export button clicked.");
new Thread(() -> {
GPX gpx = GPX.builder().addTrack(track -> track.addSegment(segment -> {
for(LocationEntity location : binder.allLocations()){
WayPoint wayPoint = locationToWayPoint(location);
if(wayPoint != null) {
segment.addPoint(wayPoint);
}
}
})).build();
Log.d(TAG, "Built GPX.");
writeToFile.write("output.gpx", outputStream -> {
try {
GPX.Writer.of(GPX.Writer.Indent.SPACE4).write(gpx, outputStream);
} catch (IOException e) {
Log.e(TAG, "Failed to write.");
}
});
}).start();
});
}
}
private static WayPoint locationToWayPoint(LocationEntity location) {
if(accuracyThreshold != null)
{
if(location.accuracy == null) {
return null;
}
if(location.accuracy > accuracyThreshold) {
return null;
}
}
WayPoint.Builder builder = WayPoint.builder()
.lat(location.latitude)
.lon(location.longitude)
.time(location.unixTime);
if(location.speed != null){
builder.speed(location.speed);
}
if(location.bearing != null){
builder.course(location.bearing);
}
if(location.altitude != null){
builder.ele(location.altitude);
}
if(includeComments) {
StringBuilder commentBuilder = new StringBuilder();
if (location.isMock) {
commentBuilder.append("Location is mock.\n");
}
if (location.accuracy != null) {
commentBuilder.append(String.format("Accuracy is expected to be within %s meters.\n", location.accuracy));
}
if (location.altitudeAccuracy != null) {
commentBuilder.append(String.format("Altitude accuracy is expected to be within %s meters.\n", location.altitudeAccuracy));
}
if (location.bearingAccuracy != null) {
commentBuilder.append(String.format("Bearing accuracy is expected to be within %s degrees.\n", location.bearingAccuracy));
}
if (location.speedAccuracy != null) {
commentBuilder.append(String.format("Speed accuracy is expected to be within %s meters per second.\n", location.speedAccuracy));
}
builder.cmt(commentBuilder.toString());
}
return builder.build();
}
@Override
@ -189,4 +92,25 @@ public class MainActivity extends AppCompatActivity implements LocationListener,
Log.d(TAG, "Service null binding.");
ServiceConnection.super.onNullBinding(name);
}
private void ensureLocationPermission()
{
int fineLocationPermission = ContextCompat
.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION);
boolean fineLocationGranted = fineLocationPermission == PackageManager.PERMISSION_GRANTED;
if(!fineLocationGranted)
{
ActivityResultLauncher<String[]> permissionsLauncher = registerForActivityResult(
new ActivityResultContracts.RequestMultiplePermissions(), isGranted -> {});
permissionsLauncher.launch(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION
});
}
else
{
Log.d(TAG, "Permissions already granted.");
}
}
}

View file

@ -1,58 +0,0 @@
package com.proculite.logmylocation;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.function.Consumer;
public class WriteToFile implements ActivityResultCallback<Uri> {
private final String TAG = WriteToFile.class.getName();
private Consumer<OutputStream> fileConsumer;
private final AppCompatActivity activityCompat;
private final ActivityResultLauncher<String> launcher;
public WriteToFile(AppCompatActivity activity, String fileMimeType)
{
this.activityCompat = activity;
this.launcher = activity.registerForActivityResult(
new ActivityResultContracts.CreateDocument(fileMimeType), this);
}
@Override
public void onActivityResult(Uri o) {
if(o == null || o.getPath() == null)
{
Log.d(TAG, "Attempting to write to null path.");
return;
}
Log.d(TAG, String.format("Writing to file: %s", o.getPath()));
try {
try (ParcelFileDescriptor fileDescriptor = this.activityCompat.getContentResolver().openFileDescriptor(o, "w")) {
if (fileDescriptor == null) {
Log.e(TAG, "Parcel file descriptor is null.");
return;
}
try (FileOutputStream fileOutputStream = new FileOutputStream(fileDescriptor.getFileDescriptor())) {
fileConsumer.accept(fileOutputStream);
}
}
} catch (IOException e) {
Log.e(TAG, "Failed to write to file.");
}
}
public void write(String fileName, Consumer<OutputStream> fileConsumer){
this.fileConsumer = fileConsumer;
launcher.launch(fileName);
}
}

View file

@ -18,13 +18,4 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/buttonExport"
app:layout_constraintTop_toBottomOf="@+id/textViewMain"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:text="Export"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</androidx.constraintlayout.widget.ConstraintLayout>