Fixed the bug where user cannot accept ncoming call
[profile/ivi/Modello_Common.git] / css / car / components / incomingCall / incomingCall.js
1 /* global launchApplication, getAppByID, acceptCall*/
2
3 /**
4  * @module CarTheme
5  */
6 var IncomingCall = function() {
7         "use strict";
8         /**
9          * Class provides accept/deny for incoming call dialog in every widget. This component is usually initialized by {{#crossLink "Bootstrap"}}{{/crossLink}} class
10      * and can be later accessed using {{#crossLink "Bootstrap/incomingCall:property"}}{{/crossLink}} property.
11      *
12      * Component uses [tizen.phone API]() to perform phone operations and
13      * [Tizen Application API](https://developer.tizen.org/dev-guide/2.2.0/org.tizen.web.device.apireference/tizen/application.html) to launch external application.
14      *
15      * This class requires following components:
16          *
17          * * {{#crossLink "BoxCaption"}}{{/crossLink}} component
18          *
19          * @class IncomingCall
20          * @constructor
21          */
22         var self = this;
23         if (!$("#incomingCall").length) {
24                 var appendHtml = '<div id="incomingCall" class="incomingCall incomingCallHide ">';
25                 var background = '<div class = "pageBgColorNormalTransparent" style ="width:100%; height: 100%"></div>';
26                 var tabs = '<div id = "incomingCallTopPanel" class="tabsTopPanel"></div>';
27                 tabs += '<div id = "incomingCallTabs" class="tabsTab"></div>';
28                 tabs += '<div id ="incomingCallTitle" class="TopPanelTitle"></div>';
29                 var callbox = '<div class = "callInfoBox">';
30                 callbox += '<div class = "callPhotoBox noContactPhoto">';
31                 callbox += '<img id = "incomingCallPhoto" class="callPhoto"></div>';
32                 callbox += '</div>';
33                 callbox += '<div id = "incomingCallName" class="callName fontSizeXLarge fontWeightBold fontColorNormal">unknown</div>';
34                 callbox += '<div id = "incomingCallNumber" class="callNumber fontSizeXLarge fontWeightBold fontColorTheme">unknown</div>';
35                 // callbox += '</div>';
36
37                 var buttons = '<div id="incomingCallAccept" class="incomingCallAccept button callButton callingFalse boxShadow4Active" onClick = "bootstrap.incomingCall.acceptIncommingCall()"></div>';
38                 buttons += '<div id="incomingCallDeny" class="incomingCallDeny button callButton callingTrue boxShadow4Active" onClick = "bootstrap.incomingCall.denyCall()"></div>';
39
40                 appendHtml += background + tabs + callbox + buttons;
41                 appendHtml += '</div></div>';
42                 $(appendHtml).appendTo("body");
43                 $("#incomingCall .TopPanelTitle").boxCaptionPlugin('init', "Incoming Call");
44         }
45 };
46 /**
47  * Method shows incoming call dialog.
48  * @method show
49  * @param  contact {contact} Contact data from phone.
50  */
51 IncomingCall.prototype.show = function(contact) {
52         "use strict";
53         if (contact) {
54                 if (contact.name) {
55                         var name = contact.name.firstName || "";
56                         name += contact.name.lastName ? " " + contact.name.lastName : "";
57                         $("#incomingCallName").html(name);
58                 }
59
60                 if (contact.phoneNumbers) {
61                         $("#incomingCallNumber").html(contact.phoneNumbers[0] && contact.phoneNumbers[0].number ? contact.phoneNumbers[0].number : "");
62                 }
63
64                 if (contact.photoURI) {
65                         $("#incomingCallPhoto").attr("src", contact.photoURI);
66                 }
67         }
68         if ($("#incomingCall").hasClass('incomingCallHide')) {
69                 $("#incomingCall").removeClass('incomingCallHide');
70                 $("#incomingCall").addClass("incomingCallShow");
71         }
72 };
73 /**
74  * Method hides incoming call dialog.
75  * @method hide
76  */
77 IncomingCall.prototype.hide = function() {
78         "use strict";
79         if ($("#incomingCall").hasClass('incomingCallShow')) {
80                 $("#incomingCall").removeClass('incomingCallShow');
81                 $("#incomingCall").addClass("incomingCallHide");
82         }
83 };
84 /**
85  * Method accepts incoming call. If application where call is accepted isn't Phone application (`Modello009.Phone`), then launches Phone application.
86  * If apllication where call is accepted is Phone application (`Modello009.Phone`), call {{#crossLink "Phone/acceptCall:method"}}{{/crossLink}} method.
87  * @method acceptIncommingCall
88  */
89 IncomingCall.prototype.acceptIncommingCall = function() {
90         "use strict";
91         /* todo add call to phone application */
92         this.hide();
93         var appId = getAppByID('Modello009.Phone');
94         /* if app isn't phone */
95         if (!appId.running) {
96                 launchApplication('Modello009.Phone');
97                 /* if app is phone */
98         } else {
99                 if (typeof(tizen) !== 'undefined' && tizen.phone) {
100                         var activeCall = tizen.phone.activeCall();
101                         acceptCall(activeCall.contact);
102                 }
103         }
104 };
105 /**
106  * Method denies incoming call. This method use api fucntion [tizen.phone.hangupCall]().
107  * @method denyCall
108  */
109 IncomingCall.prototype.denyCall = function() {
110         "use strict";
111         /* todo deny call */
112         this.hide();
113         if (typeof(tizen) !== 'undefined' && tizen.phone) {
114                 tizen.phone.hangupCall(function(result) {
115                         console.log(result.message);
116                 });
117         }
118 };