Fixes to bring PhoneGap Android closer to spec

This commit is contained in:
Joe Bowser 2009-07-17 13:23:51 -07:00
parent 0344665c79
commit 08c42eac97
10 changed files with 1011 additions and 1292 deletions

View File

@ -1,91 +0,0 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>PhoneGap</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" charset="utf-8">
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
var deviceInfo = function(){
document.getElementById("platform").innerHTML = Device.getPlatform();
document.getElementById("version").innerHTML = Device.getVersion();
document.getElementById("uuid").innerHTML = Device.getUuid();
}
var getLocation = function() {
var suc = function(p){
alert(p.latitude + " " + p.longitude);
};
var fail = function(){};
navigator.geolocation.getCurrentPosition(suc,fail);
}
var beep = function(){
navigator.notification.beep(2);
}
var vibrate = function(){
navigator.notification.vibrate(0);
}
var getContact = function(){
var suc = function(c){ alert("Contact 4: " + c.contacts[3].name); };
var fail = function(){};
navigator.ContactManager.get(suc, fail);
}
var watchAccel = function() {
var suc = function(a){
document.getElementById('x').innerHTML = roundNumber(a.x);
document.getElementById('y').innerHTML = roundNumber(a.y);
document.getElementById('z').innerHTML = roundNumber(a.z);
};
var fail = function(){};
var opt = {};
opt.frequency = 100;
timer = navigator.accelerometer.watchAcceleration(suc,fail,opt);
}
function roundNumber(num) {
var dec = 3;
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
var preventBehavior = function(e) {
e.preventDefault();
};
function init(){
document.addEventListener("touchmove", preventBehavior, false);
deviceInfo();
}
</script>
</head>
<body onload="init();" id="stage" class="theme">
<h1>Welcome to PhoneGap!</h1>
<h2>this file is located at assets/index.html</h2>
<div id="info">
<h4>Platform: <span id="platform">&nbsp;</span></h4>
<h4>Version: <span id="version">&nbsp;</span></h4>
<h4>UUID: <span id="uuid">&nbsp;</span></h4>
</div>
<dl id="accel-data">
<dt>X:</dt><dd id="x">&nbsp;</dd>
<dt>Y:</dt><dd id="y">&nbsp;</dd>
<dt>Z:</dt><dd id="z">&nbsp;</dd>
</dl>
<a href="#" class="btn large" onclick="watchAccel();">Watch Accelerometer</a>
<a href="#" class="btn large" onclick="getLocation();">Get Location</a>
<a href="tel://411" class="btn large">Call 411</a>
<a href="#" class="btn" onclick="beep();">Beep</a>
<a href="#" class="btn" onclick="vibrate();">Vibrate</a>
<!--
<a href="#" class="btn large" onclick="getContact();">Get Fourth Contact</a>
-->
</body>
</html>

View File

@ -1,630 +0,0 @@
/**
* This class contains acceleration information
* @constructor
* @param {Number} x The force applied by the device in the x-axis.
* @param {Number} y The force applied by the device in the y-axis.
* @param {Number} z The force applied by the device in the z-axis.
*/
function Acceleration(x, y, z) {
/**
* The force applied by the device in the x-axis.
*/
this.x = x;
/**
* The force applied by the device in the y-axis.
*/
this.y = y;
/**
* The force applied by the device in the z-axis.
*/
this.z = z;
/**
* The time that the acceleration was obtained.
*/
this.timestamp = new Date().getTime();
}
/**
* This class specifies the options for requesting acceleration data.
* @constructor
*/
function AccelerationOptions() {
/**
* The timeout after which if acceleration data cannot be obtained the errorCallback
* is called.
*/
this.timeout = 10000;
}
/**
* This class provides access to device accelerometer data.
* @constructor
*/
function Accelerometer() {
/**
* The last known acceleration.
*/
this.lastAcceleration = null;
}
/**
* Asynchronously aquires the current acceleration.
* @param {Function} successCallback The function to call when the acceleration
* data is available
* @param {Function} errorCallback The function to call when there is an error
* getting the acceleration data.
* @param {AccelerationOptions} options The options for getting the accelerometer data
* such as timeout.
*/
Accelerometer.prototype.getCurrentAcceleration = function(successCallback, errorCallback, options) {
// If the acceleration is available then call success
// If the acceleration is not available then call error
// Created for iPhone, Iphone passes back _accel obj litteral
if (typeof successCallback == "function") {
var accel = new Acceleration(_accel.x,_accel.y,_accel.z);
Accelerometer.lastAcceleration = accel;
successCallback(accel);
}
}
/**
* Asynchronously aquires the acceleration repeatedly at a given interval.
* @param {Function} successCallback The function to call each time the acceleration
* data is available
* @param {Function} errorCallback The function to call when there is an error
* getting the acceleration data.
* @param {AccelerationOptions} options The options for getting the accelerometer data
* such as timeout.
*/
Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallback, options) {
navigator.accelerometer.getCurrentAcceleration(successCallback, errorCallback, options);
// TODO: add the interval id to a list so we can clear all watches
var frequency = (options != undefined)? options.frequency : 10000;
return setInterval(function() {
navigator.accelerometer.getCurrentAcceleration(successCallback, errorCallback, options);
}, frequency);
}
/**
* Clears the specified accelerometer watch.
* @param {String} watchId The ID of the watch returned from #watchAcceleration.
*/
Accelerometer.prototype.clearWatch = function(watchId) {
clearInterval(watchId);
}
if (typeof navigator.accelerometer == "undefined") navigator.accelerometer = new Accelerometer();
/**
* This class provides access to the device media, interfaces to both sound and video
* @constructor
*/
function Media(src) {
this.src = src;
}
Media.prototype.play = function() {
}
Media.prototype.pause = function() {
}
Media.prototype.stop = function() {
}
/**
* This class contains information about any Media errors.
* @constructor
*/
function MediaError() {
this.code = null,
this.message = "";
}
MediaError.MEDIA_ERR_ABORTED = 1;
MediaError.MEDIA_ERR_NETWORK = 2;
MediaError.MEDIA_ERR_DECODE = 3;
MediaError.MEDIA_ERR_NONE_SUPPORTED = 4;
//if (typeof navigator.audio == "undefined") navigator.audio = new Media(src);
/**
* This class provides access to the device camera.
* @constructor
*/
function Camera() {
}
/**
*
* @param {Function} successCallback
* @param {Function} errorCallback
* @param {Object} options
*/
Camera.prototype.getPicture = function(successCallback, errorCallback, options) {
}
if (typeof navigator.camera == "undefined") navigator.camera = new Camera();
/**
* This class provides access to the device contacts.
* @constructor
*/
function Contact() {
this.name = "";
this.phone = "";
this.address = "";
}
/**
*
* @param {Object} successCallback
* @param {Object} errorCallback
* @param {Object} options
*/
Contact.prototype.get = function(successCallback, errorCallback, options) {
}
function ContactManager() {
// Dummy object to hold array of contacts
this.contacts = [];
this.timestap = new Date().getTime();
}
ContactManager.prototype.get = function(successCallback, errorCallback, options) {
// Interface
}
if (typeof navigator.ContactManager == "undefined") navigator.ContactManager = new ContactManager();
/**
* This class provides generic read and write access to the mobile device file system.
*/
function File() {
/**
* The data of a file.
*/
this.data = "";
/**
* The name of the file.
*/
this.name = "";
}
/**
* Reads a file from the mobile device. This function is asyncronous.
* @param {String} fileName The name (including the path) to the file on the mobile device.
* The file name will likely be device dependant.
* @param {Function} successCallback The function to call when the file is successfully read.
* @param {Function} errorCallback The function to call when there is an error reading the file from the device.
*/
File.prototype.read = function(fileName, successCallback, errorCallback) {
}
/**
* Writes a file to the mobile device.
* @param {File} file The file to write to the device.
*/
File.prototype.write = function(file) {
}
if (typeof navigator.file == "undefined") navigator.file = new File();
/**
* This class provides access to device GPS data.
* @constructor
*/
function Geolocation() {
/**
* The last known GPS position.
*/
this.lastPosition = null;
}
/**
* Asynchronously aquires the current position.
* @param {Function} successCallback The function to call when the position
* data is available
* @param {Function} errorCallback The function to call when there is an error
* getting the position data.
* @param {PositionOptions} options The options for getting the position data
* such as timeout.
*/
Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options) {
// If the position is available then call success
// If the position is not available then call error
}
/**
* Asynchronously aquires the position repeatedly at a given interval.
* @param {Function} successCallback The function to call each time the position
* data is available
* @param {Function} errorCallback The function to call when there is an error
* getting the position data.
* @param {PositionOptions} options The options for getting the position data
* such as timeout and the frequency of the watch.
*/
Geolocation.prototype.watchPosition = function(successCallback, errorCallback, options) {
// Invoke the appropriate callback with a new Position object every time the implementation
// determines that the position of the hosting device has changed.
this.getCurrentPosition(successCallback, errorCallback, options);
var frequency = (options != undefined)? options.frequency : 10000;
var that = this;
return setInterval(function() {
that.getCurrentPosition(successCallback, errorCallback, options);
//navigator.geolocation.getCurrentPosition(successCallback, errorCallback, options);
}, frequency);
}
/**
* Clears the specified position watch.
* @param {String} watchId The ID of the watch returned from #watchPosition.
*/
Geolocation.prototype.clearWatch = function(watchId) {
clearInterval(watchId);
}
if (typeof navigator.geolocation == "undefined") navigator.geolocation = new Geolocation();
/**
* This class provides access to native mapping applications on the device.
*/
function Map() {
}
/**
* Shows a native map on the device with pins at the given positions.
* @param {Array} positions
*/
Map.prototype.show = function(positions) {
}
if (typeof navigator.map == "undefined") navigator.map = new Map();
/**
* This class provides access to notifications on the device.
*/
function Notification() {
}
/**
* Causes the device to blink a status LED.
* @param {Integer} count The number of blinks.
* @param {String} colour The colour of the light.
*/
Notification.prototype.blink = function(count, colour) {
}
/**
* Causes the device to vibrate.
* @param {Integer} mills The number of milliseconds to vibrate for.
*/
Notification.prototype.vibrate = function(mills) {
}
/**
* Causes the device to beep.
* @param {Integer} count The number of beeps.
* @param {Integer} volume The volume of the beep.
*/
Notification.prototype.beep = function(count, volume) {
}
// TODO: of course on Blackberry and Android there notifications in the UI as well
if (typeof navigator.notification == "undefined") navigator.notification = new Notification();
/**
* This class provides access to the device orientation.
* @constructor
*/
function Orientation() {
/**
* The last known orientation.
*/
this.lastOrientation = null;
}
/**
* Asynchronously aquires the current orientation.
* @param {Function} successCallback The function to call when the orientation
* is known.
* @param {Function} errorCallback The function to call when there is an error
* getting the orientation.
*/
Orientation.prototype.getCurrentOrientation = function(successCallback, errorCallback) {
// If the position is available then call success
// If the position is not available then call error
}
/**
* Asynchronously aquires the orientation repeatedly at a given interval.
* @param {Function} successCallback The function to call each time the orientation
* data is available.
* @param {Function} errorCallback The function to call when there is an error
* getting the orientation data.
*/
Orientation.prototype.watchOrientation = function(successCallback, errorCallback) {
// Invoke the appropriate callback with a new Position object every time the implementation
// determines that the position of the hosting device has changed.
this.getCurrentPosition(successCallback, errorCallback);
return setInterval(function() {
navigator.orientation.getCurrentOrientation(successCallback, errorCallback);
}, 10000);
}
/**
* Clears the specified orientation watch.
* @param {String} watchId The ID of the watch returned from #watchOrientation.
*/
Orientation.prototype.clearWatch = function(watchId) {
clearInterval(watchId);
}
if (typeof navigator.orientation == "undefined") navigator.orientation = new Orientation();
/**
* This class contains position information.
* @param {Object} lat
* @param {Object} lng
* @param {Object} acc
* @param {Object} alt
* @param {Object} altacc
* @param {Object} head
* @param {Object} vel
* @constructor
*/
function Position(lat, lng, acc, alt, altacc, head, vel) {
/**
* The latitude of the position.
*/
this.latitude = lat;
/**
* The longitude of the position,
*/
this.longitude = lng;
/**
* The accuracy of the position.
*/
this.accuracy = acc;
/**
* The altitude of the position.
*/
this.altitude = alt;
/**
* The altitude accuracy of the position.
*/
this.altitudeAccuracy = altacc;
/**
* The direction the device is moving at the position.
*/
this.heading = head;
/**
* The velocity with which the device is moving at the position.
*/
this.velocity = vel;
/**
* The time that the position was obtained.
*/
this.timestamp = new Date().getTime();
}
/**
* This class specifies the options for requesting position data.
* @constructor
*/
function PositionOptions() {
/**
* Specifies the desired position accuracy.
*/
this.enableHighAccuracy = true;
/**
* The timeout after which if position data cannot be obtained the errorCallback
* is called.
*/
this.timeout = 10000;
}
/**
* This class contains information about any GSP errors.
* @constructor
*/
function PositionError() {
this.code = null;
this.message = "";
}
PositionError.UNKNOWN_ERROR = 0;
PositionError.PERMISSION_DENIED = 1;
PositionError.POSITION_UNAVAILABLE = 2;
PositionError.TIMEOUT = 3;
/**
* This class provides access to the device SMS functionality.
* @constructor
*/
function Sms() {
}
/**
* Sends an SMS message.
* @param {Integer} number The phone number to send the message to.
* @param {String} message The contents of the SMS message to send.
* @param {Function} successCallback The function to call when the SMS message is sent.
* @param {Function} errorCallback The function to call when there is an error sending the SMS message.
* @param {PositionOptions} options The options for accessing the GPS location such as timeout and accuracy.
*/
Sms.prototype.send = function(number, message, successCallback, errorCallback, options) {
}
if (typeof navigator.sms == "undefined") navigator.sms = new Sms();
/**
* This class provides access to the telephony features of the device.
* @constructor
*/
function Telephony() {
}
/**
* Calls the specifed number.
* @param {Integer} number The number to be called.
*/
Telephony.prototype.call = function(number) {
}
if (typeof navigator.telephony == "undefined") navigator.telephony = new Telephony();
// Android specific overrides here
Notification.prototype.vibrate = function(mills)
{
Device.vibrate(mills);
}
/*
* On the Android, we don't beep, we notify you with your
* notification! We shouldn't keep hammering on this, and should
* review what we want beep to do.
*/
Notification.prototype.beep = function(count, volume)
{
Device.beep(count);
}
/*
* Since we can't guarantee that we will have the most recent, we just try our best!
*
* Also, the API doesn't specify which version is the best version of the API
*/
Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options)
{
Geolocation.global_success = successCallback;
Geolocation.fail = errorCallback;
Geo.getCurrentLocation();
}
// Run the global callback
Geolocation.gotCurrentPosition = function(lat, lng)
{
if (lat == "undefined" || lng == "undefined")
{
this.fail();
}
else
{
p = {};
p.latitude = lat;
p.longitude = lng;
this.global_success(p);
}
}
/*
* This turns on the GeoLocator class, which has two listeners.
* The listeners have their own timeouts, and run independently of this process
* In this case, we return the key to the watch hash
*/
Geolocation.prototype.watchPosition = function(successCallback, errorCallback, options)
{
var frequency = (options != undefined)? options.frequency : 10000;
if (!this.listeners)
{
this.listeners = []
}
var key = this.listeners.push( {"success" : successCallback, "fail" : failCallback }) - 1;
// TO-DO: Get the names of the method and pass them as strings to the Java.
return Geolocation.start(frequency, key);
}
/*
* Retrieve and stop this listener from listening to the GPS
*
*/
Geolocation.success = function(key, lat, lng)
{
this.listeners[key].success(lat,lng);
}
Geolocation.prototype.fail = function(key)
{
this.listeners[key].fail();
}
Geolocation.prototype.clearWatch = function(watchId)
{
Geo.stop(watchId);
}
/* Identical to the iPhone, except we have to create this in the JS */
_accel = {}
_accel.x = 0;
_accel.y = 0;
_accel.z = 0;
function gotAccel(x,y,z)
{
_accel.x = x;
_accel.y = y;
_accel.z = z;
}
Accelerometer.base_method = Accelerometer.prototype.watchAcceleration
Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallback, options)
{
Accel.start();
Accelerometer.base_method(successCallback, errorCallback, options);
}
Accelerometer.prototype.clearWatch = function(watchId){
clearInterval(watchId);
Accel.stop();
}

