Add:graphics_android:Camera support
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Thu, 12 Nov 2009 14:49:37 +0000 (14:49 +0000)
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Thu, 12 Nov 2009 14:49:37 +0000 (14:49 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@2740 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/navit/android.c
navit/navit/android/src/org/navitproject/navit/NavitCamera.java [new file with mode: 0644]
navit/navit/android/src/org/navitproject/navit/NavitGraphics.java
navit/navit/android/src/org/navitproject/navit/NavitSensors.java [new file with mode: 0644]
navit/navit/graphics.h
navit/navit/graphics/android/graphics_android.c

index aa18887..ac58d38 100644 (file)
@@ -149,3 +149,11 @@ Java_org_navitproject_navit_NavitWatch_WatchCallback( JNIEnv* env, jobject thiz,
 }
 
 
+JNIEXPORT void JNICALL
+Java_org_navitproject_navit_NavitSensors_SensorCallback( JNIEnv* env, jobject thiz, int id, int sensor, float x, float y, float z)
+{
+       dbg(1,"enter %p %p %f %f %f\n",thiz, (void *)id,x,y,z);
+       callback_call_4((struct callback *)id, sensor, &x, &y, &z);
+}
+
+
diff --git a/navit/navit/android/src/org/navitproject/navit/NavitCamera.java b/navit/navit/android/src/org/navitproject/navit/NavitCamera.java
new file mode 100644 (file)
index 0000000..18d56fb
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.navitproject.navit;
+
+import android.app.Activity;
+import android.widget.TextView;
+import android.os.Bundle;
+import android.os.Debug;
+import android.os.Message;
+import android.os.Handler;
+import android.content.Context;
+import android.util.Log;
+import android.hardware.Camera;
+import android.hardware.Camera.Parameters;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.PixelFormat;
+import android.view.SurfaceHolder;
+import android.view.SurfaceView;
+import java.io.IOException;
+
+
+
+
+public class NavitCamera extends SurfaceView implements SurfaceHolder.Callback {
+        SurfaceHolder mHolder;
+       static Camera mCamera = Camera.open();
+
+
+
+       NavitCamera(Context context) 
+       {
+               super(context);
+                 mHolder = getHolder();
+        mHolder.addCallback(this);
+        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
+           Log.e("NavitCamera","Creator");
+
+               
+       }
+
+
+    public void surfaceCreated(SurfaceHolder holder) {
+        // The Surface has been created, acquire the camera and tell it where
+        // to draw.
+        try {
+           mCamera.setPreviewDisplay(holder);
+        } catch (IOException exception) {
+            mCamera.release();
+            mCamera = null;
+            // TODO: add more exception handling logic here
+        }
+           Log.e("NavitCamera","surfaceCreated");
+    }
+
+    public void surfaceDestroyed(SurfaceHolder holder) {
+        // Surface will be destroyed when we return, so stop the preview.
+        // Because the CameraDevice object is not a shared resource, it's very
+        // important to release it when the activity is paused.
+        mCamera.stopPreview();
+        mCamera = null;
+           Log.e("NavitCamera","surfaceDestroyed");
+    }
+
+    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
+        // Now that the size is known, set up the camera parameters and begin
+        // the preview.
+       Log.e("NavitCamera","surfaceChanged "+w+"x"+h);
+        Camera.Parameters parameters = mCamera.getParameters();
+        parameters.setPreviewSize(w, h);
+        mCamera.setParameters(parameters);
+        mCamera.startPreview();
+    }
+
+}
+
index c43037a..3628f67 100644 (file)
@@ -25,9 +25,20 @@ import android.graphics.*;
 import android.os.Bundle;
 import android.os.Debug;
 import android.view.*;
+import android.view.Window;
 import android.util.Log;
+import android.widget.RelativeLayout;
 import java.util.ArrayList;
 import java.lang.String;
