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