da37003f604e57a2d58808024cacc759740e6f4e
[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 callTimeTextTemplate = {"font" : "bold 40px Arial", "fillStyle" : "white", "textAlign" : "left",  "textBaseline" : "top",
20     "shadowOffsetX" : 0, "shadowOffsetY" : 0, "shadowBlur" : 0, "shadowColor" : "rgba(0, 0, 0, 1.0)"};
21
22 var startTime = 0;
23 var callTime = 0;
24 var timerInterval;
25 var stateText = " ";
26
27 function updateNumber(number)
28 {
29     currentNumber = number;
30
31     if (number.length === 10)
32     {
33         number = number.slice(0,3) + '-' + number.slice(3,6) + '-' + number.slice(6,10);                
34     }
35
36     callPage.getObj("displayedNumber").text = number;           
37 }
38
39 function updateCallStateText(newState)
40 {
41     stateText = newState;
42     var callStateTextObj = callPage.getObj("callStateText")
43         callStateTextObj.text = newState;
44
45     switch(newState)
46     {
47         case "Dialing..." :
48             callStateTextObj.fillStyle = "white";
49             callStateTextObj.strokeStyle = "#00ADFF";
50             callStateTextObj.shadowColor = "#00ADFF"; 
51             break;
52         case "Incoming Call..." :
53             callStateTextObj.fillStyle = "white";
54             callStateTextObj.strokeStyle = "#FF8C00";
55             callStateTextObj.lineWidth = 1;
56             callStateTextObj.shadowColor = "#FF8C00";
57             break;
58         case "Active" :
59             callStateTextObj.fillStyle = "white";
60             callStateTextObj.strokeStyle = "green";
61             callStateTextObj.lineWidth = 1;
62             callStateTextObj.shadowColor = "rgba(0,200,0,0.7)";
63             break;              
64     }
65
66     callStateTextObj.shadowBlur = 30;   
67 }
68
69 function startTimer()
70 {
71     var today = new Date();
72     startTime = today.getTime();
73
74     if (timerInterval)
75         clearInterval(timerInterval);
76
77     timerInterval = setInterval( function() {
78             var today = new Date();
79             var currentTime = today.getTime();
80             callTime = Math.floor((currentTime - startTime) / 1000);
81
82             var hours = Math.floor(callTime / 3600);
83             callTime -= hours * 3600;
84             var mins = Math.floor(callTime / 60);
85             var secs = callTime - (mins * 60) < 10 ? "0" + (callTime - (mins * 60)).toString() : (callTime - (mins * 60)).toString();
86
87             hours = hours > 10 ? hours : "0" + hours.toString();
88             mins = mins > 10 ? mins : "0" + mins.toString();
89
90             callPage.getObj("callTime").text = hours + ":" + mins + ":" + secs;         
91             callPage.drawMenu();
92             }, 1000);
93 }
94
95 function stopTimer()
96 {
97     clearInterval(timerInterval);
98     timerInterval = undefined;
99     startTime = 0;
100     callTime = 0;
101 }
102
103 function initButtons(initState)
104 {
105     var incomingBar = callPage.getObj("incomingBar");
106
107     if (initState == "dialing" || initState == "activeCall")
108         var buttonWidth = screenWidth * 0.6;
109     else
110         var buttonWidth = (screenWidth * 0.8) / 2;
111
112     var buttonHeight = screenHeight * 0.15;
113
114     var endCallButton;
115
116     if (initState == "dialing" || initState == "activeCall")
117     {
118         endCallButton = callPage.addObject(buttonCtx, "button", {"name" : "endCallButton", "image": images.incomingDeclineButton, "icon": images.endCallIcon, "iconWidth" : 100, "iconHeight" : 100, "xLoc" : screenWidth * 0.2, 
119                 "yLoc" : incomingBar.yLoc + incomingBar.height - buttonHeight - 50, "width" : buttonWidth, "height" : buttonHeight} );
120
121         callPage.getObj("acceptCallButton").visible = false;                                                                                            
122     }
123     else
124     {
125         acceptCallButton = callPage.addObject(buttonCtx, "button", {"name" : "acceptCallButton", "image": images.incomingAcceptButton, "icon": images.callIcon, "iconWidth" : 100, "iconHeight" : 100, "xLoc" : screenWidth * 0.1, 
126                 "yLoc" : incomingBar.yLoc + incomingBar.height - buttonHeight - 50, "width" : buttonWidth, "height" : buttonHeight, "visible" : true} );        
127
128         endCallButton = callPage.addObject(buttonCtx, "button", {"name" : "endCallButton", "image": images.incomingDeclineButton, "icon": images.endCallIcon, "iconWidth" : 100, "iconHeight" : 100, 
129                 "xLoc" : acceptCallButton.xLoc + acceptCallButton.width + 20, "yLoc" : incomingBar.yLoc + incomingBar.height - buttonHeight - 50, "width" : buttonWidth, "height" : buttonHeight} );                                                                                                    
130
131         acceptCallButton.onClick = function(){
132             try {
133                 console.log("Accepting call")
134                     activeCall.accept();
135             } catch (err) {
136                 console.log("Failed to accept call");
137             }
138
139             currentState = "activeCall";                        
140             initButtons("activeCall");
141             callPage.drawMenu();
142             activeCall.accept();
143         };
144
145     }           
146
147     endCallButton.onClick = function(){                     
148         try {
149             activeCall.end();
150         } catch (err) {
151             console.log("Attempting to hangup a non-active call");
152         }
153         currentState = "idle";
154         switchMenu(mainPage);                   
155         stopTimer();
156     };
157
158 }
159
160 function initCallPage(initState)
161 {          
162
163     var incomingBar = callPage.addObject(mainCtx, "shape", {"name" : "incomingBar", "xLoc" : -20, "yLoc" : screenHeight * 0.2, "width" : screenWidth + 40, 
164             "height" : screenHeight * 0.6, "fillStyle" : "#51504F", "strokeStyle" : "#B3BF3C", "lineWidth" : 5}); 
165
166     var displayedNumber = callPage.addObject(mainCtx, "text", {"name" : "displayedNumber", "xLoc" : screenWidth / 2, "yLoc" : incomingBar.yLoc + incomingBar.height * 0.4, "width" : screenWidth * 0.75, 
167             "height" : screenHeight * 0.4, "text" : currentNumber, "template" : incomingTextTemplate});
168
169     var clockIcon = callPage.addObject(mainCtx, "image", {"name" : "clock", "image": images.clockIcon, "xLoc" : screenWidth * 0.7, 
170             "yLoc" : incomingBar.yLoc + 30, "width" : 50, "height" : 50} );
171
172     var callStateText = callPage.addObject(mainCtx, "text", {"name" : "callStateText", "xLoc" : 15, "yLoc" : clockIcon.yLoc, "width" : screenWidth * 0.2, 
173             "height" : 100, "text" : stateText, "template" : callTimeTextTemplate});
174
175     var callTime = callPage.addObject(mainCtx, "text", {"name" : "callTime", "xLoc" : clockIcon.xLoc + clockIcon.width + 10, "yLoc" : clockIcon.yLoc, "width" : screenWidth * 0.2, 
176             "height" : 100, "text" : "00:00:00", "template" : callTimeTextTemplate});
177
178     initButtons(initState);   
179 }
180
181
182
183