mediaplayer: qml: pause agl-service-mediaplayer on A2DP stream
[apps/mediaplayer.git] / app / MediaPlayer.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 AGL.Demo.Controls 1.0
21
22 ApplicationWindow {
23     id: root
24
25     Item {
26         id: player
27
28         property string title: ""
29         property string album: ""
30         property string artist: ""
31
32         property int duration: 0
33         property int position: 0
34
35         property string cover_art: ""
36         property string status: ""
37
38         function time2str(value) {
39             return Qt.formatTime(new Date(value), 'mm:ss')
40         }
41     }
42
43     Item {
44         id: bluetooth
45
46         property string deviceAddress: ""
47         property bool connected: false
48         property bool av_connected: false
49
50         property int position: 0
51         property int duration: 0
52
53         property string artist: ""
54         property string title: ""
55         property string state: "stopped"
56
57         // AVRCP Target UUID
58         property string avrcp_uuid: "0000110e-0000-1000-8000-00805f9b34fb"
59
60         function connect_profiles() {
61             var address = bluetooth.deviceAddress;
62             bluetooth_connection.connect(address, "a2dp")
63             bluetooth_connection.connect(address, "avrcp")
64         }
65
66         function disconnect_profiles() {
67             var address = bluetooth.deviceAddress;
68             bluetooth_connection.disconnect(address, "a2dp")
69             bluetooth_connection.disconnect(address, "avrcp")
70         }
71
72         function set_avrcp_controls(cmd) {
73             bluetooth_connection.set_avrcp_controls(bluetooth.deviceAddress, cmd)
74         }
75     }
76
77     Connections {
78         target: bluetooth_connection
79
80         onDeviceListEvent: {
81             var address = ""
82             for (var i = 0; i < data.list.length; i++) {
83                 var item = data.list[i]
84                 if (item.Connected == "True" && item.UUIDs.indexOf(bluetooth.avrcp_uuid) >= 0) {
85                     address = item.Address
86
87                     bluetooth.connected = true
88                     mediaplayer.pause()
89
90                     //NOTE: This hack is here for when MediaPlayer is started
91                     //      with an existing connection.
92                     bluetooth.av_connected = item.AVPConnected == "True" 
93                 }
94             }
95             if (!address)
96                 bluetooth.connected = false
97             else
98                 bluetooth.deviceAddress = address
99         }
100
101         onDeviceUpdatedEvent: {
102             var metadata = data.Metadata
103
104             if (data.Connected == "False")
105                 return
106
107             if (!bluetooth.av_connected && data.AVPConnected == "True")
108                 mediaplayer.pause()
109
110             bluetooth.connected = data.Connected == "True"
111             bluetooth.av_connected = data.AVPConnected == "True"
112             bluetooth.deviceAddress = data.Address
113
114             if ('Position' in metadata)
115                 bluetooth.position = metadata.Position
116
117             if ('Duration' in metadata)
118                 bluetooth.duration = metadata.Duration
119
120             if ('Status' in metadata)
121                 bluetooth.state = metadata.Status
122
123             if ('Artist' in metadata)
124                 bluetooth.artist = metadata.Artist
125
126             if ('Title' in metadata)
127                 bluetooth.title = metadata.Title
128         }
129     }
130
131     Connections {
132         target: mediaplayer
133
134         onPlaylistChanged: {
135             playlist_model.clear();
136
137             for (var i = 0; i < playlist.list.length; i++) {
138                 var item = playlist.list[i]
139
140                 playlist_model.append({ "index": item.index, "artist": item.artist ? item.artist : '', "title": item.title ? item.title : '' })
141
142                 if (item.selected) {
143                     playlistview.currentIndex = i
144                 }
145             }
146         }
147
148         onMetadataChanged: {
149             player.title = metadata.title
150             player.album = metadata.album
151             player.artist = metadata.artist
152
153             if (metadata.duration) {
154                 player.duration = metadata.duration
155             }
156
157             if (metadata.position) {
158                 player.position = metadata.position
159             }
160
161             if (metadata.status) {
162                 player.status = metadata.status
163             }
164
165             if (metadata.image) {
166                 player.cover_art = metadata.image
167             }
168
169             playlistview.currentIndex = metadata.index
170         }
171     }
172
173     Timer {
174         id: timer
175         interval: 250
176         running: (bluetooth.av_connected && bluetooth.state == "playing")
177         repeat: true
178         onTriggered: {
179             bluetooth.position = bluetooth.position + 250
180         }
181     }
182
183     ListModel {
184         id: playlist_model
185     }
186
187     ColumnLayout {
188         anchors.fill: parent
189         Item {
190             Layout.fillWidth: true
191             Layout.fillHeight: true
192             Layout.preferredHeight: 1080
193             clip: true
194             Image {
195                 id: albumart
196                 anchors.left: parent.left
197                 anchors.right: parent.right
198                 anchors.bottom: parent.bottom
199                 height: sourceSize.height * width / sourceSize.width
200                 fillMode: Image.PreserveAspectCrop
201                 source: player.cover_art ? player.cover_art : ''
202                 visible: bluetooth.av_connected == false
203             }
204
205             Item {
206                 anchors.left: parent.left
207                 anchors.right: parent.right
208                 anchors.bottom: parent.bottom
209                 height :307
210                 Rectangle {
211                     anchors.fill: parent
212                     color: 'black'
213                     opacity: 0.75
214                 }
215
216                 ColumnLayout {
217                     anchors.fill: parent
218                     anchors.margins: root.width * 0.02
219                     Item {
220                         Layout.fillWidth: true
221                         Layout.fillHeight: true
222                         Row {
223                             spacing: 20
224                             //ToggleButton {
225                             //    id: random
226                             //    visible: bluetooth.connected == false
227                             //    offImage: './images/AGL_MediaPlayer_Shuffle_Inactive.svg'
228                             //    onImage: './images/AGL_MediaPlayer_Shuffle_Active.svg'
229                             //}
230                             ToggleButton {
231                                 id: loop
232                                 visible: bluetooth.connected == false
233                                 //checked: player.loop_state
234                                 offImage: './images/AGL_MediaPlayer_Loop_Inactive.svg'
235                                 onImage: './images/AGL_MediaPlayer_Loop_Active.svg'
236                                 onClicked: { mediaplayer.loop(checked) }
237                             }
238                         }
239                         ColumnLayout {
240                             anchors.fill: parent
241                             Label {
242                                 id: title
243                                 Layout.alignment: Layout.Center
244                                 text: bluetooth.av_connected ? bluetooth.title : (player.title ? player.title : '')
245                                 horizontalAlignment: Label.AlignHCenter
246                                 verticalAlignment: Label.AlignVCenter
247                             }
248                             Label {
249                                 Layout.alignment: Layout.Center
250                                 text: bluetooth.av_connected ? bluetooth.artist : (player.artist ? player.artist : '')
251                                 horizontalAlignment: Label.AlignHCenter
252                                 verticalAlignment: Label.AlignVCenter
253                                 font.pixelSize: title.font.pixelSize * 0.6
254                             }
255                         }
256                     }
257                     Slider {
258                         id: slider
259                         Layout.fillWidth: true
260                         to: bluetooth.av_connected ? bluetooth.duration : player.duration
261                         enabled: bluetooth.av_connected == false
262                         value: bluetooth.av_connected ? bluetooth.position : player.position
263                         function getPosition() {
264                             if (bluetooth.av_connected) {
265                                 return player.time2str(bluetooth.position)
266                             }
267                             return player.time2str(player.position)
268                         }
269                         Label {
270                             id: position
271                             anchors.left: parent.left
272                             anchors.bottom: parent.top
273                             font.pixelSize: 32
274                             text: slider.getPosition()
275                         }
276                         Label {
277                             id: duration
278                             anchors.right: parent.right
279                             anchors.bottom: parent.top
280                             font.pixelSize: 32
281                             text: bluetooth.av_connected ? player.time2str(bluetooth.duration) : player.time2str(player.duration)
282                         }
283                         onPressedChanged: mediaplayer.seek(value)
284                     }
285                     RowLayout {
286                         Layout.fillHeight: true
287 //                        Image {
288 //                            source: './images/AGL_MediaPlayer_Playlist_Inactive.svg'
289 //                        }
290 //                        Image {
291 //                            source: './images/AGL_MediaPlayer_CD_Inactive.svg'
292 //                        }
293                         Item { Layout.fillWidth: true }
294                         ImageButton {
295                             id: previous
296                             offImage: './images/AGL_MediaPlayer_BackArrow.svg'
297                             onClicked: {
298                                 if (bluetooth.av_connected) {
299                                     bluetooth.set_avrcp_controls("Previous")
300                                     bluetooth.position = 0
301                                 } else {
302                                     mediaplayer.previous()
303                                 }
304                             }
305                         }
306                         ImageButton {
307                             id: play
308                             offImage: './images/AGL_MediaPlayer_Player_Play.svg'
309                             onClicked: {
310                                 if (bluetooth.av_connected) {
311                                     bluetooth.set_avrcp_controls("Play")
312                                 } else {
313                                     mediaplayer.play()
314                                 }
315                             }
316                             states: [
317                                 State {
318                                     when: player.status == "playing"
319                                     PropertyChanges {
320                                         target: play
321                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
322                                         onClicked: {
323                                             player.status = ""
324                                             mediaplayer.pause()
325                                         }
326                                     }
327                                 },
328                                 State {
329                                     when: bluetooth.av_connected && bluetooth.state == "playing"
330                                     PropertyChanges {
331                                         target: play
332                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
333                                         onClicked: bluetooth.set_avrcp_controls("Pause")
334                                     }
335                                 }
336
337                             ]
338                         }
339                         ImageButton {
340                             id: forward
341                             offImage: './images/AGL_MediaPlayer_ForwardArrow.svg'
342                             onClicked: {
343                                 if (bluetooth.av_connected) {
344                                     bluetooth.set_avrcp_controls("Next")
345                                 } else {
346                                     mediaplayer.next()
347                                 }
348                             }
349                         }
350
351                         Item { Layout.fillWidth: true }
352  
353                         ToggleButton {
354                               visible: bluetooth.connected
355                               checked: bluetooth.av_connected
356                               offImage: './images/AGL_MediaPlayer_Bluetooth_Inactive.svg'
357                               onImage: './images/AGL_MediaPlayer_Bluetooth_Active.svg'
358
359                               onClicked: {
360                                   if (bluetooth.av_connected) {
361                                       bluetooth.disconnect_profiles()
362                                   } else {
363                                       bluetooth.connect_profiles()
364                                   }
365                               }
366                         }
367                     }
368                 }
369             }
370         }
371         Item {
372             Layout.fillWidth: true
373             Layout.fillHeight: true
374             Layout.preferredHeight: 407
375
376             ListView {
377                 anchors.fill: parent
378                 id: playlistview
379                 visible: bluetooth.av_connected == false
380                 clip: true
381                 header: Label {
382                     x: 50
383                     text: 'PLAYLIST'
384                     opacity: 0.5
385                 }
386                 model: playlist_model
387                 currentIndex: -1
388
389                 delegate: MouseArea {
390                     id: delegate
391                     width: ListView.view.width
392                     height: ListView.view.height / 4
393                     RowLayout {
394                         anchors.fill: parent
395                         anchors.leftMargin: 50
396                         anchors.rightMargin: 50
397                         ColumnLayout {
398                             Layout.fillWidth: true
399                             Label {
400                                 Layout.fillWidth: true
401                                 text: model.title
402                             }
403                             Label {
404                                 Layout.fillWidth: true
405                                 text: model.artist
406                                 color: '#00ADDC'
407                                 font.pixelSize: 32
408                             }
409                         }
410                         //Label {
411                         //    text: player.time2str(model.duration)
412                         //    color: '#00ADDC'
413                         //    font.pixelSize: 32
414                         //}
415                     }
416                     onClicked: {
417                         mediaplayer.picktrack(playlistview.model.get(index).index)
418                     }
419                 }
420
421                 highlight: Rectangle {
422                     color: 'white'
423                     opacity: 0.25
424                 }
425             }
426         }
427     }
428 }