Skip to content

Commit

Permalink
removed static context from AbstractSensor
Browse files Browse the repository at this point in the history
fixing up privacy-level sliders, part #1
  • Loading branch information
fmetzger committed Feb 26, 2013
1 parent b6a5b84 commit 04d23d1
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 42 deletions.
30 changes: 14 additions & 16 deletions Sensorium/src/at/univie/sensorium/SensorPreference.java
Expand Up @@ -44,8 +44,8 @@
*/
public class SensorPreference extends Preference implements OnSeekBarChangeListener {

private int mMaxValue = 100;
private int mMinValue = 0;
private final int mMaxValue = Privacy.PrivacyLevel.FULL.value();
private final int mMinValue = Privacy.PrivacyLevel.NO.value();
private int mInterval = 1;
private int mCurrentValue;

Expand All @@ -56,24 +56,20 @@ public class SensorPreference extends Preference implements OnSeekBarChangeListe

public SensorPreference(Context context, AbstractSensor sensor) {
super(context);

mMaxValue = Privacy.PrivacyLevel.NO.value();
mMinValue = Privacy.PrivacyLevel.FULL.value();

mInterval = 1;

mPrivacyLevel = new SeekBar(context);
mPrivacyLevel.setMax(mMaxValue - mMinValue);
mPrivacyLevel.setOnSeekBarChangeListener(this);

init(context);
this.sensor = sensor;
}

public SensorPreference(Context context) {
super(context);

mMaxValue = Privacy.PrivacyLevel.NO.value();
mMinValue = Privacy.PrivacyLevel.FULL.value();
init(context);
}

private void init(Context context){
// mMaxValue = Privacy.PrivacyLevel.NO.value();
// mMinValue = Privacy.PrivacyLevel.FULL.value();
// mMinValue = Privacy.PrivacyLevel.NO.value();
// mMaxValue = Privacy.PrivacyLevel.FULL.value();

mInterval = 1;

Expand Down Expand Up @@ -152,7 +148,8 @@ protected void updateView(View view) {

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int newValue = progress + mMinValue;
// int newValue = progress + mMinValue;
int newValue = mMaxValue - progress; // slider is reverse to the actual values, so subtract maxval

if (newValue > mMaxValue)
newValue = mMaxValue;
Expand All @@ -165,6 +162,7 @@ else if (mInterval != 1 && newValue % mInterval != 0)
seekBar.setProgress(mCurrentValue - mMinValue);
return;
}
Log.d("SENSORVALUE", String.valueOf(newValue));
sensor.setPrivacylevel(Privacy.PrivacyLevel.fromInt(newValue));
if(newValue == Privacy.PrivacyLevel.FULL.value() && sensor.isEnabled()){
sensor.disable();
Expand Down
3 changes: 3 additions & 0 deletions Sensorium/src/at/univie/sensorium/logging/JSONLogger.java
Expand Up @@ -131,6 +131,9 @@ private void writeObject(AbstractSensor sensor){

try {
jw.beginObject();
Log.d("WRITER", sensor.getName());
Log.d("WRITER", sensor.getPrivacylevel().toString());
Log.d("WRITER", sensor.getPrivacylevel().name());
jw.name("privacy-level").value(sensor.getPrivacylevel().value());
for(SensorValue value: valuelist){
SensorValue privatized = Privacy.anonymize(value, sensor.getPrivacylevel());
Expand Down
10 changes: 5 additions & 5 deletions Sensorium/src/at/univie/sensorium/privacy/Privacy.java
Expand Up @@ -38,7 +38,7 @@
public class Privacy {

public static enum PrivacyLevel {
NO(0, "Full Sensor Access"), LOW(1, "Low Privacy"), MED(2, "Medium Privacy"), HIGH(4, "High Privacy"), FULL(5, "Sensor Disabled");
NO(0, "Full Sensor Access"), LOW(1, "Low Privacy"), MED(2, "Medium Privacy"), HIGH(3, "High Privacy"), FULL(4, "Sensor Disabled");

private int value;
private String name;
Expand All @@ -59,15 +59,15 @@ public String toString() {

public static PrivacyLevel fromInt(int x) {
switch (x) {
case 4:
case 0:
return NO;
case 3:
case 1:
return LOW;
case 2:
return MED;
case 1:
case 3:
return HIGH;
case 0:
case 4:
return FULL;
}
return null;
Expand Down
14 changes: 10 additions & 4 deletions Sensorium/src/at/univie/sensorium/sensors/AbstractSensor.java
Expand Up @@ -41,11 +41,9 @@ public abstract class AbstractSensor {
private String description = "";
private Privacy.PrivacyLevel plevel;

protected static Context context;
protected String name = "";

public AbstractSensor(Context context) {
this.context = context;
this.listeners = new LinkedList<SensorChangeListener>();
this.plevel = Privacy.PrivacyLevel.FULL;
}
Expand All @@ -55,7 +53,7 @@ public void enable() {
try {
_enable();

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefs.edit().putBoolean(this.getClass().getName(), true).commit();

int state = prefs.getInt(this.getClass().getName() + "-level", Privacy.PrivacyLevel.FULL.value());
Expand All @@ -79,7 +77,7 @@ public void enable() {
public void disable() {
// if(enabled){
try {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefs.edit().putBoolean(this.getClass().getName(), false).commit();
enabled = false;

Expand Down Expand Up @@ -189,4 +187,12 @@ private void unsetallValues() {
s.unsetValue();
}
}

/**
* Convenience method to access the application context
* @return
*/
protected Context getContext(){
return SensorRegistry.getInstance().getContext();
}
}
4 changes: 2 additions & 2 deletions Sensorium/src/at/univie/sensorium/sensors/BatterySensor.java
Expand Up @@ -87,12 +87,12 @@ public void onReceive(Context context, Intent intent) {
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
batteryIntent = context.getApplicationContext().registerReceiver(batteryReceiver, batteryLevelFilter);
batteryIntent = getContext().getApplicationContext().registerReceiver(batteryReceiver, batteryLevelFilter);
}

@Override
protected void _disable() {
if(batteryIntent != null)
context.getApplicationContext().unregisterReceiver(batteryReceiver);
getContext().getApplicationContext().unregisterReceiver(batteryReceiver);
}
}
Expand Up @@ -56,7 +56,7 @@ public BluetoothSensor(Context context) {
public void run() {
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
bluetoothIntent = context.getApplicationContext().registerReceiver(bluetoothReceiver, filter);
bluetoothIntent = getContext().getApplicationContext().registerReceiver(bluetoothReceiver, filter);
bluetoothAdapter.startDiscovery();
Log.d("scanTask", "restart bluetooth scanning");

Expand Down Expand Up @@ -123,7 +123,7 @@ else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
@Override
protected void _disable() {
if(bluetoothIntent != null)
context.getApplicationContext().unregisterReceiver(bluetoothReceiver);
getContext().getApplicationContext().unregisterReceiver(bluetoothReceiver);
handler.removeCallbacks(scanTask);
scannedDevices.clear();
sScannedDevices.setValue(scannedDevices);
Expand Down
Expand Up @@ -77,7 +77,7 @@ public void run() {

@Override
protected void _enable() {
TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
TelephonyManager telephonyManager = ((TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE));
String imei = telephonyManager.getDeviceId();
if (imei != null)
tac.setValue(imei.substring(0, 6));
Expand All @@ -86,7 +86,7 @@ protected void _enable() {
vendorname.setValue(Build.MANUFACTURER);
modelname.setValue(Build.MODEL);

ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager activityManager = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);

Expand Down
Expand Up @@ -88,7 +88,7 @@ public void onLocationChanged(Location loc) {
speed.setValue(loc.getSpeed());
timestamp.setValue(loc.getTime());

Geocoder myLocation = new Geocoder(context.getApplicationContext(), Locale.getDefault());
Geocoder myLocation = new Geocoder(getContext().getApplicationContext(), Locale.getDefault());
List<Address> list = null;
try {
list = myLocation.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
Expand Down Expand Up @@ -142,7 +142,7 @@ public void onGpsStatusChanged(int event) {
}
};

locationManager = ((LocationManager) context
locationManager = ((LocationManager) getContext()
.getSystemService(Context.LOCATION_SERVICE));
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_UPDATE_TIME_INTERVAL,
GPS_UPDATE_MINIMAL_DISTANCE, locationListener);
Expand Down
Expand Up @@ -16,6 +16,7 @@
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.util.Log;
import at.univie.sensorium.SensorRegistry;

public class InterfacesSensor extends AbstractSensor {

Expand Down Expand Up @@ -46,7 +47,7 @@ public InterfacesSensor(final Context context) {
@Override
protected void _enable() {
interfaceFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
interfaceIntent = context.getApplicationContext().registerReceiver(interfaceReceiver, interfaceFilter);
interfaceIntent = getContext().getApplicationContext().registerReceiver(interfaceReceiver, interfaceFilter);

Log.d("SEATTLE DET STATE", connectivityManager.getActiveNetworkInfo().getDetailedState().toString());
// Log.d("SEATTLE EXTRA",
Expand All @@ -70,7 +71,7 @@ private static class InterfaceAsyncTask extends AsyncTask {

@Override
protected Object doInBackground(Object... params) {
ConnectivityManager connMananger = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
ConnectivityManager connMananger = (ConnectivityManager) SensorRegistry.getInstance().getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connMananger.getActiveNetworkInfo();
LinkedList<String> addresses = new LinkedList<String>();
try {
Expand Down
Expand Up @@ -72,7 +72,7 @@ public void onLocationChanged(Location loc) {
timestamp.setValue(loc.getTime());
speed.setValue(loc.getSpeed());

Geocoder myLocation = new Geocoder(context.getApplicationContext(), Locale.getDefault());
Geocoder myLocation = new Geocoder(getContext().getApplicationContext(), Locale.getDefault());
List<Address> list = null;
try {
list = myLocation.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
Expand Down Expand Up @@ -105,7 +105,7 @@ public void onProviderDisabled(String provider) {
}
};

locationManager = ((LocationManager) context.getSystemService(Context.LOCATION_SERVICE));
locationManager = ((LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE));
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

}
Expand Down
2 changes: 1 addition & 1 deletion Sensorium/src/at/univie/sensorium/sensors/RadioSensor.java
Expand Up @@ -68,7 +68,7 @@ public RadioSensor(Context context) {
@Override
protected void _enable() {

telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
telephonyManager = ((TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE));
GsmCellLocation gsmCell = (GsmCellLocation) telephonyManager.getCellLocation();

timestamp.setValue(System.currentTimeMillis());
Expand Down
Expand Up @@ -39,7 +39,7 @@ public WifiConnectionSensor(Context context){
private Runnable scanTask = new Runnable() {
@Override
public void run() {
mainWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mainWifi = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = mainWifi.getConnectionInfo(); // the network that Android is connected to
ssid.setValue(info.getSSID());
ssid_hidden.setValue(info.getHiddenSSID());
Expand Down
6 changes: 3 additions & 3 deletions Sensorium/src/at/univie/sensorium/sensors/WifiSensor.java
Expand Up @@ -42,7 +42,7 @@ public WifiSensor(Context context) {
@Override
public void run() {
wifiFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
wifiIntent = context.getApplicationContext().registerReceiver(wifiReceiver, wifiFilter);
wifiIntent = getContext().getApplicationContext().registerReceiver(wifiReceiver, wifiFilter);
scannedDevices.clear();
mainWifi.startScan();
Log.d("scanTask", "restart scanning");
Expand All @@ -53,7 +53,7 @@ public void run() {

@Override
protected void _enable() {
mainWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mainWifi = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);

wifiReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Expand Down Expand Up @@ -88,7 +88,7 @@ public void onReceive(Context context, Intent intent) {
@Override
protected void _disable() {
if(wifiIntent != null)
context.getApplicationContext().unregisterReceiver(wifiReceiver);
getContext().getApplicationContext().unregisterReceiver(wifiReceiver);
handler.removeCallbacks(scanTask);
scannedDevices.clear();
scannedDevicesSV.setValue(scannedDevices);
Expand Down

0 comments on commit 04d23d1

Please sign in to comment.