From 04ae48a26ece6b6e21010f3d187f173837f99c23 Mon Sep 17 00:00:00 2001
From: Matt Ranostay <matt.ranostay@konsulko.com>
Date: Wed, 13 Jun 2018 23:12:06 -0700
Subject: mediaplayer: fix race condition with local media and bluetooth a2dp

If media from storage is played and a bluetooth stream starts the play/pause
button will enter an always playing state from the UI when the bluetooth
connection ends.

This patchset fixes this within the MediaPlayer.qml to show the expected
UI results.

Bug-AGL: SPEC-1496
Change-Id: I6ee0dfa2a46d7cf294b2dbf23d2aa54857fb8ffc
Signed-off-by: Matt Ranostay <matt.ranostay@konsulko.com>
---
 app/MediaPlayer.qml | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/app/MediaPlayer.qml b/app/MediaPlayer.qml
index a0a1455..7dfaf06 100644
--- a/app/MediaPlayer.qml
+++ b/app/MediaPlayer.qml
@@ -33,7 +33,7 @@ ApplicationWindow {
         property int position: 0
 
         property string cover_art: ""
-        property string status: ""
+        property string status: "stopped"
 
         function time2str(value) {
             return Qt.formatTime(new Date(value), 'mm:ss')
@@ -104,8 +104,10 @@ ApplicationWindow {
             if (data.Connected == "False")
                 return
 
-            if (!bluetooth.av_connected && data.AVPConnected == "True")
+            if (!bluetooth.av_connected && data.AVPConnected == "True") {
                 mediaplayer.pause()
+                player.status = "stopped"
+            }
 
             bluetooth.connected = data.Connected == "True"
             bluetooth.av_connected = data.AVPConnected == "True"
@@ -305,22 +307,14 @@ ApplicationWindow {
                         }
                         ImageButton {
                             id: play
-                            offImage: './images/AGL_MediaPlayer_Player_Play.svg'
-                            onClicked: {
-                                if (bluetooth.av_connected) {
-                                    bluetooth.set_avrcp_controls("Play")
-                                } else {
-                                    mediaplayer.play()
-                                }
-                            }
                             states: [
                                 State {
-                                    when: player.status == "playing"
+                                    when: !bluetooth.av_connected && player.status == "playing"
                                     PropertyChanges {
                                         target: play
                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
                                         onClicked: {
-                                            player.status = ""
+                                            player.status = "stopped"
                                             mediaplayer.pause()
                                         }
                                     }
@@ -332,8 +326,23 @@ ApplicationWindow {
                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
                                         onClicked: bluetooth.set_avrcp_controls("Pause")
                                     }
+                                },
+                                State {
+                                    when: !bluetooth.av_connected && player.status != "playing"
+                                    PropertyChanges {
+                                        target: play
+                                        offImage: './images/AGL_MediaPlayer_Player_Play.svg'
+                                        onClicked: mediaplayer.play()
+                                    }
+                                },
+                                State {
+                                    when: bluetooth.av_connected && bluetooth.state != "playing"
+                                    PropertyChanges {
+                                        target: play
+                                        offImage: './images/AGL_MediaPlayer_Player_Play.svg'
+                                        onClicked: bluetooth.set_avrcp_controls("Play")
+                                    }
                                 }
-
                             ]
                         }
                         ImageButton {
-- 
cgit