storage helper only keeps on track for now - old ones get deleted

This commit is contained in:
y20k 2016-09-26 15:25:25 +02:00
parent 4f1017ced9
commit deed2f624d

View file

@ -34,7 +34,11 @@ import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -53,8 +57,11 @@ public class StorageHelper implements TrackbookKeys {
/* Constructor */ /* Constructor */
public StorageHelper(Activity activity) { public StorageHelper(Activity activity) {
// store activity
mActivity = activity; mActivity = activity;
mFolder = mActivity.getExternalFilesDir("Tracks");
// set name sub-directory to "tracks"
mFolder = mActivity.getExternalFilesDir("tracks");
// mFolder = getTracksDirectory(); // mFolder = getTracksDirectory();
// create folder if necessary // create folder if necessary
@ -71,6 +78,8 @@ public class StorageHelper implements TrackbookKeys {
Date recordingStart = track.getRecordingStart(); Date recordingStart = track.getRecordingStart();
if (mFolder.exists() && mFolder.isDirectory() && mFolder.canWrite() && recordingStart != null) { if (mFolder.exists() && mFolder.isDirectory() && mFolder.canWrite() && recordingStart != null) {
File lastTrackFile = getLastTrack();
// construct filename from track recording date // construct filename from track recording date
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
String fileName = dateFormat.format(recordingStart) + ".trackbook"; String fileName = dateFormat.format(recordingStart) + ".trackbook";
@ -86,8 +95,12 @@ public class StorageHelper implements TrackbookKeys {
bw.write(json); bw.write(json);
} catch (IOException e) { } catch (IOException e) {
LogHelper.e(LOG_TAG, "Unable to saving track to external storage (IOException): " + file.toString()); LogHelper.e(LOG_TAG, "Unable to saving track to external storage (IOException): " + file.toString());
return false;
} }
// if write was successful delete last file
lastTrackFile.delete();
return true; return true;
} else { } else {
@ -128,9 +141,10 @@ public class StorageHelper implements TrackbookKeys {
/* Gets the last track from directory */ /* Gets the last track from directory */
public File getLastTrack() { public File getLastTrack() {
if (mFolder != null && mFolder.isDirectory()) { if (mFolder != null && mFolder.isDirectory()) {
File[] files = mFolder.listFiles(); List<File> files = new ArrayList<File>(Arrays.asList(mFolder.listFiles()));
Collections.sort(files);
// TODO // TODO
return files[0]; return files.get(files.size() - 1);
} }
// TODO // TODO
return null; return null;