Skip to content

Commit

Permalink
Limit devicemotion and deviceorientation to Android devices only …
Browse files Browse the repository at this point in the history
…(UA) (#15)
  • Loading branch information
gzuidhof authored Sep 10, 2024
1 parent 5de837e commit bb34ebe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# changelog

## Unreleased

* Fix `deviceorientation` and `devicemotion` deprecation warnings in Firefox browsers on desktop.

## 0.1.8
**Date**: 2024-06-20

Expand Down
21 changes: 21 additions & 0 deletions src/signals/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ const M = Math;
*/
let ssig: Signals | undefined;

/**
* Returns true if the browser is an Android device based on the user agent string (very naively).
* @internal
*/
function isAndroidUA() {
return /Android/i.test(navigator.userAgent);
}

/**
* Computes rolling stats of a variable.
* Retrigger continuously resets the timer when subsequent "on" events happen.
Expand Down Expand Up @@ -350,6 +358,13 @@ export class Signals {
g: false,
};

if (!isAndroidUA()) {
// We limit this signal to Android phones, which is generally the only place where we would expect to get
// this data anyhow.
return sig;
}


window[x]("devicemotion", (e) => {
sig.ts = e.timeStamp;
sig.i = e.interval;
Expand Down Expand Up @@ -382,6 +397,12 @@ export class Signals {
bd: bd.s,
};

if (!isAndroidUA()) {
// We limit this signal to Android phones, which is generally the only place where we would expect to get
// this data anyhow.
return sig;
}

let hasPrevious: true | undefined;
window[x]("deviceorientation", (e) => {
if (e.gamma == null || e.beta == null || e.alpha == null) return;
Expand Down

0 comments on commit bb34ebe

Please sign in to comment.