-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Local logfiles implementation (draft) and FTP
- Loading branch information
1 parent
0f1e492
commit f589fb4
Showing
10 changed files
with
338 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
app/src/main/java/it/francescogabbrielli/apps/sensorlogger/LogFileWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package it.francescogabbrielli.apps.sensorlogger; | ||
|
||
import android.util.Log; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class LogFileWriter extends LogTarget { | ||
|
||
private final static String TAG = LogFTPUploader.class.getSimpleName(); | ||
|
||
private File folder; | ||
private ExecutorService exec; | ||
|
||
public LogFileWriter(File folder) { | ||
this.folder = folder; | ||
exec = Executors.newSingleThreadExecutor(); | ||
} | ||
|
||
@Override | ||
public void send(final byte[] data, final String filename, final Runnable callback) { | ||
exec.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
FileOutputStream out = null; | ||
try { | ||
out = new FileOutputStream(new File(folder, filename)); | ||
out.write(data); | ||
if (callback!=null) | ||
callback.run(); | ||
Log.d(TAG, "Data written to " + filename); | ||
} catch (Exception e) { | ||
Log.e(TAG, "Error writing file", e); | ||
} finally { | ||
try { | ||
out.close(); | ||
} catch (Exception e) { | ||
|
||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
exec.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
new Thread() { | ||
@Override | ||
public void run() { | ||
try { | ||
exec.awaitTermination(5, TimeUnit.SECONDS); | ||
exec.shutdown(); | ||
} catch(InterruptedException e) { | ||
Log.e(TAG, "Unexpected interruption", e); | ||
} | ||
} | ||
}.start(); | ||
} | ||
}); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/it/francescogabbrielli/apps/sensorlogger/LogTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package it.francescogabbrielli.apps.sensorlogger; | ||
|
||
/** | ||
* Generic target where to log data | ||
* | ||
* TODO: move executor framework here | ||
*/ | ||
public abstract class LogTarget { | ||
|
||
/** | ||
* Send data to the target | ||
* | ||
* TODO: add a kind of listener interface support instead of single callbacks | ||
*/ | ||
public abstract void send(final byte[] data, final String filename, final Runnable callback); | ||
|
||
/** | ||
* Implement final cleanup | ||
*/ | ||
public abstract void close(); | ||
|
||
} |
Oops, something went wrong.