From: maomao <2144439972@qq.com> Date: Mon, 21 Apr 2025 09:13:40 +0000 (+0800) Subject: 【睡眠】UI X-Git-Tag: leiniao_debug_1.0.0_20250922_020716~364 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=aa1527598f0f9408bacf9cb2275bd98d9faab9ff;p=mirrors%2FFD230.git 【睡眠】UI --- diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepCardView.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepCardView.h new file mode 100644 index 000000000..624d48908 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepCardView.h @@ -0,0 +1,37 @@ +/*** + * @Date: 2024-06-25 10:41:32 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-09-03 15:10:21 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepCardView.h + */ +#ifndef SLEEP_CARD_VIEW_H +#define SLEEP_CARD_VIEW_H + +#include "components/root_view.h" +#include "components/ui_card_page.h" +#include "wearable_log.h" +#include "sleep/include/SleepControlView.h" +namespace OHOS +{ + class SleepControlView; + class SleepCardView : public UICardPage + { + public: + SleepCardView(); + virtual ~SleepCardView(); + static SleepCardView *GetInstance(void); + + bool InitView(void); + void PreLoad(void) override; + void UnLoad(void) override; + UISwipeView *GetSleepCardView(); + + private: + void ClearAll(void); + SleepControlView *sleepcontrolView{nullptr}; + UISwipeView *SleepSwipView{nullptr}; + UISwipeView::OnSwipeListener *sleepOnSwipeListener{nullptr}; + bool viewiInitStatus{false}; + }; +} +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepControlPresenter.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepControlPresenter.h new file mode 100644 index 000000000..29cfd42be --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepControlPresenter.h @@ -0,0 +1,55 @@ + +/*** + * @Date: 2024-06-25 10:32:53 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-06-25 15:31:40 + * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepControlPresenter.h + */ +#ifndef Sleep_CONTROL_PRESENTER_H +#define Sleep_CONTROL_PRESENTER_H + +#include "Presenter.h" +#include "View.h" +#include "sleep/include/SleepView.h" +#include "sleep/include/SleepModel.h" +#include "sleep/include/SleepControlView.h" +namespace OHOS +{ + class SleepOnSwipeListener : public UISwipeView::OnSwipeListener + { + public: + SleepOnSwipeListener() {} + ~SleepOnSwipeListener() {} + void OnSwipe(UISwipeView &view) override; + }; + + class SleepControlView; + class SleepControlPresenter : public Presenter, + public UIView::OnClickListener, + public RootView::OnKeyActListener, + public UIView::OnDragListener, + public UISwipeView::OnSwipeListener + { + public: + SleepControlPresenter(); + virtual ~SleepControlPresenter(); + + void OnStart() override; + void OnPause() override; + void OnResume() override; + void OnStop() override; + + static SleepControlPresenter *GetInstance(); + + private: + bool OnClick(UIView &view, const ClickEvent &event) override; + bool OnDrag(UIView &view, const DragEvent &event) override; + bool OnDragEnd(UIView &view, const DragEvent &event) override; + bool OnKeyAct(UIView &view, const KeyEvent &event) override; + void OnSwipe(UISwipeView &view) override; + + bool X_DRAG_FLAG{false}; + }; +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepControlView.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepControlView.h new file mode 100644 index 000000000..4c43d188c --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepControlView.h @@ -0,0 +1,74 @@ +/*** + * @Date: 2024-06-25 10:41:08 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-09-02 09:22:33 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepControlView.h + */ +/*** + * @Date: 2024-06-25 10:41:08 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-06-25 17:26:24 + * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepControlView.h + */ + +#ifndef SleepControl_VIEW_H +#define SleepControl_VIEW_H + +//#include "BaseView.h" +#include "View.h" +#include "components/ui_swipe_view.h" +#include "sleep/include/SleepControlView.h" +#include "sleep/include/SleepTimeView.h" +# include "sleep/include/SleepNapView.h" +#include "sleep/include/SleepControlPresenter.h" + +namespace OHOS +{ + typedef enum + { + MAINSLEEPVIEW = 0, // 主视图 + SLEEPTIMEVIEW, // 详细视图 + SLEEPNAPVIEW, //小睡 + MAX_SLEEPVIEW, + } SleepControlPageId; + +#define SLEEPVIEW "sleepview" + + class SleepControlPresenter; + class SleepControlView : public View + { + + public: + SleepControlView(); + virtual ~SleepControlView(); + static SleepControlView *GetInstance(void); + UISwipeView *SleepControlViewInit(void); + void ClearAll(void); + + AppGroupView *SleepPageInit(void); + AppGroupView *SleepTimePageInit(void); + AppGroupView *SleepNapPageInit(void); + + SleepView *GetSleepView(void); + SleepTimeView *GetSleepTimeView(void); + SleepNapView *GetSleepNapView(void); + + UISwipeView *GetSleepControlPageView(void); + void ChangeView(int16_t hideView, int16_t showView); + + public: + void OnStart() override; + + private: + SleepView *sleepView{nullptr}; + SleepTimeView *sleeptimeView{nullptr}; + SleepNapView *sleepnapView{nullptr}; + + private: + int16_t maxPage = MAX_SLEEPVIEW; + UISwipeView *SleepSwipeViewList{nullptr}; + AppGroupView *recordCard[MAX_SLEEPVIEW]{nullptr}; + AppGroupView *CreatePage(uint8_t SleepControlPageId); + }; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepModel.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepModel.h new file mode 100644 index 000000000..2a4f8faa5 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepModel.h @@ -0,0 +1,170 @@ + + +#ifndef Sleep_MODEL_H +#define Sleep_MODEL_H + +#include +#include +// #include "AppViewIDs.h" +// #include "UiConfig.h" +// #include "sleep/include/SleepView.h" +// #include "sleep/include/SleepControlPresenter.h" +#include "string" + +#define SLEEP_TIME_PATH "/user/userdata/sleep/%s/sleep_time.bin" +#define SLEEP_STATUS_PATH "/user/userdata/sleep/%s/sleep_status.bin" + +#define NAP_PATH "/user/userdata/sleep/%s/nap.bin" +#define SLEEP_DIR_PATH "/user/userdata/sleep/%s" + +#define SLEEP_DATA_DIR "/user/userdata/sleep/" + +#define SLEEP_STATUS_LENGTH 80 + +namespace OHOS +{ +#pragma pack(push, 1) + typedef struct + { + uint32_t fall_asleep_timestamp; // 入睡 + uint32_t wake_up_timestamp; // 出睡 + uint16_t total_time; // 总时长 + } NAP_T; + + typedef struct + { + uint32_t fall_asleep_timestamp; // 入睡 + uint32_t wake_up_timestamp; // 出睡 + uint16_t total_time; // 总时长 + } SLEEP_T; + + typedef struct + { + uint32_t timestamp; //时间戳 + uint8_t stat; // 状态 + } SLEEP_STAT_T; + +#pragma pack (pop) + + // bool save_nap_data(NAP_T *data); + // uint32_t get_nap_data_size(void); + // bool sleep_mutex_init(void); + uint32_t read_sleep_nap_data(char *data, uint32_t size, FILE *fp); + void set_nap_data(NAP_T *data); + void set_sleep_data(SLEEP_T *data); + // bool save_sleep_data(SLEEP_T *data); + bool read_sleep_data(SLEEP_T *data); + void read_wear_data(void); + bool read_nap_data(NAP_T *data); + // void clean_sleep_data(void); + // bool create_sleep_folder(void); + std::string get_sleep_asleep_time_str(void); + std::string get_sleep_wake_time_str(void); + std::string get_wear_status_str(void); + std::string get_sleep_star_str(void); + //void clean_sleep_file(void); + // 睡眠状态 + enum class SLEEPSTATE + { + WAKE = 0, + SLEEPNOTIFY, + SLEPP, + }; + // 睡眠存储数据 + typedef struct + { + std::string sleeptime; // 睡眠时间 + std::string waketime; // 醒来时间 + uint32_t sleep_timestamp; // 睡眠时间戳 + uint32_t wake_timestamp; // 醒来时间戳 + int totaltime; // 睡眠总时间 + } SleepDataUI; + class SleepModel + { + public: + static SleepModel &GetInstance(void); + void update_time_info(uint32_t timestamp, std::string &timeStr); + void update_sleep(SLEEP_T *data); + void ReInitData(); // 重新刷新UI + void Msg_ReInitData(); + void read_sleep_file_data(); + void msg_save_sleep_data(); + + void set_sleep_goal(); + int get_sleep_hour(); + int get_sleep_min(); + int get_sleep_goal(); + int get_sleep_total(); + + //清除睡眠数据 + void clean_sleep_data(void); + + std::string get_rest_str(); // 解析入睡时间 + std::string get_wake_str(); // 解析醒来时间 + std::string get_goal_str(); // 解析目标休息时间 + std::string get_notifiy(); // 获取提醒时间 + std::string get_sleep_notifiy(); // 获取睡眠时间 + std::string get_wake_notifiy(); // 获取起床时间 + + //小睡数据 + int get_nap_hour(); + int get_nap_min(); + std::string get_napsleep_str(); // 解析入睡时间 + std::string get_napwake_str(); // 解析醒来时间 + + + void set_sleep_state(SLEEPSTATE state); + SLEEPSTATE get_sleep_state(); + SleepDataUI *ReadSleepData(); // 读取睡眠数据 + bool SaveSleepData(const SleepDataUI &data); // 更新睡眠数据 + bool DeleteSleepData(); // 清空睡眠文件 + + bool CheckSleepTime();//对睡眠时间进行校验 + + void UpdateSleepView(); + std::string GetUpdateSleepTime(); + + /*定时器相关*/ + void Sleep_StartTimer(uint32_t interval); + void Sleep_StopTimer(void); + + /*主界面卡片相关*/ + void SetCardId(int cardid) + { + CradId = cardid; + } + int GetCardId() + { + return CradId; + } + + private: + SleepModel(); + SleepModel(const SleepModel &); + SleepModel &operator=(const SleepModel &); + virtual ~SleepModel(); + + std::string sleepStr{"--"}; + std::string wakeStr{"--"}; + std::string goalStr{"--"}; + std::string napsleepStr{"--"}; + std::string napwakeStr{"--"}; + + std::string goalnotifyStr{"--"}; + std::string goalsleepStr{"--"}; + std::string goalwakeStr{"--"}; + + std ::string sleep_time_str{"--"}; + + int sleep_total_time = 0; + int goal_total_time = 0; + int nap_total_time = 0; + + uint32_t sleep_update_time{0}; + + SLEEPSTATE sleepstate_ = SLEEPSTATE::WAKE; + int CradId = 0; // 主界面状态 + }; +} + +#endif // Sleep_MODEL_H \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepModelC.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepModelC.h new file mode 100644 index 000000000..9a2874675 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepModelC.h @@ -0,0 +1,31 @@ +/* + * @Author: penglei 1624952103@qq.com + * @Date: 2024-10-03 17:09:01 + * @LastEditors: penglei 1624952103@qq.com + * @LastEditTime: 2024-10-03 18:12:11 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModelC.h + * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE + */ +#ifndef SLEEPMODELC_H +#define SLEEPMODELC_H + +#ifdef __cplusplus +#if __cplusplus +extern "C" +{ +#endif +#endif + void custom_save_sleep_data(void); + void custom_save_nap_data(void); + void custom_read_sleep_data(void); + void custom_read_nap_data(void); + void custom_sleep_model_updata(void); + void update_sleep_data(uint32_t sleep_timestamp,uint32_t wake_timestamp,uint32_t totaltime); + void update_nap_data(uint32_t sleep_timestamp,uint32_t wake_timestamp,uint32_t totaltime); + void custom_read_wear_data(void); +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif +#endif diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepNapView.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepNapView.h new file mode 100644 index 000000000..82d1cdd00 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepNapView.h @@ -0,0 +1,69 @@ +/*** + * @Date: 2024-06-25 14:50:26 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditNap: 2024-07-01 15:10:12 + * @FilePath: + * /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepNapView.h + */ +/*** + * @Date: 2024-06-25 14:50:26 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditNap: 2024-06-25 17:29:03 + * @FilePath: + * /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepNapView.h + */ +/*** + * @Date: 2024-06-25 14:50:26 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditNap: 2024-06-25 16:30:55 + * @FilePath: + * /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepNapView.h + */ + +#ifndef Sleep_Nap_VIEW_H +#define Sleep_Nap_VIEW_H + +//#include "BaseView.h" +#include "View.h" + +#include "components/root_view.h" +#include "components/ui_circle_progress.h" +#include "components/ui_image_animator.h" +#include "components/ui_image_view.h" +#include "components/ui_label_ext.h" +#include "components/ui_scroll_view.h" +#include "components/ui_view_group.h" + +#include "AppGroupView.h" +#include "wearable_log.h" +#include + +namespace OHOS { +class SleepNapView : public AppGroupView { + +public: + SleepNapView(); + virtual ~SleepNapView(); + static SleepNapView *GetInstance(void); + bool InitPage(void); + virtual bool InitView(void *caller) override; + void ClearAll(); + bool GetInitViewState(void); + void SleepDataRefresh(); + +private: + UILabelExt *napsleeptitle{nullptr}; + UILabelExt *napwaketitle{nullptr}; + UILabelExt *naptitle{nullptr}; + UILabelExt *napsleep{nullptr}; + UILabelExt *napwake{nullptr}; + UILabelExt *apptitle{nullptr}; // 详情 + + UIImageView *transverseline_1{nullptr}; // 横线1 + UIImageView *transverseline_2{nullptr}; // 横线2 + UIImageView *verticalline{nullptr}; // 竖线 + bool viewInitStatus{false}; +}; +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepTimeView.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepTimeView.h new file mode 100644 index 000000000..41404f998 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepTimeView.h @@ -0,0 +1,73 @@ +/*** + * @Date: 2024-06-25 14:50:26 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-07-01 15:10:12 + * @FilePath: + * /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepTimeView.h + */ +/*** + * @Date: 2024-06-25 14:50:26 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-06-25 17:29:03 + * @FilePath: + * /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepTimeView.h + */ +/*** + * @Date: 2024-06-25 14:50:26 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-06-25 16:30:55 + * @FilePath: + * /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepTimeView.h + */ + +#ifndef Sleep_Time_VIEW_H +#define Sleep_Time_VIEW_H + +//#include "BaseView.h" +#include "View.h" + +#include "components/root_view.h" +#include "components/ui_circle_progress.h" +#include "components/ui_image_animator.h" +#include "components/ui_image_view.h" +#include "components/ui_label_ext.h" +#include "components/ui_scroll_view.h" +#include "components/ui_view_group.h" + +#include "AppGroupView.h" +#include "wearable_log.h" +#include + +namespace OHOS { +class SleepTimeView : public AppGroupView { + +public: + SleepTimeView(); + virtual ~SleepTimeView(); + static SleepTimeView *GetInstance(void); + bool InitPage(void); + virtual bool InitView(void *caller) override; + void ClearAll(); + bool GetInitViewState(void); + void SleepDataRefresh(); + +private: + void DumpWeatherInfo(); + +private: + UILabelExt *resttitle{nullptr}; // 作息时间标题 + UILabelExt *sleeptitle{nullptr}; // 入睡时间标题 + UILabelExt *waketitle{nullptr}; // 醒来标题 + UILabelExt *goaltime{nullptr}; // 目标时长 + UILabelExt *goal{nullptr}; // 作息时间 + UILabelExt *sleep{nullptr}; // 入睡时间 + UILabelExt *wake{nullptr}; // 醒来时间 + UIImageView *transverseline_1{nullptr}; // 横线1 + UIImageView *transverseline_2{nullptr}; // 横线2 + UIImageView *verticalline{nullptr}; // 竖线 + UIImageView *upiconimage{nullptr}; + bool viewInitStatus{false}; +}; +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepView.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepView.h new file mode 100644 index 000000000..b506f3902 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/include/SleepView.h @@ -0,0 +1,60 @@ +/*** + * @Date: 2024-06-25 10:32:22 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-06-25 17:52:16 + * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepView.h + */ + +#ifndef Sleep_VIEW_H +#define Sleep_VIEW_H + +//#include "BaseView.h" +#include "View.h" + +#include "components/root_view.h" +#include "components/ui_view_group.h" +#include "components/ui_scroll_view.h" +#include "components/ui_label_ext.h" +#include "components/ui_image_view.h" +#include "components/ui_image_animator.h" +#include "components/ui_circle_progress.h" + +#include "wearable_log.h" +#include "AppGroupView.h" +#include + +namespace OHOS +{ + class SleepView : public AppGroupView + { + public: + SleepView(); + virtual ~SleepView(); + static SleepView *GetInstance(void); + bool InitPage(void); + virtual bool InitView(void *caller) override; + void ClearAll(); + bool GetInitViewState(void); + void SleepDataRefresh(); // 刷新睡眠数据 + + private: + UICircleProgress *circleProgress_{nullptr}; // 进度条 + + UILabelExt *title{nullptr}; + UILabelExt *hour{nullptr}; + UILabelExt *minute{nullptr}; + + UILabel *time_hour{nullptr}; // 睡眠时间[小时] + UILabel *time_minute{nullptr}; // 睡眠时间[分钟] + + UILabelExt *sleep_update_time{nullptr}; // 目标时间 + + UIImageView *roomimage{nullptr}; + UIImageView *upiconimage{nullptr}; + UIImageView *progress_image = nullptr; + + bool viewInitStatus{false}; + }; +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepCardView.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepCardView.cpp new file mode 100644 index 000000000..97ce35cfc --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepCardView.cpp @@ -0,0 +1,103 @@ +/*** + * @Date: 2024-06-25 10:41:40 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-06-26 11:27:04 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepCardView.cpp + */ +#include "sleep/include/SleepCardView.h" +#include "sleep/include/SleepControlView.h" +#include "sleep/include/SleepControlPresenter.h" +namespace OHOS +{ + static SleepCardView *g_pSleepCardView = nullptr; + SleepCardView::SleepCardView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepCardView::SleepCardView"); + SetPosition(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION); + g_pSleepCardView = this; + } + SleepCardView::~SleepCardView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepCardView::~SleepCardView"); + ClearAll(); + g_pSleepCardView = nullptr; + } + SleepCardView *SleepCardView::GetInstance() + { + return g_pSleepCardView; + } + void SleepCardView::ClearAll(void) + { + if (viewiInitStatus == true) + { + RemoveAll(); + if (sleepcontrolView != nullptr) + { + if (sleepOnSwipeListener != nullptr) + { + delete sleepOnSwipeListener; + sleepOnSwipeListener = nullptr; + } + + if (sleepcontrolView != nullptr) + { + delete sleepcontrolView; + sleepcontrolView = nullptr; + } + if(SleepSwipView) + { + delete SleepSwipView; + SleepSwipView = nullptr; + } + } + } + } + + bool SleepCardView::InitView(void) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepCardView::InitView"); + sleepcontrolView = new SleepControlView(); + if (sleepcontrolView == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepView::new SleepView fail!"); + return false; + } + + SleepSwipView = dynamic_cast(sleepcontrolView->GetSleepControlPageView()); + if (SleepSwipView == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepView->SleepViewInit() == nullptr!!"); + return false; + } + + sleepOnSwipeListener = static_cast(new SleepOnSwipeListener()); + if (sleepOnSwipeListener == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sleepOnSwipeListener new fail"); + return false; + } + SleepSwipView->SetOnSwipeListener(sleepOnSwipeListener); + Add(SleepSwipView); + + return true; + } + UISwipeView *SleepCardView::GetSleepCardView() + { + return SleepSwipView; + } + void SleepCardView::PreLoad(void) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepCardView::PreLoad viewiInitStatus %d",viewiInitStatus); + if (!viewiInitStatus) + { + InitView(); + viewiInitStatus = true; + } + } + + void SleepCardView::UnLoad(void) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepCardView::UnLoad"); + } + +} \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepControlPresenter.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepControlPresenter.cpp new file mode 100644 index 000000000..1dcecc1f3 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepControlPresenter.cpp @@ -0,0 +1,121 @@ +/*** + * @Date: 2024-06-25 10:33:08 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-07-12 20:00:41 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepControlPresenter.cpp + */ +#include "AppViewIDs.h" +#include "NativeAbility.h" +#include "sleep/include/SleepControlPresenter.h" +#include "common/key_code.h" +#include "common/key_code.h" +#include +#include +#include "UiConfig.h" +#include "string.h" +#include "gfx_utils/mem_check.h" +#include "NativeRegisterManager.h" +#include "uicomponents/UIComponents.h" +namespace OHOS +{ + + REGIST_MENU_PRO(VIEW_SLEEP, SleepControlView, SleepControlPresenter, + PNG_APPLIST_SLEEP_IMAGE_BIG, PNG_APPLIST_SLEEP_IMAGE, PNG_APPLIST_SLEEP_IMAGE, "睡眠", STR_REMOTE_SHOOTING); + + // 当前Presenter属性实例化的内存指针 + static SleepControlPresenter *g_pSleepControlPresenter = nullptr; + + SleepControlPresenter::SleepControlPresenter() + { + // SleepModel = &SleepModel::GetInstance(); + g_pSleepControlPresenter = this; +#ifdef MEM_CHECK + MemCheck::GetInstance()->EnableLeakCheck(true); +#endif + } + + SleepControlPresenter::~SleepControlPresenter() + { + g_pSleepControlPresenter = nullptr; +#ifdef MEM_CHECK + MemCheck::GetInstance()->EnableLeakCheck(false); +#endif + } + + SleepControlPresenter *SleepControlPresenter::GetInstance() + { + + return g_pSleepControlPresenter; + } + + void SleepControlPresenter::OnStart() + { + Presenter::OnStart(); + } + + void SleepControlPresenter::OnPause(void) + { + Presenter::OnPause(); + } + + void SleepControlPresenter::OnResume(void) + { + Presenter::OnResume(); + } + + void SleepControlPresenter::OnStop(void) + { + Presenter::OnStop(); + } + + bool SleepControlPresenter::OnDrag(UIView &view, const DragEvent &event) + { + UNUSED(view); + if ((event.GetDeltaX() > X_DRAG_OFFSET) && (event.GetDeltaX() > (DOUBLES * MATH_ABS(event.GetDeltaY())))) // 拖动过程中判断是否是水平滑动 + { + SleepControlPresenter::X_DRAG_FLAG = true; + } + return true; + } + + bool SleepControlPresenter::OnDragEnd(UIView &view, const DragEvent &event) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, " SleepControlPresenter::OnDragEnd"); + UNUSED(view); + if (event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT && SleepControlPresenter::X_DRAG_FLAG) + { + NativeAbility::GetInstance().Back(TransitionType::TRANSITION_BACK_TO_HEXAGONS); + } + return true; + } + + bool SleepControlPresenter::OnClick(UIView &view, const ClickEvent &event) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, " SleepControlPresenter::OnClick"); + + UNUSED(view); + UNUSED(event); + return true; + } + + // 增加按键事件 + bool SleepControlPresenter::OnKeyAct(UIView &view, const KeyEvent &event) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SettingPresenter::OnKeyAct"); + uint16 keyID = event.GetKeyId(); + uint16_t keyState = event.GetState(); + // 如果是power键就返回上一级 + if (keyID == static_cast(ZliteKeyCode::ZLITE_KEY_POWER) && keyState == 1 && uapi_get_screen_state() == 0) + { + NativeAbility::GetInstance().Back(TransitionType::TRANSITION_BACK_TO_HEXAGONS); + } + return true; + } + + void SleepControlPresenter::OnSwipe(UISwipeView &view) {} + void SleepOnSwipeListener::OnSwipe(UISwipeView &view) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepOnSwipeListener::OnSwipe,current tab index = %d", view.GetCurrentPage()); + return; + } +} \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepControlView.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepControlView.cpp new file mode 100644 index 000000000..dd1492e04 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepControlView.cpp @@ -0,0 +1,217 @@ +/*** + * @Date: 2024-09-02 14:46:10 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-09-03 16:01:25 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepControlView.cpp + */ +/*** + * @Date: 2024-06-25 10:41:15 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-06-25 14:54:49 + * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepControlControlView.cpp + */ + +#include "sleep/include/SleepControlView.h" +#include "sleep/include/SleepControlPresenter.h" +#include "sleep/include/SleepModel.h" +#include "wearable_log.h" +namespace OHOS +{ + static SleepControlView *g_pSleepControlView{nullptr}; + SleepControlView::SleepControlView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepControlView::SleepControlView"); + g_pSleepControlView = this; + } + + SleepControlView::~SleepControlView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SleepControlView::~SleepControlView"); + ClearAll(); + g_pSleepControlView = nullptr; + } + + SleepControlView *SleepControlView::GetInstance(void) + { + return g_pSleepControlView; + } + + void SleepControlView::ClearAll(void) + { + if (SleepSwipeViewList != nullptr) + { + for (int32_t i = 0; i < maxPage; i++) + { + recordCard[i] = nullptr; + } + + SleepSwipeViewList->RemoveAll(); + + delete sleepView; + sleepView = nullptr; + + delete sleeptimeView; + sleeptimeView = nullptr; + + delete sleepnapView; + sleepnapView = nullptr; + + delete SleepSwipeViewList; + SleepSwipeViewList = nullptr; + } + } + /*初始化主睡眠界面VIEW*/ + UISwipeView *SleepControlView::SleepControlViewInit(void) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepControlView::SleepControlViewInit"); + SleepSwipeViewList = new UISwipeView(UISwipeView::VERTICAL); + if (nullptr == SleepSwipeViewList) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepSwipeViewList new fail!!"); + return nullptr; + } + + SleepSwipeViewList->SetStyle(STYLE_BACKGROUND_COLOR, Color::Black().full); + SleepSwipeViewList->SetPosition(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION); + // SleepSwipeViewList->SetAnimatorTime(50); + SleepSwipeViewList->SetDraggable(true); + SleepSwipeViewList->SetTouchable(true); + for (uint16_t i = 0; i < maxPage; i++) + { + recordCard[i] = CreatePage(i); + if (nullptr == recordCard[i]) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "recordCard[%d] is nullptr!!", i); + return nullptr; + } + recordCard[i]->InitView(this); + SleepSwipeViewList->Add(recordCard[i]); + } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepControlView InitPage over!!"); + return SleepSwipeViewList; + } + + /*获取睡眠主界面*/ + UISwipeView *SleepControlView::GetSleepControlPageView() + { + return SleepControlViewInit(); + } + + /*OnStart 增加界面控制*/ + void SleepControlView::OnStart() + { + View::OnStart(); + AddViewToRootContainer(new UIViewGroup()); + GetRootContainer()->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight()); + GetRootContainer()->SetTouchable(true); + GetRootContainer()->SetDraggable(true); + AddViewToRootContainer(GetSleepControlPageView()); + // RootView::GetInstance()->Add(GetSleepControlPageView()); + + if (SleepSwipeViewList == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepSwipeViewList is nullptr!"); + return; + } + + auto sleepPresenter = dynamic_cast(OHOS::SleepControlPresenter::GetInstance()); + if (sleepPresenter == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to get SleepControlPresenter instance"); + return; + } + + SleepSwipeViewList->SetOnClickListener(sleepPresenter); + SleepSwipeViewList->SetOnDragListener(sleepPresenter); + SleepSwipeViewList->SetOnSwipeListener(sleepPresenter); + + RootView::OnKeyActListener *keyactListener = dynamic_cast(sleepPresenter); + if (keyactListener == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to set key act listener"); + return; + } + + RootView::GetInstance()->SetOnKeyActListener(keyactListener); + } + + + SleepView *SleepControlView::GetSleepView(void) + { + return sleepView; + } + + SleepTimeView *SleepControlView::GetSleepTimeView(void) + { + return sleeptimeView; + } + SleepNapView *SleepControlView::GetSleepNapView(void) + { + return sleepnapView; + } + AppGroupView *SleepControlView::SleepPageInit(void) + { + sleepView = new SleepView(); + if (sleepView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sleepView::new SleepView fail!"); + return nullptr; + } + + return sleepView; + } + + AppGroupView *SleepControlView::SleepTimePageInit(void) + { + sleeptimeView = new SleepTimeView(); + if (sleeptimeView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sleeptimeView::new SleepTimeView fail!"); + return nullptr; + } + + return sleeptimeView; + } + AppGroupView *SleepControlView::SleepNapPageInit(void) + { + sleepnapView = new SleepNapView(); + if (sleepnapView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sleepnapView::new SleepNapView fail!"); + return nullptr; + } + + return sleepnapView; + } + + struct SleepControlViewMapper + { + const int16_t viewId; + AppGroupView *(SleepControlView::*func)(void); + }; + + static const SleepControlViewMapper g_pageMapper[] = { + {MAINSLEEPVIEW, &SleepControlView::SleepPageInit}, + {SLEEPTIMEVIEW, &SleepControlView::SleepTimePageInit}, + {SLEEPNAPVIEW, &SleepControlView::SleepNapPageInit}, + }; + + AppGroupView *SleepControlView::CreatePage(uint8_t SleepControlPageId) + { + uint16_t i; + AppGroupView *view = nullptr; + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CreatePage SleepControlPageId:%d", SleepControlPageId); + + for (i = 0; i < sizeof(g_pageMapper) / sizeof(g_pageMapper[0]); i++) + { + if (g_pageMapper[i].viewId == SleepControlPageId) + { // 正确使用 SleepControlPageId 来匹配视图ID + view = (this->*(g_pageMapper[i].func))(); // 如果找到匹配项,则调用相应的函数创建视图 + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CreatePage view:%p", view); + return view; // 返回创建的视图 + } + } + return nullptr; // 如果没有找到匹配的视图ID,返回 nullptr + } + +} \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepModel.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepModel.cpp new file mode 100644 index 000000000..b4024fd01 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepModel.cpp @@ -0,0 +1,1019 @@ +/* + * @Author: penglei 1624952103@qq.com + * @Date: SLEPPNOTIFY24-06-26 19:36:48 + * @LastEditors: penglei 1624952103@qq.com + * @LastEditTime: 2024-11-27 11:15:14 + * @FilePath: + * /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp + * @Description: + * + * Copyright (c) SLEPPNOTIFY24 by ${git_name_email}, All Rights Reserved. + */ + +#include "sleep/include/SleepModel.h" +#include "ConfigModel.h" +#include "UiConfig.h" +#include "custom_utils.h" +#include "heart_blood/health_service/health_service.h" +#include "kv_store.h" +#include "samgr_lite.h" +#include "securec.h" +#include "sleep/include/SleepControlPresenter.h" +#include "ui_resource_image.h" +#include "wearable_log.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ConfigModelC.h" +#include "MainModel.h" +#include "broadcast_feature.h" +#include "broadcast_service.h" +#include "cmsis_os.h" +#include "custom_runtime.h" +//#include "cywee_service.h" +#include "power_display_service.h" +#include "sleep/include/SleepModelC.h" +#include "thread_adapter.h" +#include "ui_service.h" +#include +#include + +extern "C" errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, + char date_separator, char time_separator, + date_format_t format); +extern "C" int get_sleep_time_from_config_model(void); +extern "C" int get_wake_up_time_from_config_model(); + + +static uint32_t len = 0; +static OHOS::SLEEP_STAT_T *wear_stat; +namespace OHOS +{ +#define SLEPPNOTIFY 20 + + static NAP_T nap_data = {0}; + static SLEEP_T sleep_data = {0}; + + static uint32_t sleep_update_timestamp = 0; + static uint32_t nap_update_timestamp = 0; + + void SleepModel::clean_sleep_data(void) + { + sleep_update_timestamp = 0; + nap_update_timestamp = 0; + memset(&sleep_data,0,sizeof(SLEEP_T)); + memset(&nap_data, 0, sizeof(NAP_T)); + } + + /*获取最新睡眠时间戳*/ + uint32_t get_sleep_update_timestamp(void) + { + if (nap_update_timestamp <= sleep_update_timestamp) + return sleep_update_timestamp; + else + return nap_update_timestamp; + } + + static char nap_file_path[64] = {0}; + + + bool is_regular_file(const std::string &file_name) + { + struct stat file_stat; + if (stat(file_name.c_str(), &file_stat) != 0) + { + return false; + } + + return S_ISREG(file_stat.st_mode); // 检查文件是否为普通文件 + } + + std::string get_wear_status_str(void) + { + std::string result; + std::string wear_status; + // 进行文件读取 + do_custom_fileoperate_service_check_msg( + CUSTOM_FILEOPERATE_MSG_TYPE_WEAR_DATA_READ, nullptr, 0); + LOS_Msleep(100); // 等待文件系统准备 + + if(len == 0) + return result; + + for(int i = 0; i < len/5; i++) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"data.timestamp : %d , data.stat : %d", wear_stat[i].timestamp,wear_stat[i].stat); + if(wear_stat[i].stat) + wear_status = "已配戴"; + else + wear_status = "未配戴"; + + std::string timestr; + char labelTime[20] = {0}; + + if (uapi_get_TIME_STR(wear_stat[i].timestamp, labelTime, '.', ':', DATE_Y_M_D_H_M) != ERRCODE_SUCC) + { + timestr = "--"; + } + else + { + timestr = labelTime; + } + + result += wear_status + ":"+ timestr + "\n"; + } + free(wear_stat); + return result; // 返回结果字符串 + } + + std::string get_sleep_star_str(void) + { + + std::string result; + + uint16_t datelen = 0; + uint8_t sleep_data[256] = {0}; + + uint32_t asleep_time = 0; + uint32_t wake_up_time = 0; + uint16_t total_time = 0; + uint16_t total_deep_sleep_time = 0; + uint16_t total_mild_sleep_time = 0; + uint16_t total_rem_sleep_time = 0; + uint16_t total_wake_time = 0; + uint32_t timestampe = 0; + int status = -1; + uint8_t index = 0; + char timestampe_str[10] = {0}; + char status_str[20] = {0}; + + char day[12] = {0}; + uapi_get_YYMMDD_Time(day, '\0'); + + char file_path[64] = {0}; + sprintf_s(file_path, 64, SLEEP_TIME_PATH, day); + + uapi_get_file_data(file_path,sleep_data,&datelen); + + if(datelen == 0) + { + PRINT("fread error datelen == 0 \n"); + return "没有文件"; + } + + asleep_time = *(uint32_t*)&sleep_data[1]; + wake_up_time = *(uint32_t*)&sleep_data[5]; + + total_time = sleep_data[9] * 60 + sleep_data[10]; + total_deep_sleep_time = sleep_data[11] * 60 + sleep_data[12]; + total_mild_sleep_time = sleep_data[13] * 60 + sleep_data[14]; + total_rem_sleep_time = sleep_data[15] * 60 + sleep_data[16]; + total_wake_time = sleep_data[17] * 60 + sleep_data[18]; + + char asleep_time_str[10] = {0}; + uapi_get_TIME_STR(asleep_time,asleep_time_str, '.', ':',DATE_H_M); + + char wake_up_time_str[10] = {0}; + uapi_get_TIME_STR(wake_up_time,wake_up_time_str, '.', ':',DATE_H_M); + + + result += std::string("入睡时间: ") + asleep_time_str + " " + std::to_string(asleep_time) + "\n"; + result += std::string("出睡时间: ") + wake_up_time_str + " " + std::to_string(wake_up_time) + "\n"; + result += std::string("总时长: ") + std::to_string(total_time) + "\n"; + result += std::string("深睡时长: ") + std::to_string(total_deep_sleep_time) + "\n"; + result += std::string("浅睡时长: ") + std::to_string(total_mild_sleep_time) + "\n"; + result += std::string("快速眼动时长: ") + std::to_string(total_rem_sleep_time) + "\n"; + result += std::string("清醒时长: ") + std::to_string(total_wake_time) + "\n"; + + // PRINT("入睡时间 : %s %d \n",asleep_time_str,asleep_time); + // PRINT("出睡时间 : %s %d \n",wake_up_time_str,wake_up_time); + // PRINT("总时长 : %d 分钟\n",total_time); + // PRINT("深睡时长 : %d 分钟\n",total_deep_sleep_time); + // PRINT("浅睡时长 : %d 分钟\n",total_mild_sleep_time); + // PRINT("快速眼动时长 : %d 分钟\n",total_rem_sleep_time); + // PRINT("清醒时长 : %d 分钟\n",total_wake_time); + + char statuspath[64] = {0}; + uapi_get_YYMMDD_Time(day, '\0'); + sprintf_s(statuspath, 64, SLEEP_STATUS_PATH, day); + + + memset(sleep_data,0,sizeof(sleep_data)); + datelen = 0; + uapi_get_file_data(statuspath,sleep_data,&datelen); + + if(datelen == 0) + { + PRINT("fread error datelen == 0 \n"); + return result += "\n 没有状态文件"; + } + + while(datelen) + { + memset(timestampe_str,0,sizeof(timestampe_str)); + memset(status_str,0,sizeof(status_str)); + timestampe = *(uint32_t*)&sleep_data[index]; + uapi_get_TIME_STR(timestampe,timestampe_str, '.', ':',DATE_H_M); + status = sleep_data[index + 4]; + //0进入睡眠 1潜睡 2深睡 3清醒 4结束睡眠 6入睡 7出睡最后一笔输出 8未出睡最后一笔输出 12快速动眼 13取消入睡 + switch(status) + { + case 0: + strcpy(status_str,"进入睡眠"); + break; + case 1: + strcpy(status_str,"浅睡"); + break; + case 2: + strcpy(status_str,"深睡"); + break; + case 3: + strcpy(status_str,"清醒"); + break; + case 4: + strcpy(status_str,"结束睡眠"); + break; + case 6: + strcpy(status_str,"入睡通知"); + break; + case 7: + strcpy(status_str,"出睡最后一笔输出"); + break; + case 9: + strcpy(status_str,"未出睡最后一笔输出"); + break; + case 12: + strcpy(status_str,"快速动眼"); + break; + case 13: + strcpy(status_str,"取消入睡"); + break; + default: + break; + + } + + // PRINT("时间 : %d %s 状态:%s\n",timestampe,timestampe_str,status_str); + + result += std::string(status_str) + std::to_string(timestampe) + "\n"; + + index+=5; + datelen -= 5; + } + return result; + } + + std::string get_sleep_asleep_time_str(void) + { + std::string timestr; + char labelTime[20] = {0}; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sleep_data.fall_asleep_timestamp : %d",sleep_data.fall_asleep_timestamp); + if (uapi_get_TIME_STR(sleep_data.fall_asleep_timestamp, labelTime, '.', ':', DATE_Y_M_D_H_M) != ERRCODE_SUCC || !sleep_data.fall_asleep_timestamp) + { + timestr = "--"; + } + else + { + timestr = labelTime; + } + return timestr; + } + + std::string get_sleep_wake_time_str(void) + { + std::string timestr; + char labelTime[20] = {0}; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sleep_data.wake_up_timestamp : %d",sleep_data.wake_up_timestamp); + if (uapi_get_TIME_STR(sleep_data.wake_up_timestamp, labelTime, '.', ':', DATE_Y_M_D_H_M) != ERRCODE_SUCC || !sleep_data.wake_up_timestamp) + { + timestr = "--"; + } + else + { + timestr = labelTime; + } + return timestr; + } + + void read_wear_data(void) + { + uint32_t size = 0; + len = uapi_get_file_size("user/userdata/wear.bin"); + if(!len) + return; + + wear_stat = (SLEEP_STAT_T *)malloc(len); + FILE *fp; + + fp = fopen("user/userdata/wear.bin", "rb"); + if (fp == NULL) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "wear file open failed"); + free(wear_stat); + return; + } + + size = fread(wear_stat, 1, len, fp); + if(size != len) + { + free(wear_stat); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "wear file read failed"); + } + fclose(fp); + } + + bool read_sleep_data(SLEEP_T *data) + { + char day[12] = {0}; + uapi_get_YYMMDD_Time(day, '\0'); + + char file_path[64] = {0}; + sprintf_s(file_path, 64, SLEEP_TIME_PATH, day); + + FILE *fp = fopen(file_path, "rb"); + if (fp == NULL) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "read_sleep_data fopen fail %s \n",file_path); + // osal_mutex_unlock(&sleep_mutex); + return false; + } + + // 读取睡眠类型 + uint8_t sleep_type; + fread(&sleep_type, 1, 1, fp); + + if (sleep_type == 0) + { + fclose(fp); + // osal_mutex_unlock(&sleep_mutex); + return false; + } + + // 入睡时间 + uint32_t fall_asleep_timestamp; + fread(&fall_asleep_timestamp, 1, 4, fp); + + // 起床时间 + uint32_t wake_up_timestamp; + fread(&wake_up_timestamp, 1, 4, fp); + + // 睡眠总时间 + uint8_t total_time_hour, total_time_minute; + fread(&total_time_hour, 1, 1, fp); + fread(&total_time_minute, 1, 1, fp); + uint16_t total_time = total_time_hour * 60 + total_time_minute; + + // 累加睡眠数据 + data->fall_asleep_timestamp = fall_asleep_timestamp; + data->wake_up_timestamp = wake_up_timestamp; + data->total_time = total_time; + + sleep_update_timestamp = wake_up_timestamp; + + fclose(fp); + + // osal_mutex_unlock(&sleep_mutex); + + return true; + } + + bool read_nap_data(NAP_T *data) + { + char day[12] = {0}; + uapi_get_YYMMDD_Time(day, '\0'); + + sprintf_s(nap_file_path, 64, NAP_PATH, day); + + FILE *fp = fopen(nap_file_path, "rb"); + if (fp == NULL) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "read_nap_data %s fopen fail", nap_file_path); + return false; + } + + if(uapi_get_file_size(nap_file_path) > 14) //存在多段小睡 UI显示最新的一次数据 + fseek(fp, -14, SEEK_END);//移到最后一段数据 + + fseek(fp, 3, SEEK_CUR); // 将文件指针移动到睡眠类型 + + //读取睡眠类型 + uint8_t sleep_type; + fread(&sleep_type, 1, 1, fp); + + if (sleep_type == 1) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sleep_type error!!!"); + fclose(fp); + return false; + } + // 入睡时间 + uint32_t fall_asleep_timestamp; + fread(&fall_asleep_timestamp, 1, 4, fp); + + // 起床时间 + uint32_t wake_up_timestamp; + fread(&wake_up_timestamp, 1, 4, fp); + + // 睡眠总时间 + uint8_t total_time_hour, total_time_minute; + fread(&total_time_hour, 1, 1, fp); + fread(&total_time_minute, 1, 1, fp); + uint16_t total_time = total_time_hour * 60 + total_time_minute; + + // 累加睡眠数据 + data->fall_asleep_timestamp = fall_asleep_timestamp; + data->wake_up_timestamp = wake_up_timestamp; + data->total_time = total_time; + + nap_update_timestamp = wake_up_timestamp; + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fall_asleep_timestamp : %d wake_up_timestamp : %d total_time : %d ", data->fall_asleep_timestamp, data->wake_up_timestamp, data->total_time); + + fclose(fp); + + return true; + } + void set_sleep_data(SLEEP_T *data) + { + memset(&sleep_data, 0, sizeof(SLEEP_T)); + memcpy_s(&sleep_data, sizeof(SLEEP_T), data, sizeof(SLEEP_T)); + // 刷新model里面的数据 + // SleepModel::GetInstance().update_sleep(&sleep_data); + } + /* -------------------------------------------------------------------------- */ + SleepModel::SleepModel() + { + // 初始化配置睡眠目标 + set_sleep_goal(); + // 刷新数据 + Msg_ReInitData(); + } + SleepModel::~SleepModel() {} + SleepModel &SleepModel::GetInstance() + { + static SleepModel instance; + return instance; + } + void SleepModel::Msg_ReInitData() { + do_custom_fileoperate_service_check_msg( + CUSTOM_FILEOPERATE_MSG_TYPE_SLEEPMODLE_UPDATE, nullptr, 0); + } + + + //更新睡眠model数据 + void SleepModel::ReInitData() + { + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "ReadSleepData sleepStr %u wakeStr %u sleep_total_time %d", + sleep_data.fall_asleep_timestamp, + sleep_data.wake_up_timestamp, sleep_data.total_time); + if (sleep_data.fall_asleep_timestamp) + { + update_time_info(sleep_data.fall_asleep_timestamp, sleepStr); + update_time_info(sleep_data.wake_up_timestamp, wakeStr); + } + else + { + sleepStr = "--"; + wakeStr = "--"; + } + sleep_total_time = sleep_data.total_time; + } + { + if (nap_data.fall_asleep_timestamp) + { + update_time_info(nap_data.fall_asleep_timestamp, napsleepStr); + update_time_info(nap_data.wake_up_timestamp, napwakeStr); + } + else + { + napsleepStr = "--"; + napwakeStr = "--"; + } + nap_total_time = nap_data.total_time; + } + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "ReadSleepData sleepStr %s wakeStr %s sleep_total_time %d nap_total_time %d napsleepStr %s napwakeStr %s", + sleepStr.c_str(), wakeStr.c_str(), sleep_total_time, nap_total_time, napsleepStr.c_str(), napwakeStr.c_str()); + + // delete data; // 释放内存 + } + + // 开机读取 + void SleepModel::read_sleep_file_data() { + custom_read_sleep_data(); + custom_read_nap_data(); + + } + // 保存睡眠数据 + void SleepModel::msg_save_sleep_data() { + // 配置睡眠数据 + int cmd = 0; + // do_cywee_service_check_msg(CYWEE_SERVICE_CMD_GET_SLEEP_DATA,&cmd); // 获取睡眠数据 但不中止睡眠 + } + + void SleepModel::UpdateSleepView() + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "睡眠数据更新"); + SleepControlView *p_SleepControlView = SleepControlView::GetInstance(); + if (p_SleepControlView != nullptr) + { + if (p_SleepControlView->GetSleepView()) + { + p_SleepControlView->GetSleepView()->SleepDataRefresh(); + } + if (p_SleepControlView->GetSleepTimeView()) + { + p_SleepControlView->GetSleepTimeView()->SleepDataRefresh(); + } + if (p_SleepControlView->GetSleepNapView()) + { + p_SleepControlView->GetSleepNapView()->SleepDataRefresh(); + } + } + } + static constexpr const char *SLEEP_JSON_PATH = + "/user/userdata/health/sleep.json"; + // 读取睡眠数据 + SleepDataUI *SleepModel::ReadSleepData() + { + + // 判断文件夹是否存在 + if (!HealthService::GetInstance()->CreateHealthFolder()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CreateHealthFolder file"); + return nullptr; + } + + // 读取文件 + std::ifstream inFile(SLEEP_JSON_PATH); + if (!inFile.is_open()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open JSON file"); + return nullptr; + } + + // 读取文件内容到字符串 + std::stringstream buffer; + buffer << inFile.rdbuf(); + std::string jsonDataString = buffer.str(); + const char *jsonData = jsonDataString.c_str(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "jsonData: %s", jsonData); + + // 关闭文件 + inFile.close(); + if (jsonDataString.empty()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "jsonDataString is empty"); + return nullptr; + } + + cJSON *sleep_root = cJSON_Parse(jsonData); + if (sleep_root == NULL) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sleep_root is empty"); + return nullptr; + } + + SleepDataUI *data = new SleepDataUI(); + + cJSON *sleeptime_item = cJSON_GetObjectItem(sleep_root, "sleeptime"); + cJSON *waketime_item = cJSON_GetObjectItem(sleep_root, "waketime"); + cJSON *totaltime_item = cJSON_GetObjectItem(sleep_root, "totaltime"); + cJSON *sleep_timestamp_item = + cJSON_GetObjectItem(sleep_root, "sleep_timestamp"); + cJSON *wake_timestamp_item = + cJSON_GetObjectItem(sleep_root, "wake_timestamp"); + + if (sleeptime_item && waketime_item && totaltime_item) + { + data->sleeptime = sleeptime_item->valuestring; + data->waketime = waketime_item->valuestring; + data->totaltime = totaltime_item->valueint; + // 增加时间戳 + data->sleep_timestamp = sleep_timestamp_item->valueint; + data->wake_timestamp = wake_timestamp_item->valueint; + } + else + { + // 读取的数据异常 + cJSON_Delete(sleep_root); + delete data; + return nullptr; + } + + cJSON_Delete(sleep_root); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ReadSleepData is ok!"); + + // 返回结构体 + return data; + } + + // 更新睡眠数据 + bool SleepModel::SaveSleepData(const SleepDataUI &data) + { + // 判断文件夹是否存在 + if (!HealthService::GetInstance()->CreateHealthFolder()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CreateHealthFolder file"); + return false; + } + + // 覆盖存储 + std::ofstream outFile(SLEEP_JSON_PATH, std::ios::trunc); + if (!outFile.is_open()) + { + // 文件创建失败 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "Failed to create or open JSON file for writing"); + return false; + } + + // 创建一个 JSON 对象 + cJSON *sleep_root = cJSON_CreateObject(); + if (sleep_root == NULL) + { + outFile.close(); + return false; + } + + cJSON_AddItemToObject(sleep_root, "sleeptime", + cJSON_CreateString(data.sleeptime.c_str())); + cJSON_AddItemToObject(sleep_root, "waketime", + cJSON_CreateString(data.waketime.c_str())); + cJSON_AddItemToObject(sleep_root, "totaltime", + cJSON_CreateNumber(data.totaltime)); + cJSON_AddItemToObject(sleep_root, "sleep_timestamp", + cJSON_CreateNumber(data.sleep_timestamp)); + cJSON_AddItemToObject(sleep_root, "wake_timestamp", + cJSON_CreateNumber(data.wake_timestamp)); + + char *jsonData = cJSON_Print(sleep_root); + if (jsonData == NULL) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to generate JSON data"); + cJSON_free(sleep_root); + outFile.close(); + return false; + } + + // 写入 JSON 数据到文件 + outFile << jsonData; + + cJSON_free(sleep_root); + free(jsonData); + outFile.close(); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateHealthData JSON is ok"); + + return true; + } + std::string SleepModel::GetUpdateSleepTime() + { + sleep_time_str.clear(); + // 获取睡眠最新时间 + sleep_update_time = get_sleep_update_timestamp(); + // 获取时间戳 + update_time_info(sleep_update_time, sleep_time_str); + return sleep_time_str; + } + //校验时间更新 + bool SleepModel::CheckSleepTime() { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CheckSleepTime %d",sleep_update_time); + // 获取睡眠最新时间 + if (sleep_update_time != 0) { + return true; + } + return false; + } + void SleepModel::update_sleep(SLEEP_T *data) + { + if (data == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid input data"); + return; + } + + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "fall_asleep_timestamp:%d wake_up_timestamp:%d total_time:%d", + data->fall_asleep_timestamp, data->wake_up_timestamp, + data->total_time); + + SleepDataUI *UIdata = ReadSleepData(); + if (UIdata != nullptr) + { + // 入睡时间是最早时间 + if (data->fall_asleep_timestamp < UIdata->sleep_timestamp || + UIdata->sleep_timestamp == 0) + { + UIdata->sleep_timestamp = data->fall_asleep_timestamp; + } + // 起床时间是最晚时间 + if (data->wake_up_timestamp > UIdata->wake_timestamp || + UIdata->wake_timestamp == 0) + { + UIdata->wake_timestamp = data->wake_up_timestamp; + } + // 总时间需要累加 + UIdata->totaltime += data->total_time; + + update_time_info(UIdata->sleep_timestamp, sleepStr); + update_time_info(UIdata->wake_timestamp, wakeStr); + UIdata->sleeptime = sleepStr; + UIdata->waketime = wakeStr; + + sleep_total_time = UIdata->totaltime; + + SaveSleepData(*UIdata); + } + delete UIdata; // Free memory if dynamically allocated + } + + // 清空睡眠数据 + bool SleepModel::DeleteSleepData() + { + // ReInitData(); // 刷新 + do_custom_fileoperate_service_check_msg(CUSTOM_FILEOPERATE_MSG_TYPE_SLEEPMODLE_UPDATE, nullptr, 0); + return true; + } + + // 时间戳->字符串 + void SleepModel::update_time_info(uint32_t timestamp, std::string &timeStr) + { + // 如果时间戳为0,设置时间字符串为默认值 + if (timestamp == 0) + { + timeStr = "--"; + return; + } + // 解析时间戳 + char labelTime[20]; + if (uapi_get_TIME_STR(timestamp, labelTime, '.', ':', DATE_H_M) == + ERRCODE_SUCC) + { + timeStr.assign(labelTime); + } + else + { + timeStr = "--"; // 如果获取失败,设置为默认值 + } + } + + /* + 设置睡眠目标 + 前2个字节是显示睡眠小时 + 后2个字节显示睡眠分钟 + 需要单独解析出来。 + */ + void SleepModel::set_sleep_goal() + { + // 将原3个字符串清空 + goalnotifyStr.clear(); + goalsleepStr.clear(); + goalwakeStr.clear(); + + int goal_sleep = get_sleep_time_from_config_model(); + int goal_wake = get_wake_up_time_from_config_model(); + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "goal_sleep:%d goal_wake:%d", + goal_sleep, goal_wake); + /* -------------------------------------------------------------------------- + */ + // 提取小时部分(前2字节) + int goal_sleep_hour = goal_sleep >> 16; + // 提取分钟部分(后2字节) + int goal_sleep_min = goal_sleep & 0x00FF; + std::ostringstream sleeposs; + sleeposs << std::setfill('0') << std::setw(2) << goal_sleep_hour << ":" + << std::setw(2) << goal_sleep_min; + // 睡眠时间点字符串 + goalsleepStr = sleeposs.str(); + + /* -------------------------------------------------------------------------- + */ + // 需要配置提醒字符串 减去SLEPPNOTIFY分钟的计算 + int notify_min = + (goal_sleep_min >= SLEPPNOTIFY) + ? goal_sleep_min - SLEPPNOTIFY + : ((goal_sleep_min + 60) - SLEPPNOTIFY); // 避免分钟数为负 + int notify_hour = goal_sleep_hour; + if (goal_sleep_min < SLEPPNOTIFY) + { // 如果需要借位 + notify_hour -= 1; // 小时减1 + } + // 确保小时数正确 + notify_hour = + (notify_hour < 0) + ? 23 + : notify_hour; // 处理从0点前SLEPPNOTIFY分钟的情况,变为前一天的23点40分 + // 格式化提醒时间字符串 + std::ostringstream notifyoss; + notifyoss << std::setfill('0') << std::setw(2) << notify_hour << ":" + << std::setw(2) << notify_min; + // 设置提醒字符串 + goalnotifyStr = notifyoss.str(); + /* -------------------------------------------------------------------------- + */ + + int goal_wake_hour = goal_wake >> 16; + int goal_wake_min = goal_wake & 0x00FF; + std::ostringstream wakeoss; + wakeoss << std::setfill('0') << std::setw(2) << goal_wake_hour << ":" + << std::setw(2) << goal_wake_min; + // 起床时间点字符串 + goalwakeStr = wakeoss.str(); + /* -------------------------------------------------------------------------- + */ + + // 睡眠字符串 以及总时长 + goalStr = goalsleepStr + "-" + goalwakeStr; + // goal_total_time 计算 + int total_hours; + if (goal_wake_hour < goal_sleep_hour) + { + total_hours = (goal_wake_hour + 24) - goal_sleep_hour; // 跨天情况 + } + else + { + total_hours = goal_wake_hour - goal_sleep_hour; + } + + int total_minutes = goal_wake_min - goal_sleep_min; + if (total_minutes < 0) + { + total_hours--; // 如果分钟差为负数,小时差减1 + total_minutes += 60; // 转换为正数,加60分钟 + } + goal_total_time = total_hours; // 确保最小时间为0小时 + + /*通知界面更新*/ + // SendMsgToUIService(UI_UPDATE_SLEEP_DATA, 0, nullptr, 0); + } + // 获取睡眠总时长(小时) + int SleepModel::get_sleep_hour() { return sleep_total_time / 60; } + // 获取睡眠总时长(分钟) + int SleepModel::get_sleep_min() { return sleep_total_time % 60; } + // 获取睡眠总时长(目标) + int SleepModel::get_sleep_goal() { return goal_total_time; } + // 获取睡眠总时长(实际) + int SleepModel::get_sleep_total() { return sleep_total_time; } + // 获取实际入睡时间str + std::string SleepModel::get_rest_str() { return sleepStr; } + // 获取实际醒来时间str + std::string SleepModel::get_wake_str() { return wakeStr; } + // 获取目标休息时间str + std::string SleepModel::get_goal_str() { return goalStr; } + // 获取 目标睡眠提醒 时间点str + std::string SleepModel::get_notifiy() { return goalnotifyStr; } + // 获取 目标睡眠 时间点str + std::string SleepModel::get_sleep_notifiy() { return goalsleepStr; } + + // 获取 目标起床 时间点str + std::string SleepModel::get_wake_notifiy() { return goalwakeStr; } + // 设置睡眠状态 + void SleepModel::set_sleep_state(SLEEPSTATE state) { sleepstate_ = state; } + // 获取睡眠状态 + SLEEPSTATE SleepModel::get_sleep_state() { return sleepstate_; } + // 小睡数据 + int SleepModel::get_nap_hour() { return nap_total_time / 60; } + int SleepModel::get_nap_min() { return nap_total_time % 60; } + // 小睡数据字符串 + std::string SleepModel::get_napsleep_str() { return napsleepStr; } + std::string SleepModel::get_napwake_str() { return napwakeStr; } + /* ---------------------------------- 睡眠定时器 + * --------------------------------- */ + + // 声明定时器变量 + static osTimerId_t g_timer = NULL; + + /*定时器到期时的处理*/ + static void Sleep_TimerCallbackProc(int data) + { + // 发起新闹钟 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Sleep_TimerCallbackProc"); + SleepModel::GetInstance().Sleep_StopTimer(); // 关闭定时器 + CustomEventPublish(TOPIC_EVENT_SLEEPNOTE, NULL, 0); // 发送睡眠弹窗 + } + + static void Sleep_TimerCallback(int data) + { + GraphicService::GetInstance()->PostGraphicEvent( + std::bind(Sleep_TimerCallbackProc, data)); + } + + void SleepModel::Sleep_StartTimer(uint32_t interval) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepModel::Sleep_StartTimer"); + if (g_timer != NULL) + { + WEARABLE_LOGE( + WEARABLE_LOG_MODULE_APP, + "CountDownModel::CountDown_StartTimer Timer already exists!!"); + return; + } + g_timer = + osTimerNew((osTimerFunc_t)Sleep_TimerCallback, osTimerOnce, NULL, NULL); + if (g_timer == NULL) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "CountDownModel::CountDown_StartTimer Timer create failed!!"); + return; + } + int tick = osMs2Tick(interval); + osStatus_t ret = osTimerStart(g_timer, tick); + if (ret != osOK) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "[CountDown_StartTimer] osTimerDelete failed, ret = %lx!!", + ret); + if (g_timer != NULL) + { + ret = osTimerDelete(g_timer); + } + g_timer = NULL; + return; + } + return; + } + + void SleepModel::Sleep_StopTimer(void) + { + // 检查定时器是否存在 + if (g_timer != NULL) + { + // 删除定时器并置为 NULL + osTimerStop(g_timer); + osStatus_t ret = osTimerDelete(g_timer); + if (ret != osOK) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "osTimerDelete failed, ret = %lx!!", ret); + } + g_timer = NULL; + } + } + /* -------------------------------------------------------------------------- */ + +#ifdef __cplusplus +#if __cplusplus + extern "C" + { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + + void custom_read_sleep_data(void) + { + memset(&sleep_data, 0, sizeof(SLEEP_T)); + read_sleep_data(&sleep_data); + } + + void custom_read_nap_data(void) + { + memset(&nap_data, 0, sizeof(NAP_T)); + read_nap_data(&nap_data); + } + + void custom_read_wear_data(void) + { + read_wear_data(); + } + + void custom_sleep_model_updata(void) + { + OHOS::SleepModel::GetInstance().ReInitData(); + } + // 更新睡眠数据 + void update_sleep_data(uint32_t sleep_timestamp, uint32_t wake_timestamp, + uint32_t totaltime) { + sleep_data.fall_asleep_timestamp = sleep_timestamp; + sleep_data.wake_up_timestamp = wake_timestamp; + sleep_data.total_time = totaltime; + sleep_update_timestamp = sleep_data.wake_up_timestamp; + } + // 更新小睡数据 + void update_nap_data(uint32_t sleep_timestamp, uint32_t wake_timestamp, + uint32_t totaltime) { + nap_data.fall_asleep_timestamp = sleep_timestamp; + nap_data.wake_up_timestamp = wake_timestamp; + nap_data.total_time = totaltime; + nap_update_timestamp = nap_data.wake_up_timestamp; + } + + +#ifdef __cplusplus +#if __cplusplus + } +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +} // namespace OHOS \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepNapView.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepNapView.cpp new file mode 100644 index 000000000..d11921f95 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepNapView.cpp @@ -0,0 +1,149 @@ +#include "sleep/include/SleepNapView.h" +#include "ConfigModel.h" +#include "common/image_cache_manager.h" +#include "sleep/include/SleepModel.h" +#include "string" +#include "uicomponents/UIComponents.h" +namespace OHOS { + +static SleepNapView *g_pSleepNapView{nullptr}; +static std::string restStr; +static std::string sleepStr; +static std::string wakeStr; +static std::string napsleepStr; +static std::string napwakeStr; +SleepNapView::SleepNapView() { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepNapView::SleepNapView"); + SetPosition(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION); + SetViewIndex(0); // 视图索引 + + g_pSleepNapView = this; +} + +SleepNapView::~SleepNapView() { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " SleepNapView::~SleepNapView"); + ClearAll(); + + g_pSleepNapView = nullptr; +} + +void SleepNapView::SleepDataRefresh() { + + int nap_hour = SleepModel::GetInstance().get_nap_hour(); + int nap_min = SleepModel::GetInstance().get_nap_min(); + + //小睡数据 + if (nap_hour != 0 || nap_min != 0) { + if (napsleep != nullptr && napwake != nullptr) { + napsleep->SetText((SleepModel::GetInstance().get_napsleep_str()).c_str()); + napwake->SetText((SleepModel::GetInstance().get_napwake_str()).c_str()); + } + } +} + +void SleepNapView::ClearAll() { + RemoveAll(); + + if (transverseline_1 != nullptr) { + delete transverseline_1; + transverseline_1 = nullptr; + } + if (transverseline_2 != nullptr) { + delete transverseline_2; + transverseline_2 = nullptr; + } + if (verticalline != nullptr) { + delete verticalline; + verticalline = nullptr; + } + + if (napsleeptitle != nullptr) { + delete napsleeptitle; + napsleeptitle = nullptr; + } + if (napwaketitle != nullptr) { + delete napwaketitle; + napwaketitle = nullptr; + } + if (napwake != nullptr) { + delete napwake; + napwake = nullptr; + } + if (napsleep != nullptr) { + delete napsleep; + napsleep = nullptr; + } + if (naptitle != nullptr) { + delete naptitle; + naptitle = nullptr; + } + if (apptitle != nullptr) { + delete apptitle; + apptitle = nullptr; + } +} + +SleepNapView *SleepNapView::GetInstance() { return g_pSleepNapView; } + + +bool SleepNapView::InitPage(void) { + /* ---------------------------------- 线条初始化 + * --------------------------------- */ + CreateLine(transverseline_1, 0, 169, 466, 2, "transverseline_1"); + CreateLine(transverseline_2, 0, 296, 466, 2, "transverseline_2"); + CreateLine(verticalline, 232, 191, 2, 82, "verticalline"); + /* -------------------------------- title初始化 + * -------------------------------- */ + //TODO:多国语言 + InitLabelExt(naptitle, 83, 90, 300, 35, "resttitle",STR_NAP, + DEFAULT_VECTOR_FONT_FILENAME_EN, 30); + InitLabelExt(napsleeptitle, 21, 203, 200, 35, "sleeptitle", STR_SLEEPTIME, + DEFAULT_VECTOR_FONT_FILENAME, 30); + InitLabelExt(napwaketitle, 213, 203, 300, 35, "waketitle", STR_WALKTIME, + DEFAULT_VECTOR_FONT_FILENAME, 30); + InitLabelExt(apptitle, 93, 300, 300, 200, "apptitle", STR_APPMORE, + DEFAULT_VECTOR_FONT_FILENAME, 26); + apptitle->SetTextColor(Color::Gray()); + /* ------------------------------- 数据label初始化 + * ------------------------------- */ + napsleepStr = "--"; + InitLabel(napsleep, 10, 238, 230, 47, "sleeplabel", napsleepStr.c_str(), + DEFAULT_VECTOR_FONT_FILENAME_EN, 40); + napsleep->SetTextColor(Color::GetColorFromRGBA(0x53, 0x20, 0xE2, 0xff)); + napwakeStr = "--"; + InitLabel(napwake, 245, 238, 230, 47, "wakelabel", napwakeStr.c_str(), + DEFAULT_VECTOR_FONT_FILENAME_EN, 40); + napwake->SetTextColor(Color::GetColorFromRGBA(0x53, 0x20, 0xE2, 0xff)); + /* -------------------------------------------------------------------------- + */ + /* -------------------------------- 将组件添加到根视图 + * ------------------------------- */ + Add(transverseline_1); + Add(transverseline_2); + Add(verticalline); + + Add(naptitle); + Add(napsleeptitle); + Add(napwaketitle); + Add(napsleep); + Add(napwake); + Add(apptitle); + + return true; +} + +bool SleepNapView::InitView(void *caller) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepNapView InitView %p", caller); + if (InitPage() == false) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepNapView::InitPage fail"); + return false; + } + SetDraggable(true); + SetTouchable(true); + SleepDataRefresh(); + return true; +} + +// 获取视图初始化状态 +bool SleepNapView::GetInitViewState(void) { return viewInitStatus; } +} // namespace OHOS \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepTimeView.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepTimeView.cpp new file mode 100644 index 000000000..292c1d135 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepTimeView.cpp @@ -0,0 +1,194 @@ +#include "sleep/include/SleepTimeView.h" +#include "ConfigModel.h" +#include "common/image_cache_manager.h" +#include "sleep/include/SleepModel.h" +#include "string" +#include "uicomponents/UIComponents.h" +namespace OHOS { + +static SleepTimeView *g_pSleepView{nullptr}; +static std::string restStr; +static std::string sleepStr; +static std::string wakeStr; +static constexpr char *upimagePath = "/user/res/Sleep/up.bin"; +SleepTimeView::SleepTimeView() { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepTimeView::SleepTimeView"); + SetPosition(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION); + SetViewIndex(0); // 视图索引 + + g_pSleepView = this; +} + +SleepTimeView::~SleepTimeView() { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " SleepTimeView::~SleepTimeView"); + ClearAll(); + + g_pSleepView = nullptr; +} + +void SleepTimeView::SleepDataRefresh() { + + int sleep_hour = SleepModel::GetInstance().get_sleep_hour(); + int sleep_min = SleepModel::GetInstance().get_sleep_min(); + int sleep_goal = SleepModel::GetInstance().get_sleep_goal(); + + // 有数据则刷新 + if (sleep_hour != 0 || sleep_min != 0) { + if (sleep != nullptr && wake != nullptr) { + sleep->SetText((SleepModel::GetInstance().get_rest_str()).c_str()); + wake->SetText((SleepModel::GetInstance().get_wake_str()).c_str()); + } + } + if (goal != nullptr) { + goal->SetText((SleepModel::GetInstance().get_goal_str()).c_str()); + } + // 更新目标时间 + if (goaltime != nullptr) { + UpdateLabel(goaltime, sleep_goal, STR_SLEEPMGOAL); + } +} + +void SleepTimeView::ClearAll() { + RemoveAll(); + if (resttitle != nullptr) { + delete resttitle; + resttitle = nullptr; + } + if (sleeptitle != nullptr) { + delete sleeptitle; + sleeptitle = nullptr; + } + if (waketitle != nullptr) { + delete waketitle; + waketitle = nullptr; + } + if (goaltime != nullptr) { + delete goaltime; + goaltime = nullptr; + } + if (goal != nullptr) { + delete goal; + goal = nullptr; + } + + if (sleep != nullptr) { + delete sleep; + sleep = nullptr; + } + if (wake != nullptr) { + delete wake; + wake = nullptr; + } + if (upiconimage != nullptr) { + delete upiconimage; + upiconimage = nullptr; + } + if (transverseline_1 != nullptr) { + delete transverseline_1; + transverseline_1 = nullptr; + } + if (transverseline_2 != nullptr) { + delete transverseline_2; + transverseline_2 = nullptr; + } + if (verticalline != nullptr) { + delete verticalline; + verticalline = nullptr; + } +} + +SleepTimeView *SleepTimeView::GetInstance() { return g_pSleepView; } + +bool SleepTimeView::InitPage(void) { + /* ---------------------------------- 线条初始化 + * --------------------------------- */ + if (transverseline_1 == nullptr) { + transverseline_1 = new UIImageView(); + } + transverseline_1->SetPosition(0, 169); + transverseline_1->Resize(466, 2); + transverseline_1->SetViewId("image1"); + transverseline_1->SetStyle(STYLE_BACKGROUND_COLOR, 0xff333333); + transverseline_1->SetStyle(STYLE_BACKGROUND_OPA, 0xff); + transverseline_1->SetVisible(true); + + if (transverseline_2 == nullptr) { + transverseline_2 = new UIImageView(); + } + transverseline_2->SetPosition(232, 191); + transverseline_2->Resize(2, 82); + transverseline_2->SetViewId("image2"); + transverseline_2->SetStyle(STYLE_BACKGROUND_COLOR, 0xff333333); + transverseline_2->SetStyle(STYLE_BACKGROUND_OPA, 0xff); + transverseline_2->SetVisible(true); + + if (verticalline == nullptr) { + verticalline = new UIImageView(); + } + verticalline->SetPosition(0, 296); + verticalline->Resize(466, 2); + verticalline->SetViewId("image1"); + verticalline->SetStyle(STYLE_BACKGROUND_COLOR, 0xff333333); + verticalline->SetStyle(STYLE_BACKGROUND_OPA, 0xff); + verticalline->SetVisible(true); + InitImageView(upiconimage, 213, 420, 40, 40, "image", upimagePath); + + /* -------------------------------- title初始化 + * -------------------------------- */ + InitLabelExt(resttitle, 83, 50, 300, 35, "resttitle", STR_REATTIME, + DEFAULT_VECTOR_FONT_FILENAME, 30); + InitLabelExt(sleeptitle, 21, 203, 200, 35, "sleeptitle", STR_SLEEPTIME, + DEFAULT_VECTOR_FONT_FILENAME, 30); + InitLabelExt(waketitle, 213, 203, 300, 35, "waketitle", STR_WALKTIME, + DEFAULT_VECTOR_FONT_FILENAME, 30); + InitLabelExt(goaltime, 83, 350, 300, 50, "lablegoal", STR_SLEEPMGOAL, + DEFAULT_VECTOR_FONT_FILENAME, 30); + goaltime->SetTextColor(Color::Gray()); + /* ------------------------------- 数据label初始化 + * ------------------------------- */ + //DEFAULT_VECTOR_FONT_FILENAME_EN + restStr = "--"; + InitLabel(goal, 83, 90, 300, 50, "restlabel", restStr.c_str(), + DEFAULT_VECTOR_FONT_FILENAME_EN, 40); + // goal->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); + goal->SetTextColor(Color::Gray()); + sleepStr = "--"; + InitLabel(sleep, 10, 238, 230, 47, "sleeplabel", sleepStr.c_str(), + DEFAULT_VECTOR_FONT_FILENAME_EN, 40); + sleep->SetTextColor(Color::GetColorFromRGBA(0x53, 0x20, 0xE2, 0xff)); + wakeStr = "--"; + InitLabel(wake, 245, 238, 230, 47, "wakelabel", wakeStr.c_str(), + DEFAULT_VECTOR_FONT_FILENAME_EN, 40); + wake->SetTextColor(Color::GetColorFromRGBA(0x53, 0x20, 0xE2, 0xff)); + + /* -------------------------------- 将组件添加到根视图 + * ------------------------------- */ + Add(resttitle); + Add(sleeptitle); + Add(waketitle); + Add(goaltime); + Add(goal); + Add(sleep); + Add(wake); + Add(transverseline_1); + Add(transverseline_2); + Add(verticalline); + Add(upiconimage); + return true; +} + +bool SleepTimeView::InitView(void *caller) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepTimeView InitView %p", caller); + if (InitPage() == false) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepTimeView::InitPage fail"); + return false; + } + SetDraggable(true); + SetTouchable(true); + SleepDataRefresh(); + return true; +} + +// 获取视图初始化状态 +bool SleepTimeView::GetInitViewState(void) { return viewInitStatus; } +} // namespace OHOS \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepView.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepView.cpp new file mode 100644 index 000000000..cbc816902 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/sleep/src/SleepView.cpp @@ -0,0 +1,254 @@ +#include "sleep/include/SleepView.h" +#include "ConfigModel.h" +#include "common/image_cache_manager.h" +#include "languagecode/LanguageInit.h" +#include "sleep/include/SleepModel.h" +#include "uicomponents/UIComponents.h" +namespace OHOS { +static constexpr char *roomimagePath = "/user/res/Sleep/room.bin"; +static constexpr char *upimagePath = "/user/res/Sleep/up.bin"; +static constexpr const char *prossimagePath = "/user/res/step/step_b.bin"; + +static SleepView *g_pSleepView{nullptr}; +SleepView::SleepView() { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepView::SleepView"); + SetPosition(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION); + SetViewIndex(0); // 视图索引 + + g_pSleepView = this; +} + +SleepView::~SleepView() { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " SleepView::~SleepView"); + ClearAll(); + + g_pSleepView = nullptr; +} + +void SleepView::SleepDataRefresh() { + // 获取睡眠数据内存 + SleepModel &sleepModel = SleepModel::GetInstance(); + sleepModel.ReInitData(); + + int sleep_hour = sleepModel.get_sleep_hour(); + int sleep_min = sleepModel.get_sleep_min(); + int total_sleep = sleepModel.get_sleep_total(); + int sleep_goal = sleepModel.get_sleep_goal(); + + // 检查是否超过24小时 + if (sleep_hour >= 24) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "异常:睡眠时长超过24小时"); + return; + } + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "SleepDataRefresh %d %d sleep_goal:%d total_sleep:%d", + sleep_hour, sleep_min, sleep_goal * 60, total_sleep); + + // 补零操作 + std::string hour_str = (sleep_hour < 10) ? "0" + std::to_string(sleep_hour) + : std::to_string(sleep_hour); + std::string min_str = (sleep_min < 10) ? "0" + std::to_string(sleep_min) + : std::to_string(sleep_min); + + // 有数据才刷新 + if (sleep_hour != 0 || sleep_min != 0) { + // 更新时间显示 + if (time_hour != nullptr) { + time_hour->SetText(hour_str.c_str()); + } + if (time_minute != nullptr) { + time_minute->SetText(min_str.c_str()); + } + + // 计算进度百分比,确保目标睡眠时间不为零 + float progress_value = 0.0f; + if (sleep_goal > 0) { + progress_value = + static_cast(total_sleep) / (sleep_goal * 60); // 计算进度 + } + + // 日志输出进度百分比 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Progress value: %.2f%%", + progress_value * 100); // 显示为百分比 + + // 更新进度条 + circleProgress_->SetValue(progress_value * + 100); // 更新进度条值,范围为 0 到 100 + circleProgress_->SetCapType(CAP_ROUND); + } + + // 更新时间 + if (sleep_update_time != nullptr) { + std::string update_time = sleepModel.GetUpdateSleepTime(); + if (!update_time.empty()) // 检查返回值是否有效 + { + std::string update_time_str; + // TODO:多国语言 + if (ConfigModel::GetInstance()->GetLanguage() == ZH_CN) { + update_time_str = update_time + " 更新"; + } else { + update_time_str = update_time + " update"; + } + sleep_update_time->SetText(update_time_str.c_str()); + // UpdateLabel(sleep_update_time, sleep_goal, STR_SLEEPMGOAL); // TODO: + // 多国语言支持 + } + } + + // 刷新视图 + RootView::GetInstance()->Invalidate(); +} + +void SleepView::ClearAll() { + RemoveAll(); + if (circleProgress_ != nullptr) { + delete circleProgress_; + circleProgress_ = nullptr; + } + if (title != nullptr) { + delete title; + title = nullptr; + } + if (hour != nullptr) { + delete hour; + hour = nullptr; + } + if (minute != nullptr) { + delete minute; + minute = nullptr; + } + if (time_hour != nullptr) { + delete time_hour; + time_hour = nullptr; + } + if (time_minute != nullptr) { + delete time_minute; + time_minute = nullptr; + } + if (sleep_update_time != nullptr) { + delete sleep_update_time; + sleep_update_time = nullptr; + } + if (roomimage != nullptr) { + delete roomimage; + roomimage = nullptr; + } + if (upiconimage != nullptr) { + delete upiconimage; + upiconimage = nullptr; + } + if (progress_image != nullptr) { + delete progress_image; + progress_image = nullptr; + } +} + +SleepView *SleepView::GetInstance() { return g_pSleepView; } + +/*初始化界面*/ +bool SleepView::InitPage(void) { + /*初始化title*/ + InitLabelExt(title, 83, 50, 300, 35, "labletitle", STR_SLEEP_YESTERDAY, + DEFAULT_VECTOR_FONT_FILENAME, 30); + /*初始化单位*/ + InitLabelExt(hour, 157, 246, 75, 75, "lablehour", STR_HOUR, + DEFAULT_VECTOR_FONT_FILENAME, 26); + hour->SetTextColor(Color::Gray()); + InitLabelExt(minute, 295, 246, 100, 75, "lableminute", STR_MIN, + DEFAULT_VECTOR_FONT_FILENAME, 26); + minute->SetTextColor(Color::Gray()); + if (ConfigModel::GetInstance()->GetLanguage() != ZH_CN) { + //需要缩小 + title->SetFont(DEFAULT_VECTOR_FONT_FILENAME,26); + hour->SetFont(DEFAULT_VECTOR_FONT_FILENAME,24); + minute->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24); + + minute->SetText("Min");//多国语言 + hour->SetText("Hour"); + } else { + minute->SetText("分钟"); + hour->SetText("小时"); + } + /*初始化image*/ + InitImageView(roomimage, 192, 127, 82, 82, "image", roomimagePath); + InitImageView(upiconimage, 213, 420, 40, 40, "image", upimagePath); + InitImageView(progress_image, 28, 80, 100, 100, "image", prossimagePath); + /*初始化目标时间*/ + // DEFAULT_VECTOR_FONT_FILENAME_EN + InitLabelExt(sleep_update_time, 83, 330, 300, 35, "lablegoal", STR_SLEEPMGOAL, + DEFAULT_VECTOR_FONT_FILENAME_EN, 30); + sleep_update_time->SetTextColor(Color::Gray()); + /*初始化睡眠时间*/ + time_hour = new UILabel(); + time_hour->SetPosition(84, 237); + time_hour->Resize(40, 75); + time_hour->SetViewId("time_hour"); + time_hour->SetText("--"); + time_hour->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); + time_hour->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + time_hour->SetFont(GILROY_BOLD_FONT, 65); + + time_minute = new UILabel(); + time_minute->SetPosition(240, 237); + time_minute->Resize(40, 75); + time_minute->SetViewId("time_minute"); + time_minute->SetText("--"); + time_minute->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); + time_minute->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + time_minute->SetFont(GILROY_BOLD_FONT, 65); + /* --------------------------------- 初始化进度条 + * --------------------------------- */ + circleProgress_ = new UICircleProgress(); + if (circleProgress_ == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "circleProgress new fail"); + return false; + } + circleProgress_->SetPosition(0, 0); + circleProgress_->Resize(466, 466); + circleProgress_->SetViewId("circleProgress1"); + circleProgress_->SetStyle(STYLE_BACKGROUND_OPA, 0x00); + circleProgress_->SetBackgroundStyle(STYLE_BACKGROUND_COLOR, 0xff000000); + circleProgress_->SetBackgroundStyle(STYLE_BACKGROUND_OPA, 0x00); + circleProgress_->SetBackgroundStyle(STYLE_BORDER_OPA, 0x00); + circleProgress_->SetBackgroundStyle(STYLE_LINE_OPA, 0x00); + circleProgress_->SetForegroundStyle(STYLE_BACKGROUND_COLOR, 0xff2195F6); + circleProgress_->SetForegroundStyle(STYLE_BORDER_COLOR, 0xff000000); + circleProgress_->SetForegroundStyle(STYLE_LINE_COLOR, 0xff5320e2); + circleProgress_->SetForegroundStyle(STYLE_LINE_WIDTH, 20); + circleProgress_->SetCenterPosition(233, 229); + circleProgress_->SetRadius(205); + circleProgress_->SetStartAngle(320); + circleProgress_->SetEndAngle(40); + circleProgress_->SetCapType(CAP_ROUND); + /* -------------------------------- 将组件添加到根视图 + * ------------------------------- */ + Add(progress_image); + Add(circleProgress_); + Add(title); + Add(hour); + Add(minute); + Add(time_hour); + Add(time_minute); + Add(sleep_update_time); + Add(upiconimage); + Add(roomimage); + + viewInitStatus = true; + return true; +} + +bool SleepView::InitView(void *caller) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepView InitView %p", caller); + if (InitPage() == false) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepView::InitPage fail"); + return false; + } + SleepDataRefresh(); // 刷新数据 + SetDraggable(true); + SetTouchable(true); + return true; +} + +bool SleepView::GetInitViewState(void) { return viewInitStatus; } +} // namespace OHOS \ No newline at end of file diff --git a/code/sdk/zhihui/utils/custom_fileoperate_service.c b/code/sdk/zhihui/utils/custom_fileoperate_service.c index 2daeb3220..374f8ca9f 100644 --- a/code/sdk/zhihui/utils/custom_fileoperate_service.c +++ b/code/sdk/zhihui/utils/custom_fileoperate_service.c @@ -57,12 +57,12 @@ void store_sport_data(void){} //进行心率列表存储 void custom_save_sleep_data(void){} void custom_save_nap_data(void){} -void custom_read_sleep_data(void){} -void custom_read_nap_data(void){} -void custom_sleep_model_updata(void){} -void update_sleep_data(uint32_t sleep_timestamp,uint32_t wake_timestamp,uint32_t totaltime){} -void update_nap_data(uint32_t sleep_timestamp,uint32_t wake_timestamp,uint32_t totaltime){} -void custom_read_wear_data(void){} +//void custom_read_sleep_data(void){} +//void custom_read_nap_data(void){} +//void custom_sleep_model_updata(void){} +//void update_sleep_data(uint32_t sleep_timestamp,uint32_t wake_timestamp,uint32_t totaltime){} +//void update_nap_data(uint32_t sleep_timestamp,uint32_t wake_timestamp,uint32_t totaltime){} +//void custom_read_wear_data(void){} #ifndef FACTORYTEST_SERVICE // void factorytest_save_aging_data(void){} // void factorytest_read_aging_data(void){}