Skip to content

Commit

Permalink
Fix accelerometer call: support both webapi and cordova
Browse files Browse the repository at this point in the history
  • Loading branch information
Lionel Laské committed Oct 20, 2024
1 parent 45f2b76 commit 7deebd7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
43 changes: 15 additions & 28 deletions activities/FractionBounce.activity/js/components/SugarDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ Vue.component('sugar-device', {
template: `<div style="display: none">{{ watchId }}</div>`,
data: function () {
return {
readyToWatch: false,
frequency: null
watchId: null
}
},
created: function () {
Expand All @@ -13,24 +12,6 @@ Vue.component('sugar-device', {
cordovaScript.setAttribute('src', '../../cordova.js');
document.head.appendChild(cordovaScript);
},
mounted: function () {
var vm = this;
//Accelerometer
if (this.isMobile()) {
document.addEventListener('deviceready', function () {
vm.readyToWatch = true;
}, false);
}

},
computed: {
watchId: function () {
if (this.readyToWatch && this.frequency) {
return navigator.accelerometer.watchAcceleration(this.accelerationCallback, null, { frequency: this.frequency });
}
return null;
}
},
methods: {
isMobile() {
var userAgent = navigator.userAgent;
Expand Down Expand Up @@ -95,14 +76,18 @@ Vue.component('sugar-device', {

watchAcceleration: function (frequency) {
var vm = this;
var accelerometer = new Accelerometer({ frequency: frequency });
if (accelerometer) {
accelerometer.addEventListener('reading', function () {
if (window.Accelerometer) {
var accelerometer = new Accelerometer({ frequency: frequency });
if (accelerometer) {
accelerometer.addEventListener('reading', function () {
vm.accelerationCallback(accelerometer);
});
accelerometer.start();
}
} else if (navigator.accelerometer) {
watchId = navigator.accelerometer.watchAcceleration(function () {
vm.accelerationCallback(accelerometer);
});
accelerometer.start();
} else {
this.frequency = frequency;
}, null, { frequency: frequency });
}
},

Expand All @@ -129,6 +114,8 @@ Vue.component('sugar-device', {
}
},
beforeDestroy: function () {
navigator.accelerometer.clearWatch(this.watchId);
if (navigator.accelerometer && this.watchId) {
navigator.accelerometer.clearWatch(this.watchId);
}
}
});
17 changes: 11 additions & 6 deletions activities/PhysicsJS.activity/js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ define(["sugar-web/activity/activity","tutorial","l10n","sugar-web/env"], functi
zoom();
}
if (readyToWatch) {
var accelerometer = new Accelerometer({ frequency: 500 });
if (accelerometer) {
accelerometer.addEventListener('reading', accelerationChanged);
accelerometer.start();
if (window.Accelerometer) {
var accelerometer = new Accelerometer({ frequency: 500 });
if (accelerometer) {
accelerometer.addEventListener('reading', accelerationChanged);
accelerometer.start();
}
readyToWatch = false;
} else if (navigator.accelerometer) {
navigator.accelerometer.watchAcceleration(accelerationChanged, null, { frequency: 500 });
readyToWatch = false;
}
readyToWatch = false;
}
});
// add the interaction
Expand Down Expand Up @@ -189,7 +194,7 @@ define(["sugar-web/activity/activity","tutorial","l10n","sugar-web/env"], functi

function accelerationChanged(accelerationEvent) {
if (!sensorMode) return;
var acceleration = accelerationEvent.target;
var acceleration = window.Accelerometer ? accelerationEvent.target : accelerationEvent;
if (acceleration.x < -4.5) {
if (acceleration.y > 4.75)
setGravity(3);
Expand Down

0 comments on commit 7deebd7

Please sign in to comment.