Add sample application[phone]
[staging/soundmanager.git] / sample / phone / app / Dialer.qml
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 import QtQuick 2.6
18 import QtQuick.Layouts 1.1
19 import QtQuick.Controls 2.0
20 import QtMultimedia 5.5
21 import AGL.Demo.Controls 1.0
22 import 'models'
23 import 'api' as API
24
25 Item {
26     id: root
27
28     function getTime() {
29         return new Date().getTime()
30     }
31
32     // Elapsed time in hh:mm:ss format
33     function getElapsedTimeString(startTime) {
34         var seconds = Math.floor((getTime() - startTime) / 1000);
35         var time = new Date(null);
36         time.setSeconds(seconds);
37         return time.toISOString().substr(11, 8);
38     }
39
40     Timer {
41         id: callTimer
42         interval: 1000
43         repeat: true
44         property var startTime
45         onTriggered: callStatusLabel.text = getElapsedTimeString(startTime)
46     }
47
48     API.Telephony {
49             id: telephony
50             url: bindingAddress
51         property string callStatus: "disconnected"
52         property string callClipColp: ""
53
54         onCallStatusChanged: {
55             if (callStatus == "incoming") {
56                 soundmgr.connect("calling")
57                 //ringtone.active = true
58                 //rejectButton.active = true
59                 //callStatusLabel.text = "Incoming call from " + callClipColp
60             } else if (callStatus == "dialing") {
61                 callStatusLabel.text = "Dialing " + callClipColp
62             } else if (callStatus == "active") {
63                 rejectButton.active = false
64                 callTimer.startTime = getTime()
65                 callTimer.restart()
66             } else if (callStatus == "disconnected") {
67                 ringtone.active = false
68                 rejectButton.active = false
69                 callButton.checked = false
70                 callTimer.stop()
71                 callStatusLabel.text = ""
72                 soundmgr.disconnect()
73             }
74         }
75     }
76     API.BindingSoundManager {
77         id: soundmgr 
78         onApproved: {
79             console.log("Sound Right Approved")
80             switch(telephony.callStatus){
81                 case "dialing":
82                 case "active":
83                 case "disconnected":
84                     telephony.dial(number.text)
85                     break;  // WARNING! these cases are not tested excluding incomming
86                 case "incoming":
87                     ringtone.active = true
88                     rejectButton.active = true
89                     callStatusLabel.text = "Incoming call from " + telephony.callClipColp
90                     
91                     break;
92                 default:
93                     break;
94             }
95         }
96         onDenied: {
97             console.log("Sound Right Denyed due to emergency")
98         }
99         onPaused:{
100             console.log("paused") // this doesn't come maybe
101         }
102     }
103
104     Loader {
105         id: ringtone
106         active: false
107         sourceComponent: Component {
108             SoundEffect {
109                 loops: SoundEffect.Infinite
110                 source: './Phone.wav'
111                 category: 'phone'
112                 Component.onCompleted: play()
113             }
114         }
115     }
116
117     signal showContacts
118     function call(contact) {
119         name.text = contact.name
120         number.text = contact.number
121         callButton.checked = true
122     }
123
124     ImageButton {
125         anchors.left: parent.left
126         anchors.top: parent.top
127         anchors.margins: 60
128         width: 172
129         height: 172
130         offImage: './images/HMI_Phone_Contacts-01.svg'
131         onClicked: root.showContacts()
132     }
133
134     ColumnLayout {
135         anchors.fill: parent
136         anchors.topMargin: 50
137         anchors.bottomMargin: 50
138         spacing: 20
139         ColumnLayout {
140             Layout.alignment: Qt.AlignHCenter
141             Label {
142                 id: name
143                 font.pixelSize: 40
144                 color: '#59FF7F'
145             }
146             TextField {
147                 id: number
148                 readOnly: true
149
150                 ImageButton {
151                     anchors.right: parent.right
152                     anchors.verticalCenter: parent.verticalCenter
153                     offImage: './images/HMI_Phone_Back_Icon.svg'
154                     onClicked: {
155                         if (number.text.length > 0)
156                             number.text = number.text.substring(0, number.text.length - 1)
157                     }
158                 }
159             }
160         }
161
162         GridLayout {
163             Layout.alignment: Qt.AlignHCenter
164             columns: 3
165             columnSpacing: 30
166             rowSpacing: 30
167             Repeater {
168                 model: ListModel {
169                     ListElement { value: '1'; image: '1' }
170                     ListElement { value: '2'; image: '2' }
171                     ListElement { value: '3'; image: '3' }
172                     ListElement { value: '4'; image: '4' }
173                     ListElement { value: '5'; image: '5' }
174                     ListElement { value: '6'; image: '6' }
175                     ListElement { value: '7'; image: '7' }
176                     ListElement { value: '8'; image: '8' }
177                     ListElement { value: '9'; image: '9' }
178                     ListElement { value: '*'; image: 'asterisk' }
179                     ListElement { value: '0'; image: '0' }
180                     ListElement { value: '#'; image: 'NumberSign' }
181                 }
182                 ImageButton {
183                     onImage: './images/HMI_Phone_Button_%1_Active-01.svg'.arg(model.image)
184                     offImage: './images/HMI_Phone_Button_%1_Inactive-01.svg'.arg(model.image)
185                     onClicked: {
186                         number.text += model.value
187                     }
188                 }
189             }
190         }
191
192         Label {
193             id: callStatusLabel
194             Layout.alignment: Qt.AlignHCenter
195             text: ""
196         }
197
198         ToggleButton {
199             id: callButton
200             Layout.alignment: Qt.AlignHCenter
201             onImage: './images/HMI_Phone_Hangup.svg'
202             offImage: './images/HMI_Phone_Call.svg'
203             property var active: (number.text.length > 0) || (telephony.callStatus == "incoming") || (telephony.callStatus == "active")
204             opacity: active ? 1 : 0.25
205
206             onCheckedChanged: {
207                 if (checked) {
208                     if (!active) {
209                         callButton.checked = false
210                         return
211                     }
212
213                     var contact = {'name': name.text, 'number': number.text}
214                     if (contact.name === '')
215                         contact.name = 'Unknown'
216                     history.insert(0, contact)
217                     if (telephony.callStatus == "incoming") {
218                         telephony.answer()
219                         ringtone.active = false;
220                     } else {
221                         soundmgr.connect()
222                         //telephony.dial(number.text)
223                     }
224                 } else {
225                     name.text = ''
226                     number.text = ''
227                     telephony.hangup()
228                     soundmgr.disconnect()
229                 }
230             }
231         }
232
233         Loader {
234             id: rejectButton
235             Layout.alignment: Qt.AlignHCenter
236             active: false
237             sourceComponent: ImageButton {
238                 offImage: './images/HMI_Phone_Hangup.svg'
239                 onClicked: {
240                     telephony.hangup()
241                     soundmgr.disconnect()
242                 }
243             }
244         }
245
246         ListView {
247             Layout.fillWidth: true
248             Layout.preferredHeight: 130
249             orientation: Qt.Horizontal
250             clip: true
251             model: CallHistoryModel { id: history }
252
253             delegate: MouseArea {
254                 width: root.width / 2.5
255                 height: ListView.view.height
256                 RowLayout {
257                     anchors.fill: parent
258                     spacing: 20
259                     Image {
260                         source: './images/HMI_Phone_Contact_BlankPhoto.svg'
261                     }
262                     ColumnLayout {
263                         Label {
264                             Layout.fillWidth: true
265                             color: '#59FF7F'
266                             text: model.name
267                         }
268
269                         Label {
270                             Layout.fillWidth: true
271                             font.pixelSize: 30
272                             text: model.number
273                         }
274                     }
275                 }
276                 onClicked: root.call(model)
277             }
278         }
279     }
280 }