/
CompassTechnology.java
107 lines (94 loc) · 3.53 KB
/
CompassTechnology.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package de.hadizadeh.positioning.roommodel.android.technologies;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import de.hadizadeh.positioning.controller.ExclusionTechnology;
import de.hadizadeh.positioning.model.SignalInformation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Exclusion Compass technology
*/
public class CompassTechnology extends ExclusionTechnology {
private float bearing;
private Context context;
//private TextView compassTv;
/**
* Creates the compass technology
*
* @param context avtivity context
* @param name name of the technology
* @param allowedDelta allowed angle (positions which are not in this angle will be excluded)
*/
public CompassTechnology(Context context, String name, double allowedDelta) {
super(name, allowedDelta / 2);
this.context = context;
//compassTv = (TextView) ((Activity) context).findViewById(R.id.compassTv);
if (context != null) {
SensorManager mySensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
if (mySensors.size() > 0) {
mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_UI);
}
}
}
/**
* Returns the current angle
*
* @return current angle
*/
@Override
public Map<String, SignalInformation> getSignalData() {
Map<String, SignalInformation> signalData = new HashMap<String, SignalInformation>();
signalData.put("compassSignal", new SignalInformation(bearing));
return signalData;
}
private SensorEventListener mySensorEventListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
float compassBearing = (float) event.values[0];
if (Math.abs(bearing - compassBearing) > 2) {
//compassTv.setText(String.valueOf(compassBearing));
}
bearing = compassBearing;
}
};
/**
* Calculates if signal data are in the angle of the current viewing point
*
* @param signalData signal data
* @param persistedValue persisted fingerprints
* @return true, if the value is not in the range, else false
*/
@Override
protected boolean isValueOutOfExclusionRange(Map<String, SignalInformation> signalData, double persistedValue) {
boolean inRange = true;
for (Map.Entry<String, SignalInformation> data : signalData.entrySet()) {
double currentValue = data.getValue().getStrength();
double min = persistedValue - allowedDelta;
double max = persistedValue + allowedDelta;
if (max >= 360) {
max -= 360;
}
if (min <= 0) {
min = 360 - min;
}
if (min > max) {
if (!(currentValue >= min || currentValue <= max)) {
inRange = false;
}
} else {
if (!(currentValue >= min && currentValue <= max)) {
inRange = false;
}
}
}
return inRange;
}
}