Compare commits
No commits in common. "dev" and "master" have entirely different histories.
|
|
@ -26,8 +26,8 @@ android {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
compileOptions {
|
compileOptions {
|
||||||
sourceCompatibility = JavaVersion.VERSION_17
|
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||||
targetCompatibility = JavaVersion.VERSION_17
|
targetCompatibility = JavaVersion.VERSION_1_8
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,19 +40,4 @@ dependencies {
|
||||||
testImplementation(libs.junit)
|
testImplementation(libs.junit)
|
||||||
androidTestImplementation(libs.ext.junit)
|
androidTestImplementation(libs.ext.junit)
|
||||||
androidTestImplementation(libs.espresso.core)
|
androidTestImplementation(libs.espresso.core)
|
||||||
|
|
||||||
val room_version = "2.6.1"
|
|
||||||
|
|
||||||
implementation("androidx.room:room-runtime:$room_version")
|
|
||||||
annotationProcessor("androidx.room:room-compiler:$room_version")
|
|
||||||
// optional - RxJava3 support for Room
|
|
||||||
implementation("androidx.room:room-rxjava3:$room_version")
|
|
||||||
|
|
||||||
// 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")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -5,10 +5,6 @@
|
||||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
||||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
|
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
|
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>
|
|
||||||
|
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
|
@ -23,9 +19,7 @@
|
||||||
<service
|
<service
|
||||||
android:name=".LocationLoggingService"
|
android:name=".LocationLoggingService"
|
||||||
android:enabled="true"
|
android:enabled="true"
|
||||||
android:exported="true"
|
android:exported="true"></service>
|
||||||
android:foregroundServiceType="location"
|
|
||||||
/>
|
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
|
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
package com.proculite.logmylocation;
|
|
||||||
|
|
||||||
import androidx.room.Database;
|
|
||||||
import androidx.room.RoomDatabase;
|
|
||||||
|
|
||||||
@Database(entities = {LocationEntity.class}, version = 3)
|
|
||||||
public abstract class LocationDatabase extends RoomDatabase {
|
|
||||||
public abstract LocationDao locationDao();
|
|
||||||
}
|
|
||||||
|
|
@ -1,53 +0,0 @@
|
||||||
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 = "bearing_accuracy")
|
|
||||||
public Float bearingAccuracy;
|
|
||||||
|
|
||||||
@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 = "elapsed_realtime_age")
|
|
||||||
public Long elapsedRealtimeAge;
|
|
||||||
|
|
||||||
@ColumnInfo(name = "is_mock")
|
|
||||||
public Boolean isMock;
|
|
||||||
}
|
|
||||||
|
|
@ -1,30 +1,22 @@
|
||||||
package com.proculite.logmylocation;
|
package com.proculite.logmylocation;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.app.NotificationChannel;
|
|
||||||
import android.app.NotificationManager;
|
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.ServiceInfo;
|
|
||||||
import android.location.Location;
|
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.app.NotificationCompat;
|
|
||||||
import androidx.core.app.ServiceCompat;
|
|
||||||
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() {
|
||||||
}
|
}
|
||||||
|
|
@ -38,37 +30,11 @@ 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.");
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
||||||
NotificationChannel notificationChannel = new NotificationChannel(
|
|
||||||
"Foreground",
|
|
||||||
"Foreground",
|
|
||||||
NotificationManager.IMPORTANCE_DEFAULT
|
|
||||||
);
|
|
||||||
NotificationManager notificationManager = getSystemService(NotificationManager.class);
|
|
||||||
notificationManager.createNotificationChannel(notificationChannel);
|
|
||||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, notificationChannel.getId());
|
|
||||||
builder.setSmallIcon(R.drawable.ic_launcher_foreground);
|
|
||||||
builder.setContentText("Started service in foreground.");
|
|
||||||
|
|
||||||
ServiceCompat.startForeground(this,3784583, builder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<LocationEntity> allLocations(){
|
|
||||||
return locationDao.getAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
public void subscribeToLocationUpdates(LocationListener locationListener)
|
public void subscribeToLocationUpdates(LocationListener locationListener)
|
||||||
{
|
{
|
||||||
|
|
@ -90,82 +56,12 @@ 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();
|
|
||||||
locationEntity.unixTime = location.getTime();
|
|
||||||
|
|
||||||
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 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
||||||
locationEntity.elapsedRealtimeAge = location.getElapsedRealtimeAgeMillis();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(location.hasBearing())
|
|
||||||
{
|
|
||||||
locationEntity.bearing = location.getBearing();
|
|
||||||
if(location.hasBearingAccuracy())
|
|
||||||
{
|
|
||||||
locationEntity.bearingAccuracy = location.getBearingAccuracyDegrees();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,9 @@
|
||||||
package com.proculite.logmylocation;
|
package com.proculite.logmylocation;
|
||||||
|
|
||||||
import android.location.LocationListener;
|
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class LocationLoggingServiceBinder extends Binder {
|
public class LocationLoggingServiceBinder extends Binder {
|
||||||
private final LocationLoggingService service;
|
public final LocationLoggingService service;
|
||||||
|
|
||||||
public LocationLoggingServiceBinder(LocationLoggingService service){
|
public LocationLoggingServiceBinder(LocationLoggingService service){
|
||||||
this.service = service;
|
this.service = service;
|
||||||
|
|
@ -15,12 +12,4 @@ public class LocationLoggingServiceBinder extends Binder {
|
||||||
public LocationLoggingService getService(){
|
public LocationLoggingService getService(){
|
||||||
return this.service;
|
return this.service;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void subscribeToLocationUpdates(LocationListener locationListener){
|
|
||||||
this.service.subscribeToLocationUpdates(locationListener);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<LocationEntity> allLocations(){
|
|
||||||
return this.service.allLocations();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ import android.location.LocationListener;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Button;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import androidx.activity.EdgeToEdge;
|
import androidx.activity.EdgeToEdge;
|
||||||
|
|
@ -19,31 +18,15 @@ import androidx.core.graphics.Insets;
|
||||||
import androidx.core.view.ViewCompat;
|
import androidx.core.view.ViewCompat;
|
||||||
import androidx.core.view.WindowInsetsCompat;
|
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 {
|
public class MainActivity extends AppCompatActivity implements LocationListener, ServiceConnection {
|
||||||
private final String TAG = MainActivity.class.getName();
|
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
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
writeToFile = new WriteToFile(this,"application/gpx");
|
|
||||||
|
|
||||||
Intent serviceIntent = new Intent(this, LocationLoggingService.class);
|
Intent serviceIntent = new Intent(this, LocationLoggingService.class);
|
||||||
startForegroundService(serviceIntent);
|
startService(serviceIntent);
|
||||||
|
|
||||||
EdgeToEdge.enable(this);
|
EdgeToEdge.enable(this);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
@ -52,8 +35,6 @@ public class MainActivity extends AppCompatActivity implements LocationListener,
|
||||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
||||||
return insets;
|
return insets;
|
||||||
});
|
});
|
||||||
|
|
||||||
exportButton = findViewById(R.id.buttonExport);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -87,90 +68,7 @@ public class MainActivity extends AppCompatActivity implements LocationListener,
|
||||||
Log.d(TAG, "Service connected.");
|
Log.d(TAG, "Service connected.");
|
||||||
|
|
||||||
LocationLoggingServiceBinder binder = (LocationLoggingServiceBinder) iBinder;
|
LocationLoggingServiceBinder binder = (LocationLoggingServiceBinder) iBinder;
|
||||||
binder.subscribeToLocationUpdates(this);
|
binder.getService().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
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -18,13 +18,4 @@
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="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>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
Loading…
Reference in a new issue