1 |
package dk.thoerup.droidradar; |
2 |
|
3 |
|
4 |
import java.util.List; |
5 |
|
6 |
import android.content.Context; |
7 |
import android.graphics.Canvas; |
8 |
import android.graphics.Paint; |
9 |
import android.graphics.RectF; |
10 |
import android.location.Location; |
11 |
import android.os.Vibrator; |
12 |
import android.util.AttributeSet; |
13 |
import android.util.Log; |
14 |
import android.view.MotionEvent; |
15 |
import android.view.SurfaceHolder; |
16 |
import android.view.SurfaceView; |
17 |
|
18 |
//public class RadarView extends View { |
19 |
public class RadarView extends SurfaceView implements SurfaceHolder.Callback, Runnable, DroidLocator.DroidsLocatedListener { |
20 |
|
21 |
int angle = 0; |
22 |
Paint p = new Paint(); |
23 |
Paint p2 = new Paint(); |
24 |
Paint p3 = new Paint(); |
25 |
|
26 |
SurfaceHolder surfaceHolder; |
27 |
int distanceBase = 500; |
28 |
|
29 |
String distanceText1; |
30 |
String distanceText2; |
31 |
String distanceText3; |
32 |
String distanceText4; |
33 |
|
34 |
float textWidth1; |
35 |
float textWidth2; |
36 |
float textWidth3; |
37 |
float textWidth4; |
38 |
|
39 |
String gpsText = "-"; |
40 |
Location currentLocation; |
41 |
|
42 |
int heading; |
43 |
String headingText = "-"; |
44 |
|
45 |
Vibrator vibrator; |
46 |
|
47 |
int width; |
48 |
int height; |
49 |
int hcenter; |
50 |
int vcenter; |
51 |
|
52 |
|
53 |
boolean mRun = true; |
54 |
|
55 |
List<DroidBean> droidBeans; |
56 |
|
57 |
DroidLocator locator = new DroidLocator(); |
58 |
|
59 |
long imei; |
60 |
|
61 |
//Thread t; |
62 |
|
63 |
public RadarView(Context context) { |
64 |
super(context); |
65 |
init(); |
66 |
} |
67 |
|
68 |
|
69 |
public RadarView(Context context, AttributeSet attr) { |
70 |
super(context,attr); |
71 |
init(); |
72 |
} |
73 |
|
74 |
public RadarView(Context context, AttributeSet attr, int defstyle) { |
75 |
super(context,attr, defstyle); |
76 |
init(); |
77 |
} |
78 |
|
79 |
|
80 |
void init() { |
81 |
p.setColor(0xaa00aa00);//Dark green |
82 |
p.setStrokeWidth(2); |
83 |
p.setStyle(Paint.Style.STROKE); |
84 |
p.setAntiAlias(true); |
85 |
p.setTextSize(1); |
86 |
|
87 |
p2.setStrokeWidth(2); |
88 |
p2.setColor(0xff00cc00);//Dark green |
89 |
p2.setStyle(Paint.Style.STROKE); |
90 |
//p2.setAntiAlias(true); |
91 |
|
92 |
|
93 |
p3.setStrokeWidth(2); |
94 |
p3.setColor(0xff00cc00);//Dark green |
95 |
p3.setStyle(Paint.Style.FILL); |
96 |
|
97 |
surfaceHolder = this.getHolder(); |
98 |
surfaceHolder.addCallback(this); |
99 |
|
100 |
|
101 |
updateDistanceTexts(); |
102 |
} |
103 |
|
104 |
protected void myDraw(Canvas canvas) { |
105 |
|
106 |
canvas.drawColor(0xff000000); |
107 |
canvas.drawLine(hcenter-5, vcenter-5, hcenter+5, vcenter+5, p); |
108 |
canvas.drawLine(hcenter-5, vcenter+5, hcenter+5, vcenter-5, p); |
109 |
canvas.drawCircle(hcenter, vcenter, 40, p); |
110 |
canvas.drawCircle(hcenter, vcenter, 80, p); |
111 |
canvas.drawCircle(hcenter, vcenter, 115, p); |
112 |
canvas.drawCircle(hcenter, vcenter, 150, p); |
113 |
|
114 |
canvas.drawText(distanceText1, (width-textWidth1)/2, vcenter+40+1, p3); |
115 |
canvas.drawText(distanceText2, (width-textWidth2)/2, vcenter+80+1, p3); |
116 |
canvas.drawText(distanceText3, (width-textWidth3)/2, vcenter+115+1, p3); |
117 |
canvas.drawText(distanceText4, (width-textWidth4)/2, vcenter+150+1, p3); |
118 |
|
119 |
|
120 |
final float lineLength = 150; |
121 |
angle -= 6; |
122 |
|
123 |
|
124 |
double base = (Math.sin(angle/360.0)*lineLength) + (float)hcenter; |
125 |
double height = (Math.cos(angle/360.0)*lineLength) + (float)vcenter; |
126 |
|
127 |
|
128 |
canvas.drawLine(hcenter, vcenter, (float)base, (float)height, p2); |
129 |
|
130 |
|
131 |
|
132 |
//left,top,right,bottom |
133 |
canvas.drawRoundRect( new RectF(20,360,180,400), 10, 10, p); |
134 |
canvas.drawLine(100, 360, 100, 400, p); |
135 |
|
136 |
canvas.drawLine(45, 380, 75, 380, p); // Minus |
137 |
canvas.drawLine(125, 380, 155, 380, p); // Plus |
138 |
canvas.drawLine(140, 365, 140, 395, p); // Plus |
139 |
|
140 |
// Gps status |
141 |
canvas.drawText("Gps accuracy", 10, 20,p3); |
142 |
canvas.drawText(gpsText, 10, 40, p3); |
143 |
|
144 |
// Heading |
145 |
canvas.drawText("Heading", 270, 20, p3); |
146 |
canvas.drawText(headingText, 280, 40, p3); |
147 |
|
148 |
|
149 |
drawLocations(canvas); |
150 |
} |
151 |
|
152 |
|
153 |
@Override //surfaceholder.CallBack |
154 |
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { |
155 |
Log.i("surfacChaned()","-"); |
156 |
} |
157 |
|
158 |
|
159 |
@Override |
160 |
public void surfaceCreated(SurfaceHolder holder) { //surfaceholder.CallBack |
161 |
Log.i("surfacCreated()","-"); |
162 |
|
163 |
width = this.getWidth(); |
164 |
height = this.getHeight(); |
165 |
hcenter = width / 2; |
166 |
vcenter = hcenter + 20; |
167 |
|
168 |
mRun = true; |
169 |
Thread drawThread = new Thread(this); |
170 |
|
171 |
drawThread.start(); |
172 |
locator.setDroidsLocatedListener(this); |
173 |
locator.setContinue(true); |
174 |
Thread locatorThread = new Thread(locator); |
175 |
locatorThread.start(); |
176 |
} |
177 |
|
178 |
|
179 |
@Override |
180 |
public void surfaceDestroyed(SurfaceHolder holder) { //surfaceholder.CallBack |
181 |
Log.i("surfacDestroyed()","-"); |
182 |
mRun = false; |
183 |
locator.setContinue(false); |
184 |
} |
185 |
|
186 |
public void run() { |
187 |
while (mRun) { |
188 |
Canvas c = null; |
189 |
try { |
190 |
c = surfaceHolder.lockCanvas(null); |
191 |
synchronized (surfaceHolder) { |
192 |
//if (mMode == STATE_RUNNING) |
193 |
// updatePhysics(); |
194 |
myDraw(c); |
195 |
} |
196 |
} finally { |
197 |
// do this in a finally so that if an exception is thrown |
198 |
// during the above, we don't leave the Surface in an |
199 |
// inconsistent state |
200 |
if (c != null) { |
201 |
surfaceHolder.unlockCanvasAndPost(c); |
202 |
} |
203 |
} |
204 |
try { |
205 |
Thread.sleep(20); |
206 |
} catch (InterruptedException e) {} |
207 |
} |
208 |
Log.i("RadarView", "Draw thread exiting"); |
209 |
} |
210 |
|
211 |
|
212 |
|
213 |
@Override |
214 |
public boolean onTouchEvent(MotionEvent event) { |
215 |
float x = event.getX(); |
216 |
float y = event.getY(); |
217 |
Log.i("TouchEvent", "Coords: " + x + "," + y ); |
218 |
if (x>25 && x<95 && y>360 && y<400) { //zoom out |
219 |
Log.i("Button", "Zoom Out"); |
220 |
if (distanceBase < 16000) { |
221 |
distanceBase *= 2; |
222 |
updateDistanceTexts(); |
223 |
} else { |
224 |
vibrator.vibrate(100); |
225 |
} |
226 |
} |
227 |
|
228 |
if (x>105 && x<175 && y>360 && y<400) { |
229 |
Log.i("Button", "Zoom In"); // zoom in |
230 |
if (distanceBase > 125) { |
231 |
distanceBase /= 2; |
232 |
updateDistanceTexts(); |
233 |
} else { |
234 |
vibrator.vibrate(100); |
235 |
} |
236 |
} |
237 |
|
238 |
|
239 |
return false; |
240 |
} |
241 |
|
242 |
void updateDistanceTexts() { |
243 |
distanceText1 = formatLength(distanceBase); |
244 |
distanceText2 = formatLength(distanceBase*2); |
245 |
distanceText3 = formatLength(distanceBase*3); |
246 |
distanceText4 = formatLength(distanceBase*4); |
247 |
|
248 |
textWidth1 = textWidth(distanceText1); |
249 |
textWidth2 = textWidth(distanceText2); |
250 |
textWidth3 = textWidth(distanceText3); |
251 |
textWidth4 = textWidth(distanceText4); |
252 |
|
253 |
} |
254 |
|
255 |
float textWidth(String text) { |
256 |
float[] width = { 0.0f }; |
257 |
p3.breakText(text, true, 200, width); |
258 |
return width[0]; |
259 |
|
260 |
} |
261 |
|
262 |
String formatLength(int meters) { |
263 |
if (meters == 1500) //yuck, special cases !!! |
264 |
return "1.5 km"; |
265 |
|
266 |
if (meters>=1000) |
267 |
return "" + (meters/1000) + " km"; |
268 |
else |
269 |
return "" + meters + " m"; |
270 |
} |
271 |
|
272 |
void setCurrentLocation(Location loc) { |
273 |
currentLocation = loc; |
274 |
gpsText = formatLength( (int)loc.getAccuracy()); |
275 |
|
276 |
locator.setCurrentLocation(loc); |
277 |
|
278 |
} |
279 |
|
280 |
void setHeading(int head) { |
281 |
heading = head; |
282 |
headingText = "" + head; |
283 |
} |
284 |
|
285 |
public void setImei(long imei) { |
286 |
this.imei = imei; |
287 |
locator.setImei(imei); |
288 |
} |
289 |
|
290 |
public void setVibrator(Vibrator v) { |
291 |
vibrator = v; |
292 |
} |
293 |
|
294 |
void drawLocations(Canvas canvas) { |
295 |
if (currentLocation != null && droidBeans != null) { |
296 |
|
297 |
for(DroidBean droid : droidBeans) { |
298 |
drawLocation(canvas, droid); |
299 |
} |
300 |
} |
301 |
} |
302 |
|
303 |
void drawLocation(Canvas canvas, DroidBean droid) { |
304 |
|
305 |
Location location = droid.getLocation(); |
306 |
|
307 |
float bearing = currentLocation.bearingTo(location); |
308 |
float distance = currentLocation.distanceTo(location); |
309 |
|
310 |
//Log.e("Bearing to", ""+bearing); |
311 |
//Log.e("Distance to", ""+distance); |
312 |
|
313 |
bearing -= (float) heading; |
314 |
|
315 |
if (distance <= (distanceBase*4)) { |
316 |
|
317 |
//convert from degrees to radians |
318 |
double bearingRad = Math.toRadians(bearing-90); |
319 |
|
320 |
double hypotenuse = (distance / (distanceBase*4.0)) * 150.0; |
321 |
double vertCathesis = Math.sin(bearingRad)*hypotenuse; |
322 |
double horzCathesis = Math.cos(bearingRad)*hypotenuse; |
323 |
|
324 |
|
325 |
//vertCathesis *= -1.0; |
326 |
|
327 |
vertCathesis += vcenter; |
328 |
horzCathesis += hcenter; |
329 |
|
330 |
droid.setGuiX((int)horzCathesis); |
331 |
droid.setGuiY((int)vertCathesis); |
332 |
|
333 |
canvas.drawCircle( (float)horzCathesis, (float)vertCathesis, 3, p3); |
334 |
|
335 |
} |
336 |
} |
337 |
|
338 |
|
339 |
@Override |
340 |
public void onDroidsLocated() { //callback |
341 |
droidBeans = locator.getDroids(); |
342 |
Log.i("RadarView", "OnDroidsLocated"); |
343 |
} |
344 |
|
345 |
} |