增加自定义表盘配置文件相对应接口
authorCaisking <3449750381@qq.com>
Fri, 26 Jul 2024 09:30:56 +0000 (17:30 +0800)
committerCaisking <3449750381@qq.com>
Fri, 26 Jul 2024 09:30:56 +0000 (17:30 +0800)
code/sdk/application/wearable/fd230app/nativeui/clock/include/MainClockView.h
code/sdk/application/wearable/fd230app/nativeui/clock/src/MainClockView.cpp
code/sdk/application/wearable/fd230app/nativeui/set_dial/include/SetDialModel.h
code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp

index 5113f3c..5c49207 100644 (file)
@@ -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};
index 05a97ff..87739fb 100644 (file)
@@ -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<BatteryDisplayType>(settings[0]); // 第一个字节:电池图标样式
-        timePosition = static_cast<TimeDisplayPosition>(settings[1]);  // 第二个字节:时间label位置
-        colorType = static_cast<ColorTypeSet>(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)
     {
index ea6abd2..8bc398d 100644 (file)
@@ -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<uint16_t, std::string> &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);
index 0080f3a..e60864a 100644 (file)
 #include <string>
 #include <sstream>
 #include <iomanip>
+#include <iostream>
+#include <fstream>
 #include <stdio.h>
+#include <sys/stat.h>
+#include <unistd.h>
 #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<char>(layout.batteryDisplay)); // 第一字节:电池图标样式
+        configFile.put(static_cast<char>(layout.timeDisplay));    // 第二字节:时间label位置
+        configFile.put(static_cast<char>(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<BatteryDisplayType>(settings[0]); // 电池图标样式
+        layout.timeDisplay = static_cast<TimeDisplayPosition>(settings[1]);   // 时间label位置
+        layout.colorTypeSet = static_cast<ColorTypeSet>(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; // 返回目录路径
+    }
 
 }