Advertisement
Guest User

mobileSpy

a guest
Sep 19th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if (!Array.prototype.last) { Array.prototype.last = function() { return this[this.length - 1]; }
  2.  
  3.   var historicMotion = { "x": [], "y": [], "z": [] }
  4.   var historicOrientation = { "x": [], "y": [], "z": [] }
  5.  
  6.   function setStatus(status) {
  7.       if(ga) { ga('set', 'dimension1', status); } //a se schimba dimensiunea cu cea necesara
  8.   }
  9.  
  10.   function updateStatus() {
  11.     let movement = mostRecentMovementOverall(75)
  12.  
  13.     if (mostRecentMovementOverall(4000) > 40) { // TODO: haven't tested this, 1,000 so it's a longer time
  14.       setStatus("conduce")
  15.     } else if (historicOrientation["z"].last() > 70 || historicOrientation["z"].last() < -70) {
  16.       setStatus("Tolanit in pat")
  17.     } else if (historicOrientation["y"].last() > 160 || historicOrientation["y"].last() < -160) {
  18.       setStatus("Tolanit in pat")
  19.     } else if (historicOrientation["y"].last() >= 30 && historicOrientation["y"].last() < 70) {
  20.       if (movement > 18) {
  21.         setStatus("In miscare")
  22.       } else {
  23.         setStatus("Sta pe loc in picioare")
  24.       }
  25.     } else if (historicOrientation["y"].last() >= 70 && historicOrientation["y"].last() < 95) {
  26.       if (movement > 18) {
  27.         setStatus("In Miscare")
  28.       } else {
  29.         setStatus("Pozitie de poza")
  30.       }
  31.     } else if (historicOrientation["y"].last() >= 95 && historicOrientation["y"].last() < 120) {
  32.       setStatus("Pozitie de poza")
  33.     } else if (Math.round(historicOrientation["z"].last()) == 0 && Math.round(historicOrientation["y"].last()) == 0) {
  34.       setStatus("Telefon pe masa")
  35.     } else {
  36.       if (movement > 18) {
  37.         setStatus("In miscare")
  38.       } else {
  39.         setStatus("Sta pe loc in picioare")
  40.       }
  41.     }
  42.   }
  43.  
  44.  
  45.   function mostRecentMovementOverall(numberOfHistoricPoints) {
  46.     return (mostRecentMovement(historicMotion["x"], numberOfHistoricPoints, true) +
  47.             mostRecentMovement(historicMotion["y"], numberOfHistoricPoints, true) +
  48.             mostRecentMovement(historicMotion["z"], numberOfHistoricPoints, true)) / 3.0
  49.   }
  50.  
  51.   // numberOfHistoricPoints: 100 is about 3 seconds
  52.   function mostRecentMovement(array, numberOfHistoricPoints, removeNegatives) {
  53.     if (array.length > numberOfHistoricPoints) {
  54.       totalSum = 0
  55.       for (var toCount = 0; toCount < numberOfHistoricPoints; toCount++) {
  56.         currentElement = array[array.length - toCount - 1]
  57.         currentElement *= (1 - toCount / numberOfHistoricPoints) // weight the most recent data more
  58.         if (currentElement < 0 && removeNegatives) currentElement = currentElement * -1
  59.         if (currentElement > 0.1 || currentElement < -0.1) totalSum += currentElement
  60.       }
  61.       return totalSum * 100 / numberOfHistoricPoints
  62.     }
  63.     return 0 // not enough data yet
  64.   }  
  65.  
  66.  
  67.   var isMobile = navigator.userAgent.match(/(iPhone|iPod|iPad|Android)/);
  68.   if (isMobile != null) {
  69.     window.addEventListener("devicemotion", motion, false);
  70.  
  71.     function motion(event) {
  72.       historicMotion["x"].push(event.acceleration.x)
  73.       historicMotion["y"].push(event.acceleration.y)
  74.       historicMotion["z"].push(event.acceleration.z)
  75.     }
  76.  
  77.     window.addEventListener("deviceorientation", orientation, false);
  78.  
  79.     function orientation(event) {
  80.       historicOrientation["x"].push(event.alpha)
  81.       historicOrientation["y"].push(event.beta)
  82.       historicOrientation["z"].push(event.gamma)
  83.     }
  84.  
  85.     setTimeout(updateStatus, 500)
  86.   }
  87. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement