diff options
author | Sergei Kobec <s.kobec@samsung.com> | 2017-03-23 13:48:24 +0200 |
---|---|---|
committer | Sergei Kobec <s.kobec@samsung.com> | 2017-03-23 15:42:55 +0200 |
commit | 9b71aef15cf0997e505cad42942537ef457ec991 (patch) | |
tree | c75022050e06d55630520c31a34d3c92db0633cf | |
parent | 35338e9a5be1d3bcbc5801617c9403e08f243adc (diff) | |
download | alarm-9b71aef15cf0997e505cad42942537ef457ec991.tar.gz alarm-9b71aef15cf0997e505cad42942537ef457ec991.tar.bz2 alarm-9b71aef15cf0997e505cad42942537ef457ec991.zip |
TizenRefApp-8209 Implement handling of alert during playing music
Change-Id: Iede3de898df898d1d76abc1959a5bdf675431c0e
Signed-off-by: Sergei Kobec <s.kobec@samsung.com>
-rw-r--r-- | alarm-app/inc/Alert/AlertView.h | 4 | ||||
-rw-r--r-- | alarm-app/src/Alert/AlertView.cpp | 11 | ||||
-rw-r--r-- | lib-common/inc/Common/SoundManager.h | 115 | ||||
-rw-r--r-- | lib-common/src/Common/SoundManager.cpp | 96 |
4 files changed, 218 insertions, 8 deletions
diff --git a/alarm-app/inc/Alert/AlertView.h b/alarm-app/inc/Alert/AlertView.h index bb2bd1a..7bfdd12 100644 --- a/alarm-app/inc/Alert/AlertView.h +++ b/alarm-app/inc/Alert/AlertView.h @@ -18,6 +18,7 @@ #define ALERT_ALERT_VIEW_H #include "Common/Model/Alarm.h" +#include "Common/SoundManager.h" #include "Ui/View.h" #include <efl_extension.h> @@ -65,8 +66,9 @@ namespace Alert Evas_Object *m_DismissButton; Evas_Object *m_SnoozeButton; Ecore_Timer *m_Timer; - player_h m_Player; Ecore_Timer *m_FeedbackTimer; + player_h m_Player; + Common::SoundManager m_SoundManager; Common::Model::Alarm m_Alarm; }; } diff --git a/alarm-app/src/Alert/AlertView.cpp b/alarm-app/src/Alert/AlertView.cpp index c73c66d..27a23dd 100644 --- a/alarm-app/src/Alert/AlertView.cpp +++ b/alarm-app/src/Alert/AlertView.cpp @@ -38,9 +38,12 @@ using namespace Common::Model; AlertView::AlertView(Common::Model::Alarm alarm) : m_DismissButton(nullptr), m_SnoozeButton(nullptr), - m_Timer(nullptr), m_Player(createPlayer()), m_FeedbackTimer(nullptr), + m_Timer(nullptr), m_FeedbackTimer(nullptr), + m_Player(createPlayer()), m_SoundManager(SOUND_STREAM_TYPE_ALARM), m_Alarm(std::move(alarm)) { + player_set_sound_stream_info(m_Player, m_SoundManager.getStreamInfo()); + m_SoundManager.acquireFocus(); } AlertView::~AlertView() @@ -119,17 +122,11 @@ bool AlertView::onBackPressed() player_h AlertView::createPlayer() { - sound_stream_info_h streamInfo = nullptr; - sound_manager_create_stream_information(SOUND_STREAM_TYPE_ALARM, nullptr, nullptr, &streamInfo); - player_h player = nullptr; player_create(&player); player_set_looping(player, true); - player_set_sound_stream_info(player, streamInfo); player_set_uri(player, DEFAULT_SOUND_PATH); - sound_manager_destroy_stream_information(streamInfo); - int err = player_prepare(player); WARN_IF_ERR(err, "player_prepare() failed."); diff --git a/lib-common/inc/Common/SoundManager.h b/lib-common/inc/Common/SoundManager.h new file mode 100644 index 0000000..8638985 --- /dev/null +++ b/lib-common/inc/Common/SoundManager.h @@ -0,0 +1,115 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef COMMON_SOUND_MANAGER_H +#define COMMON_SOUND_MANAGER_H + +#include <functional> +#include <sound_manager.h> + +#define SOUND_STREAM_FOCUS_CHANGED_BY_NONE -1 + +namespace Common +{ + /** + * @brief Create sound manager for sound interaction with other applications. + */ + class EXPORT_API SoundManager + { + public: + /** + * @brief Media types. + */ + enum MediaType + { + MediaTypeRecorder, + MediaTypePlayer + }; + /** + * @brief Called when the focus changed. + * @param[in] mask The focus mask. + * @param[in] state The focus state. + * @param[in] reason The change reason. + * @see sound_stream_focus_mask_e + * @see sound_stream_focus_state_e + * @see sound_stream_focus_change_reason_e + */ + typedef std::function<void(sound_stream_focus_mask_e mask, sound_stream_focus_state_e state, + sound_stream_focus_change_reason_e reason)> FocusChangeCallback; + + /** + * @brief Create sound manager. + * @param[in] type Stream type. + */ + explicit SoundManager(sound_stream_type_e type); + ~SoundManager(); + SoundManager(const SoundManager &) = delete; + SoundManager &operator=(const SoundManager &) = delete; + + /** + * @brief Get stream information handle. + */ + sound_stream_info_h getStreamInfo() const; + + /** + * @brief Acquires the stream focus. + * @see releaseFocus() + */ + void acquireFocus(); + + /** + * @brief Release the stream focus. + * @pre Call acquireFocus() before calling this function. + * @see acquireFocus() + */ + void releaseFocus(); + + /** + * @brief Set focus change callback. + * @param[in] callback Recording callback. + */ + void setFocusChangeCallback(FocusChangeCallback callback); + + /** + * @brief Whether the sound is focused by a call. + * @param[in] type Media type. + * @return true if focused by call. + */ + static bool isSoundFocusedByCall(MediaType type); + + /** + * @brief Get the reason of sound focus change. + * @param[in] type Media type. + * @return sound_stream_focus_change_reason_e on success, SOUND_STREAM_FOCUS_CHANGED_BY_NONE - otherwise. + * @see sound_stream_focus_change_reason_e + */ + static int getSoundFocusChangedReason(MediaType type); + + private: + void onFocusState(sound_stream_info_h streamInfo, sound_stream_focus_mask_e focusMask, + sound_stream_focus_state_e focusState, sound_stream_focus_change_reason_e reason, + int soundBehavior, const char *extraInfo); + + sound_stream_info_h m_StreamInfo; + FocusChangeCallback m_OnFocusChanged; + + sound_stream_focus_mask_e m_FocusMask; + sound_stream_focus_state_e m_FocusState; + sound_stream_focus_change_reason_e m_ChangeReason; + }; +} + +#endif /* COMMON_SOUND_MANAGER_H */ diff --git a/lib-common/src/Common/SoundManager.cpp b/lib-common/src/Common/SoundManager.cpp new file mode 100644 index 0000000..521b9e3 --- /dev/null +++ b/lib-common/src/Common/SoundManager.cpp @@ -0,0 +1,96 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Common/SoundManager.h" +#include "Utils/Callback.h" +#include <Ecore.h> +#include <stdlib.h> + +using namespace Common; + +SoundManager::SoundManager(sound_stream_type_e type) + : m_FocusMask(SOUND_STREAM_FOCUS_FOR_BOTH) + , m_FocusState(SOUND_STREAM_FOCUS_STATE_RELEASED) + , m_ChangeReason(SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA) +{ + sound_manager_create_stream_information(type, + makeCallbackWithLastParam(&SoundManager::onFocusState), this, &m_StreamInfo); +} + +SoundManager::~SoundManager() +{ + releaseFocus(); + sound_manager_destroy_stream_information(m_StreamInfo); +} + +sound_stream_info_h SoundManager::getStreamInfo() const +{ + return m_StreamInfo; +} + +void SoundManager::acquireFocus() +{ + sound_manager_set_focus_reacquisition(m_StreamInfo, true); + sound_manager_acquire_focus(m_StreamInfo, SOUND_STREAM_FOCUS_FOR_BOTH, SOUND_BEHAVIOR_NONE, nullptr); +} + +void SoundManager::releaseFocus() +{ + sound_manager_set_focus_reacquisition(m_StreamInfo, false); + sound_manager_release_focus(m_StreamInfo, SOUND_STREAM_FOCUS_FOR_BOTH, SOUND_BEHAVIOR_NONE, nullptr); +} + +void SoundManager::setFocusChangeCallback(FocusChangeCallback callback) +{ + m_OnFocusChanged = std::move(callback); +} + +bool SoundManager::isSoundFocusedByCall(MediaType type) +{ + int reason = getSoundFocusChangedReason(type); + return ((reason == SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE) + || (reason == SOUND_STREAM_FOCUS_CHANGED_BY_CALL)); +} + +int SoundManager::getSoundFocusChangedReason(MediaType type) +{ + typedef int (*GetCurrentFocusFunc) (sound_stream_focus_change_reason_e *, int *, char **); + static GetCurrentFocusFunc getCurrentFocus[] { + /* Recorder = */ sound_manager_get_current_recording_focus, + /* Player = */ sound_manager_get_current_playback_focus, + }; + + sound_stream_focus_change_reason_e acquiredBy = SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA; + int err = getCurrentFocus[type](&acquiredBy, nullptr, nullptr); + + return (err == SOUND_MANAGER_ERROR_NONE) ? acquiredBy : SOUND_STREAM_FOCUS_CHANGED_BY_NONE; +} + +void SoundManager::onFocusState(sound_stream_info_h streamInfo, sound_stream_focus_mask_e focusMask, + sound_stream_focus_state_e focusState, sound_stream_focus_change_reason_e reason, + int soundBehavior, const char *extraInfo) +{ + m_FocusMask = focusMask; + m_FocusState = focusState; + m_ChangeReason = reason; + + ecore_main_loop_thread_safe_call_async([](void *data) { + SoundManager *manager = (SoundManager *)data; + if (manager->m_OnFocusChanged) { + manager->m_OnFocusChanged(manager->m_FocusMask, manager->m_FocusState, manager->m_ChangeReason); + } + }, this); +} |