1 /** 2 * Copyright (c) 2011 The Chromium Authors. All rights reserved. 3 * Use of this source code is governed by a BSD-style license that can be 4 * found in the LICENSE file. 5 */ 6 7 var a1Timer = null; 8 var a2Timer = null; 9 var port = null; 10 var iconFlashTimer = null; 11 12 var HOUR_MS = 1000 * 60 * 60; 13 14 // Override from common.js 15 window.stopFlashingIcon = function() { 16 window.clearTimeout(iconFlashTimer); 17 chrome.browserAction.setIcon({'path': 'clock-19.png'}); 18 }; 19 20 // Override from common.js 21 window.flashIcon = function() { 22 var flashes = 10; 23 function flash() { 24 if (flashes == 0) { 25 stopFlashingIcon(); 26 return; 27 } 28 29 if (flashes % 2 == 0) { 30 chrome.browserAction.setIcon({'path': 'clock-highlighted-19.png'}); 31 } else { 32 chrome.browserAction.setIcon({'path': 'clock-19.png'}); 33 } 34 flashes--; 35 iconFlashTimer = window.setTimeout(flash, 500); 36 } 37 flash(); 38 }; 39 40 function setTimer(alarmHours, alarmMinutes) { 41 var alarmTime = (alarmHours * 60 + alarmMinutes) * 60 * 1000; 42 var d = new Date(); 43 var now = d.getHours() * HOUR_MS + 44 d.getMinutes() * 60 * 1000 + 45 d.getSeconds() * 1000; 46 var delta = (alarmTime - now); 47 48 if (delta >= -5000 && delta < 1000) { 49 ringAlarm(alarmHours, alarmMinutes); 50 if (port) { 51 port.postMessage({'cmd': 'anim'}); 52 } 53 return null; 54 } 55 56 if (delta < 0) { 57 delta += HOUR_MS * 24; 58 } 59 if (delta >= 1000) { 60 if (delta > HOUR_MS) { 61 delta = HOUR_MS; 62 } 63 console.log('Timer set for ' + delta + ' ms'); 64 return window.setTimeout(resetTimers, delta); 65 } 66 67 return null; 68 }; 69 70 function resetTimers() { 71 if (a1Timer) { 72 window.clearTimeout(a1Timer); 73 } 74 75 try { 76 var a1_on = (localStorage['a1_on'] == 'true'); 77 var a1_tt = localStorage['a1_tt'] || DEFAULT_A1_TT; 78 var a1_ampm = localStorage['a1_ampm'] || DEFAULT_A1_AMPM; 79 if (a1_on) { 80 var alarmHoursMinutes = parseTime(a1_tt, a1_ampm); 81 var alarmHours = alarmHoursMinutes[0]; 82 var alarmMinutes = alarmHoursMinutes[1]; 83 a1Timer = setTimer(alarmHours, alarmMinutes); 84 } 85 } catch (e) { 86 console.log(e); 87 } 88 89 try { 90 var a2_on = (localStorage['a2_on'] == 'true'); 91 var a2_tt = localStorage['a2_tt'] || DEFAULT_A2_TT; 92 var a2_ampm = localStorage['a2_ampm'] || DEFAULT_A2_AMPM; 93 if (a2_on) { 94 var alarmHoursMinutes = parseTime(a2_tt, a2_ampm); 95 var alarmHours = alarmHoursMinutes[0]; 96 var alarmMinutes = alarmHoursMinutes[1]; 97 a2Timer = setTimer(alarmHours, alarmMinutes); 98 } 99 } catch (e) { 100 console.log(e); 101 } 102 103 if (a1_on || a2_on) { 104 chrome.browserAction.setIcon({'path': 'clock-19.png'}); 105 } else { 106 chrome.browserAction.setIcon({'path': 'clock-disabled-19.png'}); 107 } 108 } 109 110 function onLocalStorageChange() { 111 resetTimers(); 112 } 113 114 function initBackground() { 115 window.addEventListener('storage', onLocalStorageChange, false); 116 117 chrome.runtime.onConnect.addListener(function(popupPort) { 118 port = popupPort; 119 port.onDisconnect.addListener(function() { 120 port = null; 121 }); 122 }); 123 } 124 125 initBackground(); 126 resetTimers(); 127