/[zanavi_public1]/navit/navit/graphics/android/graphics_android.c
ZANavi

Contents of /navit/navit/graphics/android/graphics_android.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Oct 28 21:19:04 2011 UTC (8 years, 9 months ago) by zoff99
File MIME type: text/plain
File size: 30727 byte(s)
import files
1 /**
2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include <unistd.h>
21 #include <glib.h>
22 #include "config.h"
23 #include "window.h"
24 #include "point.h"
25 #include "graphics.h"
26 #include "color.h"
27 #include "plugin.h"
28 #include "event.h"
29 #include "debug.h"
30 #include "callback.h"
31 #include "android.h"
32
33 int dummy;
34
35 struct graphics_priv {
36 jclass NavitGraphicsClass;
37 jmethodID NavitGraphics_draw_polyline, NavitGraphics_draw_polyline2, NavitGraphics_draw_polyline_dashed, NavitGraphics_draw_polygon, NavitGraphics_draw_polygon2, NavitGraphics_draw_rectangle, NavitGraphics_draw_circle, NavitGraphics_draw_text, NavitGraphics_draw_image, NavitGraphics_draw_bigmap, NavitGraphics_send_osd_values, NavitGraphics_draw_mode, NavitGraphics_draw_drag, NavitGraphics_overlay_disable, NavitGraphics_overlay_resize, NavitGraphics_SetCamera,NavitGraphicsClass_rotate_and_scale_bitmap;
38
39 jclass PaintClass;
40 jmethodID Paint_init,Paint_setStrokeWidth,Paint_setARGB;
41
42 jobject NavitGraphics;
43 jobject Paint;
44
45 jclass BitmapFactoryClass;
46 jmethodID BitmapFactory_decodeFile, BitmapFactory_decodeResource;
47
48 jclass BitmapClass;
49 jmethodID Bitmap_getHeight, Bitmap_getWidth;
50
51 jclass ContextClass;
52 jmethodID Context_getResources;
53
54 jclass ResourcesClass;
55 jobject Resources;
56 jmethodID Resources_getIdentifier;
57
58 struct callback_list *cbl;
59 struct window win;
60 };
61
62 struct graphics_font_priv {
63 int size;
64 };
65
66 struct graphics_gc_priv {
67 struct graphics_priv *gra;
68 int linewidth;
69 enum draw_mode_num mode;
70 int a,r,g,b;
71 };
72
73 struct graphics_image_priv {
74 jobject Bitmap;
75 int width;
76 int height;
77 struct point hot;
78 };
79
80 static GHashTable *image_cache_hash = NULL;
81
82 static int
83 find_class_global(char *name, jclass *ret)
84 {
85 *ret=(*jnienv)->FindClass(jnienv, name);
86 if (! *ret) {
87 dbg(0,"Failed to get Class %s\n",name);
88 return 0;
89 }
90 (*jnienv)->NewGlobalRef(jnienv, *ret);
91 return 1;
92 }
93
94 static int
95 find_method(jclass class, char *name, char *args, jmethodID *ret)
96 {
97 *ret = (*jnienv)->GetMethodID(jnienv, class, name, args);
98 if (*ret == NULL) {
99 dbg(0,"Failed to get Method %s with signature %s\n",name,args);
100 return 0;
101 }
102 return 1;
103 }
104
105 static int
106 find_static_method(jclass class, char *name, char *args, jmethodID *ret)
107 {
108 *ret = (*jnienv)->GetStaticMethodID(jnienv, class, name, args);
109 if (*ret == NULL) {
110 dbg(0,"Failed to get static Method %s with signature %s\n",name,args);
111 return 0;
112 }
113 return 1;
114 }
115
116 static void
117 graphics_destroy(struct graphics_priv *gr)
118 {
119 }
120
121 static void font_destroy(struct graphics_font_priv *font)
122 {
123 g_free(font);
124 }
125
126 static struct graphics_font_methods font_methods = {
127 font_destroy
128 };
129
130 static struct graphics_font_priv *font_new(struct graphics_priv *gr, struct graphics_font_methods *meth, char *font, int size, int flags)
131 {
132 struct graphics_font_priv *ret=g_new0(struct graphics_font_priv, 1);
133 *meth=font_methods;
134
135 ret->size=size;
136 return ret;
137 }
138
139 static void
140 gc_destroy(struct graphics_gc_priv *gc)
141 {
142 //dbg(0,"EEnter\n");
143
144 g_free(gc);
145 }
146
147 static void
148 gc_set_linewidth(struct graphics_gc_priv *gc, int w)
149 {
150 gc->linewidth = w;
151 }
152
153 static void
154 gc_set_dashes(struct graphics_gc_priv *gc, int w, int offset, unsigned char *dash_list, int n)
155 {
156 // should call a java routine that does something like:
157 //
158 // paint.setPathEffect(new DashPathEffect(new float[] { 8, 3 }, 1));
159 }
160
161 static void
162 gc_set_foreground(struct graphics_gc_priv *gc, struct color *c)
163 {
164 gc->r = c->r >> 8;
165 gc->g = c->g >> 8;
166 gc->b = c->b >> 8;
167 gc->a = c->a >> 8;
168 }
169
170 static void
171 gc_set_background(struct graphics_gc_priv *gc, struct color *c)
172 {
173 }
174
175 static struct graphics_gc_methods gc_methods = {
176 gc_destroy,
177 gc_set_linewidth,
178 gc_set_dashes,
179 gc_set_foreground,
180 gc_set_background
181 };
182
183 static struct graphics_gc_priv *gc_new(struct graphics_priv *gr, struct graphics_gc_methods *meth)
184 {
185 //dbg(0,"EEnter\n");
186
187 struct graphics_gc_priv *ret=g_new0(struct graphics_gc_priv, 1);
188 *meth=gc_methods;
189
190 ret->gra = gr;
191 ret->a = ret->r = ret->g = ret->b = 255;
192 ret->linewidth=1;
193 return ret;
194 }
195
196 static void image_destroy(struct graphics_image_priv *img)
197 {
198 // unused?
199 }
200
201 static struct graphics_image_methods image_methods = {
202 image_destroy
203 };
204
205
206 static struct graphics_image_priv *
207 image_new(struct graphics_priv *gra, struct graphics_image_methods *meth, char *path, int *w, int *h, struct point *hot, int rotation)
208 {
209 //dbg(0,"EEnter\n");
210
211 struct graphics_image_priv* ret = NULL;
212
213 if ( !g_hash_table_lookup_extended( image_cache_hash, path, NULL, (gpointer)&ret) )
214 {
215 ret=g_new0(struct graphics_image_priv, 1);
216 jstring string;
217 int id;
218
219 dbg(1,"enter %s\n",path);
220 if (!strncmp(path,"res/drawable/",13)) {
221 jstring a=(*jnienv)->NewStringUTF(jnienv, "drawable");
222 jstring b=(*jnienv)->NewStringUTF(jnienv, "com.zoffcc.applications.zanavi");
223 char *path_noext=g_strdup(path+13);
224 char *pos=strrchr(path_noext, '.');
225 if (pos)
226 *pos='\0';
227 dbg(1,"path_noext=%s\n",path_noext);
228 string = (*jnienv)->NewStringUTF(jnienv, path_noext);
229 g_free(path_noext);
230 id=(*jnienv)->CallIntMethod(jnienv, gra->Resources, gra->Resources_getIdentifier, string, a, b);
231 dbg(1,"id=%d\n",id);
232 if (id)
233 ret->Bitmap=(*jnienv)->CallStaticObjectMethod(jnienv, gra->BitmapFactoryClass, gra->BitmapFactory_decodeResource, gra->Resources, id);
234 (*jnienv)->DeleteLocalRef(jnienv, b);
235 (*jnienv)->DeleteLocalRef(jnienv, a);
236 } else {
237 string = (*jnienv)->NewStringUTF(jnienv, path);
238 ret->Bitmap=(*jnienv)->CallStaticObjectMethod(jnienv, gra->BitmapFactoryClass, gra->BitmapFactory_decodeFile, string);
239 // there should be a check here, if we really want any rotation/scaling
240 // otherwise the call is overkill
241 ret->Bitmap=(*jnienv)->CallStaticObjectMethod(jnienv, gra->NavitGraphicsClass, gra->NavitGraphicsClass_rotate_and_scale_bitmap, ret->Bitmap, *w, *h, rotation);
242 }
243 dbg(1,"result=%p\n",ret->Bitmap);
244 if (ret->Bitmap) {
245 (*jnienv)->NewGlobalRef(jnienv, ret->Bitmap);
246 (*jnienv)->DeleteLocalRef(jnienv, ret->Bitmap);
247 ret->width=(*jnienv)->CallIntMethod(jnienv, ret->Bitmap, gra->Bitmap_getWidth);
248 ret->height=(*jnienv)->CallIntMethod(jnienv, ret->Bitmap, gra->Bitmap_getHeight);
249 dbg(1,"w=%d h=%d for %s\n",ret->width,ret->height,path);
250 ret->hot.x=ret->width/2;
251 ret->hot.y=ret->height/2;
252 } else {
253 g_free(ret);
254 ret=NULL;
255 dbg(0,"Failed to open %s\n",path);
256 }
257 (*jnienv)->DeleteLocalRef(jnienv, string);
258 g_hash_table_insert(image_cache_hash, g_strdup( path ), (gpointer)ret );
259 }
260 if (ret) {
261 *w=ret->width;
262 *h=ret->height;
263 if (hot)
264 *hot=ret->hot;
265 }
266
267 return ret;
268 }
269
270 static void initPaint(struct graphics_priv *gra, struct graphics_gc_priv *gc)
271 {
272 //dbg(0,"EEnter\n");
273
274 float wf = gc->linewidth;
275 (*jnienv)->CallVoidMethod(jnienv, gc->gra->Paint, gra->Paint_setStrokeWidth, wf);
276 (*jnienv)->CallVoidMethod(jnienv, gc->gra->Paint, gra->Paint_setARGB, gc->a, gc->r, gc->g, gc->b);
277 }
278
279 static void
280 draw_lines(struct graphics_priv *gra, struct graphics_gc_priv *gc, struct point *p, int count)
281 {
282 jint pc[count*2];
283 int i;
284 jintArray points;
285 if (count <= 0)
286 return;
287 points = (*jnienv)->NewIntArray(jnienv,count*2);
288 for (i = 0 ; i < count ; i++) {
289 pc[i*2]=p[i].x;
290 pc[i*2+1]=p[i].y;
291 }
292 initPaint(gra, gc);
293 (*jnienv)->SetIntArrayRegion(jnienv, points, 0, count*2, pc);
294 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_draw_polyline, gc->gra->Paint, points);
295 (*jnienv)->DeleteLocalRef(jnienv, points);
296 }
297
298 static void
299 draw_lines2(struct graphics_priv *gra, struct graphics_gc_priv *gc, struct point *p, int count, int order, int oneway)
300 {
301 jint pc[count*2];
302 int i;
303 jintArray points;
304 if (count <= 0)
305 return;
306 points = (*jnienv)->NewIntArray(jnienv,count*2);
307 for (i = 0 ; i < count ; i++) {
308 pc[i*2]=p[i].x;
309 pc[i*2+1]=p[i].y;
310 }
311 initPaint(gra, gc);
312 (*jnienv)->SetIntArrayRegion(jnienv, points, 0, count*2, pc);
313 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_draw_polyline2, gc->gra->Paint, points, order, oneway);
314 (*jnienv)->DeleteLocalRef(jnienv, points);
315 }
316
317
318 static void
319 draw_lines_dashed(struct graphics_priv *gra, struct graphics_gc_priv *gc, struct point *p, int count, int order, int oneway)
320 {
321 jint pc[count*2];
322 int i;
323 jintArray points;
324 if (count <= 0)
325 return;
326 points = (*jnienv)->NewIntArray(jnienv,count*2);
327 for (i = 0 ; i < count ; i++) {
328 pc[i*2]=p[i].x;
329 pc[i*2+1]=p[i].y;
330 }
331 initPaint(gra, gc);
332 (*jnienv)->SetIntArrayRegion(jnienv, points, 0, count*2, pc);
333 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_draw_polyline_dashed, gc->gra->Paint, points, order, oneway);
334 (*jnienv)->DeleteLocalRef(jnienv, points);
335 }
336
337 static void
338 draw_polygon(struct graphics_priv *gra, struct graphics_gc_priv *gc, struct point *p, int count)
339 {
340 jint pc[count*2];
341 int i;
342 jintArray points;
343 if (count <= 0)
344 return;
345 points = (*jnienv)->NewIntArray(jnienv,count*2);
346 for (i = 0 ; i < count ; i++) {
347 pc[i*2]=p[i].x;
348 pc[i*2+1]=p[i].y;
349 }
350 initPaint(gra, gc);
351 (*jnienv)->SetIntArrayRegion(jnienv, points, 0, count*2, pc);
352 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_draw_polygon, gc->gra->Paint, points);
353 (*jnienv)->DeleteLocalRef(jnienv, points);
354 }
355
356 static void
357 draw_polygon2(struct graphics_priv *gra, struct graphics_gc_priv *gc, struct point *p, int count, int order, int oneway)
358 {
359 jint pc[count*2];
360 int i;
361 jintArray points;
362 if (count <= 0)
363 return;
364 points = (*jnienv)->NewIntArray(jnienv,count*2);
365 for (i = 0 ; i < count ; i++) {
366 pc[i*2]=p[i].x;
367 pc[i*2+1]=p[i].y;
368 }
369 initPaint(gra, gc);
370 (*jnienv)->SetIntArrayRegion(jnienv, points, 0, count*2, pc);
371 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_draw_polygon2, gc->gra->Paint, points, order, oneway);
372 (*jnienv)->DeleteLocalRef(jnienv, points);
373 }
374
375
376 static void
377 draw_rectangle(struct graphics_priv *gra, struct graphics_gc_priv *gc, struct point *p, int w, int h)
378 {
379 initPaint(gra, gc);
380 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_draw_rectangle, gc->gra->Paint, p->x, p->y, w, h);
381 }
382
383 static void
384 draw_circle(struct graphics_priv *gra, struct graphics_gc_priv *gc, struct point *p, int r)
385 {
386 initPaint(gra, gc);
387 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_draw_circle, gc->gra->Paint, p->x, p->y, r);
388 }
389
390
391 static void
392 draw_text(struct graphics_priv *gra, struct graphics_gc_priv *fg, struct graphics_gc_priv *bg, struct graphics_font_priv *font, char *text, struct point *p, int dx, int dy)
393 {
394 dbg(1,"enter %s\n", text);
395 initPaint(gra, fg);
396 jstring string = (*jnienv)->NewStringUTF(jnienv, text);
397 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_draw_text, fg->gra->Paint, p->x, p->y, string, font->size, dx, dy);
398 (*jnienv)->DeleteLocalRef(jnienv, string);
399 }
400
401 static void
402 draw_image(struct graphics_priv *gra, struct graphics_gc_priv *fg, struct point *p, struct graphics_image_priv *img)
403 {
404 //dbg(0,"EEnter\n");
405
406 dbg(1,"enter %p\n",img);
407 initPaint(gra, fg);
408 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_draw_image, fg->gra->Paint, p->x, p->y, img->Bitmap);
409
410 }
411
412 static void
413 draw_bigmap(struct graphics_priv *gra, struct graphics_gc_priv *fg, int yaw, int order, float clat, float clng, int x, int y, int scx, int scy, int px, int py, int valid)
414 {
415 //dbg(0,"EEnter\n");
416
417 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_draw_bigmap, yaw, order, clat, clng, x, y, scx, scy, px, py, valid);
418 }
419
420 static void
421 send_osd_values(struct graphics_priv *gra, struct graphics_gc_priv *fg, char *id, char *text1, char *text2, char *text3, int i1, int i2, int i3, int i4, float f1, float f2, float f3)
422 {
423 //dbg(0,"EEnter\n");
424
425 jstring string1 = (*jnienv)->NewStringUTF(jnienv, id);
426 jstring string2 = (*jnienv)->NewStringUTF(jnienv, text1);
427 jstring string3 = (*jnienv)->NewStringUTF(jnienv, text2);
428 jstring string4 = (*jnienv)->NewStringUTF(jnienv, text3);
429 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_send_osd_values, string1, string2, string3, string4, i1, i2, i3, i4, f1, f2, f3);
430 (*jnienv)->DeleteLocalRef(jnienv, string1);
431 (*jnienv)->DeleteLocalRef(jnienv, string2);
432 (*jnienv)->DeleteLocalRef(jnienv, string3);
433 (*jnienv)->DeleteLocalRef(jnienv, string4);
434 }
435
436
437 static void
438 draw_image_warp(struct graphics_priv *gr, struct graphics_gc_priv *fg, struct point *p, int count, char *data)
439 {
440 }
441
442 static void
443 draw_restore(struct graphics_priv *gr, struct point *p, int w, int h)
444 {
445 }
446
447 static void draw_drag(struct graphics_priv *gra, struct point *p)
448 {
449 //dbg(0,"EEnter\n");
450
451 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_draw_drag, p ? p->x : 0, p ? p->y : 0);
452 }
453
454 static void
455 background_gc(struct graphics_priv *gr, struct graphics_gc_priv *gc)
456 {
457 }
458
459 static void
460 draw_mode(struct graphics_priv *gra, enum draw_mode_num mode)
461 {
462 //dbg(0,"EEnter\n");
463
464 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_draw_mode, (int)mode);
465 }
466
467 static struct graphics_priv * overlay_new(struct graphics_priv *gr, struct graphics_methods *meth, struct point *p, int w, int h, int alpha, int wraparound);
468
469 static void *
470 get_data(struct graphics_priv *this, const char *type)
471 {
472 //dbg(0,"EEnter\n");
473
474 if (strcmp(type,"window"))
475 return NULL;
476 return &this->win;
477 }
478
479 static void image_free(struct graphics_priv *gr, struct graphics_image_priv *priv)
480 {
481 }
482
483 static void get_text_bbox(struct graphics_priv *gr, struct graphics_font_priv *font, char *text, int dx, int dy, struct point *ret, int estimate)
484 {
485 //dbg(0,"EEnter\n");
486
487 int len = g_utf8_strlen(text, -1);
488 int xMin = 0;
489 int yMin = 0;
490 int yMax = 13*font->size/256;
491 int xMax = 9*font->size*len/256;
492
493 ret[0].x = xMin;
494 ret[0].y = -yMin;
495 ret[1].x = xMin;
496 ret[1].y = -yMax;
497 ret[2].x = xMax;
498 ret[2].y = -yMax;
499 ret[3].x = xMax;
500 ret[3].y = -yMin;
501 }
502
503 static void overlay_disable(struct graphics_priv *gra, int disable)
504 {
505 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_overlay_disable, disable);
506 }
507
508 static void overlay_resize(struct graphics_priv *gra, struct point *pnt, int w, int h, int alpha, int wraparound)
509 {
510 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_overlay_resize, pnt ? pnt->x:0 , pnt ? pnt->y:0, w, h, alpha, wraparound);
511 }
512
513 static int
514 set_attr(struct graphics_priv *gra, struct attr *attr)
515 {
516 switch (attr->type) {
517 case attr_use_camera:
518 (*jnienv)->CallVoidMethod(jnienv, gra->NavitGraphics, gra->NavitGraphics_SetCamera, attr->u.num);
519 return 1;
520 default:
521 return 0;
522 }
523 }
524
525 static struct graphics_methods graphics_methods = {
526 graphics_destroy,
527 draw_mode,
528 draw_lines,
529 draw_lines2,
530 draw_lines_dashed,
531 draw_polygon,
532 draw_polygon2,
533 draw_rectangle,
534 draw_circle,
535 draw_text,
536 draw_image,
537 draw_bigmap,
538 send_osd_values,
539 draw_image_warp,
540 draw_restore,
541 draw_drag,
542 font_new,
543 gc_new,
544 background_gc,
545 overlay_new,
546 image_new,
547 get_data,
548 image_free,
549 get_text_bbox,
550 overlay_disable,
551 overlay_resize,
552 set_attr,
553 };
554
555 static void
556 resize_callback(struct graphics_priv *gra, int w, int h)
557 {
558 //dbg(0,"EEnter\n");
559
560 // dbg(0,"w=%d h=%d ok\n",w,h);
561 callback_list_call_attr_2(gra->cbl, attr_resize, (void *)w, (void *)h);
562 }
563
564 static void
565 motion_callback(struct graphics_priv *gra, int x, int y)
566 {
567 //dbg(0,"EEnter\n");
568
569 struct point p;
570 p.x=x;
571 p.y=y;
572 callback_list_call_attr_1(gra->cbl, attr_motion, (void *)&p);
573 }
574
575 static void
576 keypress_callback(struct graphics_priv *gra, char *s)
577 {
578 callback_list_call_attr_1(gra->cbl, attr_keypress, s);
579 }
580
581 static void
582 button_callback(struct graphics_priv *gra, int pressed, int button, int x, int y)
583 {
584 //dbg(0,"EEnter\n");
585
586 struct point p;
587 p.x=x;
588 p.y=y;
589 // dbg(0,"XXXXXXXYYYYYYYYY\n");
590 callback_list_call_attr_3(gra->cbl, attr_button, (void *)pressed, (void *)button, (void *)&p);
591 }
592
593
594 static int
595 set_activity(jobject graphics)
596 {
597 //dbg(0,"EEnter\n");
598
599 jclass ActivityClass;
600 jmethodID cid;
601
602 ActivityClass = (*jnienv)->GetObjectClass(jnienv, android_activity);
603 // dbg(0,"at 5\n");
604 if (ActivityClass == NULL) {
605 dbg(0,"no activity class found\n");
606 return 0;
607 }
608 // dbg(0,"at 6\n");
609 cid = (*jnienv)->GetMethodID(jnienv, ActivityClass, "setContentView", "(Landroid/view/View;)V");
610 if (cid == NULL) {
611 dbg(0,"no setContentView method found\n");
612 return 0;
613 }
614 // dbg(0,"at 7\n");
615 (*jnienv)->CallVoidMethod(jnienv, android_activity, cid, graphics);
616 // dbg(0,"at 8\n");
617 return 1;
618 }
619
620 static int
621 graphics_android_init(struct graphics_priv *ret, struct graphics_priv *parent, struct point *pnt, int w, int h, int alpha, int wraparound, int use_camera)
622 {
623 struct callback *cb;
624 jmethodID cid;
625
626 dbg(0,"at 2 jnienv=%p\n",jnienv);
627 if (!find_class_global("android/graphics/Paint", &ret->PaintClass))
628 return 0;
629 if (!find_method(ret->PaintClass, "<init>", "(I)V", &ret->Paint_init))
630 return 0;
631 if (!find_method(ret->PaintClass, "setARGB", "(IIII)V", &ret->Paint_setARGB))
632 return 0;
633 if (!find_method(ret->PaintClass, "setStrokeWidth", "(F)V", &ret->Paint_setStrokeWidth))
634 return 0;
635
636 if (!find_class_global("android/graphics/BitmapFactory", &ret->BitmapFactoryClass))
637 return 0;
638 if (!find_static_method(ret->BitmapFactoryClass, "decodeFile", "(Ljava/lang/String;)Landroid/graphics/Bitmap;", &ret->BitmapFactory_decodeFile))
639 return 0;
640 if (!find_static_method(ret->BitmapFactoryClass, "decodeResource", "(Landroid/content/res/Resources;I)Landroid/graphics/Bitmap;", &ret->BitmapFactory_decodeResource))
641 return 0;
642
643 if (!find_class_global("android/graphics/Bitmap", &ret->BitmapClass))
644 return 0;
645 if (!find_method(ret->BitmapClass, "getHeight", "()I", &ret->Bitmap_getHeight))
646 return 0;
647 if (!find_method(ret->BitmapClass, "getWidth", "()I", &ret->Bitmap_getWidth))
648 return 0;
649
650 if (!find_class_global("android/content/Context", &ret->ContextClass))
651 return 0;
652 if (!find_method(ret->ContextClass, "getResources", "()Landroid/content/res/Resources;", &ret->Context_getResources))
653 return 0;
654
655
656 ret->Resources=(*jnienv)->CallObjectMethod(jnienv, android_activity, ret->Context_getResources);
657 if (ret->Resources)
658 (*jnienv)->NewGlobalRef(jnienv, ret->Resources);
659 if (!find_class_global("android/content/res/Resources", &ret->ResourcesClass))
660 return 0;
661 if (!find_method(ret->ResourcesClass, "getIdentifier", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", &ret->Resources_getIdentifier))
662 return 0;
663
664 if (!find_class_global("com/zoffcc/applications/zanavi/NavitGraphics", &ret->NavitGraphicsClass))
665 return 0;
666 dbg(0,"at 3\n");
667 cid = (*jnienv)->GetMethodID(jnienv, ret->NavitGraphicsClass, "<init>", "(Landroid/app/Activity;Lcom/zoffcc/applications/zanavi/NavitGraphics;IIIIIII)V");
668 if (cid == NULL) {
669 dbg(0,"no method found\n");
670 return 0; /* exception thrown */
671 }
672 dbg(0,"at 4 android_activity=%p\n",android_activity);
673 ret->NavitGraphics=(*jnienv)->NewObject(jnienv, ret->NavitGraphicsClass, cid, android_activity, parent ? parent->NavitGraphics : NULL, pnt ? pnt->x:0 , pnt ? pnt->y:0, w, h, alpha, wraparound, use_camera);
674 dbg(0,"result=%p\n",ret->NavitGraphics);
675 if (ret->NavitGraphics)
676 (*jnienv)->NewGlobalRef(jnienv, ret->NavitGraphics);
677
678 /* Create a single global Paint, otherwise android will quickly run out
679 * of global refs.*/
680 /* 0x101 = text kerning (default), antialiasing */
681 ret->Paint=(*jnienv)->NewObject(jnienv, ret->PaintClass, ret->Paint_init, 0x101);
682
683 dbg(0,"result=%p\n",ret->Paint);
684 if (ret->Paint)
685 (*jnienv)->NewGlobalRef(jnienv, ret->Paint);
686
687 cid = (*jnienv)->GetMethodID(jnienv, ret->NavitGraphicsClass, "setSizeChangedCallback", "(I)V");
688 if (cid == NULL) {
689 dbg(0,"no SetResizeCallback method found\n");
690 return 0; /* exception thrown */
691 }
692 cb=callback_new_1(callback_cast(resize_callback), ret);
693 (*jnienv)->CallVoidMethod(jnienv, ret->NavitGraphics, cid, (int)cb);
694
695 cid = (*jnienv)->GetMethodID(jnienv, ret->NavitGraphicsClass, "setButtonCallback", "(I)V");
696 if (cid == NULL) {
697 dbg(0,"no SetButtonCallback method found\n");
698 return 0; /* exception thrown */
699 }
700 cb=callback_new_1(callback_cast(button_callback), ret);
701 (*jnienv)->CallVoidMethod(jnienv, ret->NavitGraphics, cid, (int)cb);
702
703 cid = (*jnienv)->GetMethodID(jnienv, ret->NavitGraphicsClass, "setMotionCallback", "(I)V");
704 if (cid == NULL) {
705 dbg(0,"no SetMotionCallback method found\n");
706 return 0; /* exception thrown */
707 }
708 cb=callback_new_1(callback_cast(motion_callback), ret);
709 (*jnienv)->CallVoidMethod(jnienv, ret->NavitGraphics, cid, (int)cb);
710
711
712 // public Bitmap rotate_and_scale_bitmap(Bitmap in, int w, int h, int angle)
713
714 if (!find_static_method(ret->NavitGraphicsClass, "rotate_and_scale_bitmap", "(Landroid/graphics/Bitmap;III)Landroid/graphics/Bitmap;", &ret->NavitGraphicsClass_rotate_and_scale_bitmap))
715 return 0;
716
717 //
718 //cid = (*jnienv)->GetMethodID(jnienv, ret->NavitGraphicsClass, "rotate_and_scale_bitmap", "(Landroid/graphics/Bitmap;III)Landroid/graphics/Bitmap;");
719 //if (cid == NULL) {
720 // dbg(0,"no rotate_and_scale_bitmap method found\n");
721 // return 0; /* exception thrown */
722 //}
723
724 cid = (*jnienv)->GetMethodID(jnienv, ret->NavitGraphicsClass, "setKeypressCallback", "(I)V");
725 if (cid == NULL) {
726 dbg(0,"no SetKeypressCallback method found\n");
727 return 0; /* exception thrown */
728 }
729 cb=callback_new_1(callback_cast(keypress_callback), ret);
730 (*jnienv)->CallVoidMethod(jnienv, ret->NavitGraphics, cid, (int)cb);
731
732 if (!find_method(ret->NavitGraphicsClass, "draw_polyline", "(Landroid/graphics/Paint;[I)V", &ret->NavitGraphics_draw_polyline))
733 return 0;
734 if (!find_method(ret->NavitGraphicsClass, "draw_polyline2", "(Landroid/graphics/Paint;[III)V", &ret->NavitGraphics_draw_polyline2))
735 return 0;
736 if (!find_method(ret->NavitGraphicsClass, "draw_polyline_dashed", "(Landroid/graphics/Paint;[III)V", &ret->NavitGraphics_draw_polyline_dashed))
737 return 0;
738 if (!find_method(ret->NavitGraphicsClass, "draw_polygon", "(Landroid/graphics/Paint;[I)V", &ret->NavitGraphics_draw_polygon))
739 return 0;
740 if (!find_method(ret->NavitGraphicsClass, "draw_polygon2", "(Landroid/graphics/Paint;[III)V", &ret->NavitGraphics_draw_polygon2))
741 return 0;
742 if (!find_method(ret->NavitGraphicsClass, "draw_rectangle", "(Landroid/graphics/Paint;IIII)V", &ret->NavitGraphics_draw_rectangle))
743 return 0;
744 if (!find_method(ret->NavitGraphicsClass, "draw_circle", "(Landroid/graphics/Paint;III)V", &ret->NavitGraphics_draw_circle))
745 return 0;
746 if (!find_method(ret->NavitGraphicsClass, "draw_text", "(Landroid/graphics/Paint;IILjava/lang/String;III)V", &ret->NavitGraphics_draw_text))
747 return 0;
748 if (!find_method(ret->NavitGraphicsClass, "draw_image", "(Landroid/graphics/Paint;IILandroid/graphics/Bitmap;)V", &ret->NavitGraphics_draw_image))
749 return 0;
750 if (!find_method(ret->NavitGraphicsClass, "draw_bigmap", "(IIFFIIIIIII)V", &ret->NavitGraphics_draw_bigmap))
751 return 0;
752 if (!find_method(ret->NavitGraphicsClass, "send_osd_values", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IIIIFFF)V", &ret->NavitGraphics_send_osd_values))
753 return 0;
754 if (!find_method(ret->NavitGraphicsClass, "draw_mode", "(I)V", &ret->NavitGraphics_draw_mode))
755 return 0;
756 if (!find_method(ret->NavitGraphicsClass, "draw_drag", "(II)V", &ret->NavitGraphics_draw_drag))
757 return 0;
758 if (!find_method(ret->NavitGraphicsClass, "overlay_disable", "(I)V", &ret->NavitGraphics_overlay_disable))
759 return 0;
760 if (!find_method(ret->NavitGraphicsClass, "overlay_resize", "(IIIIII)V", &ret->NavitGraphics_overlay_resize))
761 return 0;
762 if (!find_method(ret->NavitGraphicsClass, "SetCamera", "(I)V", &ret->NavitGraphics_SetCamera))
763 return 0;
764 #if 0
765 set_activity(ret->NavitGraphics);
766 #endif
767 return 1;
768 }
769
770 static int
771 graphics_android_fullscreen(struct window *win, int on)
772 {
773 return 1;
774 }
775
776 static jclass NavitClass;
777 static jmethodID Navit_disableSuspend, Navit_exit;
778
779 static void
780 graphics_android_disable_suspend(struct window *win)
781 {
782 //dbg(0,"enter\n");
783 (*jnienv)->CallVoidMethod(jnienv, android_activity, Navit_disableSuspend);
784 }
785
786
787
788 static struct graphics_priv *
789 graphics_android_new(struct navit *nav, struct graphics_methods *meth, struct attr **attrs, struct callback_list *cbl)
790 {
791 //dbg(0,"EEnter\n");
792
793 struct graphics_priv *ret;
794 struct attr *attr;
795 int use_camera=0;
796 if (!event_request_system("android","graphics_android"))
797 return NULL;
798 ret=g_new0(struct graphics_priv, 1);
799
800 ret->cbl=cbl;
801 *meth=graphics_methods;
802 ret->win.priv=ret;
803 ret->win.fullscreen=graphics_android_fullscreen;
804 ret->win.disable_suspend=graphics_android_disable_suspend;
805 if ((attr=attr_search(attrs, NULL, attr_use_camera))) {
806 use_camera=attr->u.num;
807 }
808 image_cache_hash = g_hash_table_new(g_str_hash, g_str_equal);
809 if (graphics_android_init(ret, NULL, NULL, 0, 0, 0, 0, use_camera)) {
810 //dbg(0,"returning %p\n",ret);
811 return ret;
812 } else {
813 g_free(ret);
814 return NULL;
815 }
816 }
817
818 static struct graphics_priv *
819 overlay_new(struct graphics_priv *gr, struct graphics_methods *meth, struct point *p, int w, int h, int alpha, int wraparound)
820 {
821 //dbg(0,"EEnter\n");
822
823 struct graphics_priv *ret=g_new0(struct graphics_priv, 1);
824 *meth=graphics_methods;
825 if (graphics_android_init(ret, gr, p, w, h, alpha, wraparound, 0)) {
826 dbg(0,"returning %p\n",ret);
827 return ret;
828 } else {
829 g_free(ret);
830 return NULL;
831 }
832 }
833
834
835 static void
836 event_android_main_loop_run(void)
837 {
838 dbg(0,"enter\n");
839 }
840
841 static void event_android_main_loop_quit(void)
842 {
843 dbg(0,"enter\n");
844 // exit(0);
845 (*jnienv)->CallVoidMethod(jnienv, android_activity, Navit_exit);
846 }
847
848
849 static jclass NavitTimeoutClass;
850 static jmethodID NavitTimeout_init;
851 static jmethodID NavitTimeout_remove;
852
853 static jclass NavitIdleClass;
854 static jmethodID NavitIdle_init;
855 static jmethodID NavitIdle_remove;
856
857 static jclass NavitWatchClass;
858 static jmethodID NavitWatch_init;
859 static jmethodID NavitWatch_remove;
860
861 static struct event_watch *
862 event_android_add_watch(void *h, enum event_watch_cond cond, struct callback *cb)
863 {
864 jobject ret;
865 ret=(*jnienv)->NewObject(jnienv, NavitWatchClass, NavitWatch_init, (int) h, (int) cond, (int)cb);
866 //dbg(0,"result for %p,%d,%p=%p\n",h,cond,cb,ret);
867 if (ret)
868 (*jnienv)->NewGlobalRef(jnienv, ret);
869 return (struct event_watch *)ret;
870 }
871
872 static void
873 event_android_remove_watch(struct event_watch *ev)
874 {
875 //dbg(0,"enter %p\n",ev);
876 if (ev) {
877 jobject obj=(jobject )ev;
878 (*jnienv)->CallVoidMethod(jnienv, obj, NavitWatch_remove);
879 (*jnienv)->DeleteGlobalRef(jnienv, obj);
880 }
881 }
882
883 static struct event_timeout *
884 event_android_add_timeout(int timeout, int multi, struct callback *cb)
885 {
886 //dbg(0,"EEnter\n");
887
888 jobject ret;
889 ret=(*jnienv)->NewObject(jnienv, NavitTimeoutClass, NavitTimeout_init, timeout, multi, (int)cb);
890 dbg(1,"result for %d,%d,%p=%p\n",timeout,multi,cb,ret);
891 if (ret)
892 (*jnienv)->NewGlobalRef(jnienv, ret);
893 return (struct event_timeout *)ret;
894 }
895
896 static void
897 event_android_remove_timeout(struct event_timeout *to)
898 {
899 //dbg(0,"EEnter\n");
900
901 dbg(1,"enter %p\n",to);
902 if (to) {
903 jobject obj=(jobject )to;
904 (*jnienv)->CallVoidMethod(jnienv, obj, NavitTimeout_remove);
905 (*jnienv)->DeleteGlobalRef(jnienv, obj);
906 }
907 }
908
909
910 static struct event_idle *
911 event_android_add_idle(int priority, struct callback *cb)
912 {
913 //dbg(0,"EEnter\n");
914
915 #if 0
916 jobject ret;
917 dbg(1,"enter\n");
918 ret=(*jnienv)->NewObject(jnienv, NavitIdleClass, NavitIdle_init, (int)cb);
919 dbg(1,"result for %p=%p\n",cb,ret);
920 if (ret)
921 (*jnienv)->NewGlobalRef(jnienv, ret);
922 return (struct event_idle *)ret;
923 #endif
924 return (struct event_idle *)event_android_add_timeout(1, 1, cb);
925 }
926
927 static void
928 event_android_remove_idle(struct event_idle *ev)
929 {
930 //dbg(0,"EEnter\n");
931
932 #if 0
933 dbg(1,"enter %p\n",ev);
934 if (ev) {
935 jobject obj=(jobject )ev;
936 (*jnienv)->CallVoidMethod(jnienv, obj, NavitIdle_remove);
937 (*jnienv)->DeleteGlobalRef(jnienv, obj);
938 }
939 #endif
940 event_android_remove_timeout((struct event_timeout *)ev);
941 }
942
943 static void
944 event_android_call_callback(struct callback_list *cb)
945 {
946 // dbg(0,"enter\n");
947 }
948
949 static struct event_methods event_android_methods = {
950 event_android_main_loop_run,
951 event_android_main_loop_quit,
952 event_android_add_watch,
953 event_android_remove_watch,
954 event_android_add_timeout,
955 event_android_remove_timeout,
956 event_android_add_idle,
957 event_android_remove_idle,
958 event_android_call_callback,
959 };
960
961 static struct event_priv *
962 event_android_new(struct event_methods *meth)
963 {
964 //dbg(0,"enter\n");
965 if (!find_class_global("com/zoffcc/applications/zanavi/NavitTimeout", &NavitTimeoutClass))
966 return NULL;
967 NavitTimeout_init = (*jnienv)->GetMethodID(jnienv, NavitTimeoutClass, "<init>", "(IZI)V");
968 if (NavitTimeout_init == NULL)
969 return NULL;
970 NavitTimeout_remove = (*jnienv)->GetMethodID(jnienv, NavitTimeoutClass, "remove", "()V");
971 if (NavitTimeout_remove == NULL)
972 return NULL;
973 #if 0
974 if (!find_class_global("com/zoffcc/applications/zanavi/NavitIdle", &NavitIdleClass))
975 return NULL;
976 NavitIdle_init = (*jnienv)->GetMethodID(jnienv, NavitIdleClass, "<init>", "(I)V");
977 if (NavitIdle_init == NULL)
978 return NULL;
979 NavitIdle_remove = (*jnienv)->GetMethodID(jnienv, NavitIdleClass, "remove", "()V");
980 if (NavitIdle_remove == NULL)
981 return NULL;
982 #endif
983
984 if (!find_class_global("com/zoffcc/applications/zanavi/NavitWatch", &NavitWatchClass))
985 return NULL;
986 NavitWatch_init = (*jnienv)->GetMethodID(jnienv, NavitWatchClass, "<init>", "(III)V");
987 if (NavitWatch_init == NULL)
988 return NULL;
989 NavitWatch_remove = (*jnienv)->GetMethodID(jnienv, NavitWatchClass, "remove", "()V");
990 if (NavitWatch_remove == NULL)
991 return NULL;
992
993 if (!find_class_global("com/zoffcc/applications/zanavi/Navit", &NavitClass))
994 return NULL;
995 Navit_disableSuspend = (*jnienv)->GetMethodID(jnienv, NavitClass, "disableSuspend", "()V");
996 if (Navit_disableSuspend == NULL)
997 return NULL;
998 Navit_exit = (*jnienv)->GetMethodID(jnienv, NavitClass, "exit", "()V");
999 if (Navit_exit == NULL)
1000 return NULL;
1001 //dbg(0,"ok\n");
1002 *meth=event_android_methods;
1003 return NULL;
1004 }
1005
1006
1007 void
1008 plugin_init(void)
1009 {
1010 //dbg(0,"enter\n");
1011 plugin_register_graphics_type("android", graphics_android_new);
1012 plugin_register_event_type("android", event_android_new);
1013 }

   
Visit the ZANavi Wiki