1 |
package dk.thoerup.droidradar; |
2 |
|
3 |
|
4 |
import java.util.Date; |
5 |
import java.util.List; |
6 |
|
7 |
import android.content.Context; |
8 |
import android.graphics.Canvas; |
9 |
import android.graphics.Paint; |
10 |
import android.graphics.RectF; |
11 |
import android.location.Location; |
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 |
|
46 |
int width; |
47 |
int height; |
48 |
int hcenter; |
49 |
int vcenter; |
50 |
|
51 |
|
52 |
boolean mRun = true; |
53 |
|
54 |
List<DroidBean> droidBeans; |
55 |
|
56 |
DroidLocator locator = new DroidLocator(); |
57 |
|
58 |
long imei; |
59 |
|
60 |
Thread t; |
61 |
|
62 |
public RadarView(Context context) { |
63 |
super(context); |
64 |
init(); |
65 |
} |
66 |
|
67 |
|
68 |
public RadarView(Context context, AttributeSet attr) { |
69 |
super(context,attr); |
70 |
init(); |
71 |
} |
72 |
|
73 |
public RadarView(Context context, AttributeSet attr, int defstyle) { |
74 |
super(context,attr, defstyle); |
75 |
init(); |
76 |
} |
77 |
|
78 |
|
79 |
void init() { |
80 |
p.setColor(0xaa00aa00);//Dark green |
81 |
p.setStrokeWidth(2); |
82 |
p.setStyle(Paint.Style.STROKE); |
83 |
p.setAntiAlias(true); |
84 |
p.setTextSize(1); |
85 |
|
86 |
p2.setStrokeWidth(2); |
87 |
p2.setColor(0xff00cc00);//Dark green |
88 |
p2.setStyle(Paint.Style.STROKE); |
89 |
//p2.setAntiAlias(true); |
90 |
|
91 |
|
92 |
p3.setStrokeWidth(2); |
93 |
p3.setColor(0xff00cc00);//Dark green |
94 |
p3.setStyle(Paint.Style.FILL); |
95 |
|
96 |
surfaceHolder = this.getHolder(); |
97 |
surfaceHolder.addCallback(this); |
98 |
|
99 |
t = new Thread(this); |
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 |
t.start(); |
169 |
locator.setDroidsLocatedListener(this); |
170 |
Thread locatorThread = new Thread(locator); |
171 |
locatorThread.start(); |
172 |
} |
173 |
|
174 |
|
175 |
@Override |
176 |
public void surfaceDestroyed(SurfaceHolder holder) { //surfaceholder.CallBack |
177 |
Log.i("surfacDestroyed()","-"); |
178 |
mRun = false; |
179 |
locator.stop(); |
180 |
} |
181 |
|
182 |
public void run() { |
183 |
while (mRun) { |
184 |
Canvas c = null; |
185 |
try { |
186 |
c = surfaceHolder.lockCanvas(null); |
187 |
synchronized (surfaceHolder) { |
188 |
//if (mMode == STATE_RUNNING) |
189 |
// updatePhysics(); |
190 |
myDraw(c); |
191 |
} |
192 |
} finally { |
193 |
// do this in a finally so that if an exception is thrown |
194 |
// during the above, we don't leave the Surface in an |
195 |
// inconsistent state |
196 |
if (c != null) { |
197 |
surfaceHolder.unlockCanvasAndPost(c); |
198 |
} |
199 |
} |
200 |
try { |
201 |
Thread.sleep(20); |
202 |
} catch (InterruptedException e) {} |
203 |
} |
204 |
} |
205 |
|
206 |
|
207 |
|
208 |
@Override |
209 |
public boolean onTouchEvent(MotionEvent event) { |
210 |
float x = event.getX(); |
211 |
float y = event.getY(); |
212 |
Log.i("TouchEvent", "Coords: " + x + "," + y ); |
213 |
if (x>25 && x<95 && y>360 && y<400) { //zoom out |
214 |
Log.i("Button", "Zoom Out"); |
215 |
if (distanceBase < 16000) { |
216 |
distanceBase *= 2; |
217 |
updateDistanceTexts(); |
218 |
} |
219 |
} |
220 |
|
221 |
if (x>105 && x<175 && y>360 && y<400) { |
222 |
Log.i("Button", "Zoom In"); // zoom in |
223 |
if (distanceBase > 125) { |
224 |
distanceBase /= 2; |
225 |
updateDistanceTexts(); |
226 |
} |
227 |
} |
228 |
|
229 |
|
230 |
return false; |
231 |
} |
232 |
|
233 |
void updateDistanceTexts() { |
234 |
distanceText1 = formatLength(distanceBase); |
235 |
distanceText2 = formatLength(distanceBase*2); |
236 |
distanceText3 = formatLength(distanceBase*3); |
237 |
distanceText4 = formatLength(distanceBase*4); |
238 |
|
239 |
textWidth1 = textWidth(distanceText1); |
240 |
textWidth2 = textWidth(distanceText2); |
241 |
textWidth3 = textWidth(distanceText3); |
242 |
textWidth4 = textWidth(distanceText4); |
243 |
|
244 |
} |
245 |
|
246 |
float textWidth(String text) { |
247 |
float[] width = { 0.0f }; |
248 |
p3.breakText(text, true, 200, width); |
249 |
return width[0]; |
250 |
|
251 |
} |
252 |
|
253 |
String formatLength(int meters) { |
254 |
if (meters == 1500) //yuck, special cases !!! |
255 |
return "1.5 km"; |
256 |
|
257 |
if (meters>=1000) |
258 |
return "" + (meters/1000) + " km"; |
259 |
else |
260 |
return "" + meters + " m"; |
261 |
} |
262 |
|
263 |
void setCurrentLocation(Location loc) { |
264 |
currentLocation = loc; |
265 |
gpsText = formatLength( (int)loc.getAccuracy()); |
266 |
|
267 |
locator.setCurrentLocation(loc); |
268 |
|
269 |
} |
270 |
|
271 |
void setHeading(int head) { |
272 |
heading = head; |
273 |
headingText = "" + head; |
274 |
} |
275 |
|
276 |
public void setImei(long imei) { |
277 |
this.imei = imei; |
278 |
locator.setImei(imei); |
279 |
} |
280 |
|
281 |
void drawLocations(Canvas canvas) { |
282 |
if (currentLocation != null && droidBeans != null) { |
283 |
|
284 |
for(DroidBean droid : droidBeans) { |
285 |
drawLocation(canvas, droid); |
286 |
} |
287 |
} |
288 |
} |
289 |
|
290 |
void drawLocation(Canvas canvas, DroidBean droid) { |
291 |
|
292 |
Location location = droid.getLocation(); |
293 |
|
294 |
float bearing = currentLocation.bearingTo(location); |
295 |
float distance = currentLocation.distanceTo(location); |
296 |
|
297 |
//Log.e("Bearing to", ""+bearing); |
298 |
//Log.e("Distance to", ""+distance); |
299 |
|
300 |
bearing -= (float) heading; |
301 |
|
302 |
if (distance <= (distanceBase*4)) { |
303 |
|
304 |
//convert from degrees to radians |
305 |
double bearingRad = Math.toRadians(bearing-90); |
306 |
|
307 |
double hypotenuse = (distance / (distanceBase*4.0)) * 150.0; |
308 |
double vertCathesis = Math.sin(bearingRad)*hypotenuse; |
309 |
double horzCathesis = Math.cos(bearingRad)*hypotenuse; |
310 |
|
311 |
|
312 |
//vertCathesis *= -1.0; |
313 |
|
314 |
vertCathesis += vcenter; |
315 |
horzCathesis += hcenter; |
316 |
|
317 |
droid.setGuiX((int)horzCathesis); |
318 |
droid.setGuiY((int)vertCathesis); |
319 |
|
320 |
canvas.drawCircle( (float)horzCathesis, (float)vertCathesis, 3, p3); |
321 |
|
322 |
} |
323 |
} |
324 |
|
325 |
|
326 |
@Override |
327 |
public void onDroidsLocated() { //callback |
328 |
droidBeans = locator.getDroids(); |
329 |
Log.i("RadarView", "OnDroidsLocated"); |
330 |
} |
331 |
|
332 |
} |