First commit
This commit is contained in:
168
Sensing/DemoLocation.java
Normal file
168
Sensing/DemoLocation.java
Normal file
@ -0,0 +1,168 @@
|
||||
/**
|
||||
* A simple sensing task that logs the location the device has remained at for
|
||||
* a defined period of time.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import android.content.*;
|
||||
import android.hardware.Sensor;
|
||||
import android.location.*;
|
||||
import android.os.*;
|
||||
import android.util.*;
|
||||
|
||||
public class DemoLocation implements LocationListener {
|
||||
|
||||
Context context;
|
||||
|
||||
List<Object> data;
|
||||
|
||||
LocationManager locMgr;
|
||||
LocationListener locLnr;
|
||||
Location lastLoc;
|
||||
|
||||
// Timer and total time definition.
|
||||
CountDownTimer timer;
|
||||
|
||||
int hours = 0;
|
||||
int minutes = 0;
|
||||
int seconds = 5;
|
||||
|
||||
// Convert the above to milliseconds.
|
||||
int timeTotal = (hours * 3600 + minutes * 60 + seconds) * 1000;
|
||||
|
||||
public void onStart(Context c, ObjectInputStream s) throws Exception {
|
||||
String TAG = getClass().getName() + "@onStart: ";
|
||||
|
||||
Log.wtf(TAG, "...");
|
||||
|
||||
context = c;
|
||||
|
||||
data = new ArrayList<>();
|
||||
|
||||
timer = new CountDownTimer(timeTotal, 1000) {
|
||||
|
||||
public void onTick(long arg0) {
|
||||
String TAG = getClass().getName() + "@onTick: ";
|
||||
// Log.wtf(TAG, arg0 / 1000 + " seconds remaining...");
|
||||
}
|
||||
|
||||
// Log the current location.
|
||||
public void onFinish() {
|
||||
String TAG = getClass().getName() + "@onFinish: ";
|
||||
|
||||
Log.wtf(TAG, lastLoc.toString());
|
||||
|
||||
// add location to data
|
||||
Map<String, Object> newData = new HashMap<>();
|
||||
|
||||
double[] values = new double[2];
|
||||
values[0] = lastLoc.getLatitude();
|
||||
values[1] = lastLoc.getLongitude();
|
||||
|
||||
newData.put("sensor", Sensor.TYPE_ALL);
|
||||
// newData.put("timestamp", lastLoc.getTime() * 1000000);
|
||||
newData.put("timestamp", System.currentTimeMillis() * 1000000L);
|
||||
newData.put("values", values);
|
||||
|
||||
data.add(newData);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// Monitor location changes.
|
||||
locMgr = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
|
||||
|
||||
Criteria criteria = new Criteria();
|
||||
|
||||
String provider = locMgr.getBestProvider(criteria, true);
|
||||
if(provider == null) {
|
||||
provider = locMgr.getBestProvider(criteria, false);
|
||||
}
|
||||
|
||||
locMgr.requestLocationUpdates(provider, 1000, 1, this);
|
||||
|
||||
Location loc = locMgr.getLastKnownLocation(provider);
|
||||
if (loc != null) {
|
||||
Log.wtf(TAG, provider + " provider selected.");
|
||||
onLocationChanged(loc);
|
||||
}
|
||||
else {
|
||||
Log.wtf(TAG, "Location not available.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean saveState(ObjectOutputStream s) {
|
||||
String TAG = getClass().getName() + "@saveState: ";
|
||||
|
||||
Log.wtf(TAG, "...");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<Object> getData() {
|
||||
String TAG = getClass().getName() + "@getData: ";
|
||||
|
||||
Log.wtf(TAG, "...");
|
||||
|
||||
List<Object> tmp = data;
|
||||
data = new ArrayList<>();
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void onStop() {
|
||||
String TAG = getClass().getName() + "@onStop: ";
|
||||
|
||||
Log.wtf(TAG, "...");
|
||||
|
||||
// Stop the timer.
|
||||
if(timer != null) {
|
||||
timer.cancel();
|
||||
}
|
||||
|
||||
// Release the location listener.
|
||||
if(locMgr != null) {
|
||||
locMgr.removeUpdates(locLnr = this);
|
||||
}
|
||||
|
||||
}
|
||||
/**/
|
||||
|
||||
// Called when the location has changed.
|
||||
@Override
|
||||
public void onLocationChanged(Location loc) {
|
||||
String TAG = getClass().getName() + "@onLocationChanged: ";
|
||||
|
||||
Log.wtf(TAG, "...");
|
||||
|
||||
Log.wtf(TAG, loc.toString());
|
||||
|
||||
lastLoc = loc;
|
||||
|
||||
if (timer != null) {
|
||||
timer.cancel();
|
||||
}
|
||||
timer.start();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderDisabled(String arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderEnabled(String arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
}
|
169
Sensing/DemoOrientation.java
Normal file
169
Sensing/DemoOrientation.java
Normal file
@ -0,0 +1,169 @@
|
||||
/**
|
||||
* A simple sensing task that identifies the orientation of the device's screen
|
||||
* and reports the accelerometer's values whenever the screen faces:
|
||||
* - TOP
|
||||
* - BOTTOM
|
||||
* - LEFT
|
||||
* - RIGHT
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.*;
|
||||
import android.util.*;
|
||||
|
||||
public class DemoOrientation implements SensorEventListener {
|
||||
|
||||
private static Context context;
|
||||
|
||||
private static int state;
|
||||
|
||||
List<Object> data;
|
||||
|
||||
private static SensorManager sensorManager;
|
||||
|
||||
// Save the last measurement outcome (do not report multiple TOPs).
|
||||
private static String face;
|
||||
|
||||
public void onStart(Context c, ObjectInputStream s) throws Exception {
|
||||
String TAG = getClass().getName() + "@onStart: ";
|
||||
|
||||
Log.wtf(TAG, "...");
|
||||
|
||||
context = c;
|
||||
|
||||
// Save a counter to debug the overall functionality of the framework.
|
||||
if (s != null) {
|
||||
state = (Integer) s.readObject();
|
||||
} else {
|
||||
state = 1;
|
||||
}
|
||||
|
||||
data = new ArrayList<>();
|
||||
|
||||
// Monitor the accelerometer of the device.
|
||||
sensorManager = (SensorManager) context.getSystemService(context.SENSOR_SERVICE);
|
||||
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
|
||||
}
|
||||
|
||||
public boolean saveState(ObjectOutputStream s) throws Exception {
|
||||
String TAG = getClass().getName() + "@saveState: ";
|
||||
|
||||
Log.wtf(TAG, "...");
|
||||
|
||||
s.writeObject(state);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public List<Object> getData() {
|
||||
String TAG = getClass().getName() + "@getData: ";
|
||||
|
||||
Log.wtf(TAG, "...");
|
||||
|
||||
List<Object> l = data;
|
||||
data = new ArrayList<>();
|
||||
|
||||
return l;
|
||||
|
||||
}
|
||||
|
||||
public void onStop() {
|
||||
String TAG = getClass().getName() + "@onStop: ";
|
||||
|
||||
Log.wtf(TAG, "...");
|
||||
|
||||
sensorManager.unregisterListener(this);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor arg0, int arg1) {
|
||||
|
||||
}
|
||||
|
||||
// Called when sensor values have changed.
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
String TAG = getClass().getName() + "@onSensorChanged: ";
|
||||
|
||||
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
|
||||
float x = event.values[0];
|
||||
float y = event.values[1];
|
||||
float z = event.values[2];
|
||||
|
||||
Map<String, Object> newData = new HashMap<>();
|
||||
|
||||
double[] values = new double[3];
|
||||
values[0] = (double) event.values[0];
|
||||
values[1] = (double) event.values[1];
|
||||
values[2] = (double) event.values[2];
|
||||
|
||||
newData.put("sensor", event.sensor.getType());
|
||||
// newData.put("timestamp", event.timestamp);
|
||||
newData.put("timestamp", System.currentTimeMillis() * 1000000L);
|
||||
newData.put("values", values);
|
||||
|
||||
// LEFT
|
||||
if(x > 9 && y > 0 && y < 1 && z > 0 && z < 1) {
|
||||
|
||||
if(!"LEFT".equals(face)) {
|
||||
Log.wtf(TAG, Integer.toString(state+1) + ". LEFT");
|
||||
|
||||
data.add(newData);
|
||||
|
||||
state++;
|
||||
|
||||
}
|
||||
|
||||
face = "LEFT";
|
||||
|
||||
// RIGHT
|
||||
} else if(x < -9 && y > 0 && y < 1 && z > 0 && z < 1) {
|
||||
|
||||
if(!"RIGHT".equals(face)) {
|
||||
Log.wtf(TAG, Integer.toString(state+1) + ". RIGHT");
|
||||
|
||||
data.add(newData);
|
||||
|
||||
state++;
|
||||
|
||||
}
|
||||
|
||||
face = "RIGHT";
|
||||
|
||||
// UP
|
||||
} else if(z > 9.5) {
|
||||
|
||||
if(!"UP".equals(face)) {
|
||||
Log.wtf(TAG, Integer.toString(state+1) + ". UP");
|
||||
|
||||
data.add(newData);
|
||||
|
||||
state++;
|
||||
}
|
||||
|
||||
face = "UP";
|
||||
|
||||
// DOWN
|
||||
} else if(z < -9.5) {
|
||||
|
||||
if(!"DOWN".equals(face)) {
|
||||
Log.wtf(TAG, Integer.toString(state+1) + ". DOWN");
|
||||
|
||||
data.add(newData);
|
||||
|
||||
state++;
|
||||
}
|
||||
|
||||
face = "DOWN";
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
119
Sensing/DemoTest.java
Normal file
119
Sensing/DemoTest.java
Normal file
@ -0,0 +1,119 @@
|
||||
/**
|
||||
* A dummy sensing task that logs the values of the accelerometer sensor of the
|
||||
* device and reports the 100th value.
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.*;
|
||||
import android.util.*;
|
||||
|
||||
public class DemoTest implements SensorEventListener {
|
||||
|
||||
private static Context context;
|
||||
|
||||
private static int state;
|
||||
|
||||
List<Object> data;
|
||||
|
||||
private static SensorManager sensorManager;
|
||||
|
||||
private static String face;
|
||||
|
||||
public void onStart(Context c, ObjectInputStream s) throws Exception {
|
||||
String TAG = getClass().getName() + "@onStart: ";
|
||||
|
||||
Log.wtf(TAG, "...");
|
||||
|
||||
context = c;
|
||||
|
||||
if (s != null) {
|
||||
state = (Integer) s.readObject();
|
||||
} else {
|
||||
state = 1;
|
||||
}
|
||||
|
||||
data = new ArrayList<>();
|
||||
|
||||
sensorManager = (SensorManager) context.getSystemService(context.SENSOR_SERVICE);
|
||||
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
|
||||
}
|
||||
|
||||
public boolean saveState(ObjectOutputStream s) throws Exception {
|
||||
String TAG = getClass().getName() + "@saveState: ";
|
||||
|
||||
Log.wtf(TAG, "...");
|
||||
|
||||
s.writeObject(state);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public List<Object> getData() {
|
||||
String TAG = getClass().getName() + "@getData: ";
|
||||
|
||||
Log.wtf(TAG, "...");
|
||||
|
||||
List<Object> l = data;
|
||||
data = new ArrayList<>();
|
||||
|
||||
return l;
|
||||
|
||||
}
|
||||
|
||||
public void onStop() {
|
||||
String TAG = getClass().getName() + "@onStop: ";
|
||||
|
||||
Log.wtf(TAG, "...");
|
||||
|
||||
sensorManager.unregisterListener(this);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor arg0, int arg1) {
|
||||
|
||||
}
|
||||
|
||||
// Called when sensor values have changed.
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
String TAG = getClass().getName() + "@onSensorChanged: ";
|
||||
|
||||
// Log.wtf(TAG, "...");
|
||||
|
||||
// We are interested only for the accelerometer values.
|
||||
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
|
||||
|
||||
Map<String, Object> newData = new HashMap<>();
|
||||
|
||||
double[] values = new double[3];
|
||||
values[0] = (double) event.values[0];
|
||||
values[1] = (double) event.values[1];
|
||||
values[2] = (double) event.values[2];
|
||||
|
||||
// Log only the 100th measurement.
|
||||
if (state % 100 == 0) {
|
||||
Log.wtf(TAG, Integer.toString(state) + ". DATA");
|
||||
|
||||
newData.put("sensor", event.sensor.getType());
|
||||
// newData.put("timestamp", event.timestamp);
|
||||
newData.put("timestamp", System.currentTimeMillis() * 1000000L);
|
||||
newData.put("values", values);
|
||||
|
||||
data.add(newData);
|
||||
|
||||
}
|
||||
|
||||
state++;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user