feat: Copy the log file to "Downloads/GPSTest" after logging (Android 11 and up)

Closes https://github.com/barbeau/gpstest/issues/633
This commit is contained in:
barbeau
2023-04-21 13:03:28 -04:00
parent dea3e77225
commit 23f0de3d7b
2 changed files with 36 additions and 6 deletions

View File

@@ -1,17 +1,24 @@
package com.android.gpstest.io;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Downloads;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import com.android.gpstest.Application;
import com.android.gpstest.R;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -24,6 +31,8 @@ public abstract class BaseFileLogger implements FileLogger {
protected final String TAG = this.getClass().getName();
protected static final String FILE_PREFIX = "gnss_log";
protected static final String DIRECTORY = "Download/GPSTest";
protected final Context context;
protected BufferedWriter fileWriter;
@@ -177,6 +186,10 @@ public abstract class BaseFileLogger implements FileLogger {
return;
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && file != null) {
copyFileToDownloads(file);
}
}
protected void logException(String errorMessage, Exception e) {
@@ -188,4 +201,21 @@ public abstract class BaseFileLogger implements FileLogger {
Log.e(TAG, errorMessage);
Toast.makeText(context, errorMessage, Toast.LENGTH_LONG).show();
}
@RequiresApi(Build.VERSION_CODES.R)
protected void copyFileToDownloads(File fileToCopy) {
ContentResolver contentResolver = context.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Downloads.DISPLAY_NAME, fileToCopy.getName());
contentValues.put(Downloads.RELATIVE_PATH, DIRECTORY);
Uri fileUri =
contentResolver.insert(
MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), contentValues);
try (OutputStream outputStream =
contentResolver.openOutputStream(fileUri)) {
Files.copy(fileToCopy.toPath(), outputStream);
} catch (IOException e) {
Log.e(TAG, "Error while writing to Downloads folder:", e);
}
}
}

View File

@@ -12,14 +12,14 @@ Steps to log and view data:
1. In the GPSTest app, go to "Settings", scroll down, tap on "Logging and Output" and under "File Output" make sure the box is checked for each data output type you'd like to see (see next sections).
1. You can share CSV and JSON log files via the "Share" button in the top action bar of the app, or by copying them off internal memory or SD card over USB.
*Note: The file output from GPSTest is buffered, so if you manually copy the file off the device while the app is running you can end up with a partial file if the buffer hasn't been fully flushed. You can force the flush of the buffer by killing the app (e.g., via the Back button), and then if you refresh your file view you should get the complete file with all your data.*
*Note: The file output from GPSTest is buffered, so if you manually copy the file off the device while the app is running you can end up with a partial file if the buffer hasn't been fully flushed. You can force the flush of the buffer by tapping the "Share" button in the app and sharing the file or the "Stop" button in the notification, and then if you refresh your file view you should get the complete file with all your data.*
#### Data output - CSV
Here are the details of file logging:
* CSV format - Same file format as the Google [GPS Measurement Tools (GNSS Logger) project](https://github.com/google/gps-measurement-tools) so you can use MATLAB tools under that project to [analyze the data](https://github.com/google/gps-measurement-tools#to-process-a-log-file-you-collected-from-gnsslogger).
* Files are saved to your Android device storage under the `gnss_log` directory.
* After you've enabled logging via Settings, each time you start the app a new file will be created named with the date and time (e.g., `gnss_log_2019_09_11_13_09_50.txt`). If you end the app (e.g., hit back button) and restart it, another file gets created.
* Files are saved to your Android device storage under the `Downloads/GPSTest` directory for Android 11 and up, and in the `gnss_log` directory for Android 10 and under.
* After you've enabled logging via Settings, each time you start the app a new file will be created named with the date and time (e.g., `gnss_log_2019_09_11_13_09_50.txt`). If you tap on the notification "Stop" button or share the file, another file gets created.
* Each row of the file is prefixed with a string designating the data type:
* `Raw` - Raw GNSS measurements
* `Fix` - Location fix information
@@ -211,7 +211,7 @@ Here's a screenshot from the GPS Measurement Tools desktop software:
![image](https://user-images.githubusercontent.com/928045/64804800-844fae00-d55d-11e9-8212-b0ef65885dc7.png)
## Accessing the system log via Android Studio
## Accessing the real-time output via Android Studio
You can view the data output from GPSTest by using Android Monitor, which is included with Android Studio.