/[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 4 - (show annotations) (download)
Sat Aug 1 08:47:10 2015 UTC (2 years, 3 months ago) by zoffadmin
File size: 12625 byte(s)
1.0.35
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 <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 * aagtl.Global_dpi_factor);
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.valueOf(this.main_aagtl.global_settings.map_position_lat), String.valueOf(this.main_aagtl.global_settings.map_position_lon)), t.getID());
111 cx = Calendar.getInstance();
112 sc = SolarPosition.getSunPosition(new Date(), this.main_aagtl.global_settings.map_position_lat, this.main_aagtl.global_settings.map_position_lon);
113
114 this.azmiuth_cache = sc.azimuth;
115 this.zenith_cache = sc.zenithAngle;
116 this.sunrise_cache = calc.getOfficialSunriseForDate(cx);
117 this.sunset_cache = calc.getOfficialSunsetForDate(cx);
118 //System.out.println("calc moon");
119 SolarEventCalculator.moonCoor_ret moon_stats = calc.computeMoon(cx);
120 moon_azimuth_cache = moon_stats.az;
121 moon_evelation_cache = moon_stats.alt;
122 }
123 //
124 this.elevation = 90 - this.zenith_cache;
125 //
126 // SUN ----------------
127 //
128 //
129 }
130
131 public void onDraw(Canvas c)
132 {
133 //System.out.println("ArrowView: onDraw");
134
135 // clear bg with color black
136 c.drawColor(Color.BLACK);
137
138 // draw grey circle around the arrow
139 // draw grey circle around the arrow
140 int indicator_radius = (int) ((Math.min(this.getWidth(), this.getHeight()) / 4) * 1.1f);
141 int indicator_dist = (Math.min(this.getHeight(), this.getWidth()) / 2) - indicator_radius / 2;
142 int[] center = new int[2];
143 center[0] = this.getWidth() / 2;
144 center[1] = this.getHeight() / 2;
145
146 RectF coord_rect = new RectF(center[0] - indicator_dist, center[1] - indicator_dist, center[0] - indicator_dist + indicator_dist * 2, center[1] - indicator_dist + indicator_dist * 2);
147
148 int start = 0; // 0°
149 int end = 360; // 360°
150 arrow_paint.setAntiAlias(true);
151 arrow_paint.setColor(COLOR_CIRCLE_OUTLINE);
152 arrow_paint.setStyle(Paint.Style.STROKE);
153 arrow_paint.setStrokeWidth(3 * aagtl.Global_dpi_factor);
154 c.drawArc(coord_rect, start, end, false, arrow_paint);
155 // draw grey circle around the arrow
156 // draw grey circle around the arrow
157
158 Coordinate my_pos = new Coordinate(this.main_aagtl.global_settings.map_position_lat, this.main_aagtl.global_settings.map_position_lon);
159
160 double display_distance = 0;
161 double display_bearing = 0;
162 if (this.main_aagtl.rose.current_target != null)
163 {
164 display_distance = my_pos.distance_to((Coordinate) (this.main_aagtl.rose.current_target));
165
166 display_bearing = my_pos.bearing_to((Coordinate) (this.main_aagtl.rose.current_target)) - this.main_aagtl.cross.gps_heading;
167 }
168
169 //if ((this.main_aagtl.cross.gps_heading != -1) && (this.main_aagtl.isGPSFix))
170 if (this.main_aagtl.cross.gps_heading != -1)
171 {
172 double display_north = Math.toRadians(this.main_aagtl.cross.gps_heading);
173 int position_x = (int) (this.getWidth() / 2 - Math.sin(display_north) * (indicator_dist * 1.15));
174 int position_y = (int) (this.getHeight() / 2 - Math.cos(display_north) * (indicator_dist * 1.15));
175
176 text_paint.setColor(Color.WHITE);
177 c.drawText("N", position_x, position_y, text_paint);
178 }
179
180 text_paint.setColor(Color.WHITE);
181 c.drawText("distance to target: " + String.valueOf(roundTwoDecimals(display_distance)) + " m", 10 * aagtl.Global_dpi_factor, 34 * aagtl.Global_dpi_factor, text_paint);
182
183 c.drawText("target: " + String.valueOf((int) display_bearing) + " °", 10 * aagtl.Global_dpi_factor, 2 * 34 * aagtl.Global_dpi_factor, text_paint);
184
185 c.drawText("my heading: " + String.valueOf((int) this.main_aagtl.cross.gps_heading) + " °", this.getWidth() / 2, 2 * 34 * aagtl.Global_dpi_factor, text_paint);
186
187 c.drawText("gps accuracy: " + String.valueOf(roundTwoDecimals(this.main_aagtl.cross.gps_acc)) + " m", 10 * aagtl.Global_dpi_factor, 3 * 34 * aagtl.Global_dpi_factor, text_paint);
188 c.drawText("gps Sats.: " + String.valueOf((int) this.main_aagtl.cross.used_sats), 10 * aagtl.Global_dpi_factor, 4 * 34 * aagtl.Global_dpi_factor, text_paint);
189
190 //
191 //
192 // SUN ----------------
193 //
194 //
195 this.calc_sun_stats();
196 c.drawText("sunrise: " + this.sunrise_cache, 10 * aagtl.Global_dpi_factor, this.getHeight() - 34 * 4 * aagtl.Global_dpi_factor, text_paint);
197 c.drawText("sunset: " + this.sunset_cache, 10 * aagtl.Global_dpi_factor, this.getHeight() - 34 * 3 * aagtl.Global_dpi_factor, text_paint);
198
199 if (elevation < -0.83)
200 {
201 c.drawText("elevation: *night*", this.getWidth() / 2, this.getHeight() - 34 * 4 * aagtl.Global_dpi_factor, text_paint);
202 }
203 else
204 {
205 c.drawText("elevation: " + roundTwoDecimals(elevation), this.getWidth() / 2, this.getHeight() - 34 * 4 * aagtl.Global_dpi_factor, text_paint);
206 }
207
208 c.drawText("azimuth: " + roundTwoDecimals(this.azmiuth_cache), this.getWidth() / 2, this.getHeight() - 34 * 3 * aagtl.Global_dpi_factor, text_paint);
209 //
210 //
211 // SUN ----------------
212 //
213 //
214
215 if (this.main_aagtl.rose.current_target != null)
216 {
217 // gc-code
218 c.drawText("Geocache: " + this.main_aagtl.rose.current_target.name, 10 * aagtl.Global_dpi_factor, this.getHeight() - 34 * 2 * aagtl.Global_dpi_factor, text_paint);
219 // cache name
220 c.drawText(this.main_aagtl.rose.current_target.title, 10 * aagtl.Global_dpi_factor, this.getHeight() - 34 * aagtl.Global_dpi_factor, text_paint);
221 }
222
223 // draw signal indicator , part 1
224 int signal_width = 15;
225 RectF coord_rect3 = new RectF(this.getWidth() - signal_width - 2, 0, this.getWidth(), this.getHeight());
226 arrow_paint.setColor(COLOR_QUALITY_OUTER);
227 arrow_paint.setStyle(Paint.Style.STROKE);
228 arrow_paint.setStrokeWidth(3);
229 c.drawRect(coord_rect3, arrow_paint);
230
231 // draw signal indicator , part 2
232 int usable_height = this.getHeight() - 1;
233 int target_height = (int) (usable_height * ((float) (this.main_aagtl.cross.used_sats) / (float) (13)));
234 RectF coord_rect7 = new RectF(this.getWidth() - signal_width - 1, usable_height - target_height, this.getWidth() - 1, usable_height);
235 arrow_paint.setColor(COLOR_QUALITY_INNER);
236 arrow_paint.setStyle(Paint.Style.FILL);
237 c.drawRect(coord_rect7, arrow_paint);
238
239 if (this.main_aagtl.rose.current_target != null)
240 {
241 arrow_paint.setStrokeWidth(1 * aagtl.Global_dpi_factor);
242 int arrow_color;
243 if (display_distance < 50)
244 {
245 arrow_color = COLOR_ARROW_ATTARGET;
246 }
247 else if (display_distance < 150)
248 {
249 arrow_color = COLOR_ARROW_NEAR;
250 }
251 else
252 {
253 arrow_color = COLOR_ARROW_DEFAULT;
254 }
255
256 if (display_distance > DISTANCE_DISABLE_ARROW)
257 {
258 //c.drawLines(pts, paint) ;
259 double[] arrow_transformed = new double[8];
260 arrow_transformed = this.__get_arrow_transformed(0, 0, this.getWidth(), this.getHeight(), display_bearing, 0.22);
261 Path p = new Path();
262 for (int i = 0; i < 4; i++)
263 {
264 if (i == 0)
265 {
266 p.moveTo((float) arrow_transformed[(i + 1) * 2 - 2], (float) arrow_transformed[(i + 1) * 2 - 1]);
267 }
268 else
269 {
270 p.lineTo((float) arrow_transformed[(i + 1) * 2 - 2], (float) arrow_transformed[(i + 1) * 2 - 1]);
271 }
272 }
273 p.close();
274 arrow_paint.setStyle(Paint.Style.FILL);
275 arrow_paint.setAntiAlias(true);
276 arrow_paint.setColor(arrow_color);
277 c.drawPath(p, arrow_paint);
278 }
279 else
280 {
281 double circle_size = Math.max(this.getHeight() / 2.5, this.getWidth() / 2.5);
282 RectF coord_rect2 = new RectF((int) (this.getWidth() / 2 - circle_size / 2), (int) (this.getHeight() / 2 - circle_size / 2), (int) (this.getWidth() / 2 + circle_size / 2), (int) (this.getHeight() / 2 + circle_size / 2));
283 arrow_paint.setStyle(Paint.Style.FILL);
284 arrow_paint.setAntiAlias(true);
285 arrow_paint.setColor(arrow_color);
286 c.drawArc(coord_rect2, 0, 360, false, arrow_paint);
287 }
288 }
289 }
290
291 public double[] __get_arrow_transformed(int x1, int y1, int width, int height, double angle, double size_offset)
292 {
293
294 double[] ARROW_SHAPE = new double[8];
295 ARROW_SHAPE[0] = 0;
296 ARROW_SHAPE[1] = -2 + this.ARROW_OFFSET;
297 ARROW_SHAPE[2] = 1;
298 ARROW_SHAPE[3] = 1 + this.ARROW_OFFSET;
299 ARROW_SHAPE[4] = 0;
300 ARROW_SHAPE[5] = 0 + this.ARROW_OFFSET;
301 ARROW_SHAPE[6] = -1;
302 ARROW_SHAPE[7] = 1 + this.ARROW_OFFSET;
303 // ARROW_SHAPE[0]=1.1;
304 // // ARROW_SHAPE=(0, -2 + this.ARROW_OFFSET, 1, + 1 + this.ARROW_OFFSET
305 // , 0, 0 + this.ARROW_OFFSET, -1, 1 + this.ARROW_OFFSET);
306
307 double multiply = (Math.min(width, height) * 0.7) / (2 * (this.ARROW_OFFSET)) * size_offset;
308 double offset_x = width / 2;
309 double offset_y = height / 2;
310
311 double s = Math.sin(Math.toRadians(angle));
312 double c = Math.cos(Math.toRadians(angle));
313
314 double[] arrow_transformed = new double[8];
315
316 for (int i = 0; i < 4; i++)
317 {
318 double x;
319 double y;
320 x = ARROW_SHAPE[(i + 1) * 2 - 2];
321 y = ARROW_SHAPE[(i + 1) * 2 - 1];
322 arrow_transformed[(i + 1) * 2 - 2] = x * multiply * c + offset_x - y * multiply * s;
323 arrow_transformed[(i + 1) * 2 - 1] = y * multiply * c + offset_y + x * multiply * s;
324 }
325 return arrow_transformed;
326 }
327
328 public void clear_stuff()
329 {
330 //
331 }
332
333 public void onSizeChanged(int width, int height, int old_w, int old_h)
334 {
335 //System.out.println("ArrowView: onSizeChanged");
336 }
337
338 }

   
Visit the aagtl Website