From: Caisking <3449750381@qq.com> Date: Wed, 3 Jul 2024 10:59:01 +0000 (+0800) Subject: 增加早安闹钟响铃 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=517ccba1a8d49bbafd354b2183cf587906651ea1;p=mirrors%2FFD230.git 增加早安闹钟响铃 --- diff --git a/code/sdk/application/wearable/service/msg_center/include/notify/sleep_notification.h b/code/sdk/application/wearable/service/msg_center/include/notify/sleep_notification.h index 0dac41be3..c5bb150f0 100644 --- a/code/sdk/application/wearable/service/msg_center/include/notify/sleep_notification.h +++ b/code/sdk/application/wearable/service/msg_center/include/notify/sleep_notification.h @@ -1,31 +1,62 @@ -/*** - * @Date: 2024-07-02 10:15:59 - * @LastEditors: Caisking 3449750381@qq.com - * @LastEditTime: 2024-07-02 17:27:39 - * @FilePath: /FD230/code/sdk/application/wearable/service/msg_center/include/notify/sleep_notification.h - */ -/*** - * @Date: 2024-07-02 10:15:59 - * @LastEditors: Caisking 3449750381@qq.com - * @LastEditTime: 2024-07-02 11:27:01 - * @FilePath: /FD230/code/sdk/application/wearable/service/msg_center/include/notify/sleep_notification.h - */ -/*** - * @Date: 2024-06-20 09:43:23 - * @LastEditors: Caisking 3449750381@qq.com - * @LastEditTime: 2024-06-20 09:47:45 - * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/service/msg_center/include/notify/message_notification.h - */ + #ifndef SLEEP_NOTIFICATION #define SLEEP_NOTIFICATION #include "BaseView.h" +#include "audio_manager.h" +#include "../../../../../../middleware/services/media/foundation/utils/include/source.h" +#include "../../../../../../middleware/services/media/foundation/player/interfaces/kits/player.h" namespace OHOS { UIViewGroup *CreateSleepNotifyView(void); void FreeSleepView(void); void SetAlarmClockView(); + using OHOS::Media::Player; + using OHOS::Media::Source; + using namespace OHOS::Media; + using namespace Audio; + using Audio::AudioManager; + + // 播放器 + class SleepRingPlayerListener : public InterruptListener + { + public: + SleepRingPlayerListener() {} + ~SleepRingPlayerListener() {} + + void SetOwner(std::shared_ptr player); + void RestFlag(); + bool IsPaused(); + bool IsDelayed(); + bool IsResumed(); + bool IsBeginStopped(); + bool IsEndStopped(); + void StopSleepRing(); + void StartSleepRing(); + void OnInterrupt(int32_t type, int32_t hint) override; + + private: + std::shared_ptr player_{nullptr}; + bool isPaused_{false}; + bool isDelayed_{false}; + bool isResumed_{false}; + bool isBeginStopped_{false}; + bool isEndStopped_{false}; + }; + + // 播放器回调 + class SleepRingPlayerCallbackImpl : public OHOS::Media::PlayerCallback + { + public: + SleepRingPlayerCallbackImpl() {} + ~SleepRingPlayerCallbackImpl() {} + void OnPlaybackComplete(); + void OnError(int32_t errorType, int32_t errorCode); + void OnInfo(int type, int extra); + void OnRewindToComplete(); + }; + class SleepViewEventListener : public UIView::OnTouchListener, public UIView::OnDragListener { public: @@ -33,6 +64,14 @@ namespace OHOS bool OnRelease(UIView &view, const ReleaseEvent &event) override; bool OnDrag(UIView &view, const DragEvent &event) override; bool OnDragEnd(UIView &view, const DragEvent &event) override; + void PopInterCancelMusic(void); + void PopInterPlayMusic(void); + int SleepRingPlayStop(void); + int SleepRingPlayStart(void); + int SleepRingPlayerInit(char *RingMusicName); + int SleepRingPlayerStat(void); + bool GetSleepNotifyFlag(void); + void SetSleepNotifyFlag(bool flag); private: SleepViewEventListener(); @@ -40,6 +79,15 @@ namespace OHOS SleepViewEventListener(const SleepViewEventListener &) = delete; const SleepViewEventListener &operator=(const SleepViewEventListener &) = delete; bool X_DRAG_FLAG{false}; + std::string uri; + std::shared_ptr playerCallback{nullptr}; + std::shared_ptr playerCtr{nullptr}; + std::map header; + std::shared_ptr source{nullptr}; + std::shared_ptr playerInterruptListener{nullptr}; + AudioSession sessionId; + AudioInterrupt interrupt; + char *RingMusicName = "alarm.mp3"; }; } diff --git a/code/sdk/application/wearable/service/msg_center/src/notify/sleep_notification.cpp b/code/sdk/application/wearable/service/msg_center/src/notify/sleep_notification.cpp index 72a69cdee..9f955f901 100644 --- a/code/sdk/application/wearable/service/msg_center/src/notify/sleep_notification.cpp +++ b/code/sdk/application/wearable/service/msg_center/src/notify/sleep_notification.cpp @@ -13,6 +13,7 @@ #include "vibrator_service.h" #include "UiConfig.h" #include "ConfigModel.h" +#include "alarm/include/AlarmClockModel.h" extern "C" void custom_VibratorFunc(OHOS::VibratorType vibratorType); namespace OHOS @@ -40,49 +41,10 @@ namespace OHOS static constexpr const char *PNG_SLEEP_BACK_IMAGE = "/user/res/Sleep/goodmorning.bin"; #define DELETEBUTTON "delete_button" +#define CLOSEBUTTON "close_button" #define NOPBUTTON "nop_button" - SleepViewEventListener::SleepViewEventListener() - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::SleepViewEventListener"); - } - - SleepViewEventListener::~SleepViewEventListener() - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::~SleepViewEventListener"); - } - - SleepViewEventListener &SleepViewEventListener::GetInstance() - { - static SleepViewEventListener instance; - return instance; - } - bool SleepViewEventListener::OnRelease(UIView &view, const ReleaseEvent &event) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::OnRelease"); - UNUSED(event); - if (strcmp(view.GetViewId(), DELETEBUTTON) == 0) - { - NotificationManager::GetInstance()->StopNotify(); - } - return true; - } - bool SleepViewEventListener::OnDrag(UIView &view, const DragEvent &event) - { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::OnDrag"); - UNUSED(view); - return true; - } - - bool SleepViewEventListener::OnDragEnd(UIView &view, const DragEvent &event) - { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::OnDragEnd"); - if (event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT) - { - NotificationManager::GetInstance()->StopNotify(); - } - return true; - } + /* -------------------------------------------------------------------------- */ /*创建信息弹窗*/ UIViewGroup *CreateSleepNotifyView(void) @@ -124,6 +86,8 @@ namespace OHOS textId = STR_MORNNING; ConfigModel::GetInstance()->SetDnotdisturb(false); // 关闭勿扰 SetAlarmClockView(); // 设置为早安视图 + SleepViewEventListener::GetInstance().PopInterPlayMusic(); + NotificationManager::GetInstance()->SetNotifyPriority(PRIORITY_TWO); return SleepGroupView; break; default: @@ -138,7 +102,7 @@ namespace OHOS WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sleeptitle:: new UILabel fail"); return nullptr; // 内存分配失败,返回 } - sleeptitle->SetPosition(83, 60, 300, 35); + sleeptitle->SetPosition(83, 60, 300, 45); sleeptitle->SetText(titleStr.c_str()); sleeptitle->SetStyle(STYLE_TEXT_FONT, 30); sleeptitle->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); @@ -189,6 +153,8 @@ namespace OHOS custom_VibratorFunc(VibratorType::VIBRATOR_TYPE_TWO); // 震动提醒 return SleepGroupView; } + + // 设置早安视图 void SetAlarmClockView() { /*初始化图片*/ @@ -196,7 +162,7 @@ namespace OHOS InitImageView(sunnyicon, 153, 95, 160, 160, "image", PNG_SLEEP_SUUNY_IMAGE); InitImageView(alarmicon, 149, 260, 29, 29, "image", PNG_SLEEP_ALARM_IMAGE); /*初始化固定label*/ - InitLabelExt(alarmtitle, 83, 50, 300, 35, "labletitle", STR_MORNNING, DEFAULT_VECTOR_FONT_FILENAME, 30); + InitLabelExt(alarmtitle, 83, 50, 300, 45, "labletitle", STR_MORNNING, DEFAULT_VECTOR_FONT_FILENAME, 30); InitLabelExt(nowlabel, 110, 243, 200, 60, "labletitle", STR_NOW, DEFAULT_VECTOR_FONT_FILENAME, 30); /*获取起床时间labe*/ std::string walkStr = SleepModel::GetInstance().get_walk_notifiy(); @@ -207,12 +173,13 @@ namespace OHOS nopbutton->SetStyle(STYLE_BACKGROUND_COLOR, 0xff4fc34c); nopbutton->SetStyle(STYLE_BORDER_RADIUS, 100); - InitButtonExt(closebutton, 238, 320, 170, 90, DELETEBUTTON, STR_CLOSE, 30); + InitButtonExt(closebutton, 238, 320, 170, 90, CLOSEBUTTON, STR_CLOSE, 30); closebutton->SetOnTouchListener(static_cast(&SleepViewEventListener::GetInstance())); closebutton->SetStyle(STYLE_BORDER_RADIUS, 100); /* -------------------------------------------------------------------------- */ SleepGroupView->Add(backimage); + SleepGroupView->Add(alarmtitle); SleepGroupView->Add(sunnyicon); SleepGroupView->Add(alarmicon); SleepGroupView->Add(nowlabel); @@ -274,4 +241,306 @@ namespace OHOS } return; } + + /* -------------------------------------------------------------------------- */ + // Presenter + SleepViewEventListener::SleepViewEventListener() + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::SleepViewEventListener"); + } + + SleepViewEventListener::~SleepViewEventListener() + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::~SleepViewEventListener"); + } + + SleepViewEventListener &SleepViewEventListener::GetInstance() + { + static SleepViewEventListener instance; + return instance; + } + bool SleepViewEventListener::OnRelease(UIView &view, const ReleaseEvent &event) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::OnRelease"); + UNUSED(event); + if (strcmp(view.GetViewId(), DELETEBUTTON) == 0) + { + NotificationManager::GetInstance()->StopNotify(); + } + if (strcmp(view.GetViewId(), CLOSEBUTTON) == 0) + { + SleepRingPlayStop(); + // SetSleepNotifyFlag(false); + NotificationManager::GetInstance()->StopNotify(); + } + return true; + } + bool SleepViewEventListener::OnDrag(UIView &view, const DragEvent &event) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::OnDrag"); + UNUSED(view); + return true; + } + + bool SleepViewEventListener::OnDragEnd(UIView &view, const DragEvent &event) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::OnDragEnd"); + if (event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT) + { + SleepRingPlayStop(); + // SetSleepNotifyFlag(false); + NotificationManager::GetInstance()->StopNotify(); + } + return true; + } + /* + * @brief 初始化闹铃播放铃声的播放器 + * @param {char*} RingMusicName 铃声文件 + */ + int SleepViewEventListener::SleepRingPlayerInit(char *RingMusicName) + { + // 本地播放文件路径和状态 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RingPlayerInit start!"); + std::string strSrcMusic("/music/ring"); + std::string tempStr("/"); + std::string strName = RingMusicName; + uri = strSrcMusic + tempStr + strName; + // 创建 AM + playerCtr = std::make_shared(); + if (playerCtr.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "playerCtr new fail"); + return OHOS_FAILURE; + } + playerCallback = std::make_shared(); + if (playerCallback.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "playerCallback new fail"); + return OHOS_FAILURE; + } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RingPlayerInit end!"); + return OHOS_SUCCESS; + } + + /* + * @brief 准备并播放铃声 + */ + int SleepViewEventListener::SleepRingPlayerStat(void) + { + // 检查播放器实例 + int32_t ret; + int32_t state; + if (playerCtr.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::SleepRingPlayerStat playerCtr nullptr"); + return OHOS_FAILURE; + } + playerCtr->GetPlayerState(state); + // 设置播放器回调 + playerCtr->SetPlayerCallback(playerCallback); + // 创建并设置音频源 + source = std::make_shared(uri, header); + if (source.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "source new fail"); + return OHOS_FAILURE; + } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepRing Player url:%s", uri.c_str()); + playerCtr->SetSource(*source); + // 配置播放器 + playerCtr->SetAudioStreamType(AUDIO_STREAM_ALARM_CLOCK); // 设置播放器的音频流类型为闹钟音乐。 + playerCtr->Prepare(); // 准备播放器对象以播放音频。 + // 请求音频焦点 + AudioManager &amIntance = AudioManager::GetInstance(); + sessionId = amIntance.MakeSessionId(); + if (sessionId == AUDIO_SESSION_ID_NONE) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "session invalid"); + return OHOS_FAILURE; + } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MakeSessionId %d", sessionId); + playerInterruptListener = std::make_shared(); + if (playerInterruptListener.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "playerInterruptListener new fail"); + return OHOS_FAILURE; + } + playerInterruptListener->SetOwner(playerCtr); + interrupt = {AUDIO_STREAM_ALARM_CLOCK, sessionId, playerInterruptListener}; + // 激活音频中断 5s超时 + if (amIntance.ActivateAudioInterrupt(interrupt) == INTERRUPT_FAILED) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ActivateAudioInterrupt faild"); + return OHOS_FAILURE; + } + // playerCtr->SetVolume(50, 50); // 将左声道和右声道音量都设置为50 + playerCtr->SetAudioSessionId(sessionId); + // 播放 + playerCtr->Play(); + return OHOS_SUCCESS; + } + /* + * @brief 停止闹铃铃声 + */ + int SleepViewEventListener::SleepRingPlayStop(void) + { + int32_t ret = 0; + AudioManager &amIntance = AudioManager::GetInstance(); + if (playerCtr.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::SleepRingPlayStop playerCtr nullptr"); + return OHOS_FAILURE; + } + + ret = playerCtr->Stop(); + playerCtr->Reset(); + playerCtr->Release(); + if (amIntance.DeactivateAudioInterrupt(interrupt) != 0) + { + return OHOS_FAILURE; + } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Reset, ret:%d", ret); + return OHOS_SUCCESS; + } + + /* + * @brief 开启闹铃铃声 + */ + int SleepViewEventListener::SleepRingPlayStart(void) + { + SleepRingPlayerInit(RingMusicName); + SleepRingPlayerStat(); + return OHOS_SUCCESS; + } + + /* + * @brief 停止铃声播放的事件 + */ + void SleepViewEventListener::PopInterCancelMusic(void) + { + SendMsgToUIService(SCREEN_TURN_OFF_TO_UI, 0, NULL, 0); + GraphicService::GetInstance()->PostGraphicEvent( + std::bind(&SleepViewEventListener::SleepRingPlayStop, &SleepViewEventListener::GetInstance())); + } + + /* + * @brief 开始铃声播放的事件 + */ + void SleepViewEventListener::PopInterPlayMusic(void) + { + int32_t timeout = -1; + SendMsgToUIService(SCREEN_TURN_ON_TO_UI, 0, &timeout, sizeof(int32_t)); // 亮屏操作 + // 将"开启闹铃铃声"任务发布到异步线程 + GraphicService::GetInstance()->PostGraphicEvent( + std::bind(&SleepViewEventListener::SleepRingPlayStart, &SleepViewEventListener::GetInstance())); + } + /* ----------------------- SleepRingPlayerListener ---------------------- */ + void SleepRingPlayerListener::SetOwner(std::shared_ptr player) + { + player_ = player; + } + + void SleepRingPlayerListener::RestFlag() + { + isPaused_ = false; + isDelayed_ = false; + isResumed_ = false; + isBeginStopped_ = false; + isEndStopped_ = false; + } + + bool SleepRingPlayerListener::IsPaused() + { + return isPaused_; + } + + bool SleepRingPlayerListener::IsDelayed() + { + return isDelayed_; + } + + bool SleepRingPlayerListener::IsResumed() + { + return isResumed_; + } + + bool SleepRingPlayerListener::IsBeginStopped() + { + return isBeginStopped_; + } + + bool SleepRingPlayerListener::IsEndStopped() + { + return isEndStopped_; + } + + static void Start(void) + { + SleepViewEventListener::GetInstance().PopInterPlayMusic(); + } + + static void Stop(void) + { + SleepViewEventListener::GetInstance().PopInterCancelMusic(); + } + + void SleepRingPlayerListener::StartSleepRing(void) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[SleepRingPlayerListener::StopSleepRing] start"); + GraphicService::GetInstance()->PostGraphicEvent(Start); + } + + void SleepRingPlayerListener::StopSleepRing(void) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[SleepRingPlayerListener::StopSleepRing] start"); + GraphicService::GetInstance()->PostGraphicEvent(Stop); + } + + /*响应外部的音频中断事件*/ + void SleepRingPlayerListener::OnInterrupt(int32_t type, int32_t hint) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OnInterrupt go in, type %d, hint %d.", type, hint); + if (type == INTERRUPT_TYPE_BEGIN && hint == INTERRUPT_HINT_PAUSE) + { + isDelayed_ = true; + } + + if (type == INTERRUPT_TYPE_BEGIN && hint == INTERRUPT_HINT_STOP) + { + StopSleepRing(); + } + else if (type == INTERRUPT_TYPE_END && isDelayed_) + { + // 重新开始闹铃的音乐播放 + StartSleepRing(); + isDelayed_ = false; + } + return; + } + + /* --------------------- SleepRingPlayerCallbackImpl -------------------- */ + void SleepRingPlayerCallbackImpl::OnPlaybackComplete(void) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OnPlaybackComplete"); + SleepViewEventListener::GetInstance().PopInterCancelMusic(); + SleepViewEventListener::GetInstance().PopInterPlayMusic(); + } + + void SleepRingPlayerCallbackImpl::OnError(int32_t errorType, int32_t errorCode) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OnError test, errorType %d, errorCode %d", errorType, errorCode); + // 此处实现代码处理错误事件 + } + + void SleepRingPlayerCallbackImpl::OnInfo(int type, int extra) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OnInfo test, type %d, extra %d.", type, extra); + // 此处实现代码处理普通事件 + } + + void SleepRingPlayerCallbackImpl::OnRewindToComplete(void) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OnRewindToComplete test"); + // 此处实现代码处理进度控制完成的事件 + } } \ No newline at end of file