added moved files

This commit is contained in:
toni
2019-01-30 16:27:56 +01:00
parent 691f3a684a
commit 80e8fd499d
7 changed files with 1597 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
package de.tonifetzer.conductorssensor.utilities;
/**
* Created by toni on 12/01/18.
*/
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import java.io.File;
/**
* Class: DataFolder
* Description: SDK save file class. Is able to open a folder on the device independent of the given
* device and android skd version.
*/
public class DataFolder {
private File folder;
private static final String TAG = "DataFolder";
public DataFolder(Context context, String folderName){
// 1) try external data folder
folder = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), folderName);
if (isOK(folder)) {return;}
// 2) try sd-card folder
folder = new File(Environment.getExternalStorageDirectory() + "/" + folderName);
if (isOK(folder)) {return;}
// 3) try internal data folder
folder = new File(context.getApplicationInfo().dataDir);
if (isOK(folder)) {return;}
// all failed
throw new RuntimeException("failed to create/access storage folder");
}
/** ensure the given folder is OK */
private static final boolean isOK(final File folder) {
folder.mkdirs();
final boolean ok = folder.exists() && folder.isDirectory();
if (ok) {
Log.d(TAG, "using: " + folder);
} else {
Log.d(TAG, "not OK: " + folder);
}
return ok;
}
public File getFolder(){
return folder;
}
}

View File

@@ -0,0 +1,138 @@
package de.tonifetzer.conductorssensor.utilities;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import de.tonifetzer.conductorssensor.R;
/**
* Created by toni on 14/01/18.
*/
public class SensorDataFileWriter {
private static final int FLUSH_LIMIT = 2*1024*1024;
private static final String TAG = "SensorDataFileWriter";
private File mSensorDataFile;
private FileOutputStream mFileOutputStream;
private StringBuilder mStringBuilder = new StringBuilder();
private final DataFolder mFolder;
private boolean mStreamOpenend = false;
public SensorDataFileWriter(Context context) {
mFolder = new DataFolder(context, "sensorOutFiles");
//write some description for this file in the first line
EditText editView = ((Activity) context).findViewById(R.id.comments);
TextView textView = ((Activity) context).findViewById(R.id.bpmText);
writeDescription("Kommentar: " + editView.getText().toString() + "\nMetronom: " + textView.getText().toString() + " bpm");
//open connection to file
open();
}
public final void writeVector3D(long ts, int id, double x, double y, double z){
synchronized (this) {
if (mStreamOpenend) {
mStringBuilder.append(ts).append(';').append(id).append(';').append(x).append(';').append(y).append(';').append(z).append('\n');
if (mStringBuilder.length() > FLUSH_LIMIT) {flush(false);}
} else {
open();
}
}
}
private void writeDescription(String str){
synchronized (this) {
mStringBuilder.append(str).append('\n').append('\n');
}
}
// flush data to disk
public final void toDisk(){
synchronized (this) {
flush(true);
close();
}
}
/** helper method for exception-less writing. DO NOT CALL DIRECTLY! */
private void _write(final byte[] data) {
try {
mFileOutputStream.write(data);
Log.d(TAG, "flushed " + data.length + " bytes to disk");
} catch (final Exception e) {
throw new RuntimeException("error while writing log-file", e);
}
}
/** helper-class for background writing */
class FlushAsync extends AsyncTask<byte[], Integer, Integer> {
@Override
protected final Integer doInBackground(byte[][] data) {
_write(data[0]);
return null;
}
};
/** flush current buffer-contents to disk */
private void flush(boolean sync) {
// fetch current buffer contents to write and hereafter empty the buffer
// this action MUST be atomic, just like the add-method
byte[] data = null;
synchronized (this) {
data = mStringBuilder.toString().getBytes(); // fetch data to write
mStringBuilder.setLength(0); // reset the buffer
}
// write
if (sync) {
// write to disk using the current thread
_write(data);
} else {
// write to disk using a background-thread
new FlushAsync().execute(new byte[][] {data});
}
}
private void open() {
mSensorDataFile = new File(mFolder.getFolder(), System.currentTimeMillis() + ".csv");
try {
mFileOutputStream = new FileOutputStream(mSensorDataFile);
mStreamOpenend = true;
Log.d(TAG, "will write to: " + mSensorDataFile.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
mStreamOpenend = false;
}
}
private void close() {
try {
mFileOutputStream.close();
mStreamOpenend = false;
} catch (IOException e) {
e.printStackTrace();
}
}
}