Fixing bug in the callback for an incoming call
[profile/ivi/webdialer.git] / js / callPage.js
1 /*
2  * Copyright (c) 2012, Intel Corporation.
3  *
4  * This program is licensed under the terms and conditions of the 
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 var phoneNumberTextTemplate = {"font" : "bold 40pt Arial", "lineWidth" : 2.5, "fillStyle" : "black", "strokeStyle" : "white", "textAlign" : "center",  "textBaseline" : "middle",
11     "shadowOffsetX" : 0, "shadowOffsetY" : 0, "shadowBlur" : 32, "shadowColor" : "rgba(0,100,150, 0.6)"};       
12
13 var incomingTextTemplate = {"font" : "bold 80pt Arial", "fillStyle" : "white", "textAlign" : "center",  "textBaseline" : "middle",
14     "shadowOffsetX" : 0, "shadowOffsetY" : 0, "shadowBlur" : 0, "shadowColor" : "rgba(0, 0, 0, 1.0)"};  
15
16 var callTimeTextTemplate = {"font" : "bold 30pt Arial", "fillStyle" : "white", "textAlign" : "left",  "textBaseline" : "top",
17     "shadowOffsetX" : 0, "shadowOffsetY" : 0, "shadowBlur" : 0, "shadowColor" : "rgba(0, 0, 0, 1.0)"};  
18
19 var startTime = 0;
20 var callTime = 0;
21 var timerInterval;
22
23 function updateNumber(number)
24 {
25     currentNumber = number;
26
27     if (number.length === 10)
28     {
29         number = number.slice(0,3) + '-' + number.slice(3,6) + '-' + number.slice(6,10);                
30     }
31
32     callPage.getObj("displayedNumber").text = number;           
33 }
34
35 function startTimer(timeText)
36 {
37     var today = new Date();
38     startTime = today.getTime();
39
40     timerInterval = setInterval( function() {
41             var today = new Date();
42             var currentTime = today.getTime();
43             callTime = Math.floor((currentTime - startTime) / 1000);
44         
45                 var hours = Math.floor(callTime / 3600);
46                 callTime -= hours * 3600;
47                 var mins = Math.floor(callTime / 60);
48                 var secs = callTime - (mins * 60) < 10 ? "0" + (callTime - (mins * 60)).toString() : (callTime - (mins * 60)).toString();
49                 
50                 hours = hours > 10 ? hours : "0" + hours.toString();
51                 mins = mins > 10 ? mins : "0" + mins.toString();
52                 
53             callPage.getObj("callTime").text = hours + ":" + mins + ":" + secs;         
54             callPage.drawMenu();
55             }, 1000);
56 }
57
58 function stopTimer()
59 {
60     clearInterval(timerInterval);
61 }
62
63 function initButtons(initState)
64 {
65         var incomingBar = callPage.getObj("incomingBar");
66         if (initState == "dialing" || initState == "activeCall")
67                 var buttonWidth = screenWidth * 0.6;
68     else
69                 var buttonWidth = (screenWidth * 0.8) / 2;
70         
71         var buttonHeight = screenHeight * 0.15;
72         
73          var endCallButton;
74
75     if (initState == "dialing" || initState == "activeCall")
76     {
77                 endCallButton = callPage.addObject(buttonCtx, "button", {"name" : "endCallButton", "image": images.incomingDeclineButton, "icon": images.endCallIcon, "iconWidth" : 100, "iconHeight" : 100, "xLoc" : screenWidth * 0.2, 
78                         "yLoc" : incomingBar.yLoc + incomingBar.height - buttonHeight - 50, "width" : buttonWidth, "height" : buttonHeight} );
79         
80                 callPage.getObj("acceptCallButton").visible = false;                                                                                            
81     }
82     else
83     {
84                 acceptCallButton = callPage.addObject(buttonCtx, "button", {"name" : "acceptCallButton", "image": images.incomingAcceptButton, "icon": images.callIcon, "iconWidth" : 100, "iconHeight" : 100, "xLoc" : screenWidth * 0.1, 
85                         "yLoc" : incomingBar.yLoc + incomingBar.height - buttonHeight - 50, "width" : buttonWidth, "height" : buttonHeight, "visible" : true} );        
86         
87                 endCallButton = callPage.addObject(buttonCtx, "button", {"name" : "endCallButton", "image": images.incomingDeclineButton, "icon": images.endCallIcon, "iconWidth" : 100, "iconHeight" : 100, 
88                         "xLoc" : acceptCallButton.xLoc + acceptCallButton.width + 20, "yLoc" : incomingBar.yLoc + incomingBar.height - buttonHeight - 50, "width" : buttonWidth, "height" : buttonHeight} );                                                                                                    
89                 
90                 acceptCallButton.onClick = function(){
91                     try {
92                         console.log("Accepting call")
93                                 activeCall.accept();
94                     } catch (err) {
95                                 console.log("Failed to accept call");
96                     }
97                     
98                     currentState = "activeCall";
99                         startTimer();
100                     initButtons("activeCall");
101                     callPage.drawMenu();
102                     activeCall.accept();
103                 };
104
105     }           
106
107     endCallButton.onClick = function(){                     
108         try {
109             activeCall.end();
110         } catch (err) {
111             console.log("Attempting to hangup a non-active call");
112         }
113         currentState = "idle";
114         switchMenu(mainPage);                   
115         stopTimer();
116     };
117
118 }
119
120 function initCallPage(initState)
121 {          
122
123     var incomingBar = callPage.addObject(mainCtx, "shape", {"name" : "incomingBar", "xLoc" : -20, "yLoc" : screenHeight * 0.2, "width" : screenWidth + 40, 
124             "height" : screenHeight * 0.6, "fillStyle" : "#51504F", "strokeStyle" : "#B3BF3C", "lineWidth" : 5}); 
125
126     var displayedNumber = callPage.addObject(mainCtx, "text", {"name" : "displayedNumber", "xLoc" : screenWidth / 2, "yLoc" : incomingBar.yLoc + incomingBar.height * 0.4, "width" : screenWidth * 0.75, 
127             "height" : screenHeight * 0.4, "text" : currentNumber, "template" : incomingTextTemplate});
128
129     var clockIcon = callPage.addObject(mainCtx, "image", {"name" : "clock", "image": images.clockIcon, "xLoc" : screenWidth * 0.7, 
130             "yLoc" : incomingBar.yLoc + 30, "width" : 50, "height" : 50} );
131
132     var callTime = callPage.addObject(mainCtx, "text", {"name" : "callTime", "xLoc" : clockIcon.xLoc + clockIcon.width + 10, "yLoc" : clockIcon.yLoc, "width" : screenWidth * 0.2, 
133             "height" : 100, "text" : "00:00:00", "template" : callTimeTextTemplate});
134         
135         initButtons(initState);
136    
137 }
138
139
140
141