View File

@ -1 +0,0 @@
This file is used to ensure this subdirectory is created.

Binary file not shown.

View File

@ -6,18 +6,19 @@
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>PhoneGap</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" charset="utf-8">
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
var deviceInfo = function(){
document.getElementById("platform").innerHTML = Device.getPlatform();
document.getElementById("version").innerHTML = Device.getVersion();
document.getElementById("uuid").innerHTML = Device.getUuid();
console.log(device.platform);
document.getElementById("platform").innerHTML = device.platform;
document.getElementById("version").innerHTML = device.version;
document.getElementById("uuid").innerHTML = device.uuid;
}
var getLocation = function() {
var suc = function(p){
alert(p.latitude + " " + p.longitude);
alert(p.coords.latitude + " " + p.coords.longitude);
};
var fail = function(){};
navigator.geolocation.getCurrentPosition(suc,fail);
@ -61,8 +62,9 @@
function init(){
document.addEventListener("touchmove", preventBehavior, false);
deviceInfo();
// Android is SLOW!!!! This is not present yet!
setTimeout(deviceInfo, 200);
document.addEventListener("touchmove", preventBehavior, false);
}
</script>

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,10 @@ public class AccelListener implements SensorListener{
WebView mAppView;
Context mCtx;
String mKey;
int mTime = 10000;
boolean started = false;
private SensorManager sensorManager;
private long lastUpdate = -1;
@ -19,20 +23,25 @@ public class AccelListener implements SensorListener{
AccelListener(Context ctx, WebView appView)
{
mCtx = ctx;
mAppView = appView;
mAppView = appView;
sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);
}
public void start()
public void start(int time)
{
sensorManager.registerListener(this,
mTime = time;
if (!started)
{
sensorManager.registerListener(this,
SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_GAME);
}
}
public void stop()
{
sensorManager.unregisterListener(this);
if(started)
sensorManager.unregisterListener(this);
}
public void onAccuracyChanged(int sensor, int accuracy) {
@ -43,7 +52,7 @@ public class AccelListener implements SensorListener{
if (sensor != SensorManager.SENSOR_ACCELEROMETER || values.length < 3)
return;
long curTime = System.currentTimeMillis();
if (lastUpdate == -1 || (curTime - lastUpdate) > 1000) {
if (lastUpdate == -1 || (curTime - lastUpdate) > mTime) {
lastUpdate = curTime;

View File

@ -94,9 +94,10 @@ public class DroidGap extends Activity {
// The PhoneGap class handles the Notification and Android Specific crap
this.gap = new PhoneGap(this, appView);
this.geo = new GeoBroker(appView, this);
this.accel = new AccelListener(this, appView);
this.accel = new AccelListener(this, appView);
// This creates the new javascript interfaces for PhoneGap
appView.addJavascriptInterface(gap, "Device");
// Ewww - It's called DroidGap again. :(
appView.addJavascriptInterface(gap, "DroidGap");
appView.addJavascriptInterface(geo, "Geo");
appView.addJavascriptInterface(accel, "Accel");
}

View File

@ -30,13 +30,20 @@ public class GeoListener {
/*
* We only need to figure out what we do when we succeed!
*/
String params;
/*
* Build the giant string to send back to Javascript!
*/
params = loc.getLatitude() + "," + loc.getLongitude() + ", " + loc.getAltitude() + "," + loc.getAccuracy() + "," + loc.getBearing();
params += "," + loc.getSpeed() + "," + loc.getTime();
if(id != "global")
{
mAppView.loadUrl("javascript:Geolocation.success(" + id + ", " + loc.getLatitude() + ", " + loc.getLongitude() + ")");
mAppView.loadUrl("javascript:navigator.geolocation.success(" + id + "," + params + ")");
}
else
{
mAppView.loadUrl("javascript:Geolocation.gotCurrentPosition(" + loc.getLatitude() + ", " + loc.getLongitude() + ")");
mAppView.loadUrl("javascript:navigator.geolocation.gotCurrentPosition(" + params + ")");
this.stop();
}
}
@ -44,7 +51,7 @@ public class GeoListener {
void fail()
{
// Do we need to know why? How would we handle this?
mAppView.loadUrl("javascript:GeoLocation.fail(" + id + ")");
mAppView.loadUrl("javascript:navigator.geolocation.fail(" + id + ")");
}
// This stops the listener

View File

@ -65,7 +65,10 @@ public class PhoneGap{
RingtoneManager beeper = new RingtoneManager(mCtx);
Uri ringtone = beeper.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone notification = beeper.getRingtone(mCtx, ringtone);
notification.play();
for (long i = 0; i < pattern; ++i)
{
notification.play();
}
}
public void vibrate(long pattern){