nativescript-pedometer
Version:
NativeScript pedometer plugin. Step counting FTW!
127 lines • 5.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var pedometer_common_1 = require("./pedometer.common");
var utils = require("tns-core-modules/utils/utils");
var STATE;
(function (STATE) {
STATE[STATE["STARTING"] = 0] = "STARTING";
STATE[STATE["STARTED"] = 1] = "STARTED";
STATE[STATE["ERROR_FAILED_TO_START"] = 2] = "ERROR_FAILED_TO_START";
STATE[STATE["STOPPED"] = 3] = "STOPPED";
})(STATE || (STATE = {}));
var Pedometer = (function (_super) {
__extends(Pedometer, _super);
function Pedometer() {
var _this = _super.call(this) || this;
_this.startSteps = 0;
_this.startTimestamp = 0;
_this.sensorManager = utils.ad.getApplicationContext().getSystemService(android.content.Context.SENSOR_SERVICE);
return _this;
}
Pedometer.prototype.isStepCountingAvailable = function () {
var _this = this;
return new Promise(function (resolve, reject) {
var sensors = _this.sensorManager.getSensorList(19);
resolve(sensors.size() > 0);
});
};
Pedometer.prototype.isDistanceAvailable = function () {
return new Promise(function (resolve, reject) {
resolve(false);
});
};
Pedometer.prototype.isFloorCountingAvailable = function () {
return new Promise(function (resolve, reject) {
resolve(false);
});
};
Pedometer.prototype.isPaceAvailable = function () {
return new Promise(function (resolve, reject) {
resolve(false);
});
};
Pedometer.prototype.isCadenceAvailable = function () {
return new Promise(function (resolve, reject) {
resolve(false);
});
};
Pedometer.prototype.isEventTrackingAvailable = function () {
return new Promise(function (resolve, reject) {
resolve(false);
});
};
Pedometer.prototype.startUpdates = function (arg) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
if (!arg || typeof arg.onUpdate !== "function") {
reject("onUpdate argument is required");
return;
}
var sensors = _this.sensorManager.getSensorList(19);
if (sensors.size() > 0) {
_this.sensor = sensors.get(0);
_this.startSteps = 0;
if (arg.fromDate) {
console.log("Note that unlike iOS, Android doesn't support historic step data. Alternative, use this: https://github.com/EddyVerbruggen/nativescript-health-data/tree/cb852903bbf8fbad7d63b6c1df799a97014746d1#query");
}
_this.startTimestamp = new Date().getTime();
_this.sensorEventListener = new android.hardware.SensorEventListener({
onSensorChanged: function (sensorEvent) {
if (sensorEvent.sensor.getType() === 19) {
if (_this.state === STATE.STOPPED) {
return;
}
_this.state = STATE.STARTED;
var steps = sensorEvent.values[0];
if (_this.startSteps === 0) {
_this.startSteps = steps;
}
steps = steps - _this.startSteps;
arg.onUpdate({
startDate: new Date(_this.startTimestamp),
endDate: new Date(),
steps: steps
});
}
},
onAccuracyChanged: function (sensor, accuracy) {
}
});
if (_this.sensorManager.registerListener(_this.sensorEventListener, _this.sensor, android.hardware.SensorManager.SENSOR_DELAY_UI)) {
_this.state = STATE.STARTING;
}
else {
_this.state = STATE.ERROR_FAILED_TO_START;
reject("Failed to start");
}
}
else {
_this.state = STATE.ERROR_FAILED_TO_START;
reject("Failed to start - No sensors found to register step counter listening to.");
}
}
catch (ex) {
reject(ex);
}
});
};
Pedometer.prototype.stopUpdates = function () {
var _this = this;
return new Promise(function (resolve, reject) {
try {
if (_this.sensorEventListener) {
_this.sensorManager.unregisterListener(_this.sensorEventListener);
_this.state = STATE.STOPPED;
}
resolve();
}
catch (ex) {
reject(ex);
}
});
};
return Pedometer;
}(pedometer_common_1.Common));
exports.Pedometer = Pedometer;
//# sourceMappingURL=pedometer.android.js.map