Skip to content

Commit

Permalink
Permissions
Browse files Browse the repository at this point in the history
- implemented better checking
- made all optional in preferences
  • Loading branch information
francescogabbrielli committed Aug 8, 2017
1 parent 061d5b9 commit 0f1e492
Show file tree
Hide file tree
Showing 12 changed files with 371 additions and 156 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "it.francescogabbrielli.apps.sensorlogger"
minSdkVersion 11
targetSdkVersion 25
versionCode 3
versionName "0.2.1"
versionCode 5
versionName "0.2.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/assets/defaults.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# milliseconds between one sample and the other (frequency in Hz = 1 / (default_logging_rate/1000))
pref_logging_rate=100

# length of file in milliseconds
pref_logging_length=15000

# upload file every (ms)
pref_logging_update=1000
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package it.francescogabbrielli.apps.sensorlogger;

import android.content.Context;
import android.content.SharedPreferences;
import android.hardware.Camera;
import android.os.Handler;
import android.os.HandlerThread;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.SurfaceHolder;

Expand All @@ -20,15 +22,19 @@ public class CameraHandlerThread extends HandlerThread {

private Camera camera;

private boolean on;

public CameraHandlerThread(Context context) {
super("CameraHandlerThread");
this.context = context;
start();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
on = prefs.getBoolean(Util.PREF_CAPTURE_CAMERA, false);
handler = new Handler(getLooper());
}

public void openCamera(final SurfaceHolder holder) {
handler.post(new Runnable() {
public void openCamera(final SurfaceHolder holder, final Runnable callback) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
Expand All @@ -40,14 +46,36 @@ public void run() {
// Util.setCameraDisplayOrientation(MainActivity.this, 0, camera);XXX: rotation?
camera.setPreviewDisplay(holder);
camera.startPreview();
if (callback!=null)
callback.run();
} catch (IOException e) {
Log.e("Camera", "Error opening camera", e);
}
}
});
}, 100);
}

public void closeCamera(){
if (camera!=null)
camera.release();
camera = null;
}

public Camera getCamera() {
return camera;
}

//XXX
public void restart() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
camera.startPreview();
} catch (Throwable t) {
Log.e("Camera", "Restarting preview?", t);
}
}
},100);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.OutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class FTPUploader {

Expand All @@ -21,25 +22,29 @@ public class FTPUploader {

private String address, user, password;

/**
* Creates a new FTP uploader, that is a wrapper around Apache Commons FTPClient with threading
* support and configuration linked to application settings
*
* TODO: add a kind of listener interface support instead of single callbacks
*
* @param context
* the application context
*/
public FTPUploader(Context context) {
client = new FTPClient();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
address = prefs.getString(Util.PREF_FTP_ADDRESS, "");
user = prefs.getString(Util.PREF_FTP_USER, "");
password = prefs.getString(Util.PREF_FTP_PW, "");
exec = Executors.newSingleThreadExecutor();
execute(null);
}

public void execute(final Runnable command) {

if (client.isConnected()) {

if(command!=null)
exec.execute(command);

} else {

/**
* Connects to the server
*/
public void connect() {
if (!client.isConnected())
exec.execute(new Runnable() {
@Override
public void run() {
Expand All @@ -52,27 +57,46 @@ public void run() {
} catch (Exception e) {
Log.e(TAG, "Can't connect to " + address + ", user: " + user);
}
if (command!=null)
command.run();
}
});
}
}
});
}

public void execute(final Runnable command) {
if (!client.isConnected())
connect();
if (command!=null)
exec.execute(command);
}

/**
* Closes current connection
*/
public void close() {
exec.execute(new Runnable() {
@Override
public void run() {
try {
client.logout();
client.disconnect();
Log.d(TAG, "Client disconnected");
} catch (Exception e) {
Log.e(TAG, "Error finalizing FTP connection", e);
if (client!=null && client.isConnected()) {
exec.execute(new Runnable() {
@Override
public void run() {
try {
client.logout();
client.disconnect();
Log.d(TAG, "Client disconnected");
} catch (Exception e) {
Log.e(TAG, "Error finalizing FTP connection", e);
}
}
exec.shutdown();
}
});
});
new Thread() {
@Override
public void run() {
try {
exec.awaitTermination(5, TimeUnit.SECONDS);
exec.shutdown();
} catch(InterruptedException e) {
Log.e(TAG, "Unexpected interruption", e);
}
}
}.start();
}
}

/**
Expand All @@ -83,7 +107,7 @@ public void run() {
* @param filename
* the filename to write to
*/
public void send(final byte[] data, final String filename) {
public void send(final byte[] data, final String filename, final Runnable callback) {
if (client.isConnected())
exec.execute(new Runnable() {
@Override
Expand All @@ -92,27 +116,30 @@ public void run() {
try {
out = client.storeFileStream(filename);
out.write(data);
if (callback!=null)
callback.run();
Log.d(TAG, "Data written to "+filename);
} catch (Exception e) {
Log.e(FTPUploader.class.getSimpleName(), "FTP Error", e);
Log.e(FTPUploader.class.getSimpleName(), "Transfer Error", e);
} finally {
try {
out.close();
client.completePendingCommand();
} catch (Exception e) {
Log.e(FTPUploader.class.getSimpleName(), "Unexpected error", e);
}
catch (Exception e) {}
}
}
});
else {
Log.d(TAG, "Retry");
execute(new Runnable() {
@Override
public void run() {
send(data, filename);
}
});
}
// else {
// Log.d(TAG, "Retry");
// execute(new Runnable() {
// @Override
// public void run() {
// send(data, filename);
// }
// });
// }
}

}
Loading

0 comments on commit 0f1e492

Please sign in to comment.