Fix for Issue #228: Align Compass support with iOS

This commit is contained in:
macdonst 2011-09-28 17:35:50 -04:00
parent 336a58ca5a
commit 821eb24a54
2 changed files with 228 additions and 181 deletions

View File

@ -9,6 +9,21 @@
if (!PhoneGap.hasResource("compass")) {
PhoneGap.addResource("compass");
CompassError = function(){
this.code = null;
};
// Capture error codes
CompassError.COMPASS_INTERNAL_ERR = 0;
CompassError.COMPASS_NOT_SUPPORTED = 20;
CompassHeading = function() {
this.magneticHeading = null;
this.trueHeading = null;
this.headingAccuracy = null;
this.timestamp = null;
};
/**
* This class provides access to device Compass data.
* @constructor
@ -111,6 +126,14 @@ Compass.prototype.clearWatch = function(id) {
}
};
Compass.prototype._castDate = function(pluginResult) {
if (pluginResult.message.timestamp) {
var timestamp = new Date(pluginResult.message.timestamp);
pluginResult.message.timestamp = timestamp;
}
return pluginResult;
};
PhoneGap.addConstructor(function() {
if (typeof navigator.compass === "undefined") {
navigator.compass = new Compass();

View File

@ -11,6 +11,7 @@ import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.phonegap.api.PhonegapActivity;
import com.phonegap.api.Plugin;
@ -38,6 +39,7 @@ public class CompassListener extends Plugin implements SensorEventListener {
float heading; // most recent heading value
long timeStamp; // time of most recent value
long lastAccessTime; // time the value was last retrieved
int accuracy; // accuracy of the sensor
private SensorManager sensorManager;// Sensor manager
Sensor mSensor; // Compass sensor returned by sensor manager
@ -105,8 +107,8 @@ public class CompassListener extends Plugin implements SensorEventListener {
return new PluginResult(PluginResult.Status.IO_EXCEPTION, AccelListener.ERROR_FAILED_TO_START);
}
}
float f = this.getHeading();
return new PluginResult(status, f);
//float f = this.getHeading();
return new PluginResult(status, getCompassHeading(), "navigator.compass._castDate");
}
else if (action.equals("setTimeout")) {
this.setTimeout(args.getLong(0));
@ -267,4 +269,26 @@ public class CompassListener extends Plugin implements SensorEventListener {
this.status = status;
}
/**
* Create the CompassHeading JSON object to be returned to JavaScript
*
* @return a compass heading
*/
private JSONObject getCompassHeading() {
JSONObject obj = new JSONObject();
try {
obj.put("magneticHeading", this.getHeading());
obj.put("trueHeading", this.getHeading());
// Since the magnetic and true heading are always the same our and accuracy
// is defined as the difference between true and magnetic always return zero
obj.put("headingAccuracy", 0);
obj.put("timestamp", this.timeStamp);
} catch (JSONException e) {
// Should never happen
}
return obj;
}
}