From 0ddef1b5733364559dd1860c8dfc8b59d1873066 Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Wed, 25 Jun 2025 16:17:53 +0800 Subject: [PATCH] =?utf8?q?=E7=9D=A1=E7=9C=A0=E5=BC=B9=E7=AA=97=E4=BB=A3?= =?utf8?q?=E7=A0=81=E5=90=88=E5=B9=B6=EF=BC=8C=E5=BE=85=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../broadcast/src/broadcast_implement.c | 1 + .../fd230/service/msg_center/CMakeLists.txt | 2 + .../include/notify/sleep_notification.h | 97 +++ .../msg_center/src/msg_center_customer.cpp | 37 +- .../src/notify/sleep_notification.cpp | 677 ++++++++++++++++++ 5 files changed, 812 insertions(+), 2 deletions(-) create mode 100644 code/sdk/application/wearable/fd230/service/msg_center/include/notify/sleep_notification.h create mode 100644 code/sdk/application/wearable/fd230/service/msg_center/src/notify/sleep_notification.cpp diff --git a/code/sdk/application/wearable/fd230/service/broadcast/src/broadcast_implement.c b/code/sdk/application/wearable/fd230/service/broadcast/src/broadcast_implement.c index 9cda922ef..47e293e05 100644 --- a/code/sdk/application/wearable/fd230/service/broadcast/src/broadcast_implement.c +++ b/code/sdk/application/wearable/fd230/service/broadcast/src/broadcast_implement.c @@ -43,6 +43,7 @@ static const TopicToCore topicToCoreArray[] = { {TOPIC_EVENT_DEBUGOUTPUT, A_CORE_MASK, 1}, {TOPIC_EVENT_GOAL_SHOW_VIEW, A_CORE_MASK, 1}, {TOPIC_EVENT_DRINKREMINDER, A_CORE_MASK, 1}, + {TOPIC_EVENT_SLEEPNOTE, A_CORE_MASK, 1}, }; /* 所有的topic,其对应的位图 */ diff --git a/code/sdk/application/wearable/fd230/service/msg_center/CMakeLists.txt b/code/sdk/application/wearable/fd230/service/msg_center/CMakeLists.txt index 66b85e274..86ae19f8c 100644 --- a/code/sdk/application/wearable/fd230/service/msg_center/CMakeLists.txt +++ b/code/sdk/application/wearable/fd230/service/msg_center/CMakeLists.txt @@ -167,6 +167,7 @@ set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/notify/message_notification.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/notify/goal_notification.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/notify/drinkReminder_notification.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/notify/sleep_notification.cpp ) else() @@ -189,6 +190,7 @@ set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/notify/message_notification.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/notify/goal_notification.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/notify/drinkReminder_notification.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/notify/sleep_notification.cpp ) endif() diff --git a/code/sdk/application/wearable/fd230/service/msg_center/include/notify/sleep_notification.h b/code/sdk/application/wearable/fd230/service/msg_center/include/notify/sleep_notification.h new file mode 100644 index 000000000..edbdd97da --- /dev/null +++ b/code/sdk/application/wearable/fd230/service/msg_center/include/notify/sleep_notification.h @@ -0,0 +1,97 @@ +#ifndef SLEEP_NOTIFICATION +#define SLEEP_NOTIFICATION +#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: + static SleepViewEventListener &GetInstance(); + 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); + + void Sleep_StartTimer(uint32_t interval = 1000 * 4); + void Sleep_StopTimer(void); + + + private: + SleepViewEventListener(); + ~SleepViewEventListener(); + 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"; + }; + +} + +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/service/msg_center/src/msg_center_customer.cpp b/code/sdk/application/wearable/fd230/service/msg_center/src/msg_center_customer.cpp index fb0d3094b..c89191896 100644 --- a/code/sdk/application/wearable/fd230/service/msg_center/src/msg_center_customer.cpp +++ b/code/sdk/application/wearable/fd230/service/msg_center/src/msg_center_customer.cpp @@ -57,6 +57,8 @@ #include "message_notification.h" #include "goal_notification.h" #include "drinkReminder_notification.h" +#include "sleep_notification.h" +#include "sleep/include/SleepModel.h" #ifdef SUPPORT_POWER_MANAGER #include "power_display_service.h" @@ -91,9 +93,10 @@ const PriorityMapper PRI_MAPPER[] = { {TOPIC_EVENT_INACTIVITY_UPDATE, PRIORITY_TWO}, // #endif {TOPIC_EVENT_FIND_WATCH,PRIORITY_TWO}, // 查找找手表 - {TOPIC_EVENT_DEBUGOUTPUT,PRIORITY_ONE}, //开发调试专用窗口 + {TOPIC_EVENT_DEBUGOUTPUT,PRIORITY_ONE}, // 开发调试专用窗口 {TOPIC_EVENT_GOAL_SHOW_VIEW,PRIORITY_TWO}, // 训练目标弹窗 - {TOPIC_EVENT_DRINKREMINDER,PRIORITY_TWO}, // 训练目标弹窗 + {TOPIC_EVENT_DRINKREMINDER,PRIORITY_TWO}, // 喝水提醒弹窗 + {TOPIC_EVENT_SLEEPNOTE,PRIORITY_TWO}, // 睡眠提醒弹窗 @@ -144,6 +147,7 @@ void MsgCenterSubscribe() MsgCenterSubscribeTopic(TOPIC_EVENT_DEBUGOUTPUT); MsgCenterSubscribeTopic(TOPIC_EVENT_GOAL_SHOW_VIEW); MsgCenterSubscribeTopic(TOPIC_EVENT_DRINKREMINDER); + MsgCenterSubscribeTopic(TOPIC_EVENT_SLEEPNOTE); } @@ -236,6 +240,11 @@ static OHOS::UIViewGroup* showDrinkReminderNotify(void) { return OHOS::CreateDrinkReminderView(); } +/*显示睡眠提醒*/ +static OHOS::UIViewGroup* showSleepNotify(void) +{ + return OHOS::CreateSleepNotifyView(); +} void MsgCenterNotifyAlarmProc(const Topic topic, Request* req) { @@ -707,6 +716,29 @@ void MsgCenterNotifyDrinkReminderProc(const Topic topic, Request *req) NotificationManager::GetInstance()->ShowNotify(showDrinkReminderNotify, (uint16_t)topic, true); } } +//睡眠提醒通知 +void MsgCenterNotifySleepProc(const Topic topic, Request* req) +{ + unused(req); + // unused(topic); + if (CheckPriority(topic)) + { + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "MsgCenterNotifySleepProc %d!",SleepModel::GetInstance().get_sleep_state()); + /*早安闹钟*/ + if(SleepModel::GetInstance().get_sleep_state()==SLEEPSTATE::WAKE) + { + NotificationManager::GetInstance()->SetNotifyPriority(PRIORITY_TWO); + SleepViewEventListener::GetInstance().PopInterPlayMusic(); + } + else + { + NotificationManager::GetInstance()->SetNotifyPriority(PRIORITY_THREE); + } + + NotificationManager::GetInstance()->ShowNotify(showSleepNotify, (uint16_t)topic, true); + } + +} #endif @@ -735,6 +767,7 @@ static MsgCenterNotifyTable g_msgCenterNotifyTable[] = { {TOPIC_EVENT_DEBUGOUTPUT,MsgCenterNotifyDebugOutProc}, {TOPIC_EVENT_GOAL_SHOW_VIEW,MsgCenterNotifyGoalShowViewProc}, {TOPIC_EVENT_DRINKREMINDER,MsgCenterNotifyDrinkReminderProc}, + {TOPIC_EVENT_SLEEPNOTE,MsgCenterNotifySleepProc}, // {TOPIC_EVENT_DEVICE_UNBIND, MsgCenterNotifyDeviceUnBindProc}, // {TOPIC_EVENT_DEVICE_UNBINDING_REQUEST, MsgCenterNotifyDeviceUnBindingRequestProc}, diff --git a/code/sdk/application/wearable/fd230/service/msg_center/src/notify/sleep_notification.cpp b/code/sdk/application/wearable/fd230/service/msg_center/src/notify/sleep_notification.cpp new file mode 100644 index 000000000..d6e43ee15 --- /dev/null +++ b/code/sdk/application/wearable/fd230/service/msg_center/src/notify/sleep_notification.cpp @@ -0,0 +1,677 @@ +#include "notification_manager.h" +#include "wearable_log.h" +#include "components/ui_button.h" +#include +#include "uiservice/include/ui_service.h" +#include +#include "components/ui_label_ext.h" +#include "uicomponents/UIComponents.h" +#include "sleep/include/SleepModel.h" +#include "sleep_notification.h" +#include "string" +#include "vibrator/service/vibrator_service.h" +#include "UiConfig.h" +#include "ConfigModel.h" +#include "alarm/include/AlarmClockModel.h" +#include "custom_runtime.h" + +#ifdef SUPPORT_BLE_SERVICE +#include "bleEvent.h" +#endif + +namespace OHOS +{ +#define MAX_SLEEP_RING 8 + + UIViewGroup *SleepGroupView{nullptr}; + + UILabel *sleeptitle{nullptr}; // 标题 + UIImageView *sleepicon{nullptr}; // 睡眠图标 + UILabelExt *sleepnotifylabel{nullptr}; // 详细label + UILabelButtonExt *sleepbutton{nullptr}; // 删除按键 + + UILabelExt *alarmtitle{nullptr}; // 标题 + UIImageView *backimage{nullptr}; + UIImageView *sunnyicon{nullptr}; + UIImageView *alarmicon{nullptr}; + UILabelExt *nowlabel{nullptr}; + UILabelExt *wakelabel{nullptr}; // 早安时间 + UILabelButtonExt *closebutton{nullptr}; // 关闭按键 + UILabelButtonExt *nopbutton{nullptr}; // 小睡按键 + + static std::string titleStr; + static constexpr const char *PNG_SLEEP_SUUNY_IMAGE = "/user/res/Sleep/sunny.bin"; + static constexpr const char *PNG_SLEEP_ALARM_IMAGE = "/user/res/Sleep/alarmicon.bin"; + 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" + + static int countsleep = 0; + /* -------------------------------------------------------------------------- */ + + /*创建睡眠弹窗*/ + UIViewGroup *CreateSleepNotifyView(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CreateSleepNotifyView"); + SleepGroupView = new UIViewGroup(); + if (SleepGroupView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CreateSleepNotifyView] SleepGroupView new fail!!"); + return nullptr; + } + SleepGroupView->SetPosition(0, 0, 466, 466); + SleepGroupView->SetViewId("sleepnotify"); + SleepGroupView->SetTouchable(true); + SleepGroupView->SetDraggable(true); + UIView::OnDragListener *dragListener = static_cast(&SleepViewEventListener::GetInstance()); + if (dragListener == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CreateSleepNotifyView] dragListener new fail!!"); + return nullptr; + } + SleepGroupView->SetOnDragListener(dragListener); + // 注册释放notify + NotificationManager::GetInstance()->RegisterNotifyCleanupFunction(FreeSleepView); + /*获取最新sleep*/ + SLEEPSTATE sleepsate = SleepModel::GetInstance().get_sleep_state(); + // TODO:适配多国语言 + UIResourceTextId textId = STR_MORNNING; + // std::string sleep_note; + switch (sleepsate) + { + case SLEEPSTATE::SLEEPNOTIFY: + titleStr = SleepModel::GetInstance().get_notifiy(); + textId = STR_SLEEP_NOTIFY; // 即将开启勿扰 + // sleep_note = "手表将在20分钟后开启睡眠模式,关闭请到控制中心"; + break; + case SLEEPSTATE::SLEPP: + titleStr = SleepModel::GetInstance().get_sleep_notifiy(); + textId = STR_SLEEP_NOTE; + // sleep_note = "已开启睡眠模式,此模式下手表将关闭所有提醒功能"; + break; + case SLEEPSTATE::WAKE: + titleStr = SleepModel::GetInstance().get_wake_notifiy(); + // textId = STR_MORNNING; + SetAlarmClockView(); // 设置为早安视图 + return SleepGroupView; + break; + default: + break; + } + + /*初始化label*/ + /* -------------------------------------------------------------------------- */ + sleeptitle = new UILabel(); + if (sleeptitle == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sleeptitle:: new UILabel fail"); + return nullptr; // 内存分配失败,返回 + } + sleeptitle->SetPosition(83, 60, 300, 45); + sleeptitle->SetText(titleStr.c_str()); + sleeptitle->SetStyle(STYLE_TEXT_FONT, 30); + sleeptitle->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); + sleeptitle->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + sleeptitle->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 40); + sleeptitle->SetVisible(true); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "sleeptitle is ok!"); + /* -------------------------------------------------------------------------- */ + /*初始化图片*/ + InitImageView(sleepicon, 193, 130, 80, 80, "image", PNG_APPLIST_SLEEP_IMAGE); + sleepicon->SetResizeMode(UIImageView::ImageResizeMode::SCALE_DOWN); + /* -------------------------------------------------------------------------- */ + /*初始化消息内容*/ + sleepnotifylabel = new UILabelExt(); + if (sleepnotifylabel == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sleepnotifylabel:: new UILabel fail"); + return nullptr; // 内存分配失败,返回 + } + sleepnotifylabel->SetPosition(53, 220, 360, 144); + sleepnotifylabel->SetViewId("sleepnotifylabel"); + sleepnotifylabel->SetTextId(textId); + // sleepnotifylabel->SetText(sleep_note.c_str()); + sleepnotifylabel->SetTextColor(Color::Gray()); + sleepnotifylabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME_EN, 30); + sleepnotifylabel->SetLineBreakMode(UILabel::LINE_BREAK_CLIP); // 换行裁剪 + sleepnotifylabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); // 居中显示 + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "sleepnotifylabel is ok"); + + /*加载清除按键*/ + InitButtonExt(sleepbutton, 0, 366, 466, 100, DELETEBUTTON, STR_KONW, 30); + const char *buttonImgSrc = "/user/res/466x100button.bin"; + sleepbutton->SetImageSrc(buttonImgSrc, buttonImgSrc); + UIView::OnTouchListener *TouchListener = static_cast(&SleepViewEventListener::GetInstance()); + if (TouchListener == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CreateSleepView] TouchListener new fail!!"); + return nullptr; + } + sleepbutton->SetOnTouchListener(TouchListener); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "sleepbutton"); + /* -------------------------------------------------------------------------- */ + SleepGroupView->Add(sleeptitle); + SleepGroupView->Add(sleepicon); + SleepGroupView->Add(sleepnotifylabel); + SleepGroupView->Add(sleepbutton); + + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Create SleepGroupView succeed!!"); + + if (true != OHOS::ConfigModel::GetInstance()->GetDnotdisturb() && (!uapi_sleep_mode_flag_get())) + { + ui_api_vibrate_once_func(); + } + + ScreenTurnOn(100000); // 亮屏操作 100000ms 10秒关闭 + + SleepViewEventListener::GetInstance().Sleep_StartTimer(); // 开启一个4s后弹窗自动销毁定时器 + + return SleepGroupView; + } + + // 设置早安视图 + void SetAlarmClockView() + { + /*初始化图片*/ + InitImageView(backimage, 0, 0, 466, 466, "image", PNG_SLEEP_BACK_IMAGE); + 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, 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 wakeStr = SleepModel::GetInstance().get_wake_notifiy(); + InitLabel(wakelabel, 190, 245, 200, 60, "labletitle", wakeStr.c_str(), DEFAULT_VECTOR_FONT_FILENAME, 30); + /*设置按键*/ + InitButtonExt(nopbutton, 58, 320, 170, 90, NOPBUTTON, STR_NAP, 30); + nopbutton->SetOnTouchListener(static_cast(&SleepViewEventListener::GetInstance())); + nopbutton->SetStyle(STYLE_BACKGROUND_COLOR, 0xff4fc34c); + nopbutton->SetStyle(STYLE_BORDER_RADIUS, 100); + + 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); + SleepGroupView->Add(wakelabel); + SleepGroupView->Add(nopbutton); + SleepGroupView->Add(closebutton); + + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Create SleepGroupView succeed!!"); + if (true != OHOS::ConfigModel::GetInstance()->GetDnotdisturb() && (!uapi_sleep_mode_flag_get())) + { + ui_api_vibrate_once_func(); + } + + ScreenTurnOn(600000); // 亮屏操作 600000ms 60秒关闭 + } + /*释放消息弹窗视图*/ + void FreeSleepView(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "FreeSleepView(void)"); + + // 销毁定时器 + SleepViewEventListener::GetInstance().Sleep_StopTimer(); + + if (SleepGroupView != nullptr) + { + SleepGroupView->RemoveAll(); + + delete sleeptitle; + sleeptitle = nullptr; + + delete sleepicon; + sleepicon = nullptr; + + delete sleepnotifylabel; + sleepnotifylabel = nullptr; + + delete sleepbutton; + sleepbutton = nullptr; + + delete alarmtitle; + alarmtitle = nullptr; + + delete backimage; + backimage = nullptr; + + delete sunnyicon; + sunnyicon = nullptr; + + delete alarmicon; + alarmicon = nullptr; + + delete nowlabel; + nowlabel = nullptr; + + delete wakelabel; + wakelabel = nullptr; + + delete closebutton; + closebutton = nullptr; + + delete nopbutton; + nopbutton = nullptr; + + RootView::GetInstance()->Remove(SleepGroupView); + delete SleepGroupView; + SleepGroupView = nullptr; + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Free SleepView succeed!!"); + ScreenTurnOff(); + } + countsleep = 0; // 重置 + return; + } + + /* -------------------------------------------------------------------------- */ + // Presenter + SleepViewEventListener::SleepViewEventListener() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::SleepViewEventListener"); + } + + SleepViewEventListener::~SleepViewEventListener() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::~SleepViewEventListener"); + } + + SleepViewEventListener &SleepViewEventListener::GetInstance() + { + static SleepViewEventListener instance; + return instance; + } + bool SleepViewEventListener::OnRelease(UIView &view, const ReleaseEvent &event) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::OnRelease"); + UNUSED(event); + /* 知道了 按键*/ + if (strcmp(view.GetViewId(), DELETEBUTTON) == 0) + { + NotificationManager::GetInstance()->StopNotify(); + } + /*关闭闹钟*/ + if (strcmp(view.GetViewId(), CLOSEBUTTON) == 0) + { + SleepViewEventListener::GetInstance().PopInterCancelMusic(); + NotificationManager::GetInstance()->StopNotify(); + } + /*小睡按键*/ + if (strcmp(view.GetViewId(), NOPBUTTON) == 0) + { + SleepViewEventListener::GetInstance().PopInterCancelMusic(); + // SleepModel::GetInstance().Sleep_StartTimer(300000); // 开启一个5分钟的定时器 + 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) + { + // 如果是早安闹钟需要关闭闹钟 + if (SLEEPSTATE::WAKE == SleepModel::GetInstance().get_sleep_state()) + SleepViewEventListener::GetInstance().PopInterCancelMusic(); + NotificationManager::GetInstance()->StopNotify(); + } + return true; + } + /* + * @brief 初始化闹铃播放铃声的播放器 + * @param {char*} RingMusicName 铃声文件 + */ + int SleepViewEventListener::SleepRingPlayerInit(char *RingMusicName) + { + // 本地播放文件路径和状态 + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RingPlayerInit start!"); +#ifdef SINGLE_USER_PARTITION + std::string strSrcMusic("/user/music/ring"); +#else + std::string strSrcMusic("/music/ring"); +#endif + 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_LOGD(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_LOGD(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_LOGD(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) + { + AlarmClockModel::GetInstance()->PopInterCancelMusic(); + 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_LOGD(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) + { + ScreenTurnOff(); + GraphicService::GetInstance()->PostGraphicEvent( + std::bind(&SleepViewEventListener::SleepRingPlayStop, &SleepViewEventListener::GetInstance())); + } + + /* + * @brief 开始铃声播放的事件 + */ + void SleepViewEventListener::PopInterPlayMusic(void) + { + // 将"开启闹铃铃声"任务发布到异步线程 + 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_LOGD(WEARABLE_LOG_MODULE_APP, "[SleepRingPlayerListener::StopSleepRing] start"); + GraphicService::GetInstance()->PostGraphicEvent(Start); + } + + void SleepRingPlayerListener::StopSleepRing(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[SleepRingPlayerListener::StopSleepRing] start"); + GraphicService::GetInstance()->PostGraphicEvent(Stop); + } + + /* 响应外部的音频中断事件 */ + void SleepRingPlayerListener::OnInterrupt(int32_t type, int32_t hint) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnInterrupt go in, type %d, hint %d.", type, hint); + + // 处理外部中断 + if (type == INTERRUPT_TYPE_BEGIN) + { + if (hint == INTERRUPT_HINT_PAUSE) + { + isDelayed_ = true; + } + else if (hint == INTERRUPT_HINT_STOP) + { + StopSleepRing(); + } + } + else if (type == INTERRUPT_TYPE_END && isDelayed_) + { + // 重新开始闹铃的音乐播放 + StartSleepRing(); + isDelayed_ = false; + } + } + + /* --------------------- SleepRingPlayerCallbackImpl -------------------- */ + void SleepRingPlayerCallbackImpl::OnPlaybackComplete(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnPlaybackComplete"); + SleepViewEventListener::GetInstance().PopInterCancelMusic(); + /*达到最大次数不再播放*/ + if (countsleep <= MAX_SLEEP_RING) + { + SleepViewEventListener::GetInstance().PopInterPlayMusic(); + countsleep++; + // printf("countsleep: %d\n", countsleep); + } + } + + void SleepRingPlayerCallbackImpl::OnError(int32_t errorType, int32_t errorCode) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnError test, errorType %d, errorCode %d", errorType, errorCode); + // 此处实现代码处理错误事件 + } + + void SleepRingPlayerCallbackImpl::OnInfo(int type, int extra) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnInfo test, type %d, extra %d.", type, extra); + // 此处实现代码处理普通事件 + } + + void SleepRingPlayerCallbackImpl::OnRewindToComplete(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnRewindToComplete test"); + // 此处实现代码处理进度控制完成的事件 + } + /* -------------------------------------------------------------------------- */ + + // 声明定时器变量 + static osTimerId_t g_timer = NULL; + + /*定时器到期时的处理*/ + static void Sleep_TimerCallbackProc(int data) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Sleep_TimerCallbackProc"); + NotificationManager::GetInstance()->StopNotify(); + } + + /* + * @brief 回调函数 + */ + static void Sleep_TimerCallback(int data) + { + GraphicService::GetInstance()->PostGraphicEvent(std::bind(Sleep_TimerCallbackProc, data)); + } + + /* + * @brief 开启定时器函数 + * @param interval 时间间隔,单位ms + */ + void SleepViewEventListener::Sleep_StartTimer(uint32_t interval) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::Sleep_StartTimer"); + /* -------------------------- 1.检查定时器是否已经存在,如果存在则返回 ------------------------- */ + if (g_timer != NULL) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CountDownModel::CountDown_StartTimer Timer already exists!!"); + return; + } + + /* --------------------------------- 2.创建定时器 -------------------------------- */ + g_timer = osTimerNew((osTimerFunc_t)Sleep_TimerCallback, osTimerOnce, NULL, NULL); + // 检查定时器创建是否成功 + if (g_timer == NULL) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CountDownModel::CountDown_StartTimer Timer create failed!!"); + return; + } + + /* ----------------------------- 3.将毫秒转换为系统时钟节拍数 ---------------------------- */ + int tick = osMs2Tick(interval); + + /* --------------------------------- 4.启动定时器 -------------------------------- */ + osStatus_t ret = osTimerStart(g_timer, tick); + /* -------------------- 5.检查定时器启动是否成功,如果启动失败,删除定时器并置为 NULL -------------------- */ + if (ret != osOK) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[CountDown_StartTimer] osTimerDelete failed, ret = %lx!!", ret); + if (g_timer != NULL) + { + ret = osTimerDelete(g_timer); + } + g_timer = NULL; + return; + } + return; + } + + /* + * @brief 停止定时器 + */ + void SleepViewEventListener::Sleep_StopTimer(void) + { + // 检查定时器是否存在 + if (g_timer != NULL) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepViewEventListener::Sleep_StopTimer"); + osTimerStop(g_timer); + // 删除定时器并置为 NULL + osStatus_t ret = osTimerDelete(g_timer); + if (ret != osOK) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret); + } + g_timer = NULL; + } + } +} \ No newline at end of file -- 2.17.1