Bug 792002 - patch 2: adjust volume after receiving AT+VGS, r=qdot, r=fabrice
authorEric Chou <echou@mozilla.com>
Thu, 27 Sep 2012 10:24:39 +0800
changeset 108224 d76eac692a190f0a7cd83f81a9b622a0f8861fcd
parent 108223 355e9f8b0c67b1db68184f07f982158e386730a1
child 108225 978c67b9efa1b32f921b889581d1a215989fd5bc
push id15445
push userechou@mozilla.com
push dateThu, 27 Sep 2012 02:25:18 +0000
treeherdermozilla-inbound@d76eac692a19 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersqdot, fabrice
bugs792002
milestone18.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 792002 - patch 2: adjust volume after receiving AT+VGS, r=qdot, r=fabrice
b2g/chrome/content/shell.js
dom/bluetooth/BluetoothHfpManager.cpp
dom/bluetooth/BluetoothHfpManager.h
--- a/b2g/chrome/content/shell.js
+++ b/b2g/chrome/content/shell.js
@@ -435,16 +435,26 @@ Services.obs.addObserver(function(aSubje
   shell.sendChromeEvent({ type: "fullscreenoriginchange",
                           fullscreenorigin: aData });
 }, "fullscreen-origin-change", false);
 
 Services.obs.addObserver(function onWebappsReady(subject, topic, data) {
   shell.sendChromeEvent({ type: 'webapps-registry-ready' });
 }, 'webapps-registry-ready', false);
 
+Services.obs.addObserver(function onBluetoothVolumeChange(subject, topic, data) {
+  if (data == 'up') {
+    shell.sendChromeEvent({ type: 'volume-up-button-press' });
+    shell.sendChromeEvent({ type: 'volume-up-button-release' });
+  } else if (data == 'down') {
+    shell.sendChromeEvent({ type: 'volume-down-button-press' });
+    shell.sendChromeEvent({ type: 'volume-down-button-release' });
+  }
+}, 'bluetooth-volume-change', false);
+
 (function Repl() {
   if (!Services.prefs.getBoolPref('b2g.remote-js.enabled')) {
     return;
   }
   const prompt = 'JS> ';
   let output;
   let reader = {
     onInputStreamReady : function repl_readInput(input) {
--- a/dom/bluetooth/BluetoothHfpManager.cpp
+++ b/dom/bluetooth/BluetoothHfpManager.cpp
@@ -5,22 +5,25 @@
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "BluetoothHfpManager.h"
 
 #include "BluetoothReplyRunnable.h"
 #include "BluetoothService.h"
 #include "BluetoothServiceUuid.h"
 
+#include "mozilla/Services.h"
+#include "nsIObserverService.h"
+
 USING_BLUETOOTH_NAMESPACE
 using namespace mozilla::ipc;
 
 static nsRefPtr<BluetoothHfpManager> sInstance = nullptr;
 
-BluetoothHfpManager::BluetoothHfpManager()
+BluetoothHfpManager::BluetoothHfpManager() : mCurrentVgs(-1)
 {
 }
 
 BluetoothHfpManager::~BluetoothHfpManager()
 {
 }
 
 //static
@@ -74,16 +77,41 @@ BluetoothHfpManager::ReceiveSocketData(U
   } else if (!strncmp(msg, "AT+CMER=", 8)) {
     SendLine("OK");
   } else if (!strncmp(msg, "AT+CHLD=?", 9)) {
     SendLine("+CHLD: (0,1,2,3)");
     SendLine("OK");
   } else if (!strncmp(msg, "AT+CHLD=", 8)) {
     SendLine("OK");
   } else if (!strncmp(msg, "AT+VGS=", 7)) {
+    // VGS range: [0, 15]
+    int newVgs = msg[7] - '0';
+
+    if (strlen(msg) > 8) {
+      newVgs *= 10;
+      newVgs += (msg[8] - '0');
+    }
+
+#ifdef DEBUG
+    NS_ASSERTION(newVgs >= 0 && newVgs <= 15, "Received invalid VGS value");
+#endif
+
+    // Currently, we send volume up/down commands to represent that
+    // volume has been changed by Bluetooth headset, and that will affect
+    // the main stream volume of our device. In the future, we may want to
+    // be able to set volume by stream.
+    nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
+    if (newVgs > mCurrentVgs) {
+      os->NotifyObservers(nullptr, "bluetooth-volume-change", NS_LITERAL_STRING("up").get());
+    } else if (newVgs < mCurrentVgs) {
+      os->NotifyObservers(nullptr, "bluetooth-volume-change", NS_LITERAL_STRING("down").get());
+    }
+
+    mCurrentVgs = newVgs;
+
     SendLine("OK");
   } else {
 #ifdef DEBUG
     nsCString warningMsg;
     warningMsg.AssignLiteral("Not handling HFP message, reply ok: ");
     warningMsg.Append(msg);
     NS_WARNING(warningMsg.get());
 #endif
--- a/dom/bluetooth/BluetoothHfpManager.h
+++ b/dom/bluetooth/BluetoothHfpManager.h
@@ -24,13 +24,15 @@ public:
 
   bool Connect(const nsAString& aDeviceObjectPath,
                BluetoothReplyRunnable* aRunnable);
   void Disconnect();
   bool SendLine(const char* aMessage);
 
 private:
   BluetoothHfpManager();
+
+  int mCurrentVgs;
 };
 
 END_BLUETOOTH_NAMESPACE
 
 #endif