mirror of
https://github.com/apache/cordova-android.git
synced 2026-05-11 00:00:05 +08:00
Getting Accelerometer working. Beep fails for an unknown reason.
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package com.nitobi.phonegap;
|
||||
|
||||
import static android.hardware.SensorManager.DATA_X;
|
||||
import static android.hardware.SensorManager.DATA_Y;
|
||||
import static android.hardware.SensorManager.DATA_Z;
|
||||
import android.hardware.SensorManager;
|
||||
import android.content.Context;
|
||||
import android.hardware.SensorListener;
|
||||
import android.webkit.WebView;
|
||||
|
||||
public class AccelListener implements SensorListener{
|
||||
|
||||
WebView mAppView;
|
||||
Context mCtx;
|
||||
private SensorManager sensorManager;
|
||||
|
||||
private long lastUpdate = -1;
|
||||
|
||||
AccelListener(Context ctx, WebView appView)
|
||||
{
|
||||
mCtx = ctx;
|
||||
mAppView = appView;
|
||||
sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
sensorManager.registerListener(this,
|
||||
SensorManager.SENSOR_ACCELEROMETER,
|
||||
SensorManager.SENSOR_DELAY_GAME);
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
sensorManager.unregisterListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(int sensor, int accuracy) {
|
||||
// This should call the FAIL method
|
||||
}
|
||||
@Override
|
||||
public void onSensorChanged(int sensor, float[] values) {
|
||||
if (sensor != SensorManager.SENSOR_ACCELEROMETER || values.length < 3)
|
||||
return;
|
||||
long curTime = System.currentTimeMillis();
|
||||
if (lastUpdate == -1 || (curTime - lastUpdate) > 1000) {
|
||||
|
||||
lastUpdate = curTime;
|
||||
|
||||
float x = values[DATA_X];
|
||||
float y = values[DATA_Y];
|
||||
float z = values[DATA_Z];
|
||||
mAppView.loadUrl("javascript:gotAccel(" + x + ", " + y + "," + z + " )");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -25,9 +25,10 @@ package com.nitobi.phonegap;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
@@ -39,8 +40,7 @@ public class DroidGap extends Activity {
|
||||
|
||||
private static final String LOG_TAG = "DroidGap";
|
||||
private WebView appView;
|
||||
|
||||
private Handler mHandler = new Handler();
|
||||
private String uri;
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
@@ -55,7 +55,7 @@ public class DroidGap extends Activity {
|
||||
|
||||
/* This changes the setWebChromeClient to log alerts to LogCat! Important for Javascript Debugging */
|
||||
|
||||
appView.setWebChromeClient(new MyWebChromeClient());
|
||||
appView.setWebChromeClient(new GapClient(this));
|
||||
appView.getSettings().setJavaScriptEnabled(true);
|
||||
appView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
|
||||
|
||||
@@ -63,7 +63,6 @@ public class DroidGap extends Activity {
|
||||
bindBrowser(appView);
|
||||
|
||||
/* Load a URI from the strings.xml file */
|
||||
String uri = null;
|
||||
Class<R.string> c = R.string.class;
|
||||
Field f;
|
||||
|
||||
@@ -72,12 +71,12 @@ public class DroidGap extends Activity {
|
||||
try {
|
||||
f = c.getField("url");
|
||||
i = f.getInt(f);
|
||||
uri = this.getResources().getString(i);
|
||||
this.uri = this.getResources().getString(i);
|
||||
} catch (Exception e)
|
||||
{
|
||||
uri = "http://www.phonegap.com";
|
||||
this.uri = "http://www.phonegap.com";
|
||||
}
|
||||
appView.loadUrl(uri);
|
||||
appView.loadUrl(this.uri);
|
||||
|
||||
}
|
||||
|
||||
@@ -90,21 +89,35 @@ public class DroidGap extends Activity {
|
||||
private void bindBrowser(WebView appView)
|
||||
{
|
||||
// The PhoneGap class handles the Notification and Android Specific crap
|
||||
PhoneGap gap = new PhoneGap(this, mHandler, appView);
|
||||
PhoneGap gap = new PhoneGap(this, appView);
|
||||
GeoBroker geo = new GeoBroker(appView, this);
|
||||
AccelListener accel = new AccelListener(this, appView);
|
||||
// This creates the new javascript interfaces for PhoneGap
|
||||
appView.addJavascriptInterface(gap, "Device");
|
||||
appView.addJavascriptInterface(geo, "Geo");
|
||||
appView.addJavascriptInterface(accel, "Accel");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Provides a hook for calling "alert" from javascript. Useful for
|
||||
* debugging your javascript.
|
||||
*/
|
||||
final class MyWebChromeClient extends WebChromeClient {
|
||||
final class GapClient extends WebChromeClient {
|
||||
|
||||
Context mCtx;
|
||||
GapClient(Context ctx)
|
||||
{
|
||||
mCtx = ctx;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
|
||||
Log.d(LOG_TAG, message);
|
||||
// This shows the dialog box. This can be commented out for dev
|
||||
AlertDialog.Builder alertBldr = new AlertDialog.Builder(mCtx);
|
||||
alertBldr.setMessage(message);
|
||||
alertBldr.setTitle("Alert");
|
||||
alertBldr.show();
|
||||
result.confirm();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.nitobi.phonegap;
|
||||
import java.util.HashMap;
|
||||
|
||||
import android.content.Context;
|
||||
import android.location.Location;
|
||||
import android.webkit.WebView;
|
||||
|
||||
/*
|
||||
|
||||
@@ -26,14 +26,13 @@ import java.util.TimeZone;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.IntentFilter;
|
||||
import android.hardware.SensorManager;
|
||||
import android.location.Location;
|
||||
import android.location.LocationProvider;
|
||||
import android.media.MediaPlayer;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Vibrator;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.webkit.WebView;
|
||||
import android.media.Ringtone;
|
||||
import android.media.RingtoneManager;
|
||||
|
||||
public class PhoneGap{
|
||||
|
||||
@@ -46,15 +45,13 @@ public class PhoneGap{
|
||||
public static String platform = "Android";
|
||||
public static String uuid;
|
||||
private Context mCtx;
|
||||
private Handler mHandler;
|
||||
private WebView mAppView;
|
||||
SmsListener mSmsListener;
|
||||
DirectoryManager fileManager;
|
||||
AudioHandler audio;
|
||||
|
||||
public PhoneGap(Context ctx, Handler handler, WebView appView) {
|
||||
public PhoneGap(Context ctx, WebView appView) {
|
||||
this.mCtx = ctx;
|
||||
this.mHandler = handler;
|
||||
this.mAppView = appView;
|
||||
|
||||
mSmsListener = new SmsListener(ctx,mAppView);
|
||||
@@ -63,16 +60,12 @@ public class PhoneGap{
|
||||
uuid = getUuid();
|
||||
}
|
||||
|
||||
public void updateAccel(){
|
||||
mHandler.post(new Runnable() {
|
||||
public void run() {
|
||||
int accelX = SensorManager.DATA_X;
|
||||
int accelY = SensorManager.DATA_Y;
|
||||
int accelZ = SensorManager.DATA_Z;
|
||||
mAppView.loadUrl("javascript:gotAcceleration(" + accelX + ", " + accelY + "," + accelZ + ")");
|
||||
}
|
||||
});
|
||||
|
||||
public void beep(long pattern)
|
||||
{
|
||||
RingtoneManager beeper = new RingtoneManager(mCtx);
|
||||
Uri ringtone = beeper.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
||||
Ringtone notification = beeper.getRingtone(mCtx, ringtone);
|
||||
notification.play();
|
||||
}
|
||||
|
||||
public void vibrate(long pattern){
|
||||
@@ -83,6 +76,11 @@ public class PhoneGap{
|
||||
vibrator.vibrate(pattern);
|
||||
}
|
||||
|
||||
public String getPlatform()
|
||||
{
|
||||
return this.platform;
|
||||
}
|
||||
|
||||
public String getUuid()
|
||||
{
|
||||
|
||||
|
||||
Reference in New Issue
Block a user