First commit
This commit is contained in:
		
							
								
								
									
										142
									
								
								Privacy/DemoCollab.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										142
									
								
								Privacy/DemoCollab.java
									
									
									
									
									
										Normal file
									
								
							@ -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 <Object> state;
 | 
			
		||||
  private List<Map<String, String>> 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<Object>) 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 <Object> list = (List<Object>) 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<Map<String, String>> 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 <Object> data = (List<Object>) ois.readObject();
 | 
			
		||||
 | 
			
		||||
    // Write to output stream.
 | 
			
		||||
    oos.writeObject(data);
 | 
			
		||||
 | 
			
		||||
    return true;
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
  /**/
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										111
									
								
								Privacy/DemoFilterLocation.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								Privacy/DemoFilterLocation.java
									
									
									
									
									
										Normal file
									
								
							@ -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 <Object> 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<Object>) 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 <Object> list = (List<Object>) 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;
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										104
									
								
								Privacy/DemoFilterTime.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								Privacy/DemoFilterTime.java
									
									
									
									
									
										Normal file
									
								
							@ -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 <Object> 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<Object>) 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 <Object> list = (List<Object>) 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;
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user