From cb2b27a4ceb2b2dd28fd85d33e1a3f6a02dd2bee Mon Sep 17 00:00:00 2001 From: Rafal Walczyna Date: Mon, 4 Jun 2018 14:14:29 +0200 Subject: [PATCH] [sound] Fix double firing of SoundModeChangeCallback 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 --- src/sound/sound_api.js | 15 ++++++++++++++- src/sound/sound_instance.cc | 5 +++++ src/sound/sound_instance.h | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/sound/sound_api.js b/src/sound/sound_api.js index 10cfc85..0eb8244 100644 --- a/src/sound/sound_api.js +++ b/src/sound/sound_api.js @@ -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) { diff --git a/src/sound/sound_instance.cc b/src/sound/sound_instance.cc index 89e0b86..c45e7d6 100644 --- a/src/sound/sound_instance.cc +++ b/src/sound/sound_instance.cc @@ -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::value result = picojson::value(newmode); diff --git a/src/sound/sound_instance.h b/src/sound/sound_instance.h index 32a4c00..a800cf3 100644 --- a/src/sound/sound_instance.h +++ b/src/sound/sound_instance.h @@ -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 -- 2.7.4