Dialer now shows incoming calls
[profile/ivi/webdialer.git] / js / dialer.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 mainCanvas, bgCanvas, buttonCanvas;
11 var mainCtx, bgCtx, buttonCtx;
12 var drawn = false;
13 var screenHeight;
14 var screenWidth;
15 var images = [];
16 var objects = [];
17 var mouseDrag = false, mouseDown = false;
18 var prevXMouse, prevYMouse;
19
20 var mainPageTitleTemplate = {"font" : "bold 40pt Arial", "lineWidth" : 2.5, "fillStyle" : "black", "strokeStyle" : "white", "textAlign" : "center",  
21     "shadowOffsetX" : 0, "shadowOffsetY" : 0, "shadowBlur" : 32, "shadowColor" : "rgba(0, 0, 110, 1.0)"};       
22
23 var mainPageButtonTemplate = {"font" : "bold 20pt Arial", "lineWidth" : 1.5, "strokeStyle" : "white", "textAlign" : "center", "textBaseline" : "middle", 
24     "shadowOffsetX" : 0, "shadowOffsetY" : 0, "shadowBlur" : 10, "shadowColor" : "black"};      
25
26 var buttonTextTemplate = {"font" : "bold 30pt Arial", "fillStyle" : "white", "textAlign" : "center", "textBaseline" : "middle",
27     "shadowOffsetX" : 0, "shadowOffsetY" : 0, "shadowBlur" : 12, "shadowColor" : "rgba(50, 50, 50, 1.0)"};
28
29 var mainPage = new MenuObject({"name" : "main", "xLoc" : 0, "yLoc" : 0, "visible" : true});
30 var callPage = new MenuObject({"name" : "incomingCall", "xLoc" : 0, "yLoc" : 0, "visible" : true});
31
32 var currentPage = mainPage;
33 var currentNumber = " ";
34 var currentState = "idle";
35
36 var activeCall = null;
37 var activeService = null;
38
39 function onMouseDown(event) 
40 {       
41
42     var selectedObj = objectsAtLocation(currentPage, event.clientX, event.clientY);             
43
44     if (selectedObj && selectedObj.onClick)
45         selectedObj.onClick(event);
46
47     prevXMouse = event.clientX;
48     prevYMouse = event.clientY;
49 }
50
51 function onMouseMove(event) 
52 {
53     var selectedObj = objectsAtLocation(currentPage, event.clientX, event.clientY);             
54     if (selectedObj && selectedObj.onMove)
55         selectedObj.onMove(event);
56
57     prevXMouse = event.clientX;
58     prevYMouse = event.clientY;         
59 }
60
61 function onMouseUp(event) 
62 {       
63
64 }
65
66 function resizeCanvas() 
67 {   
68     screenWidth = $(window).width();
69     screenHeight = $(window).height();
70
71     $(mainCanvas).attr('width', screenWidth);
72     $(mainCanvas).attr('height', screenHeight);
73     $(bgCanvas).attr('width', screenWidth);
74     $(bgCanvas).attr('height', screenHeight);
75     $(buttonCanvas).attr('width', screenWidth);
76     $(buttonCanvas).attr('height', screenHeight);
77     $(mouseClicksLayer).attr('width', screenWidth);
78     $(mouseClicksLayer).attr('height', screenHeight);
79
80     if (currentPage)
81         initPages();   
82 }
83
84 function loadImages(sources, callback)
85 {
86     var loadedImages = 0;
87     var numImages = 0;
88
89     for (var src in sources) 
90     {
91         numImages++;
92     }
93     for (var src in sources) 
94     {
95         images[src] = new Image();
96         images[src].onload = function(){
97             if (++loadedImages >= numImages) {          
98                 callback(images);
99             }
100         };
101         images[src].src = sources[src];
102     }
103 }
104
105 function init() 
106 {   
107     var activeAccount = null;
108     var listener = {
109         "onAccountUpdated": function(account) {}, 
110         "onAccountAdded": function(account) { 
111             if (activeService == null) {
112                 activeAccount = account;
113                 activeService = tizen.call.getCallService(activeAccount.id);
114             }
115             
116             // TODO: Remove banner warning of no available modems
117             console.log("Modem available for account: " + activeAccount.id);
118         }, 
119         "onAccountRemoved": function(id) {
120             if (activeAccount && activeAccount.id == id) {
121                 // grab any account available
122                 activeAccount = tizen.account.findServices('')[0];
123                 try {
124                     activeService = tizen.call.getCallService(activeAccount.id);
125                 } catch (err) {
126                     // TODO: Add banner letting the user know that calls can not
127                     // be made since there are no active modems
128                     console.log("No available modems!");
129                 }
130             }
131         }
132     }
133     tizen.account.addAccountListener(listener);
134
135     if (tizen.account.findServices('').length == 0) {
136         // TODO: Add banner letting the user know that calls can not
137         // be made since there are no active modems
138         console.log("No available modems!");
139     }
140
141     var handler = {
142         onIncoming: function(call) {
143         activeCall = call;
144             currentState = "incomingCall";
145             updateNumber(call.callData.LineIdentification);
146             initCallPage("incomingCall");
147             switchMenu(callPage);
148         },
149         onDialing: function(call) {
150             activeCall = call;
151         },
152         onAlerting: function(call) {
153             activeCall = call;
154         },
155         onDisconnected: function(call, disconnectReason) {
156             activeCall = null;
157         },
158         onDisconnecting: function(call) {
159         },
160         onAccepted: function(call) {
161             activeCall = call;
162         },
163         onActivated: function(call) {
164         },
165         onError: function(call){
166             console.log("onError: "); console.log(call);
167         }
168     };
169     tizen.call.addCallHandler(handler);
170
171     mainCanvas = document.getElementById("mainCanvas");
172     bgCanvas = document.getElementById("bgCanvas");
173     buttonCanvas = document.getElementById("buttonCanvas");
174     mouseClicksLayer = document.getElementById("mouseClicks");
175
176     mainCtx = mainCanvas.getContext("2d");
177     bgCtx = bgCanvas.getContext("2d");
178     buttonCtx = buttonCanvas.getContext("2d");
179
180     var sources = {
181            phoneIcon: "images/bluetooth-smartphone.png",
182            callButton: "images/ivi_btn-call.png",
183            callButtonPressed: "images/ivi_btn-call-active.png",
184            closeButton: "images/ivi_btn-close.png",
185            deleteButton: "images/ivi_btn-delete.png",
186            deleteButtonActive: "images/ivi_btn-delete-active.png",
187            endCallButton: "images/ivi_btn-endcall.png",
188            endCallButtonActive: "images/ivi_btn-endcall-active.png",
189            incomingAcceptButton: "images/ivi_btn-incomingcall-accept.png",
190            incomingAcceptButtonActive: "images/ivi_btn-incomingcall-accept-active.png",
191            incomingDeclineButton: "images/ivi_btn-incomingcall-decline.png",
192            incomingDeclineButtonActive: "images/ivi_btn-incomingcall-decline-active.png",
193            listItem: "images/ivi_btn-list.png",
194            listItemActive: "images/ivi_btn-list-active.png",
195            numberButton: "images/ivi_btn-numbers.png",
196            numberButtonActive: "images/ivi_btn-numbers-active.png",
197            buttonBG: "images/ivi_buttonarea.png",
198            callIcon: "images/ivi_icon-call.png",
199            deleteIcon: "images/ivi_icon-delete.png",
200            endCallIcon: "images/ivi_icon-endcall.png",
201            deleteListItemIcon: "images/ivi_icon-list-delete.png",
202            deleteListItemIconActive: "images/ivi_icon-list-delete-active.png",
203            clockIcon: "images/ivi_icon-time.png",
204            textArea: "images/ivi_textarea.png",
205            bgImage: "images/ivi-v1_ivi-background.jpg" 
206     };
207
208     loadImages(sources, resizeCanvas);
209
210     resizeCanvas();
211     resizeCanvas();     
212
213     $(window).bind('resize', resizeCanvas);
214
215     mouseClicksLayer.addEventListener('mousedown', onMouseDown);
216     mouseClicksLayer.addEventListener('mousemove', onMouseMove);
217     mouseClicksLayer.addEventListener('mouseup', onMouseUp);
218 }
219
220 function switchMenu(nextMenu)
221 {   
222     currentPage.visible = false;
223     currentPage = nextMenu; 
224     currentPage.visible = true;
225     currentPage.drawMenu();
226 }
227
228 function initPages()
229 {
230     initMainPage();
231     initCallPage(currentState);
232
233     bgCtx.drawImage(images.bgImage, 0, 0, screenWidth, screenHeight);
234     currentPage.drawMenu();
235 }
236
237 $(document).ready(function () {
238         init();
239         });