mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-19 23:42:53 +08:00
143 lines
4.3 KiB
JavaScript
Executable File
143 lines
4.3 KiB
JavaScript
Executable File
/*
|
|
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
|
|
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
|
|
*
|
|
* Copyright (c) 2005-2010, Nitobi Software Inc.
|
|
* Copyright (c) 2010-2011, IBM Corporation
|
|
*/
|
|
|
|
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
|
|
*/
|
|
var Compass = function() {
|
|
/**
|
|
* The last known Compass position.
|
|
*/
|
|
this.lastHeading = null;
|
|
|
|
/**
|
|
* List of compass watch timers
|
|
*/
|
|
this.timers = {};
|
|
};
|
|
|
|
Compass.ERROR_MSG = ["Not running", "Starting", "", "Failed to start"];
|
|
|
|
/**
|
|
* Asynchronously aquires the current heading.
|
|
*
|
|
* @param {Function} successCallback The function to call when the heading data is available
|
|
* @param {Function} errorCallback The function to call when there is an error getting the heading data. (OPTIONAL)
|
|
* @param {PositionOptions} options The options for getting the heading data such as timeout. (OPTIONAL)
|
|
*/
|
|
Compass.prototype.getCurrentHeading = function(successCallback, errorCallback, options) {
|
|
|
|
// successCallback required
|
|
if (typeof successCallback !== "function") {
|
|
console.log("Compass Error: successCallback is not a function");
|
|
return;
|
|
}
|
|
|
|
// errorCallback optional
|
|
if (errorCallback && (typeof errorCallback !== "function")) {
|
|
console.log("Compass Error: errorCallback is not a function");
|
|
return;
|
|
}
|
|
|
|
// Get heading
|
|
PhoneGap.exec(successCallback, errorCallback, "Compass", "getHeading", []);
|
|
};
|
|
|
|
/**
|
|
* Asynchronously aquires the heading repeatedly at a given interval.
|
|
*
|
|
* @param {Function} successCallback The function to call each time the heading data is available
|
|
* @param {Function} errorCallback The function to call when there is an error getting the heading data. (OPTIONAL)
|
|
* @param {HeadingOptions} options The options for getting the heading data such as timeout and the frequency of the watch. (OPTIONAL)
|
|
* @return String The watch id that must be passed to #clearWatch to stop watching.
|
|
*/
|
|
Compass.prototype.watchHeading= function(successCallback, errorCallback, options) {
|
|
|
|
// Default interval (100 msec)
|
|
var frequency = (options !== undefined) ? options.frequency : 100;
|
|
|
|
// successCallback required
|
|
if (typeof successCallback !== "function") {
|
|
console.log("Compass Error: successCallback is not a function");
|
|
return;
|
|
}
|
|
|
|
// errorCallback optional
|
|
if (errorCallback && (typeof errorCallback !== "function")) {
|
|
console.log("Compass Error: errorCallback is not a function");
|
|
return;
|
|
}
|
|
|
|
// Make sure compass timeout > frequency + 10 sec
|
|
PhoneGap.exec(
|
|
function(timeout) {
|
|
if (timeout < (frequency + 10000)) {
|
|
PhoneGap.exec(null, null, "Compass", "setTimeout", [frequency + 10000]);
|
|
}
|
|
},
|
|
function(e) { }, "Compass", "getTimeout", []);
|
|
|
|
// Start watch timer to get headings
|
|
var id = PhoneGap.createUUID();
|
|
navigator.compass.timers[id] = setInterval(
|
|
function() {
|
|
PhoneGap.exec(successCallback, errorCallback, "Compass", "getHeading", []);
|
|
}, (frequency ? frequency : 1));
|
|
|
|
return id;
|
|
};
|
|
|
|
|
|
/**
|
|
* Clears the specified heading watch.
|
|
*
|
|
* @param {String} id The ID of the watch returned from #watchHeading.
|
|
*/
|
|
Compass.prototype.clearWatch = function(id) {
|
|
|
|
// Stop javascript timer & remove from timer list
|
|
if (id && navigator.compass.timers[id]) {
|
|
clearInterval(navigator.compass.timers[id]);
|
|
delete navigator.compass.timers[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();
|
|
}
|
|
});
|
|
}
|