From a6765ae76d0356436e38667cb246d8ca491464a2 Mon Sep 17 00:00:00 2001 From: Gina Yeh Date: Sat, 8 Jun 2013 23:26:01 +0800 Subject: [PATCH] Bug 872907 - Patch 4: Implement NotifyStatusChagned() and NotifyAudioManager(), r=echou, r=mrbkap --- dom/bluetooth/BluetoothA2dpManager.cpp | 66 ++++++++++++++++++++- dom/bluetooth/BluetoothA2dpManager.h | 13 +++- dom/bluetooth/BluetoothHfpManager.cpp | 27 +++++++-- dom/bluetooth/BluetoothHfpManager.h | 6 +- dom/bluetooth/BluetoothOppManager.cpp | 4 +- dom/bluetooth/BluetoothOppManager.h | 4 +- dom/bluetooth/BluetoothProfileManagerBase.h | 4 +- 7 files changed, 110 insertions(+), 14 deletions(-) diff --git a/dom/bluetooth/BluetoothA2dpManager.cpp b/dom/bluetooth/BluetoothA2dpManager.cpp index 541a9ecb25b..4414d125797 100644 --- a/dom/bluetooth/BluetoothA2dpManager.cpp +++ b/dom/bluetooth/BluetoothA2dpManager.cpp @@ -24,7 +24,7 @@ using namespace mozilla; USING_BLUETOOTH_NAMESPACE namespace { - StaticAutoPtr gBluetoothA2dpManager; + StaticRefPtr gBluetoothA2dpManager; StaticRefPtr sA2dpObserver; bool gInShutdown = false; } // anonymous namespace @@ -231,6 +231,8 @@ BluetoothA2dpManager::HandleSinkPropertyChanged(const BluetoothSignal& aSignal) // Indicates if a stream is setup to a A2DP sink on the remote device. MOZ_ASSERT(value.type() == BluetoothValue::Tbool); mConnected = value.get_bool(); + NotifyStatusChanged(); + NotifyAudioManager(); } else if (name.EqualsLiteral("Playing")) { // Indicates if a stream is active to a A2DP sink on the remote device. MOZ_ASSERT(value.type() == BluetoothValue::Tbool); @@ -278,3 +280,65 @@ BluetoothA2dpManager::HandleSinkStateChanged(SinkState aState) mSinkState = aState; } + +void +BluetoothA2dpManager::NotifyStatusChanged() +{ + MOZ_ASSERT(NS_IsMainThread()); + + NS_NAMED_LITERAL_STRING(type, BLUETOOTH_A2DP_STATUS_CHANGED); + InfallibleTArray parameters; + + BluetoothValue v = mConnected; + parameters.AppendElement( + BluetoothNamedValue(NS_LITERAL_STRING("connected"), v)); + + v = mDeviceAddress; + parameters.AppendElement( + BluetoothNamedValue(NS_LITERAL_STRING("address"), v)); + + if (!BroadcastSystemMessage(type, parameters)) { + NS_WARNING("Failed to broadcast system message to settings"); + return; + } +} + +void +BluetoothA2dpManager::NotifyAudioManager() +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsCOMPtr obs = + do_GetService("@mozilla.org/observer-service;1"); + NS_ENSURE_TRUE_VOID(obs); + + nsAutoString data; + data.AppendInt(mConnected); + + if (NS_FAILED(obs->NotifyObservers(this, + BLUETOOTH_A2DP_STATUS_CHANGED, + data.BeginReading()))) { + NS_WARNING("Failed to notify bluetooth-a2dp-status-changed observsers!"); + } +} + +void +BluetoothA2dpManager::OnGetServiceChannel(const nsAString& aDeviceAddress, + const nsAString& aServiceUuid, + int aChannel) +{ +} + +void +BluetoothA2dpManager::OnUpdateSdpRecords(const nsAString& aDeviceAddress) +{ +} + +void +BluetoothA2dpManager::GetAddress(nsAString& aDeviceAddress) +{ + aDeviceAddress = mDeviceAddress; +} + +NS_IMPL_ISUPPORTS0(BluetoothA2dpManager) + diff --git a/dom/bluetooth/BluetoothA2dpManager.h b/dom/bluetooth/BluetoothA2dpManager.h index 09fbcf8e5c1..d8fab1c2b35 100644 --- a/dom/bluetooth/BluetoothA2dpManager.h +++ b/dom/bluetooth/BluetoothA2dpManager.h @@ -8,6 +8,7 @@ #define mozilla_dom_bluetooth_bluetootha2dpmanager_h__ #include "BluetoothCommon.h" +#include "BluetoothProfileManagerBase.h" BEGIN_BLUETOOTH_NAMESPACE @@ -23,9 +24,11 @@ class BluetoothA2dpManagerObserver; class BluetoothValue; class BluetoothSocket; -class BluetoothA2dpManager +class BluetoothA2dpManager : public BluetoothProfileManagerBase { public: + NS_DECL_ISUPPORTS + static BluetoothA2dpManager* Get(); ~BluetoothA2dpManager(); @@ -34,6 +37,11 @@ public: void HandleSinkPropertyChanged(const BluetoothSignal& aSignal); nsresult HandleShutdown(); + virtual void OnGetServiceChannel(const nsAString& aDeviceAddress, + const nsAString& aServiceUuid, + int aChannel) MOZ_OVERRIDE; + virtual void GetAddress(nsAString& aDeviceAddress) MOZ_OVERRIDE; + private: BluetoothA2dpManager(); bool Init(); @@ -41,6 +49,9 @@ private: void HandleSinkStateChanged(SinkState aState); + void NotifyStatusChanged(); + void NotifyAudioManager(); + bool mConnected; bool mPlaying; nsString mDeviceAddress; diff --git a/dom/bluetooth/BluetoothHfpManager.cpp b/dom/bluetooth/BluetoothHfpManager.cpp index 5ad87efb33c..debd4613f0a 100644 --- a/dom/bluetooth/BluetoothHfpManager.cpp +++ b/dom/bluetooth/BluetoothHfpManager.cpp @@ -8,6 +8,7 @@ #include "BluetoothHfpManager.h" +#include "BluetoothA2dpManager.h" #include "BluetoothReplyRunnable.h" #include "BluetoothService.h" #include "BluetoothSocket.h" @@ -62,7 +63,7 @@ using namespace mozilla::ipc; USING_BLUETOOTH_NAMESPACE namespace { - StaticAutoPtr gBluetoothHfpManager; + StaticRefPtr gBluetoothHfpManager; StaticRefPtr sHfpObserver; bool gInShutdown = false; static const char kHfpCrlf[] = "\xd\xa"; @@ -526,7 +527,7 @@ BluetoothHfpManager::NotifyDialer(const nsAString& aCommand) } void -BluetoothHfpManager::NotifyAudioManager(const nsAString& aAddress) +BluetoothHfpManager::NotifyAudioManager(bool aStatus) { MOZ_ASSERT(NS_IsMainThread()); @@ -534,9 +535,12 @@ BluetoothHfpManager::NotifyAudioManager(const nsAString& aAddress) do_GetService("@mozilla.org/observer-service;1"); NS_ENSURE_TRUE_VOID(obs); - if (NS_FAILED(obs->NotifyObservers(nullptr, + nsAutoString data; + data.AppendInt(aStatus); + + if (NS_FAILED(obs->NotifyObservers(this, BLUETOOTH_SCO_STATUS_CHANGED, - aAddress.BeginReading()))) { + data.BeginReading()))) { NS_WARNING("Failed to notify bluetooth-sco-status-changed observsers!"); } } @@ -1114,6 +1118,10 @@ BluetoothHfpManager::Disconnect() mSocket->Disconnect(); mSocket = nullptr; } + + BluetoothA2dpManager* a2dp = BluetoothA2dpManager::Get(); + NS_ENSURE_TRUE_VOID(a2dp); + a2dp->Disconnect(); } bool @@ -1495,6 +1503,10 @@ BluetoothHfpManager::OnConnectSuccess(BluetoothSocket* aSocket) NotifyStatusChanged(NS_LITERAL_STRING("bluetooth-hfp-status-changed")); ListenSco(); + + BluetoothA2dpManager* a2dp = BluetoothA2dpManager::Get(); + NS_ENSURE_TRUE_VOID(a2dp); + a2dp->Connect(mDeviceAddress); } void @@ -1623,7 +1635,7 @@ BluetoothHfpManager::OnScoConnectSuccess() mScoRunnable = nullptr; } - NotifyAudioManager(mDeviceAddress); + NotifyAudioManager(true); NotifyStatusChanged(NS_LITERAL_STRING("bluetooth-sco-status-changed")); mScoSocketStatus = mScoSocket->GetConnectionStatus(); @@ -1647,7 +1659,7 @@ BluetoothHfpManager::OnScoDisconnect() { if (mScoSocketStatus == SocketConnectionStatus::SOCKET_CONNECTED) { ListenSco(); - NotifyAudioManager(EmptyString()); + NotifyAudioManager(false); NotifyStatusChanged(NS_LITERAL_STRING("bluetooth-sco-status-changed")); } } @@ -1752,3 +1764,6 @@ BluetoothHfpManager::IsScoConnected() } return false; } + +NS_IMPL_ISUPPORTS0(BluetoothHfpManager) + diff --git a/dom/bluetooth/BluetoothHfpManager.h b/dom/bluetooth/BluetoothHfpManager.h index 00aca4214fc..c5a5a75a910 100644 --- a/dom/bluetooth/BluetoothHfpManager.h +++ b/dom/bluetooth/BluetoothHfpManager.h @@ -55,6 +55,8 @@ class BluetoothHfpManager : public BluetoothSocketObserver , public BluetoothProfileManagerBase { public: + NS_DECL_ISUPPORTS + static BluetoothHfpManager* Get(); ~BluetoothHfpManager(); @@ -68,6 +70,7 @@ public: const nsAString& aServiceUuid, int aChannel) MOZ_OVERRIDE; virtual void OnUpdateSdpRecords(const nsAString& aDeviceAddress) MOZ_OVERRIDE; + virtual void GetAddress(nsAString& aDeviceAddress) MOZ_OVERRIDE; void Connect(const nsAString& aDeviceAddress, const bool aIsHandsfree, @@ -87,7 +90,6 @@ public: bool IsConnected(); bool IsScoConnected(); - void GetAddress(nsAString& aDeviceAddress); private: class GetVolumeTask; @@ -114,7 +116,7 @@ private: void NotifyDialer(const nsAString& aCommand); void NotifyStatusChanged(const nsAString& aType); - void NotifyAudioManager(const nsAString& aAddress); + void NotifyAudioManager(bool aStatus); bool SendCommand(const char* aCommand, uint32_t aValue = 0); bool SendLine(const char* aMessage); diff --git a/dom/bluetooth/BluetoothOppManager.cpp b/dom/bluetooth/BluetoothOppManager.cpp index 20e5d7bc7f6..a8ef8f4f6b4 100644 --- a/dom/bluetooth/BluetoothOppManager.cpp +++ b/dom/bluetooth/BluetoothOppManager.cpp @@ -86,7 +86,7 @@ static const uint32_t kUpdateProgressBase = 50 * 1024; */ static const uint32_t kPutRequestHeaderSize = 6; -StaticAutoPtr sInstance; +StaticRefPtr sInstance; /* * FIXME / Bug 806749 @@ -1508,6 +1508,8 @@ BluetoothOppManager::OnUpdateSdpRecords(const nsAString& aDeviceAddress) } } +NS_IMPL_ISUPPORTS0(BluetoothOppManager) + bool BluetoothOppManager::AcquireSdcardMountLock() { diff --git a/dom/bluetooth/BluetoothOppManager.h b/dom/bluetooth/BluetoothOppManager.h index 3119a358b2a..3129c882291 100644 --- a/dom/bluetooth/BluetoothOppManager.h +++ b/dom/bluetooth/BluetoothOppManager.h @@ -29,6 +29,8 @@ class BluetoothOppManager : public BluetoothSocketObserver , public BluetoothProfileManagerBase { public: + NS_DECL_ISUPPORTS + /* * Channel of reserved services are fixed values, please check * function add_reserved_service_records() in @@ -76,7 +78,6 @@ public: // Return true if there is an ongoing file-transfer session, please see // Bug 827267 for more information. bool IsTransferring(); - void GetAddress(nsAString& aDeviceAddress); // Implement interface BluetoothSocketObserver void ReceiveSocketData( @@ -90,6 +91,7 @@ public: const nsAString& aServiceUuid, int aChannel) MOZ_OVERRIDE; virtual void OnUpdateSdpRecords(const nsAString& aDeviceAddress) MOZ_OVERRIDE; + virtual void GetAddress(nsAString& aDeviceAddress) MOZ_OVERRIDE; private: BluetoothOppManager(); diff --git a/dom/bluetooth/BluetoothProfileManagerBase.h b/dom/bluetooth/BluetoothProfileManagerBase.h index d4db51ca362..0e3d5fd027c 100644 --- a/dom/bluetooth/BluetoothProfileManagerBase.h +++ b/dom/bluetooth/BluetoothProfileManagerBase.h @@ -13,14 +13,14 @@ BEGIN_BLUETOOTH_NAMESPACE -class BluetoothProfileManagerBase +class BluetoothProfileManagerBase : public nsISupports { public: virtual void OnGetServiceChannel(const nsAString& aDeviceAddress, const nsAString& aServiceUuid, int aChannel) = 0; - virtual void OnUpdateSdpRecords(const nsAString& aDeviceAddress) = 0; + virtual void GetAddress(nsAString& aDeviceAddress) = 0; }; END_BLUETOOTH_NAMESPACE