From 9574e509e2ca9b26b13037b3776f4412313bce1e Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Fri, 28 Nov 2008 14:58:51 -0800 Subject: [PATCH] Preliminary Accelerometer Support for Android! --- src/com/nitobi/phonegap/Orientation.java | 48 ++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/com/nitobi/phonegap/Orientation.java diff --git a/src/com/nitobi/phonegap/Orientation.java b/src/com/nitobi/phonegap/Orientation.java new file mode 100644 index 00000000..ae38e44e --- /dev/null +++ b/src/com/nitobi/phonegap/Orientation.java @@ -0,0 +1,48 @@ +package com.nitobi.phonegap; + + +import android.content.Context; +import android.hardware.SensorManager; +import android.hardware.SensorListener; +import android.webkit.WebView; + +public class Orientation implements SensorListener{ + + private WebView mAppView; + private SensorManager sensorManager; + private Context mCtx; + + Orientation(WebView kit, Context ctx) { + mAppView = kit; + mCtx = ctx; + sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE); + this.resumeAccel(); + } + + public void onSensorChanged(int sensor, final float[] values) { + if (sensor != SensorManager.SENSOR_ACCELEROMETER || values.length < 3) + return; + float x = values[0]; + float y = values[1]; + float z = values[2]; + mAppView.loadUrl("javascript:gotAcceleration(" + x + ", " + y + "," + z + ")"); + } + + public void onAccuracyChanged(int arg0, int arg1) { + // This is a stub method. + + } + + public void pauseAccel() + { + sensorManager.unregisterListener(this); + } + + public void resumeAccel() + { + sensorManager.registerListener(this, + SensorManager.SENSOR_ACCELEROMETER, + SensorManager.SENSOR_DELAY_GAME); + } + +}