Skip to content

Commit

Permalink
Tests ExportToStorageTask
Browse files Browse the repository at this point in the history
  • Loading branch information
Binnette committed Dec 2, 2024
1 parent 629c723 commit 63feaf1
Show file tree
Hide file tree
Showing 3 changed files with 407 additions and 201 deletions.
120 changes: 78 additions & 42 deletions app/src/main/java/net/osmtracker/gpx/ExportToStorageTask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.osmtracker.gpx;

import static net.osmtracker.util.FileSystemUtils.getUniqueChildNameFor;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
Expand All @@ -15,35 +17,57 @@
import java.io.File;
import java.util.Date;

import static net.osmtracker.util.FileSystemUtils.getUniqueChildNameFor;

import androidx.core.content.ContextCompat;

/**
* Exports to the external storage / SD card
* in a folder defined by the user.
* ExportToStorageTask is responsible for exporting track data to the device's storage.
* It extends the ExportTrackTask class and provides specific implementations for
* exporting track data to a directory on the external storage.
*/
public class ExportToStorageTask extends ExportTrackTask {

private static final String TAG = ExportToStorageTask.class.getSimpleName();
private String ERROR_MESSAGE;


private final String ERROR_MESSAGE;
private final DataHelper dataHelper;
private final SharedPreferences sharedPreferences;

/**
* Constructor for ExportToStorageTask.
*
* @param context the context of the application
* @param trackId the IDs of the tracks to be exported
*/
public ExportToStorageTask(Context context, long... trackId) {
this(context, new DataHelper(context), trackId);
}

/**
* Constructor for ExportToStorageTask with a DataHelper instance.
*
* @param context the context of the application
* @param dataHelper the DataHelper instance for accessing track data
* @param trackId the IDs of the tracks to be exported
*/
public ExportToStorageTask(Context context, DataHelper dataHelper, long... trackId) {
super(context, trackId);
this.dataHelper = dataHelper;
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
ERROR_MESSAGE = context.getString(R.string.error_create_track_dir);
}

/**
* Gets the directory where the track data will be exported.
*
* @param startDate the start date of the track
* @return the directory where the track data will be exported
* @throws ExportTrackException if the directory cannot be created
*/
@Override
protected File getExportDirectory(Date startDate) throws ExportTrackException {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

String trackName = getSanitizedTrackNameByStartDate(startDate);
boolean shouldCreateDirectoryPerTrack = shouldCreateDirectoryPerTrack(preferences);
File finalExportDirectory = getBaseExportDirectory(preferences);
Log.d(TAG, "absolute dir: " + finalExportDirectory.getAbsolutePath().toString());
boolean shouldCreateDirectoryPerTrack = shouldCreateDirectoryPerTrack();
File finalExportDirectory = getBaseExportDirectory();
Log.d(TAG, "absolute dir: " + finalExportDirectory.getAbsolutePath());

if( shouldCreateDirectoryPerTrack && trackName.length() >= 1){
if( shouldCreateDirectoryPerTrack && !trackName.isEmpty()){
String uniqueFolderName = getUniqueChildNameFor(finalExportDirectory, trackName, "");
finalExportDirectory = new File(finalExportDirectory, uniqueFolderName);
finalExportDirectory.mkdirs();
Expand All @@ -54,46 +78,58 @@ protected File getExportDirectory(Date startDate) throws ExportTrackException {
return finalExportDirectory;
}

/**
* Gets a sanitized track name based on the start date.
*
* @param startDate the start date of the track
* @return the sanitized track name
*/
public String getSanitizedTrackNameByStartDate(Date startDate) {
Track track = dataHelper.getTrackByStartDate(startDate);

String trackName = "";
if (track != null) {
trackName = track.getName();
}
if (trackName != null && !trackName.isEmpty()) {
trackName = trackName.replace("/", "_").trim();
}
return trackName;
}

/**
*
* @param startDate
* @return
*/
public String getSanitizedTrackNameByStartDate(Date startDate){

DataHelper dh = new DataHelper(context);
Track track = dh.getTrackByStartDate(startDate);

String trackName = "";
if(track != null){
trackName = track.getName();
}
if(trackName != null && trackName.length() >= 1) {
trackName = trackName.replace("/", "_").trim();
}
return trackName;
}

public boolean shouldCreateDirectoryPerTrack(SharedPreferences prefs){
return prefs.getBoolean(OSMTracker.Preferences.KEY_OUTPUT_DIR_PER_TRACK,
/**
* Determines whether a separate directory should be created for each track.
*
* @return true if a separate directory should be created for each track, false otherwise
*/
public boolean shouldCreateDirectoryPerTrack(){
return sharedPreferences.getBoolean(OSMTracker.Preferences.KEY_OUTPUT_DIR_PER_TRACK,
OSMTracker.Preferences.VAL_OUTPUT_GPX_OUTPUT_DIR_PER_TRACK);

}

// Checks if a volume containing external storage is available for read and write.
/**
* Checks if external storage is writable.
*
* @return true if external storage is writable, false otherwise
*/
private boolean isExternalStorageWritable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

// Create before returning if not exists
public File getBaseExportDirectory(SharedPreferences prefs) throws ExportTrackException {
/**
* Gets the base directory where the track data will be exported.
* Creates the directory if it does not exist.
*
* @return the base directory where the track data will be exported
* @throws ExportTrackException if the directory cannot be created or is not writable
*/
public File getBaseExportDirectory() throws ExportTrackException {

if (!isExternalStorageWritable()) {
throw new ExportTrackException(
context.getResources().getString(R.string.error_externalstorage_not_writable));
}
String exportDirectoryNameInPreferences = prefs.getString(
String exportDirectoryNameInPreferences = sharedPreferences.getString(
OSMTracker.Preferences.KEY_STORAGE_DIR, OSMTracker.Preferences.VAL_STORAGE_DIR);
Log.d(TAG,"exportDirectoryNameInPreferences: " + exportDirectoryNameInPreferences);

Expand Down
Loading

0 comments on commit 63feaf1

Please sign in to comment.