From 8535836a031f0ae2ff75d7b49b71b43a5f2136fe Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Fri, 26 Jul 2024 17:30:56 +0800 Subject: [PATCH] =?utf8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89?= =?utf8?q?=E8=A1=A8=E7=9B=98=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E7=9B=B8?= =?utf8?q?=E5=AF=B9=E5=BA=94=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/clock/include/MainClockView.h | 7 +- .../nativeui/clock/src/MainClockView.cpp | 84 ------------- .../nativeui/set_dial/include/SetDialModel.h | 16 +++ .../nativeui/set_dial/src/SetDialModel.cpp | 115 ++++++++++++++++++ 4 files changed, 134 insertions(+), 88 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/clock/include/MainClockView.h b/code/sdk/application/wearable/fd230app/nativeui/clock/include/MainClockView.h index 5113f3c50..5c4920735 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/clock/include/MainClockView.h +++ b/code/sdk/application/wearable/fd230app/nativeui/clock/include/MainClockView.h @@ -90,14 +90,13 @@ namespace OHOS void UpdateBatteryColor(void); /*配置文件*/ void SetDialPath(std::string dialpath); - bool InitialConfig(int Id); + /*配置函数*/ bool InitBackground(const char *filename); bool InitTimeView(TimeDisplayPosition type); // 时间 bool InitBatteryView(BatteryDisplayType type); // 电量 bool InitColor(ColorTypeSet type); // 颜色 - /*返回文件夹路径*/ - std::string GetSaveDir(int Id); + private: void InitStepValue(); @@ -108,7 +107,7 @@ namespace OHOS bool InitClock(void); bool LoadClockImage(Image *view, uint32_t resId); bool LoadClockImage(UIImageView *view, uint32_t resId); - void CreateDirectoryIfNotExists(const std::string &path); + /*表盘背景*/ UIImageView *curFace{nullptr}; diff --git a/code/sdk/application/wearable/fd230app/nativeui/clock/src/MainClockView.cpp b/code/sdk/application/wearable/fd230app/nativeui/clock/src/MainClockView.cpp index 05a97ffa8..87739fb10 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/clock/src/MainClockView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/clock/src/MainClockView.cpp @@ -136,90 +136,6 @@ namespace OHOS dialFile.clear(); dialFile = dialpath; } - /* - 从配置文件读取设置 - 配置文件名:表盘ID - 第一个字节:电池图标样式 - 第二个字节:时间label位置 - 第三个字节:字体颜色 - */ - bool MainClockView::InitialConfig(int Id) - { - std::string filename = APP_CUSTOM_DIAL_PATH + std::to_string(Id) + ".bin"; - - // 打开文件 - std::ifstream configFile(filename, std::ios::binary); - if (!configFile.is_open()) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Unable to open config file: %s", filename.c_str()); - return false; // 文件打开失败 - } - - // 读取配置 - char settings[3]; // 读取3个字节 - configFile.read(settings, sizeof(settings)); - if (configFile.gcount() != sizeof(settings)) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to read config file or insufficient file content!"); - configFile.close(); - return false; // 读取失败 - } - - // 为成员变量赋值 - batterySetting = static_cast(settings[0]); // 第一个字节:电池图标样式 - timePosition = static_cast(settings[1]); // 第二个字节:时间label位置 - colorType = static_cast(settings[2]); // 第三个字节:字体颜色 - - // 关闭文件 - configFile.close(); - - return true; // 成功读取配置 - } - /* - 获取存储文件夹 - Id(0~4)通过ID返回对应的文件夹路径 - 0 - 1 - 2 - 3 - 4 - */ - /*创建文件夹*/ - void MainClockView::CreateDirectoryIfNotExists(const std::string &path) - { - struct stat info; - if (stat(path.c_str(), &info) != 0) - { - if (mkdir(path.c_str(), 0755) != 0) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error: Could not create directory %s. Errno: %d", path.c_str(), errno); - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Created directory: %s", path.c_str()); - } - } - else if (info.st_mode & S_IFDIR) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Directory already exists: %s", path.c_str()); - } - } - /*获取文件夹路径*/ - std::string MainClockView::GetSaveDir(int Id) - { - // 首先检查Id是否在0~4的范围内 - if (Id < 0 || Id > 4) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error: Id must be between 0 and 4."); - return ""; // 或者返回一个空字符串 - } - - std::string folderPath = std::string(APP_CUSTOM_DIAL_PATH) + "/" + std::to_string(Id); // 修复这一行 - CreateDirectoryIfNotExists(folderPath); // 确保目录存在 - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Directory is %s", folderPath.c_str()); - - return folderPath; // 返回目录路径 - } bool MainClockView::InitClock(void) { diff --git a/code/sdk/application/wearable/fd230app/nativeui/set_dial/include/SetDialModel.h b/code/sdk/application/wearable/fd230app/nativeui/set_dial/include/SetDialModel.h index ea6abd2e3..8bc398dc5 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/set_dial/include/SetDialModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/set_dial/include/SetDialModel.h @@ -37,6 +37,13 @@ namespace OHOS DEFULTDIAL, DOWNLOADDIAL, }; + struct DialLayout + { + uint8_t dialId; + uint8_t batteryDisplay; // 电池配置 + uint8_t timeDisplay; // 时间位置 + uint8_t colorTypeSet; // 颜色配置 + }; class SetDialModel { public: @@ -54,8 +61,17 @@ namespace OHOS uint16_t GetDialIdFromName(const std::string &dialName); void PrintRegisteredDials(const std::unordered_map &dials); int GetAllDialIDs(char *allidStr); + + /*表盘map文件*/ bool readDialfile(); bool saveDialfile(); + /*自定义表盘配置文件*/ + bool saveDialConfig(const DialLayout &layout); + bool loadDialConfig(int Id, DialLayout &layout); + /*获取存储自定义表盘文件夹路径*/ + std::string GetSaveDir(int Id); + /*【判断文件夹是否存在 不存在创建】*/ + void CreateDirectoryIfNotExists(const std::string &path); private: void LoadAllDialFromRam(void); diff --git a/code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp index 0080f3aaa..e60864a26 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp @@ -28,8 +28,13 @@ #include #include #include +#include +#include #include +#include +#include #include "NativeAbility.h" +#include "clock/include/MainClockView.h" namespace OHOS { // 定义特殊Id @@ -520,5 +525,115 @@ namespace OHOS } return 0; } + /* --------------------------------- 自定义表盘配置 -------------------------------- */ + // 保存自定义表盘配置到文件 + bool SetDialModel::saveDialConfig(const DialLayout &layout) + { + std::string filename = APP_CUSTOM_DIAL_PATH + std::to_string(layout.dialId) + ".bin"; + + // 创建或打开文件 + std::ofstream configFile(filename, std::ios::binary | std::ios::out | std::ios::trunc); + if (!configFile.is_open()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Unable to open config file for writing: %s", filename.c_str()); + return false; // 文件打开失败 + } + + // 将结构体的三个数据按1个字节的存储到文件 + configFile.put(static_cast(layout.batteryDisplay)); // 第一字节:电池图标样式 + configFile.put(static_cast(layout.timeDisplay)); // 第二字节:时间label位置 + configFile.put(static_cast(layout.colorTypeSet)); // 第三字节:字体颜色 + + // 关闭文件 + configFile.close(); + return true; // 成功写入配置 + } + + /* + 从配置文件读取设置 + 配置文件名:表盘ID + 第一个字节:电池图标样式 + 第二个字节:时间label位置 + 第三个字节:字体颜色 + */ + bool SetDialModel::loadDialConfig(int Id, DialLayout &layout) + { + std::string filename = APP_CUSTOM_DIAL_PATH + std::to_string(Id) + ".bin"; + + // 打开文件 + std::ifstream configFile(filename, std::ios::binary); + if (!configFile.is_open()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Unable to open config file for reading: %s", filename.c_str()); + return false; // 文件打开失败 + } + + // 读取配置 + char settings[3]; // 读取3个字节 + configFile.read(settings, sizeof(settings)); + if (configFile.gcount() != sizeof(settings)) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to read config file or insufficient file content!"); + configFile.close(); + return false; // 读取失败 + } + + // 为成员变量赋值 + layout.batteryDisplay = static_cast(settings[0]); // 电池图标样式 + layout.timeDisplay = static_cast(settings[1]); // 时间label位置 + layout.colorTypeSet = static_cast(settings[2]); // 字体颜色 + + // 关闭文件 + configFile.close(); + return true; // 成功读取配置 + } + + /*创建文件夹*/ + void SetDialModel::CreateDirectoryIfNotExists(const std::string &path) + { + struct stat info; + if (stat(path.c_str(), &info) != 0) + { + if (mkdir(path.c_str(), 0755) != 0) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error: Could not create directory %s. Errno: %d", path.c_str(), errno); + } + else + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Created directory: %s", path.c_str()); + } + } + else if (info.st_mode & S_IFDIR) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Directory already exists: %s", path.c_str()); + } + } + + /* + 获取存储文件夹 + Id(0~4)通过ID返回对应的文件夹路径 + 0 + 1 + 2 + 3 + 4 + */ + std::string SetDialModel::GetSaveDir(int Id) + { + // 首先检查Id是否在0~4的范围内 + if (Id < 0 || Id > 4) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error: Id must be between 0 and 4."); + return ""; // 或者返回一个空字符串 + } + // 检查 APP_CUSTOM_DIAL_PATH 是否存在 + CreateDirectoryIfNotExists(APP_CUSTOM_DIAL_PATH); + + std::string folderPath = std::string(APP_CUSTOM_DIAL_PATH) + "/" + std::to_string(Id); // 修复这一行 + CreateDirectoryIfNotExists(folderPath); // 确保目录存在 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Directory is %s", folderPath.c_str()); + + return folderPath; // 返回目录路径 + } } -- 2.17.1