Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
- Settings loading in background

Bugfixes
- Settings (relative to permissions) were not retained in the first attempt of granting permissions
- Camera opening and disposal
- Error selecting some sensors in recording
  • Loading branch information
francescogabbrielli committed Aug 9, 2017
1 parent f589fb4 commit f986b62
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 65 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 5
versionName "0.2.3"
versionCode 6
versionName "0.2.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,24 @@ public void openCamera(final SurfaceHolder holder, final Runnable callback) {
public void run() {
try {
camera = Camera.open();
Log.v(getName(), "on");
Camera.Parameters params = camera.getParameters();
params.setPreviewSize(1280,720);
params.setPictureSize(1280,720);
camera.setParameters(params);
Log.v(getName(), "size reset to 1280x720");
// Util.setCameraDisplayOrientation(MainActivity.this, 0, camera);XXX: rotation?
camera.setPreviewDisplay(holder);
Log.v(getName(), "display");
camera.startPreview();
Log.v(getName(), "start");
if (callback!=null)
callback.run();
} catch (IOException e) {
Log.e("Camera", "Error opening camera", e);
Log.e(getName(), "Error opening camera", e);
}
}
}, 100);
}, 50);
}

public void closeCamera(){
Expand All @@ -73,7 +77,7 @@ public void run() {
try {
camera.startPreview();
} catch (Throwable t) {
Log.e("Camera", "Restarting preview?", t);
Log.e(getName(), "Restarting preview?", t);
}
}
},100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void run() {
* the filename to write to
*/
@Override
public void send(final byte[] data, final String filename, final Runnable callback) {
public void send(final byte[] data, final String filename, final SyncCallbackThread scThread) {
if (client.isConnected())
exec.execute(new Runnable() {
@Override
Expand All @@ -116,8 +116,8 @@ public void run() {
try {
out = client.storeFileStream(filename);
out.write(data);
if (callback!=null)
callback.run();
if (scThread!=null)
scThread.releaseLock();
Log.d(TAG, "Data written to "+filename);
} catch (Exception e) {
Log.e(TAG, "Transfer Error", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ public LogFileWriter(File folder) {
}

@Override
public void send(final byte[] data, final String filename, final Runnable callback) {
public void send(final byte[] data, final String filename, final SyncCallbackThread scThread) {
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();
if (scThread!=null)
scThread.releaseLock();
Log.d(TAG, "Data written to " + filename);
} catch (Exception e) {
Log.e(TAG, "Error writing file", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class LogTarget {
*
* 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);
public abstract void send(final byte[] data, final String filename, final SyncCallbackThread scThread);

/**
* Implement final cleanup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void onPictureTaken(final byte[] imageData, final Camera camera) {
}
});
} catch (Exception e) {
log(null, sensorsData, null);
Log.e(TAG, "Error taking picture", e);
}
else
Expand Down Expand Up @@ -133,38 +134,34 @@ public void onShutter() {
/** Log data to targets */
private void log(byte[] imageData, byte[] sensorsData, final Camera camera) {

// Sync the callbacks
final SyncCallbackThread syncCallbackThread = new SyncCallbackThread(new Runnable() {
// Sync the callbacks (for restarting the camera preview)
final SyncCallbackThread syncCallbackThread = new SyncCallbackThread() {
@Override
public void run() {
public void finalRun() {
camera.startPreview();
}
});
Runnable syncCallback = new Runnable() {
@Override
public void run() {
syncCallbackThread.releaseLock();
}
};

// Local file
if (fileWriter != null)
try {
syncCallbackThread.addLock();
fileWriter.send(sensorsData, filenameData, null);
if (imageData!=null)
fileWriter.send(imageData, filenameFrame, syncCallback);
if (imageData!=null) {
syncCallbackThread.addLock();
fileWriter.send(imageData, filenameFrame, syncCallbackThread);
}
} catch(Exception e) {
Log.e(TAG, "Error writing samples to local file: " + e.getMessage(), e);
}

// FTP
if (ftpUploader != null)
try {
syncCallbackThread.addLock();
ftpUploader.send(sensorsData, filenameData, null);
if (imageData!=null)
ftpUploader.send(imageData, filenameFrame, syncCallback);
if (imageData!=null) {
syncCallbackThread.addLock();
ftpUploader.send(imageData, filenameFrame, syncCallbackThread);
}
} catch (Exception e) {
Log.e(TAG, "Error sending samples through FTP: " + e.getMessage(), e);
}
Expand Down Expand Up @@ -210,7 +207,7 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
filenameData = defaults.getProperty("filename_data");
filenameFrame = defaults.getProperty("filename_frame");
if (prefs.getBoolean(Util.PREF_LOGGING_SAVE, false)) {
if (prefs.getBoolean(Util.PREF_LOGGING_LOCAL, false)) {
fileWriter = new LogFileWriter(new File(Environment.getExternalStorageDirectory(), getString(R.string.app_folder)));
}
if (prefs.getBoolean(Util.PREF_FTP, false)) {
Expand Down Expand Up @@ -253,7 +250,7 @@ private void verifyPermissions() {
requests.add(Manifest.permission.CAMERA);
if(prefs.getBoolean(Util.PREF_FTP, false))
requests.add(Manifest.permission.INTERNET);
if(prefs.getBoolean(Util.PREF_LOGGING_SAVE, false))
if(prefs.getBoolean(Util.PREF_LOGGING_LOCAL, false))
requests.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);

// Check if we have requested permission
Expand Down Expand Up @@ -296,19 +293,26 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}

protected void onPermissionGranted(String permission) {
if (Manifest.permission.CAMERA.equals(permission))
Log.d(TAG, "Permission granted: "+permission);
if (Manifest.permission.CAMERA.equals(permission)) {
setupCamera();
else if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission))
prefs.edit().putBoolean(Util.PREF_CAPTURE_CAMERA, true).commit();
} else if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission)) {
new File(Environment.getExternalStorageDirectory(), getString(R.string.app_folder)).mkdirs();
prefs.edit().putBoolean(Util.PREF_LOGGING_LOCAL, true).commit();
} else if (Manifest.permission.INTERNET.equals(permission)) {
prefs.edit().putBoolean(Util.PREF_FTP, true).commit();
}
}

protected void onPermissionDenied(String permission) {
Log.d(TAG, "Permission denied: "+permission);
if (Manifest.permission.CAMERA.equals(permission))
prefs.edit().putBoolean(Util.PREF_CAPTURE_CAMERA, false).commit();
else if (Manifest.permission.INTERNET.equals(permission))
prefs.edit().putBoolean(Util.PREF_FTP, false).commit();
else if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission))
prefs.edit().putBoolean(Util.PREF_LOGGING_SAVE, false).commit();
prefs.edit().putBoolean(Util.PREF_LOGGING_LOCAL, false).commit();
}
//
// ----------------------------------- ---------------------- ----------------------------------
Expand All @@ -319,6 +323,7 @@ else if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission))
// ---------------------------------- CAMERA SURFACE CALLBACKS ---------------------------------
//
public void surfaceCreated(SurfaceHolder holder) {
Log.d(TAG, "Open camera");
cameraHandlerThread.openCamera(holder, null);
// new Runnable() {
// @Override
Expand All @@ -331,13 +336,14 @@ public void surfaceCreated(SurfaceHolder holder) {
public void surfaceChanged(final SurfaceHolder holder, final int format, final int w, final int h) {

Camera camera = cameraHandlerThread.getCamera();
Log.d(TAG, "Camera change: "+camera);
if (camera==null) {
cameraHandlerThread.openCamera(holder, new Runnable() {
@Override
public void run() {
surfaceChanged(holder, format, w, h);
}
});
// cameraHandlerThread.openCamera(holder, new Runnable() {
// @Override
// public void run() {
// surfaceChanged(holder, format, w, h);
// }
// });
return;
}

Expand Down Expand Up @@ -368,6 +374,7 @@ public void run() {
}

public void surfaceDestroyed(SurfaceHolder holder) {
Log.d(TAG, "Close camera");
cameraHandlerThread.closeCamera();
}
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import android.util.Log;

import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -127,11 +130,11 @@ public int compare(Sensor s1, Sensor s2) {

if (!prefs.getBoolean(Util.PREF_RECORDING, false)) {

final SensorEvent[] readings = new SensorEvent[255];
final Map<Integer, SensorEvent> readings = new HashMap<>();
final SensorEventListener listener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
readings[event.sensor.getType()] = event;
readings.put(event.sensor.getType(), event);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Expand Down Expand Up @@ -208,7 +211,7 @@ public void run() {
if (flagTime)
sb.append(String.valueOf((t - start) / 1000f));
for (Sensor s : sensors) {
SensorEvent event = readings[s.getType()];
SensorEvent event = readings.get(s.getType());
int l = Util.getSensorMaxLength(s);
for (int i=0; i<l; i++) {
sb.append(',');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
Expand All @@ -23,6 +24,7 @@
import android.util.Log;
import android.view.MenuItem;

import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

Expand Down Expand Up @@ -234,18 +236,31 @@ public void onCreate(Bundle savedInstanceState) {
}

private void addPreferencesFromSensors() {
PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(getActivity());

final PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(getActivity());
screen.setOrderingAsAdded(true);

List<Sensor> sensors = Util.getSensors(getActivity());
for (Sensor s : sensors) {
CheckBoxPreference p = new CheckBoxPreference(getActivity());
p.setTitle(Util.getSensorName(s));
p.setSummary(s.getName() + "/" + s.getVendor());
p.setDefaultValue(false);
p.setKey("pref_sensor_"+s.getType());
screen.addPreference(p);
}
new AsyncTask<Void, Void, List<CheckBoxPreference>>() {
@Override
protected List<CheckBoxPreference> doInBackground(Void... params) {
List<Sensor> sensors = Util.getSensors(getActivity());
List<CheckBoxPreference> list = new LinkedList<>();
for (Sensor s : sensors) {
CheckBoxPreference p = new CheckBoxPreference(getActivity());
p.setTitle(Util.getSensorName(s));
p.setSummary(s.getName() + "/" + s.getVendor());
p.setDefaultValue(false);
p.setKey("pref_sensor_" + s.getType());
list.add(p);
}
return list;
}
@Override
protected void onPostExecute(List<CheckBoxPreference> list) {
for (CheckBoxPreference p : list)
screen.addPreference(p);
}
}.execute();

setPreferenceScreen(screen);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@

import android.util.Log;

public class SyncCallbackThread extends Thread {
public abstract class SyncCallbackThread extends Thread {

private final static long SYNC_TIMEOUT = 15000;

private int locks;
private Runnable finalCallback;

public SyncCallbackThread(Runnable callback) {
this.finalCallback = callback;
}

public synchronized void addLock() {
locks++;
Expand All @@ -36,6 +31,9 @@ public void run() {
} catch (InterruptedException ie) {
//Log.e(TAG, "Log sync interrupted", ie);
}
finalCallback.run();
finalRun();
}

protected abstract void finalRun();

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class Util {
public final static String PREF_LOGGING_HEADERS = "pref_logging_headers";
public final static String PREF_LOGGING_TIME = "pref_logging_time";
public final static String PREF_LOGGING_TIMESTAMP = "pref_logging_timestamp";
public final static String PREF_LOGGING_SAVE = "pref_logging_save";
public final static String PREF_LOGGING_LOCAL = "pref_logging_local";

public final static String PREF_CAPTURE_CAMERA = "pref_capture_camera";
public final static String PREF_CAPTURE_SOUND = "pref_capture_sound";
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/xml/pref_capture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<!-- A 'parent' preference, which enables/disables child preferences (below)
when checked/unchecked. -->
<CheckBoxPreference
<SwitchPreference
android:defaultValue="false"
android:key="pref_capture_camera"
android:title="@string/pref_capture_camera_title"
android:summary="@string/pref_capture_camera_description"/>

<CheckBoxPreference
<SwitchPreference
android:enabled="false"
android:defaultValue="false"
android:key="pref_capture_screen"
Expand Down
Loading

0 comments on commit f986b62

Please sign in to comment.