[sound] Fix double firing of SoundModeChangeCallback 21/180821/2
authorRafal Walczyna <r.walczyna@partner.samsung.com>
Mon, 4 Jun 2018 12:14:29 +0000 (14:14 +0200)
committerRafal Walczyna <r.walczyna@partner.samsung.com>
Tue, 12 Jun 2018 06:16:55 +0000 (08:16 +0200)
Because of native functions logic callback was firing twice,
sometimes with wrong (transitional) value

[verification] TCT Auto + Manual 100% passrate

Change-Id: I18bab460ecc16e39817318d76461ebe63a65d0bf
Signed-off-by: Rafal Walczyna <r.walczyna@partner.samsung.com>
src/sound/sound_api.js
src/sound/sound_instance.cc
src/sound/sound_instance.h

index 10cfc85..0eb8244 100644 (file)
@@ -136,9 +136,22 @@ SoundManager.prototype.getVolume = function(type) {
 };
 
 var _soundModeChangeListener;
+var _currentSoundMode;
+var _isFirstSoundModeChange = true;
 
+// Native side sometimes fires sound change callback two times in a row
+// with different values of sound mode. One of this value is only transitional
+// value caused by hazard of two values which should change simultaneously.
+// By waiting whether second callback would fire we bypass this problem.
 function _soundModeChangeListenerCallback(result) {
-  native_.callIfPossible(_soundModeChangeListener, native_.getResultObject(result));
+  _currentSoundMode = result;
+  if (_isFirstSoundModeChange) {
+    _isFirstSoundModeChange = false;
+    setTimeout(function () {
+      _isFirstSoundModeChange = true;
+      native_.callIfPossible(_soundModeChangeListener, native_.getResultObject(_currentSoundMode));
+    }, 100);
+  }
 }
 
 SoundManager.prototype.setSoundModeChangeListener = function(callback) {
index 89e0b86..c45e7d6 100644 (file)
@@ -132,6 +132,11 @@ void SoundInstance::SoundManagerUnsetSoundModeChangeListener(const picojson::val
 
 void SoundInstance::OnSoundModeChange(const std::string& newmode) {
   ScopeLogger();
+  if (current_sound_mode == newmode) {
+    LoggerD("New sound mode equals to current sound mode");
+    return;
+  }
+  current_sound_mode = newmode;
   picojson::value event = picojson::value(picojson::object());
   picojson::object& obj = event.get<picojson::object>();
   picojson::value result = picojson::value(newmode);
index 32a4c00..a800cf3 100644 (file)
@@ -45,6 +45,9 @@ class SoundInstance : public common::ParsedInstance, public SoundManagerSoundMod
   void OnSoundModeChange(const std::string& newmode);
 
   SoundManager manager_;
+
+  // It is used in OnSoundModeChange function to prevent double firing of callback
+  std::string current_sound_mode;
 };
 
 }  // namespace sound