1 |
package dk.thoerup.friendradar; |
package dk.thoerup.droidradar; |
2 |
|
|
3 |
|
|
4 |
|
import java.util.Date; |
5 |
|
import java.util.List; |
6 |
|
|
7 |
import android.content.Context; |
import android.content.Context; |
8 |
import android.graphics.Canvas; |
import android.graphics.Canvas; |
9 |
import android.graphics.Paint; |
import android.graphics.Paint; |
16 |
import android.view.SurfaceView; |
import android.view.SurfaceView; |
17 |
|
|
18 |
//public class RadarView extends View { |
//public class RadarView extends View { |
19 |
public class RadarView extends SurfaceView implements SurfaceHolder.Callback, Runnable { |
public class RadarView extends SurfaceView implements SurfaceHolder.Callback, Runnable, DroidLocator.DroidsLocatedListener { |
20 |
|
|
21 |
int angle = 0; |
int angle = 0; |
22 |
Paint p = new Paint(); |
Paint p = new Paint(); |
23 |
Paint p2 = new Paint(); |
Paint p2 = new Paint(); |
51 |
|
|
52 |
boolean mRun = true; |
boolean mRun = true; |
53 |
|
|
54 |
|
List<DroidBean> droidBeans; |
55 |
|
|
56 |
|
DroidLocator locator = new DroidLocator(); |
57 |
|
|
58 |
|
long imei; |
59 |
|
|
60 |
Thread t; |
Thread t; |
61 |
|
|
62 |
public RadarView(Context context) { |
public RadarView(Context context) { |
106 |
canvas.drawColor(0xff000000); |
canvas.drawColor(0xff000000); |
107 |
canvas.drawLine(hcenter-5, vcenter-5, hcenter+5, vcenter+5, p); |
canvas.drawLine(hcenter-5, vcenter-5, hcenter+5, vcenter+5, p); |
108 |
canvas.drawLine(hcenter-5, vcenter+5, hcenter+5, vcenter-5, p); |
canvas.drawLine(hcenter-5, vcenter+5, hcenter+5, vcenter-5, p); |
109 |
canvas.drawCircle(hcenter, vcenter+5, 40, p); |
canvas.drawCircle(hcenter, vcenter, 40, p); |
110 |
canvas.drawCircle(hcenter, vcenter+5, 80, p); |
canvas.drawCircle(hcenter, vcenter, 80, p); |
111 |
canvas.drawCircle(hcenter, vcenter+5, 115, p); |
canvas.drawCircle(hcenter, vcenter, 115, p); |
112 |
canvas.drawCircle(hcenter, vcenter+5, 150, p); |
canvas.drawCircle(hcenter, vcenter, 150, p); |
113 |
|
|
114 |
canvas.drawText(distanceText1, (width-textWidth1)/2, vcenter+40+6, p3); |
canvas.drawText(distanceText1, (width-textWidth1)/2, vcenter+40+1, p3); |
115 |
canvas.drawText(distanceText2, (width-textWidth2)/2, vcenter+80+6, p3); |
canvas.drawText(distanceText2, (width-textWidth2)/2, vcenter+80+1, p3); |
116 |
canvas.drawText(distanceText3, (width-textWidth3)/2, vcenter+115+6, p3); |
canvas.drawText(distanceText3, (width-textWidth3)/2, vcenter+115+1, p3); |
117 |
canvas.drawText(distanceText4, (width-textWidth4)/2, vcenter+150+6, p3); |
canvas.drawText(distanceText4, (width-textWidth4)/2, vcenter+150+1, p3); |
118 |
|
|
119 |
final float correction = 5; |
|
120 |
final float lineLength = 150; |
final float lineLength = 150; |
121 |
angle -= 6; |
angle -= 6; |
122 |
|
|
123 |
|
|
124 |
double base = (Math.sin(angle/360.0)*lineLength) + (float)hcenter; |
double base = (Math.sin(angle/360.0)*lineLength) + (float)hcenter; |
125 |
double height = (Math.cos(angle/360.0)*lineLength) + (float)vcenter; |
double height = (Math.cos(angle/360.0)*lineLength) + (float)vcenter; |
|
height += correction; |
|
126 |
|
|
127 |
|
|
128 |
canvas.drawLine(hcenter, vcenter, (float)base, (float)height, p2); |
canvas.drawLine(hcenter, vcenter, (float)base, (float)height, p2); |
166 |
vcenter = hcenter + 20; |
vcenter = hcenter + 20; |
167 |
|
|
168 |
t.start(); |
t.start(); |
169 |
|
locator.setDroidsLocatedListener(this); |
170 |
|
Thread locatorThread = new Thread(locator); |
171 |
|
locatorThread.start(); |
172 |
} |
} |
173 |
|
|
174 |
|
|
176 |
public void surfaceDestroyed(SurfaceHolder holder) { //surfaceholder.CallBack |
public void surfaceDestroyed(SurfaceHolder holder) { //surfaceholder.CallBack |
177 |
Log.i("surfacDestroyed()","-"); |
Log.i("surfacDestroyed()","-"); |
178 |
mRun = false; |
mRun = false; |
179 |
|
locator.stop(); |
180 |
} |
} |
181 |
|
|
182 |
public void run() { |
public void run() { |
263 |
void setCurrentLocation(Location loc) { |
void setCurrentLocation(Location loc) { |
264 |
currentLocation = loc; |
currentLocation = loc; |
265 |
gpsText = formatLength( (int)loc.getAccuracy()); |
gpsText = formatLength( (int)loc.getAccuracy()); |
266 |
|
|
267 |
|
locator.setCurrentLocation(loc); |
268 |
|
|
269 |
} |
} |
270 |
|
|
271 |
void setHeading(int head) { |
void setHeading(int head) { |
273 |
headingText = "" + head; |
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) { |
void drawLocations(Canvas canvas) { |
282 |
if (currentLocation != null) { |
if (currentLocation != null && droidBeans != null) { |
|
Location loc = new Location("gps"); |
|
|
loc.setLatitude(56.394908); //Hedemøllevej |
|
|
loc.setLongitude(9.666356); |
|
|
|
|
|
// loc.setLatitude(56.298307); //Kongensbro |
|
|
// loc.setLongitude(9.669293); |
|
283 |
|
|
284 |
loc.setLatitude(56.398577); //Langå |
for(DroidBean droid : droidBeans) { |
285 |
loc.setLongitude(9.899515); |
drawLocation(canvas, droid); |
286 |
|
} |
|
|
|
|
drawLocation(canvas, loc); |
|
287 |
} |
} |
288 |
} |
} |
289 |
|
|
290 |
void drawLocation(Canvas canvas, Location location) { |
void drawLocation(Canvas canvas, DroidBean droid) { |
291 |
|
|
292 |
|
Location location = droid.getLocation(); |
293 |
|
|
294 |
float bearing = currentLocation.bearingTo(location); |
float bearing = currentLocation.bearingTo(location); |
295 |
float distance = currentLocation.distanceTo(location); |
float distance = currentLocation.distanceTo(location); |
307 |
double hypotenuse = (distance / (distanceBase*4.0)) * 150.0; |
double hypotenuse = (distance / (distanceBase*4.0)) * 150.0; |
308 |
double vertCathesis = Math.sin(bearingRad)*hypotenuse; |
double vertCathesis = Math.sin(bearingRad)*hypotenuse; |
309 |
double horzCathesis = Math.cos(bearingRad)*hypotenuse; |
double horzCathesis = Math.cos(bearingRad)*hypotenuse; |
310 |
|
|
311 |
|
|
312 |
//vertCathesis *= -1.0; |
//vertCathesis *= -1.0; |
313 |
|
|
314 |
vertCathesis += vcenter; |
vertCathesis += vcenter; |
315 |
horzCathesis += hcenter; |
horzCathesis += hcenter; |
316 |
|
|
317 |
|
droid.setGuiX((int)horzCathesis); |
318 |
|
droid.setGuiY((int)vertCathesis); |
319 |
|
|
320 |
canvas.drawCircle( (float)horzCathesis, (float)vertCathesis, 3, p3); |
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 |
} |
} |