/[aagtl_public1]/src/com/zoffcc/applications/aagtl/ArrowView.java
aagtl

Contents of /src/com/zoffcc/applications/aagtl/ArrowView.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Sun Aug 5 13:48:36 2012 UTC (11 years, 7 months ago) by zoffadmin
File size: 12407 byte(s)
initial import of aagtl source code
1 /**
2 * aagtl Advanced Geocaching Tool for Android
3 * loosely based on agtl by Daniel Fett <fett@danielfett.de>
4 * Copyright (C) 2010 - 2012 Zoff.cc <aagtl@work.zoff.cc>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 package com.zoffcc.applications.aagtl;
22
23 import java.util.Calendar;
24 import java.util.Date;
25 import java.util.TimeZone;
26
27 import android.content.Context;
28 import android.graphics.Canvas;
29 import android.graphics.Color;
30 import android.graphics.Paint;
31 import android.graphics.Path;
32 import android.graphics.RectF;
33 import android.graphics.Typeface;
34 import android.os.SystemClock;
35 import android.widget.ImageView;
36 import bpi.sdbm.illuminance.SolarPosition;
37
38 import com.luckycatlabs.sunrisesunset.SunriseSunsetCalculator;
39 import com.luckycatlabs.sunrisesunset.calculator.SolarEventCalculator;
40 import com.luckycatlabs.sunrisesunset.dto.Location2;
41
42 public class ArrowView extends ImageView
43 {
44
45 Paint arrow_paint = new Paint(0);
46 Paint text_paint = new Paint(0);
47
48 aagtl main_aagtl;
49
50 long mLastCalcSunMillis = -1;
51 public double azmiuth_cache = -1;
52 public double zenith_cache = -1;
53 public String sunrise_cache = "";
54 public String sunset_cache = "";
55 public double elevation = 0;
56
57 public double moon_azimuth_cache = -1;
58 public double moon_evelation_cache = -1;
59
60 Boolean must_calc_new = true;
61 SunriseSunsetCalculator calc = null;
62 Calendar cx = null;
63 SolarPosition.SunCoordinates sc = null;
64
65 int COLOR_QUALITY_INNER = Color.parseColor("#348017");
66 int COLOR_QUALITY_OUTER = Color.parseColor("#150517");
67 int COLOR_ARROW_ATTARGET = Color.parseColor("#C11B17");
68 int COLOR_ARROW_NEAR = Color.parseColor("#F87217");
69 int COLOR_ARROW_DEFAULT = Color.parseColor("#348017");
70 int COLOR_ARROW_OUTER_LINE = Color.parseColor("#150517");
71 int COLOR_CIRCLE_OUTLINE = Color.parseColor("#424242");
72 int DISTANCE_DISABLE_ARROW = 2; // 2 meters
73 int NORTH_INDICATOR_SIZE = 20; // 20 pixels
74
75 double ARROW_OFFSET = 1.0 / 3.0; // Offset to center of arrow, calculated as 2-x = sqrt(1^2+(x+1)^2)
76
77 public ArrowView(Context context, aagtl main_aagtl)
78 {
79 super(context);
80
81 text_paint.setColor(Color.WHITE);
82 //text_paint.setStyle(Paint.Style.FILL);
83 text_paint.setTextSize(19);
84 text_paint.setTypeface(Typeface.DEFAULT_BOLD);
85 text_paint.setAntiAlias(true);
86
87 this.main_aagtl = main_aagtl;
88 this.clear_stuff();
89 }
90
91 public String roundTwoDecimals(double d)
92 {
93 return String.format("%.2f", d);
94 }
95
96 public void calc_sun_stats()
97 {
98 //
99 //
100 // SUN ----------------
101 //
102 //
103 this.must_calc_new = (SystemClock.elapsedRealtime() - mLastCalcSunMillis) > 60000; // every 60 seconds calc new
104
105 if ((this.must_calc_new) || (this.azmiuth_cache == -1))
106 {
107 this.mLastCalcSunMillis = SystemClock.elapsedRealtime();
108 TimeZone t = TimeZone.getDefault();
109 //System.out.println(t.getID());
110 calc = new SunriseSunsetCalculator(new Location2(String
111 .valueOf(this.main_aagtl.global_settings.map_position_lat), String
112 .valueOf(this.main_aagtl.global_settings.map_position_lon)), t.getID());
113 cx = Calendar.getInstance();
114 sc = SolarPosition.getSunPosition(new Date(),
115 this.main_aagtl.global_settings.map_position_lat,
116 this.main_aagtl.global_settings.map_position_lon);
117
118 this.azmiuth_cache = sc.azimuth;
119 this.zenith_cache = sc.zenithAngle;
120 this.sunrise_cache = calc.getOfficialSunriseForDate(cx);
121 this.sunset_cache = calc.getOfficialSunsetForDate(cx);
122 //System.out.println("calc moon");
123 SolarEventCalculator.moonCoor_ret moon_stats = calc.computeMoon(cx);
124 moon_azimuth_cache = moon_stats.az;
125 moon_evelation_cache = moon_stats.alt;
126 }
127 //
128 this.elevation = 90 - this.zenith_cache;
129 //
130 // SUN ----------------
131 //
132 //
133 }
134
135 public void onDraw(Canvas c)
136 {
137 //System.out.println("ArrowView: onDraw");
138
139
140 // draw grey circle around the arrow
141 // draw grey circle around the arrow
142 int indicator_radius = (int) ((Math.min(this.getWidth(), this.getHeight()) / 4) * 1.1f);
143 int indicator_dist = (Math.min(this.getHeight(), this.getWidth()) / 2) - indicator_radius / 2;
144 int[] center = new int[2];
145 center[0] = this.getWidth() / 2;
146 center[1] = this.getHeight() / 2;
147
148 RectF coord_rect = new RectF(center[0] - indicator_dist, center[1] - indicator_dist,
149 center[0] - indicator_dist + indicator_dist * 2, center[1] - indicator_dist
150 + indicator_dist * 2);
151
152 int start = 0; // 0°
153 int end = 360; // 360°
154 arrow_paint.setAntiAlias(true);
155 arrow_paint.setColor(COLOR_CIRCLE_OUTLINE);
156 arrow_paint.setStyle(Paint.Style.STROKE);
157 arrow_paint.setStrokeWidth(3);
158 c.drawArc(coord_rect, start, end, false, arrow_paint);
159 // draw grey circle around the arrow
160 // draw grey circle around the arrow
161
162
163 Coordinate my_pos = new Coordinate(this.main_aagtl.global_settings.map_position_lat,
164 this.main_aagtl.global_settings.map_position_lon);
165
166 double display_distance = 0;
167 double display_bearing = 0;
168 if (this.main_aagtl.rose.current_target != null)
169 {
170 display_distance = my_pos.distance_to((Coordinate) (this.main_aagtl.rose.current_target));
171
172 display_bearing = my_pos.bearing_to((Coordinate) (this.main_aagtl.rose.current_target))
173 - this.main_aagtl.cross.gps_heading;
174 }
175
176 //if ((this.main_aagtl.cross.gps_heading != -1) && (this.main_aagtl.isGPSFix))
177 if (this.main_aagtl.cross.gps_heading != -1)
178 {
179 double display_north = Math.toRadians(this.main_aagtl.cross.gps_heading);
180 int position_x = (int) (this.getWidth() / 2 - Math.sin(display_north)
181 * (indicator_dist * 1.15));
182 int position_y = (int) (this.getHeight() / 2 - Math.cos(display_north)
183 * (indicator_dist * 1.15));
184
185 text_paint.setColor(Color.WHITE);
186 c.drawText("N", position_x, position_y, text_paint);
187 }
188
189 text_paint.setColor(Color.WHITE);
190 c.drawText(
191 "distance to target: " + String.valueOf(roundTwoDecimals(display_distance)) + " m", 10,
192 34, text_paint);
193
194 c.drawText("target: " + String.valueOf((int) display_bearing) + " °", 10, 2 * 34, text_paint);
195
196 c.drawText("my heading: " + String.valueOf((int) this.main_aagtl.cross.gps_heading) + " °",
197 this.getWidth() / 2, 2 * 34, text_paint);
198
199 c.drawText("gps accuracy: " + String.valueOf(roundTwoDecimals(this.main_aagtl.cross.gps_acc))
200 + " m", 10, 3 * 34, text_paint);
201 c.drawText("gps Sats.: " + String.valueOf((int) this.main_aagtl.cross.used_sats), 10, 4 * 34,
202 text_paint);
203
204
205 //
206 //
207 // SUN ----------------
208 //
209 //
210 this.calc_sun_stats();
211 c.drawText("sunrise: " + this.sunrise_cache, 10, this.getHeight() - 34 * 4, text_paint);
212 c.drawText("sunset: " + this.sunset_cache, 10, this.getHeight() - 34 * 3, text_paint);
213
214
215 if (elevation < -0.83)
216 {
217 c.drawText("elevation: *night*", this.getWidth() / 2, this.getHeight() - 34 * 4,
218 text_paint);
219 }
220 else
221 {
222 c.drawText("elevation: " + roundTwoDecimals(elevation), this.getWidth() / 2, this
223 .getHeight() - 34 * 4, text_paint);
224 }
225
226 c.drawText("azimuth: " + roundTwoDecimals(this.azmiuth_cache), this.getWidth() / 2, this
227 .getHeight() - 34 * 3, text_paint);
228 //
229 //
230 // SUN ----------------
231 //
232 //
233
234
235 if (this.main_aagtl.rose.current_target != null)
236 {
237 // gc-code
238 c.drawText("Geocache: " + this.main_aagtl.rose.current_target.name, 10,
239 this.getHeight() - 34 * 2, text_paint);
240 // cache name
241 c.drawText(this.main_aagtl.rose.current_target.title, 10, this.getHeight() - 34,
242 text_paint);
243 }
244
245 // draw signal indicator , part 1
246 int signal_width = 15;
247 RectF coord_rect3 = new RectF(this.getWidth() - signal_width - 2, 0, this.getWidth(), this
248 .getHeight());
249 arrow_paint.setColor(COLOR_QUALITY_OUTER);
250 arrow_paint.setStyle(Paint.Style.STROKE);
251 arrow_paint.setStrokeWidth(3);
252 c.drawRect(coord_rect3, arrow_paint);
253
254 // draw signal indicator , part 2
255 int usable_height = this.getHeight() - 1;
256 int target_height = (int) (usable_height * ((float) (this.main_aagtl.cross.used_sats) / (float) (13)));
257 RectF coord_rect7 = new RectF(this.getWidth() - signal_width - 1, usable_height
258 - target_height, this.getWidth() - 1, usable_height);
259 arrow_paint.setColor(COLOR_QUALITY_INNER);
260 arrow_paint.setStyle(Paint.Style.FILL);
261 c.drawRect(coord_rect7, arrow_paint);
262
263
264 if (this.main_aagtl.rose.current_target != null)
265 {
266 arrow_paint.setStrokeWidth(1);
267 int arrow_color;
268 if (display_distance < 50)
269 {
270 arrow_color = COLOR_ARROW_ATTARGET;
271 }
272 else if (display_distance < 150)
273 {
274 arrow_color = COLOR_ARROW_NEAR;
275 }
276 else
277 {
278 arrow_color = COLOR_ARROW_DEFAULT;
279 }
280
281 if (display_distance > DISTANCE_DISABLE_ARROW)
282 {
283 //c.drawLines(pts, paint) ;
284 double[] arrow_transformed = new double[8];
285 arrow_transformed = this.__get_arrow_transformed(0, 0, this.getWidth(), this
286 .getHeight(), display_bearing, 0.22);
287 Path p = new Path();
288 for (int i = 0; i < 4; i++)
289 {
290 if (i == 0)
291 {
292 p.moveTo((float) arrow_transformed[(i + 1) * 2 - 2],
293 (float) arrow_transformed[(i + 1) * 2 - 1]);
294 }
295 else
296 {
297 p.lineTo((float) arrow_transformed[(i + 1) * 2 - 2],
298 (float) arrow_transformed[(i + 1) * 2 - 1]);
299 }
300 }
301 p.close();
302 arrow_paint.setStyle(Paint.Style.FILL);
303 arrow_paint.setAntiAlias(true);
304 arrow_paint.setColor(arrow_color);
305 c.drawPath(p, arrow_paint);
306 }
307 else
308 {
309 double circle_size = Math.max(this.getHeight() / 2.5, this.getWidth() / 2.5);
310 RectF coord_rect2 = new RectF((int) (this.getWidth() / 2 - circle_size / 2),
311 (int) (this.getHeight() / 2 - circle_size / 2),
312 (int) (this.getWidth() / 2 + circle_size / 2),
313 (int) (this.getHeight() / 2 + circle_size / 2));
314 arrow_paint.setStyle(Paint.Style.FILL);
315 arrow_paint.setAntiAlias(true);
316 arrow_paint.setColor(arrow_color);
317 c.drawArc(coord_rect2, 0, 360, false, arrow_paint);
318 }
319 }
320 }
321
322 public double[] __get_arrow_transformed(int x1, int y1, int width, int height, double angle,
323 double size_offset)
324 {
325
326 double[] ARROW_SHAPE = new double[8];
327 ARROW_SHAPE[0] = 0;
328 ARROW_SHAPE[1] = -2 + this.ARROW_OFFSET;
329 ARROW_SHAPE[2] = 1;
330 ARROW_SHAPE[3] = 1 + this.ARROW_OFFSET;
331 ARROW_SHAPE[4] = 0;
332 ARROW_SHAPE[5] = 0 + this.ARROW_OFFSET;
333 ARROW_SHAPE[6] = -1;
334 ARROW_SHAPE[7] = 1 + this.ARROW_OFFSET;
335 // ARROW_SHAPE[0]=1.1;
336 // // ARROW_SHAPE=(0, -2 + this.ARROW_OFFSET, 1, + 1 + this.ARROW_OFFSET
337 // , 0, 0 + this.ARROW_OFFSET, -1, 1 + this.ARROW_OFFSET);
338
339
340 double multiply = (Math.min(width, height) * 0.7) / (2 * (this.ARROW_OFFSET)) * size_offset;
341 double offset_x = width / 2;
342 double offset_y = height / 2;
343
344 double s = Math.sin(Math.toRadians(angle));
345 double c = Math.cos(Math.toRadians(angle));
346
347 double[] arrow_transformed = new double[8];
348
349 for (int i = 0; i < 4; i++)
350 {
351 double x;
352 double y;
353 x = ARROW_SHAPE[(i + 1) * 2 - 2];
354 y = ARROW_SHAPE[(i + 1) * 2 - 1];
355 arrow_transformed[(i + 1) * 2 - 2] = x * multiply * c + offset_x - y * multiply * s;
356 arrow_transformed[(i + 1) * 2 - 1] = y * multiply * c + offset_y + x * multiply * s;
357 }
358 return arrow_transformed;
359 }
360
361 public void clear_stuff()
362 {
363 //
364 }
365
366 public void onSizeChanged(int width, int height, int old_w, int old_h)
367 {
368 //System.out.println("ArrowView: onSizeChanged");
369 }
370
371 }

   
Visit the aagtl Website