Eggtimer/basic.js
changeset 1 8452783e7980
parent 0 0b6daedcf7e1
child 2 2da62bb63910
equal deleted inserted replaced
0:0b6daedcf7e1 1:8452783e7980
     2  * JavaScript file
     2  * JavaScript file
     3  */
     3  */
     4 
     4 
     5 var SETUP_ID = 1;
     5 var SETUP_ID = 1;
     6 
     6 
     7 var timervalue = 0;
     7 var timervalue = 60;
     8 var initialTimer = 0;
     8 var initialTimer = 60;
     9 var timeoutId = null;
     9 var timeoutId = null;
       
    10 var paused = true;
       
    11 var displayUp = true;
    10 
    12 
    11 function init()
    13 function init()
    12 {
    14 {
    13 	showMainWindow();
    15 	showMainWindow();
    14 	widget.setDisplayPortrait();
    16 	widget.setDisplayPortrait();
       
    17 	watchSensorNotifications();
       
    18 }
       
    19 
       
    20 // Call this function to add a callback that will be notified of orientation
       
    21 // changes
       
    22 function watchSensorNotifications() {
       
    23 	var sensors = device.getServiceObject("Service.Sensor", "ISensor");
       
    24 	var SensorParams = {
       
    25 		SearchCriterion : "Orientation"
       
    26 	};
       
    27 	var result = sensors.ISensor.FindSensorChannel(SensorParams);
       
    28 
       
    29 	if (result.ErrorCode != 0) {
       
    30 		var errorCode = result.ErrorCode;
       
    31 		var errorMessage = result.ErrorMessage;
       
    32 		return null;
       
    33 	}
       
    34 	var result2 = sensors.ISensor.RegisterForNotification(
       
    35 			{ ChannelInfoMap : result.ReturnValue[0], 
       
    36 				ListeningType : "ChannelData" }, sensorCallback);
       
    37 	if (result.ErrorCode == 0) {
       
    38 		var transactionId = result.TransactionID;
       
    39 		return transactionId;
       
    40 	} else {
       
    41 		var errorCode = result.ErrorCode;
       
    42 		var errorMessage = result.ErrorMessage;
       
    43 		return null;
       
    44 	}
       
    45 }
       
    46 
       
    47 function turn(up) {
       
    48 	if (up != displayUp) {
       
    49 		displayUp = up;
       
    50 		timervalue = initialTimer - timervalue;
       
    51 		showValues();
       
    52 	}
       
    53 }
       
    54 
       
    55 // This function can be passed as callback to
       
    56 // sensors.ISensor.RegisterForNotification method
       
    57 function sensorCallback(transactionId, code, result) {
       
    58 	if (result.ErrorCode == 0) {
       
    59 		var dataType = result.ReturnValue.DataType;
       
    60 		var orientation = result.ReturnValue.DeviceOrientation;
       
    61 
       
    62 		if (orientation == "DisplayUp") {
       
    63 			turn(true);
       
    64 		} else if (orientation == "DisplayDown") {
       
    65 			turn(false);
       
    66 		}
       
    67 	} else {
       
    68 		var errorCode = result.ErrorCode;
       
    69 		var errorMessage = result.ErrorMessage;
       
    70 	}
    15 }
    71 }
    16 
    72 
    17 function startTimer() {
    73 function startTimer() {
    18 	var hr = parseInt(document.getElementById("hrs").value);
    74 	var hr = parseInt(document.getElementById("hrs").value);
    19 	var min = parseInt(document.getElementById("min").value);
    75 	var min = parseInt(document.getElementById("min").value);
    31 	}
    87 	}
    32 	timeoutId = window.setInterval(tick, 1000);
    88 	timeoutId = window.setInterval(tick, 1000);
    33 	tick();
    89 	tick();
    34 }
    90 }
    35 
    91 
       
    92 function showValues() {
       
    93 	var hrs = Math.floor(timervalue / 3600);
       
    94 	var mins = Math.floor((timervalue % 3600) / 60);
       
    95 	var sec = timervalue % 60;
       
    96 	
       
    97 	var sz = pad(hrs) + ":" + pad(mins) + ":" + pad(sec);
       
    98 	document.getElementById("timervalue").innerHTML = sz;
       
    99 	var sand=document.getElementById("sand");
       
   100 	var sandBottom = document.getElementById("sand-bottom");
       
   101 	var top, bottom;
       
   102 	top = (50 * (initialTimer-timervalue)/initialTimer).toFixed(0) + "%";
       
   103 	bottom = "50%";
       
   104 	if (displayUp) {
       
   105 		sand.style.top = top;
       
   106 		sand.style.bottom = bottom;
       
   107 	} else {
       
   108 		sandBottom.style.bottom = top;
       
   109 		sandBottom.style.top = bottom;
       
   110 	}
       
   111 	bottom = "0%";
       
   112 	top = (50 + (50 * timervalue/initialTimer)).toFixed(0) + "%";
       
   113 	if (displayUp) {
       
   114 		sandBottom.style.top = top;
       
   115 		sandBottom.style.bottom = bottom;
       
   116 	} else {
       
   117 		sand.style.top = bottom;
       
   118 		sand.style.bottom = top;
       
   119 	}
       
   120 }
       
   121 
    36 function tick() {
   122 function tick() {
    37 	if (timervalue > 0) {
   123 	if (timervalue > 0) {
    38 		timervalue = timervalue - 1;
   124 		timervalue = timervalue - 1;
    39 		var hrs = Math.floor(timervalue / 3600);
   125 		showValues();
    40 		var mins = Math.floor((timervalue % 3600) / 60);
       
    41 		var sec = timervalue % 60;
       
    42 		
       
    43 		var sz = pad(hrs) + ":" + pad(mins) + ":" + pad(sec);
       
    44 		document.getElementById("timervalue").innerHTML = sz;
       
    45 		document.getElementById("sand").style.top=(50 * (initialTimer-timervalue)/initialTimer).toFixed(0) + "%";
       
    46 		document.getElementById("sand-bottom").style.top=(50+(50 * timervalue/initialTimer)).toFixed(0) + "%";
       
    47 		if (timervalue == 0) {
   126 		if (timervalue == 0) {
    48 			cancelTimer();
   127 			cancelTimer();
    49 		}
   128 		}
    50 	}
   129 	}
    51 }
   130 }
    82 	var item = menu.getMenuItemById(SETUP_ID);
   161 	var item = menu.getMenuItemById(SETUP_ID);
    83 	menu.remove(item);
   162 	menu.remove(item);
    84 	menu.setRightSoftkeyLabel("Cancel", showMainWindow);
   163 	menu.setRightSoftkeyLabel("Cancel", showMainWindow);
    85 }
   164 }
    86 
   165 
    87 var paused = false;
       
    88 
   166 
    89 function pauseStart() {
   167 function pauseStart() {
    90 	if (paused) {
   168 	if (paused) {
    91 		startCountdown();
   169 		startCountdown();
    92 		document.getElementById("pausestart").style.backgroundPosition = "0px 0px";
   170 		document.getElementById("pausestart").style.backgroundPosition = "0px 0px";