From 7deebd7fdb68fa5789a2abb24937c510c194f2ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lionel=20Lask=C3=A9?= Date: Sun, 20 Oct 2024 17:08:08 +0200 Subject: [PATCH] Fix accelerometer call: support both webapi and cordova --- .../js/components/SugarDevice.js | 43 +++++++------------ activities/PhysicsJS.activity/js/activity.js | 17 +++++--- 2 files changed, 26 insertions(+), 34 deletions(-) diff --git a/activities/FractionBounce.activity/js/components/SugarDevice.js b/activities/FractionBounce.activity/js/components/SugarDevice.js index 6783fe2fd..3a3b39a19 100644 --- a/activities/FractionBounce.activity/js/components/SugarDevice.js +++ b/activities/FractionBounce.activity/js/components/SugarDevice.js @@ -3,8 +3,7 @@ Vue.component('sugar-device', { template: `
{{ watchId }}
`, data: function () { return { - readyToWatch: false, - frequency: null + watchId: null } }, created: function () { @@ -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; @@ -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 }); } }, @@ -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); + } } }); \ No newline at end of file diff --git a/activities/PhysicsJS.activity/js/activity.js b/activities/PhysicsJS.activity/js/activity.js index 33d43398c..a05f2ec68 100644 --- a/activities/PhysicsJS.activity/js/activity.js +++ b/activities/PhysicsJS.activity/js/activity.js @@ -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 @@ -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);