make switch actually look good and work correctly
[tricorder.git] / js / tricorder.js
index dbd188d93bb5ae05ff326113bb7ab5e70f4a9c3d..92885dae4b231310554f4ee36c8795553ba6ac6c 100644 (file)
@@ -309,6 +309,79 @@ var gModSound = {
   },
 }
 
+var gModEnv = {
+  activate: function() {
+    gSounds.scan.play();
+    document.getElementById("envunavail").style.display = "none";
+    document.getElementById("envavail").style.display = "block";
+    window.addEventListener("devicelight", this.lightEvent, true);
+    window.addEventListener("deviceproximity", this.proxEvent, true);
+    setTimeout(function() {
+      if ((document.getElementById("envLight").textContent == "...") &&
+          (document.getElementById("envDistance").textContent == "...")) {
+        gModEnv.deactivate();
+      }
+    }, 5000);
+    try {
+      var cameras = navigator.mozCameras.getListOfCameras();
+      for (i = 0; i < cameras.length; i++) {
+        var promise = navigator.mozCameras.getCamera(cameras[i], {},
+          function(aCamera) {
+            if (aCamera.capabilities.flashModes.indexOf('torch') !== -1) {
+              gModEnv.foundFlashCamera(aCamera);
+            }
+          },
+          function(aError) { console.log("camera error: " + aError); }
+        );
+      }
+    } catch (e) {
+      // camera api not supported
+      document.getElementById("envFlashUnavail").style.display = "block";
+      document.getElementById("envFlashAvail").style.display = "none";
+    }
+  },
+  foundFlashCamera(aCamera) {
+    this.flashCamera = aCamera;
+    document.getElementById("envFlashOn").onclick = function() { console.log("on"); gModEnv.switchFlashlight(true); };
+    document.getElementById("envFlashOff").onclick = function() { console.log("off"); gModEnv.switchFlashlight(false); };
+    document.getElementById("envFlashUnavail").style.display = "none";
+    document.getElementById("envFlashAvail").style.display = "block";
+  },
+  deactivate: function() {
+    gSounds.scan.pause();
+    window.removeEventListener("devicelight", this.lightEvent, true);
+    window.removeEventListener("deviceproximity", this.proxEvent, true);
+    document.getElementById("envunavail").style.display = "block";
+    document.getElementById("envavail").style.display = "none";
+    document.getElementById("envLight").textContent = "...";
+    document.getElementById("envDistance").textContent = "...";
+  },
+  lightEvent: function(lightData) {
+    // See http://www.w3.org/TR/ambient-light/
+    document.getElementById("envLight").textContent = lightData.value + " lux";
+  },
+  proxEvent: function(proxData) {
+    // See http://www.w3.org/TR/2012/WD-proximity-20120712/
+    if (proxData.value >= proxData.max) {
+      document.getElementById("envDistance").textContent = "(maximum, >= " + proxData.value + " cm)";
+    }
+    else if (proxData.value <= proxData.min) {
+      document.getElementById("envDistance").textContent = "(minimum, <= " + proxData.value + " cm)";
+    }
+    else {
+      document.getElementById("envDistance").textContent = proxData.value + " cm";
+    }
+  },
+  flashCamera: null,
+  switchFlashlight: function(aEnabled) {
+    if (this.flashCamera) {
+      this.flashCamera.flashMode = aEnabled ? 'torch' : 'off';
+      document.getElementById("envFlashOn").disabled = aEnabled;
+      document.getElementById("envFlashOff").disabled = !aEnabled;
+    }
+  }
+}
+
 var gModDev = {
   activate: function() {
     gSounds.scan.play();