mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 20:33:07 +08:00
Fix for Issue #228: Align Compass support with iOS
This commit is contained in:
parent
336a58ca5a
commit
821eb24a54
@ -9,6 +9,21 @@
|
|||||||
if (!PhoneGap.hasResource("compass")) {
|
if (!PhoneGap.hasResource("compass")) {
|
||||||
PhoneGap.addResource("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.
|
* This class provides access to device Compass data.
|
||||||
* @constructor
|
* @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() {
|
PhoneGap.addConstructor(function() {
|
||||||
if (typeof navigator.compass === "undefined") {
|
if (typeof navigator.compass === "undefined") {
|
||||||
navigator.compass = new Compass();
|
navigator.compass = new Compass();
|
||||||
|
@ -11,6 +11,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import com.phonegap.api.PhonegapActivity;
|
import com.phonegap.api.PhonegapActivity;
|
||||||
import com.phonegap.api.Plugin;
|
import com.phonegap.api.Plugin;
|
||||||
@ -38,6 +39,7 @@ public class CompassListener extends Plugin implements SensorEventListener {
|
|||||||
float heading; // most recent heading value
|
float heading; // most recent heading value
|
||||||
long timeStamp; // time of most recent value
|
long timeStamp; // time of most recent value
|
||||||
long lastAccessTime; // time the value was last retrieved
|
long lastAccessTime; // time the value was last retrieved
|
||||||
|
int accuracy; // accuracy of the sensor
|
||||||
|
|
||||||
private SensorManager sensorManager;// Sensor manager
|
private SensorManager sensorManager;// Sensor manager
|
||||||
Sensor mSensor; // Compass sensor returned by 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);
|
return new PluginResult(PluginResult.Status.IO_EXCEPTION, AccelListener.ERROR_FAILED_TO_START);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
float f = this.getHeading();
|
//float f = this.getHeading();
|
||||||
return new PluginResult(status, f);
|
return new PluginResult(status, getCompassHeading(), "navigator.compass._castDate");
|
||||||
}
|
}
|
||||||
else if (action.equals("setTimeout")) {
|
else if (action.equals("setTimeout")) {
|
||||||
this.setTimeout(args.getLong(0));
|
this.setTimeout(args.getLong(0));
|
||||||
@ -267,4 +269,26 @@ public class CompassListener extends Plugin implements SensorEventListener {
|
|||||||
this.status = status;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user