diff --git a/Privacy/DemoCollab.java b/Privacy/DemoCollab.java new file mode 100644 index 0000000..c2df3a3 --- /dev/null +++ b/Privacy/DemoCollab.java @@ -0,0 +1,142 @@ +/** + * The collaborative version of the "DemoFilterTime" privacy mechanism. + */ + +import java.util.*; +import java.io.*; + +import android.content.Context; +import android.util.*; + +@SuppressWarnings("unchecked") +public class DemoCollab { + + private Context context; + private int pref; + private List state; + private List> peers; + + public void onStart (Context c, int i, ObjectInputStream ois) throws Exception { + String TAG = getClass().getName() + "@onStart: "; + + Log.wtf(TAG, "..."); + + // Get application context. + context = c; + + // Get privacy preferences. + pref = i; + + // Restore state. + if (ois != null) { + Log.wtf(TAG, "Restoring state..."); + state = (List) ois.readObject(); + } else { + Log.wtf(TAG, "Initializing state..."); + state = new ArrayList<>(); + } + + peers = new ArrayList<>(); + + } + + public void onStop () { + String TAG = getClass().getName() + "@onStop: "; + + Log.wtf(TAG, "..."); + + } + + public void onPreferenceChanged (int i) { + String TAG = getClass().getName() + "@onPreferenceChanged: "; + + Log.wtf(TAG, "" + i); + + pref = i; + + } + + public boolean saveState (ObjectOutputStream oos) { + String TAG = getClass().getName() + "@saveState: "; + + Log.wtf(TAG, "..."); + + // s.writeObject(state); + + return false; + + } + + public int processData (ObjectInputStream ois, ObjectOutputStream oos) throws Exception { + String TAG = getClass().getName() + "@processData: "; + + Log.wtf(TAG, "..."); + + // Get list of data. + List list = (List) ois.readObject(); + + // Edit each data entry of the data list. + for (Object o : list) { + + Map data = (Map) o; + + data.put("device", data.get("device")); + data.put("task", data.get("task")); + data.put("sensor", data.get("sensor")); + data.put("values", data.get("values")); + + float min = -1; + float max = 1; + + Random r = new Random(); + float d = min + (max - min) * r.nextFloat(); // -1 < d < 1 + + long var = (long) (((float) pref / 10) * d * 3600000000000L); // (pref/10) * random * (1hr) + + data.put("timestamp", (Long) data.get("timestamp") + var); + + } + + oos.writeObject(list); + + // If a peer is present... + if(!peers.isEmpty()) { + // Collaborate! + return Integer.parseInt(peers.get(0).get("id")); + } + return 0; + + } + + /** + * Collaborative stuff. + */ + /**/ + public void onPeersChanged (List> peers) { + String TAG = getClass().getName() + "@onPeersChanged: "; + + Log.wtf(TAG, "..."); + + this.peers = peers; + + Log.wtf(TAG, peers.toString()); + + } + + public boolean aggregateData (ObjectInputStream ois, ObjectOutputStream oos) throws Exception { + String TAG = getClass().getName() + "@aggregateData: "; + + Log.wtf(TAG, "..."); + + // Read input stream. + List data = (List) ois.readObject(); + + // Write to output stream. + oos.writeObject(data); + + return true; + + } + /**/ + +} diff --git a/Privacy/DemoFilterLocation.java b/Privacy/DemoFilterLocation.java new file mode 100644 index 0000000..f8719c2 --- /dev/null +++ b/Privacy/DemoFilterLocation.java @@ -0,0 +1,111 @@ +/** + * A simple implementation which adds noise to the location component of the + * data that is produced by the "DemoLocation" sensing task. + */ + +import java.util.*; +import java.io.*; + +import android.content.Context; +import android.util.*; + +@SuppressWarnings("unchecked") +public class DemoFilterLocation { + + private static Context context; + private static int pref; + private static List state; + + public void onStart (Context c, int i, ObjectInputStream ois) throws Exception { + String TAG = getClass().getName() + "@onStart: "; + + Log.wtf(TAG, "..."); + + // Get application context. + context = c; + + // Get privacy preferences. + pref = i; + + // Restore state. + if (ois != null) { + Log.wtf(TAG, "Restoring state..."); + state = (List) ois.readObject(); + } else { + Log.wtf(TAG, "Initializing state..."); + state = new ArrayList<>(); + } + + } + + public void onStop () { + String TAG = getClass().getName() + "@onStop: "; + + Log.wtf(TAG, "..."); + + } + + public void onPreferenceChanged (int i) { + String TAG = getClass().getName() + "@onPreferenceChanged: "; + + Log.wtf(TAG, "" + i); + + pref = i; + + } + + public boolean saveState (ObjectOutputStream oos) throws Exception { + String TAG = getClass().getName() + "@saveState: "; + + Log.wtf(TAG, "..."); + + // s.writeObject(state); + + return false; + + } + + public int processData (ObjectInputStream ois, ObjectOutputStream oos) throws Exception { + String TAG = getClass().getName() + "@processData: "; + + Log.wtf(TAG, "..."); + + // Get list of data. + List list = (List) ois.readObject(); + + // Edit each data entry of the data list. + for (Object o : list) { + + Map data = (Map) o; + + data.put("device", data.get("device")); + data.put("task", data.get("task")); + data.put("sensor", data.get("sensor")); + data.put("timestamp", data.get("timestamp")); + + double[] location = (double[]) data.get("values"); + + double x0 = location[0]; // lat + double y0 = location[1]; // lng + + double r = (double) pref * 10 / 111300; + + double w = r * Math.sqrt(Math.random()); + double t = 2 * Math.PI * Math.random(); + double x = w * Math.cos(t); + double y = w * Math.sin(t); + + double x1 = x / Math.cos(y0); + + double values[] = {x1 + x0, y + y0}; + + data.put("values", values); + } + + oos.writeObject(list); + + return 0; + + } + +} diff --git a/Privacy/DemoFilterTime.java b/Privacy/DemoFilterTime.java new file mode 100644 index 0000000..5bcb7ce --- /dev/null +++ b/Privacy/DemoFilterTime.java @@ -0,0 +1,104 @@ +/** + * A simple implementation which adds noise to the time component of the + * data that is produced by any sensing task. + */ + +import java.util.*; +import java.io.*; + +import android.content.Context; +import android.util.*; + +@SuppressWarnings("unchecked") +public class DemoFilterTime { + + private static Context context; + private static int pref; + private static List state; + + public void onStart (Context c, int i, ObjectInputStream ois) throws Exception { + String TAG = getClass().getName() + "@onStart: "; + + Log.wtf(TAG, "..."); + + // Get application context. + context = c; + + // Get privacy preferences. + pref = i; + + // Restore state. + if (ois != null) { + Log.wtf(TAG, "Restoring state..."); + state = (List) ois.readObject(); + } else { + Log.wtf(TAG, "Initializing state..."); + state = new ArrayList<>(); + } + + } + + public void onStop () { + String TAG = getClass().getName() + "@onStop: "; + + Log.wtf(TAG, "..."); + + } + + public void onPreferenceChanged (int i) { + String TAG = getClass().getName() + "@onPreferenceChanged: "; + + Log.wtf(TAG, "" + i); + + pref = i; + + } + + public boolean saveState (ObjectOutputStream oos) throws Exception { + String TAG = getClass().getName() + "@saveState: "; + + Log.wtf(TAG, "..."); + + // s.writeObject(state); + + return false; + + } + + public int processData (ObjectInputStream ois, ObjectOutputStream oos) throws Exception { + String TAG = getClass().getName() + "@processData: "; + + Log.wtf(TAG, "..."); + + // Get list of data. + List list = (List) ois.readObject(); + + // Edit each data entry of the data list. + for (Object o : list) { + + Map data = (Map) o; + + data.put("device", data.get("device")); + data.put("task", data.get("task")); + data.put("sensor", data.get("sensor")); + data.put("values", data.get("values")); + + float min = -1; + float max = 1; + + Random r = new Random(); + float d = min + (max - min) * r.nextFloat(); // -1 < d < 1 + + long var = (long) (((float) pref / 10) * d * 3600000000000L); // (pref/10) * random * (1hr) + + data.put("timestamp", (Long) data.get("timestamp") + var); + + } + + oos.writeObject(list); + + return 0; + + } + +} diff --git a/Sensing/DemoLocation.java b/Sensing/DemoLocation.java new file mode 100644 index 0000000..f9ac4bf --- /dev/null +++ b/Sensing/DemoLocation.java @@ -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 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 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 getData() { + String TAG = getClass().getName() + "@getData: "; + + Log.wtf(TAG, "..."); + + List 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 + } + +} diff --git a/Sensing/DemoOrientation.java b/Sensing/DemoOrientation.java new file mode 100644 index 0000000..8210eb2 --- /dev/null +++ b/Sensing/DemoOrientation.java @@ -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 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 getData() { + String TAG = getClass().getName() + "@getData: "; + + Log.wtf(TAG, "..."); + + List 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 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"; + + } + } + } +} diff --git a/Sensing/DemoTest.java b/Sensing/DemoTest.java new file mode 100644 index 0000000..4a99084 --- /dev/null +++ b/Sensing/DemoTest.java @@ -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 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 getData() { + String TAG = getClass().getName() + "@getData: "; + + Log.wtf(TAG, "..."); + + List 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 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++; + + } + + } + +}