+import android.app.Activity;
+import android.content.Context;
+import android.hardware.Camera;
+import android.os.Bundle;
+import android.view.SurfaceHolder;
+import android.view.SurfaceView;
+import android.view.Window;
+import java.io.IOException;
+
 
 public class NavitGraphics {
        private NavitGraphics parent_graphics;
@@ -40,7 +51,8 @@ public class NavitGraphics {
        int overlay_disabled;
        float trackball_x,trackball_y;
        View view;
-       public NavitGraphics(Activity activity, NavitGraphics parent, int x, int y, int w, int h, int alpha, int wraparound) {
+       RelativeLayout relativelayout;
+       public NavitGraphics(Activity activity, NavitGraphics parent, int x, int y, int w, int h, int alpha, int wraparound, int camera) {
                if (parent == null) {
                        view=new View(activity) {
        @Override protected void onDraw(Canvas canvas)
@@ -181,7 +193,16 @@ public class NavitGraphics {
                        };
                        view.setFocusable(true);
                        view.setFocusableInTouchMode(true);
-                       activity.setContentView(view);
+                       if (camera != 0) {
+                               activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
+                               relativelayout=new RelativeLayout(activity);
+                               NavitCamera camera=new NavitCamera(activity);
+                               relativelayout.addView(camera);
+                               relativelayout.addView(view);
+                               activity.setContentView(relativelayout);
+                       } else {
+                               activity.setContentView(view);
+                       }
                        view.requestFocus();
                } else {
                        draw_bitmap=Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
@@ -280,9 +301,9 @@ public class NavitGraphics {
        }
        protected void draw_mode(int mode)
        {
-               if (mode == 1 && parent_graphics == null)
+               if (mode == 2 && parent_graphics == null)
                        view.invalidate();
-               if (mode == 0 && parent_graphics != null)
+               if (mode == 1 || (mode == 0 && parent_graphics != null)) 
                        draw_bitmap.eraseColor(0);
                        
        }
diff --git a/navit/navit/android/src/org/navitproject/navit/NavitSensors.java b/navit/navit/android/src/org/navitproject/navit/NavitSensors.java
new file mode 100644 (file)
index 0000000..960cda7
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.navitproject.navit;
+
+import android.app.Activity;
+import android.widget.TextView;
+import android.os.Bundle;
+import android.os.Debug;
+import android.os.Message;
+import android.os.Handler;
+import android.content.Context;
+import android.util.Log;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorManager;
+import android.hardware.SensorListener;
+import android.hardware.SensorEventListener;
+
+
+
+public class NavitSensors implements SensorEventListener {
+       private SensorManager mSensorManager;
+       private int callbackid;
+       public native void SensorCallback(int id, int sensor, float x, float y, float z);
+
+
+       NavitSensors(Context context, int cbid) 
+       {
+               mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
+               mSensorManager.registerListener((SensorEventListener)this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
+               mSensorManager.registerListener((SensorEventListener)this, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
+               callbackid=cbid;
+       }
+
+       public void
+       onAccuracyChanged(Sensor sensor, int accuracy)
+       {
+       }
+
+       public void
+       onSensorChanged(SensorEvent sev)
+       {
+               // Log.e("NavitSensor","Type:" + sev.sensor.getType() + " X:" + sev.values[0] + " Y:"+sev.values[1]+" Z:"+sev.values[2]);
+               SensorCallback(callbackid, sev.sensor.getType(), sev.values[0], sev.values[1], sev.values[2]);
+       }
+}
+
index 11c7939..02adecf 100644 (file)
@@ -35,7 +35,7 @@ struct display_list;
 struct mapset;
 
 enum draw_mode_num {
-       draw_mode_begin, draw_mode_end, draw_mode_cursor, draw_mode_end_lazy
+       draw_mode_begin, draw_mode_begin_clear, draw_mode_end, draw_mode_cursor, draw_mode_end_lazy
 };
 
 struct graphics_priv;
index 0ef6275..43af6ae 100644 (file)
@@ -174,8 +174,10 @@ static struct graphics_gc_priv *gc_new(struct graphics_priv *gr, struct graphics
        ret->gra=gr;    
        ret->Paint=(*jnienv)->NewObject(jnienv, gr->PaintClass, gr->Paint_init);
        dbg(1,"result=%p\n",ret->Paint);
-       if (ret->Paint)
+       if (ret->Paint) {
                (*jnienv)->NewGlobalRef(jnienv, ret->Paint);
+               (*jnienv)->DeleteLocalRef(jnienv, ret->Paint);
+       }
        return ret;
 }
 
@@ -222,6 +224,7 @@ image_new(struct graphics_priv *gra, struct graphics_image_methods *meth, char *
        dbg(1,"result=%p\n",ret->Bitmap);
        if (ret->Bitmap) {
                (*jnienv)->NewGlobalRef(jnienv, ret->Bitmap);
+               (*jnienv)->DeleteLocalRef(jnienv, ret->Bitmap);
                *w=(*jnienv)->CallIntMethod(jnienv, ret->Bitmap, gra->Bitmap_getWidth);
                *h=(*jnienv)->CallIntMethod(jnienv, ret->Bitmap, gra->Bitmap_getHeight);
                dbg(1,"w=%d h=%d for %s\n",*w,*h,path);
@@ -499,13 +502,13 @@ graphics_android_init(struct graphics_priv *ret, struct graphics_priv *parent, s
        if (!find_class_global("org/navitproject/navit/NavitGraphics", &ret->NavitGraphicsClass))
                return 0;
        dbg(0,"at 3\n");
-       cid = (*jnienv)->GetMethodID(jnienv, ret->NavitGraphicsClass, "<init>", "(Landroid/app/Activity;Lorg/navitproject/navit/NavitGraphics;IIIIII)V");
+       cid = (*jnienv)->GetMethodID(jnienv, ret->NavitGraphicsClass, "<init>", "(Landroid/app/Activity;Lorg/navitproject/navit/NavitGraphics;IIIIIII)V");
        if (cid == NULL) {
                dbg(0,"no method found\n");
                return 0; /* exception thrown */
        }
        dbg(0,"at 4 android_activity=%p\n",android_activity);
-       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);
+       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, 0);
        dbg(0,"result=%p\n",ret->NavitGraphics);
        if (ret->NavitGraphics)
                (*jnienv)->NewGlobalRef(jnienv, ret->NavitGraphics);