1 |
package dk.thoerup.friendradar; |
2 |
|
3 |
import java.util.List; |
4 |
|
5 |
import android.app.Activity; |
6 |
import android.content.Context; |
7 |
import android.hardware.Sensor; |
8 |
import android.hardware.SensorEvent; |
9 |
import android.hardware.SensorEventListener; |
10 |
import android.hardware.SensorManager; |
11 |
import android.location.Location; |
12 |
import android.location.LocationListener; |
13 |
import android.location.LocationManager; |
14 |
import android.net.UrlQuerySanitizer.ValueSanitizer; |
15 |
import android.os.Bundle; |
16 |
import android.util.Log; |
17 |
|
18 |
public class RadarActivity extends Activity implements LocationListener { |
19 |
|
20 |
LocationManager locManager; |
21 |
SensorManager sensorManager; |
22 |
|
23 |
RadarView radar; |
24 |
@Override |
25 |
public void onCreate(Bundle savedInstanceState) { |
26 |
super.onCreate(savedInstanceState); |
27 |
setContentView(R.layout.main); |
28 |
radar = (RadarView) findViewById(R.id.radar); |
29 |
|
30 |
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); |
31 |
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); |
32 |
} |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
@Override |
38 |
protected void onResume() { |
39 |
super.onResume(); |
40 |
|
41 |
if (locManager.isProviderEnabled("gps")) { |
42 |
locManager.requestLocationUpdates("gps", 0, 0, this); |
43 |
} else { |
44 |
// ToDo: alert the user about missing gps provider |
45 |
} |
46 |
|
47 |
List<Sensor> orientationSensors = sensorManager.getSensorList(Sensor.TYPE_ORIENTATION); |
48 |
Log.e("sensors1", "sensor count" + orientationSensors.size()); |
49 |
if (orientationSensors.size() > 0) { |
50 |
sensorManager.registerListener(orientationListener, orientationSensors.get(0), SensorManager.SENSOR_DELAY_NORMAL); |
51 |
} else { |
52 |
// ToDo: alert the user about missing orientation sensor |
53 |
Log.e("sensor", "no orientation sensor"); |
54 |
} |
55 |
|
56 |
} |
57 |
|
58 |
|
59 |
|
60 |
@Override |
61 |
protected void onPause() { |
62 |
super.onPause(); |
63 |
|
64 |
locManager.removeUpdates(this); |
65 |
sensorManager.unregisterListener(orientationListener); |
66 |
} |
67 |
|
68 |
|
69 |
|
70 |
|
71 |
@Override |
72 |
public void onLocationChanged(Location location) { |
73 |
radar.setCurrentLocation(location); |
74 |
} |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
@Override |
80 |
public void onProviderDisabled(String provider) { |
81 |
} |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
@Override |
87 |
public void onProviderEnabled(String provider) { |
88 |
} |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
@Override |
94 |
public void onStatusChanged(String provider, int status, Bundle extras) { |
95 |
} |
96 |
|
97 |
SensorEventListener orientationListener = new SensorEventListener() { |
98 |
|
99 |
@Override |
100 |
public void onSensorChanged(SensorEvent event) { |
101 |
radar.setHeading( (int)event.values[0]); |
102 |
} |
103 |
|
104 |
@Override |
105 |
public void onAccuracyChanged(Sensor sensor, int accuracy) { |
106 |
} |
107 |
}; |
108 |
} |