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