application sources from tizen_2.2
[apps/web/sample/ExercisePlanner.git] / project / js / app.onAlarm.js
1 /*jslint devel:true*/
2 /*global ExercisePlanner:false, tizen:false, Audio:false*/
3 /**
4  * These method are using when alarm is call.
5  */
6 (function () {
7         "use strict";
8         ExercisePlanner.prototype.findCurrentAlarm = function () {
9                 var currentTimeInMinutes = parseInt(this.applicationStartTime.getTime() / 1000, 10),
10                         listOfAlarms = tizen.alarm.getAll();
11
12                 return listOfAlarms.filter(function (item) {
13                         // alarm relative has not date property;
14                         if (!item.date) {
15                                 return false;
16                         }
17
18                         // +40/-10 seconds tolerance;
19                         if (parseInt(item.date.getTime() / 1000, 10) < (currentTimeInMinutes + 40)
20                                         && parseInt(item.date.getTime() / 1000, 10) > (currentTimeInMinutes - 10)) {
21                                 return true;
22                         }
23                 });
24         };
25
26         ExercisePlanner.prototype.wait = function () {
27                 // lastAlert -> change +1min
28                 var snozeTime = 1, currentAlarm = this.currentAlarm, newDate = new Date(), alarm;
29
30                 if (currentAlarm) {
31                         newDate.setMinutes(newDate.getMinutes() + snozeTime);
32                         // period value must be set so application started by alert will know current alert;
33                         alarm = new tizen.AlarmAbsolute(newDate, tizen.alarm.PERIOD_WEEK * 10);
34                         tizen.alarm.add(alarm, this.selfId);
35                         // -- remove old snooze alarm
36                         if (currentAlarm.period === tizen.alarm.PERIOD_WEEK * 10) {
37                                 tizen.alarm.remove(currentAlarm.id);
38                         }
39                         this.stopMusic();
40                 }
41
42                 this.exit();
43                 // or tizen.application.hide();
44         };
45
46         ExercisePlanner.prototype.ok = function () {
47                 this.config.count += 1;
48                 this.exit();
49         };
50
51         ExercisePlanner.prototype.startMusic = function () {
52
53                 if (!this.audioOfAlert) {
54                         this.audioOfAlert = new Audio();
55                 }
56                 this.audioOfAlert.src = 'WebContent/Runner.mp3';
57                 this.audioOfAlert.load();
58                 this.audioOfAlert.play();
59         };
60
61         ExercisePlanner.prototype.stopMusic = function () {
62                 if (this.audioOfAlert) {
63                         this.audioOfAlert.pause();
64                 }
65         };
66 }());
67