添加全局的ConfigModel类,用以读取保存组件状态json文件
authorluozheng <1732036298@qq.com>
Wed, 28 Feb 2024 10:29:24 +0000 (18:29 +0800)
committerluozheng <1732036298@qq.com>
Wed, 28 Feb 2024 10:29:24 +0000 (18:29 +0800)
code/sdk/application/wearable/fd230app/CMakeLists.txt
code/sdk/application/wearable/fd230app/nativelauncher/src/NativeLauncher.cpp
code/sdk/application/wearable/fd230app/nativeui/config/ConfigModel.json [new file with mode: 0644]
code/sdk/application/wearable/fd230app/nativeui/config/include/ConfigModel.h [new file with mode: 0644]
code/sdk/application/wearable/fd230app/nativeui/config/src/ConfigModel.cpp [new file with mode: 0644]
code/sdk/application/wearable/fd230app/nativeui/displaybrightness/include/DisplayBrightnessModel.h
code/sdk/application/wearable/fd230app/nativeui/displaybrightness/src/DisplayBrightnessModel.cpp
code/sdk/application/wearable/fd230app/nativeui/displaybrightness/src/DisplayBrightnessPresent.cpp
code/sdk/application/wearable/fd230app/nativeui/displaybrightness/src/DisplayBrightnessView.cpp
code/sdk/build/config/target_config/brandy/mk_fs_image/fd230/fs/user/res/ConfigModel/ConfigModel.json [new file with mode: 0644]

index 472bdbe..4cd279d 100644 (file)
@@ -240,6 +240,8 @@ set(SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/traing/src/TraingView.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/language/LanguageInit.cpp
 
+    ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/config/src/ConfigModel.cpp
+
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/weather/src/MainWeatherPresenter.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/weather/src/MainWeatherView.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/weather/src/WeatherReportPresenter.cpp
index ad5a1c7..034dabb 100644 (file)
 #include "dialupdate/include/DialUpdatePresenter.h"
 #include "dialupdate/include/DialUpdateView.h"
 
+#include "config/include/ConfigModel.h"
+
 #ifdef SUPPORT_ALIPAY_SEC
 #include "AlipayMainView.h"
 #include "AlipayPresenter.h"
@@ -225,6 +227,11 @@ namespace OHOS
         }
 #endif
         /* -------------------------------------------------------------------------- */
+
+        /* ------------------------------- ConfigModel ------------------------------ */
+        ConfigModel::GetInstance()->LoadJsonData();
+        /* -------------------------------------------------------------------------- */
+
         // 初始化所有预加载slice
         InitBtHeadsetEvent(); // 蓝牙ct和tg适配初始化
         InitAllNativeSlices();
diff --git a/code/sdk/application/wearable/fd230app/nativeui/config/ConfigModel.json b/code/sdk/application/wearable/fd230app/nativeui/config/ConfigModel.json
new file mode 100644 (file)
index 0000000..7897dae
--- /dev/null
@@ -0,0 +1,3 @@
+{
+    "RaiseToBrightnessCheckBoxButtonState":true
+}
\ No newline at end of file
diff --git a/code/sdk/application/wearable/fd230app/nativeui/config/include/ConfigModel.h b/code/sdk/application/wearable/fd230app/nativeui/config/include/ConfigModel.h
new file mode 100644 (file)
index 0000000..ce53ac0
--- /dev/null
@@ -0,0 +1,127 @@
+#ifndef _CONFIG_MODEL_H_
+#define _CONFIG_MODEL_H_
+
+#include <iostream>
+#include <fstream>
+#include <filesystem>
+#include "cJSON.h"
+#include "wearable_log.h"
+
+namespace OHOS
+{
+    class ConfigModel
+    {
+    public:
+        static ConfigModel *GetInstance();
+        cJSON *GetcJSON(void);
+        bool LoadJsonData(void);
+        template <class T>
+        T ReadJsonData(const cJSON *json_obj, const char *field_name);
+        template <class T>
+        void WriteJsonData(cJSON *json_obj, const char *field_name, T data);
+
+    private:
+        ConfigModel();                                        // 私有构造函数,确保无法在外部实例化
+        ConfigModel(const ConfigModel &) = delete;            // 禁止拷贝构造
+        ConfigModel &operator=(const ConfigModel &) = delete; // 禁止赋值运算符
+        ~ConfigModel();
+
+    private:
+        const std::string filePath = "/user/res/ConfigModel/ConfigModel.json";
+        cJSON *cjson_json{nullptr};
+        int RaiseToBrightnessCheckBoxButtonState;
+    };
+
+    /// @brief 通用的读取数据函数,根据字段名称从 JSON 对象中读取对应值
+    /// @tparam T
+    /// @param json_obj json 对象 field_name 字段名称
+    /// @return 读取到的对应值
+    template <class T>
+    T ConfigModel::ReadJsonData(const cJSON *json_obj, const char *field_name)
+    {
+        const cJSON *field_item = cJSON_GetObjectItem(json_obj, field_name);
+        if (field_item != NULL)
+        {
+            if (cJSON_IsString(field_item))
+            {
+                return T(field_item->valuestring);
+            }
+            else if (cJSON_IsNumber(field_item))
+            {
+                return T(field_item->valuedouble);
+            }
+            else if (cJSON_IsBool(field_item))
+            {
+                return T(field_item->valueint);
+            }
+            else if (cJSON_IsArray(field_item))
+            {
+                return T(cJSON_GetArraySize(field_item));
+            }
+            else
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Unsupported data type");
+                return T("Unsupported data type");
+            }
+        }
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Field not found");
+        return T("Field not found");
+    }
+
+    /// @brief 通用的写JSON数据函数
+    /// @tparam T
+    /// @param json_obj json对象
+    /// @param field_name 字段名称
+    /// @param data 要写入的数据
+    template <class T>
+    void ConfigModel::WriteJsonData(cJSON *json_obj, const char *field_name, T data)
+    {
+        if (json_obj == nullptr)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "json_obj is nullptr!");
+            return;
+        }
+
+        cJSON *data_obj = cJSON_CreateNull();
+        if constexpr (std::is_same<T, int>::value || std::is_same<T, long>::value || std::is_same<T, long long>::value)
+        {
+            data_obj = cJSON_CreateNumber(static_cast<double>(data));
+        }
+        else if constexpr (std::is_same<T, double>::value || std::is_same<T, float>::value)
+        {
+            data_obj = cJSON_CreateNumber(data);
+        }
+        else if constexpr (std::is_same<T, bool>::value)
+        {
+            data_obj = cJSON_CreateBool(data);
+        }
+        else if constexpr (std::is_same<T, const char *>::value || std::is_same<T, std::string>::value)
+        {
+            data_obj = cJSON_CreateString(data);
+        }
+
+        if (data_obj != nullptr)
+        {
+            cJSON_ReplaceItemInObject(json_obj, field_name, data_obj);
+
+            char *jsonString = cJSON_Print(json_obj);
+            if (jsonString != nullptr)
+            {
+                FILE *file = fopen(filePath.c_str(), "wb");
+                if (file)
+                {
+                    //fputs(jsonString, file);
+                    fwrite(jsonString, 1, strlen(jsonString), file); // 将 JSON 字符串写入文件
+                    fclose(file);
+                }
+                free(jsonString);
+            }
+            else
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Open JSON filePath fail!");
+            }
+        }
+    }
+
+}
+#endif
\ No newline at end of file
diff --git a/code/sdk/application/wearable/fd230app/nativeui/config/src/ConfigModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/config/src/ConfigModel.cpp
new file mode 100644 (file)
index 0000000..8d40ca8
--- /dev/null
@@ -0,0 +1,47 @@
+#include "config/include/ConfigModel.h"
+
+namespace OHOS
+{
+    ConfigModel::ConfigModel()
+    {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " ConfigModel::ConfigModel");
+    }
+
+    ConfigModel::~ConfigModel()
+    {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " ConfigModel::~ConfigModel");
+    }
+
+    ConfigModel *ConfigModel::GetInstance()
+    {
+        static ConfigModel instance;
+        return &instance;
+    }
+
+    /// @brief 获取cjson对象
+    cJSON *ConfigModel::GetcJSON(void)
+    {
+        return cjson_json;
+    }
+
+    /// @brief 加载json文件
+    /// @param filePath 指定要加载和解析的 JSON 文件的路径
+    /// @return true 加载json文件成功,反之失败
+    bool ConfigModel::LoadJsonData()
+    {
+        std::ifstream file(filePath);
+        if (file.is_open())
+        {
+            std::string json_string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
+            file.close();
+            // 解析JSON数据
+            cjson_json = cJSON_Parse(json_string.c_str());
+            return true;
+        }
+        else
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Open JSON filePath fail!:%s", filePath.c_str());
+        }
+        return false;
+    }
+}
\ No newline at end of file
index a06e95e..82e0b54 100644 (file)
@@ -13,7 +13,8 @@ namespace OHOS
         bool BrightnessControlViewSliderValuesInit(void);\r
         void KeepScreenDurationRadioButtonSlectStatus(unsigned int value);\r
         bool ScreenDurationRadioButtonSlectStatusInit(void);\r
-\r
+        void SetScreenDurationTime(unsigned int time);\r
+        unsigned int GetScreenDurationTime(void);\r
     private:\r
         DisplayBrightnessModel();\r
         DisplayBrightnessModel(const DisplayBrightnessModel &);\r
@@ -24,6 +25,7 @@ namespace OHOS
         bool RaiseToBrightnessCheckBoxButtonState{false};    // 抬腕按钮状态 ,默认补选中\r
         unsigned char ScreenBrightnessValue{255};            // 亮度值 ,默认最大亮度\r
         unsigned int ScreenDurationRadioButtonSlectValue{1}; // 亮屏时长 ,1对应10秒后熄屏\r
+        unsigned int ScreenDurationTime;\r
     };\r
 }\r
 \r
index 461fe63..ec7b5ff 100644 (file)
@@ -1,10 +1,15 @@
 #include "displaybrightness/include/DisplayBrightnessModel.h"
 #include "displaybrightness/include/DisplayBrightnessPresenter.h"
+#include "config/include/ConfigModel.h"
 #include "wearable_log.h"
 namespace OHOS
 {
 
-    DisplayBrightnessModel::DisplayBrightnessModel() {}
+    DisplayBrightnessModel::DisplayBrightnessModel()
+    {
+        ConfigModel *p_ConfigModel = ConfigModel::GetInstance();
+        RaiseToBrightnessCheckBoxButtonState = p_ConfigModel->ReadJsonData<bool>(p_ConfigModel->GetcJSON(), "RaiseToBrightnessCheckBoxButtonState");
+    }
 
     DisplayBrightnessModel::~DisplayBrightnessModel() {}
 
@@ -57,7 +62,7 @@ namespace OHOS
     }
 
     /// @brief 保存哪个亮屏时长按钮被选中
-    /// @param value 0x001 5秒后熄屏 0x010 10秒后熄屏 0x100 15秒后熄屏
+    /// @param value 0 5秒后熄屏 1 10秒后熄屏 2 15秒后熄屏
     void DisplayBrightnessModel::KeepScreenDurationRadioButtonSlectStatus(unsigned int value)
     {
         ScreenDurationRadioButtonSlectValue = value;
@@ -78,4 +83,16 @@ namespace OHOS
         return true;
     }
 
+    /// @brief 保存设置的亮屏时间,单位ms
+    /// @param time
+    void DisplayBrightnessModel::SetScreenDurationTime(unsigned int time)
+    {
+        ScreenDurationTime = time;
+    }
+
+    // 获取亮屏时长,单位ms
+    unsigned int DisplayBrightnessModel::GetScreenDurationTime(void)
+    {
+        return ScreenDurationTime;
+    }
 }
index 5917208..35a82a6 100644 (file)
@@ -227,18 +227,21 @@ namespace OHOS
                 {\r
                     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Switched to 5 seconds");\r
                     display_api->set_screen_auto_off_timeout(SCREENDURATIONFIVE);\r
+                    DisplayBrightnessModel::GetInstance()->SetScreenDurationTime(SCREENDURATIONFIVE);\r
                     return true;\r
                 }\r
                 else if (p_DisplayBrightnessView->GetScreenDurationView()->GetRadioButton(ScreenDurationRadioButton2) == (UIView *)m_view)\r
                 {\r
                     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Switched to 10 seconds");\r
                     display_api->set_screen_auto_off_timeout(SCREENDURATIONTEN);\r
+                    DisplayBrightnessModel::GetInstance()->SetScreenDurationTime(SCREENDURATIONTEN);\r
                     return true;\r
                 }\r
                 else if (p_DisplayBrightnessView->GetScreenDurationView()->GetRadioButton(ScreenDurationRadioButton3) == (UIView *)m_view)\r
                 {\r
                     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Switched to 15 seconds");\r
                     display_api->set_screen_auto_off_timeout(SCREENDURATIONFIFTEENTH);\r
+                    DisplayBrightnessModel::GetInstance()->SetScreenDurationTime(SCREENDURATIONFIFTEENTH);\r
                     return true;\r
                 }\r
             }\r
index 5671128..4b24a89 100644 (file)
@@ -1,6 +1,7 @@
 #include "displaybrightness/include/DisplayBrightnessView.h"\r
 #include "displaybrightness/include/DisplayBrightnessModel.h"\r
 #include "wearable_log.h"\r
+#include "config/include/ConfigModel.h"\r
 namespace OHOS\r
 {\r
     static DisplayBrightnessView *g_pDisplayBrightnessView{nullptr};\r
@@ -63,6 +64,12 @@ namespace OHOS
             if (state == UICheckBox::UICheckBoxState::SELECTED)\r
             {\r
                 DisplayBrightnessModel::GetInstance()->SetRaiseToBrightnessCheckBoxButtonStatus(true);\r
+                ConfigModel::GetInstance()->WriteJsonData(ConfigModel::GetInstance()->GetcJSON(),"RaiseToBrightnessCheckBoxButtonState",true);\r
+            }\r
+            else\r
+            {\r
+                DisplayBrightnessModel::GetInstance()->SetRaiseToBrightnessCheckBoxButtonStatus(false);\r
+                ConfigModel::GetInstance()->WriteJsonData(ConfigModel::GetInstance()->GetcJSON(),"RaiseToBrightnessCheckBoxButtonState",false);\r
             }\r
 \r
             delete RaiseToBrightnessCheckBoxButton;\r
diff --git a/code/sdk/build/config/target_config/brandy/mk_fs_image/fd230/fs/user/res/ConfigModel/ConfigModel.json b/code/sdk/build/config/target_config/brandy/mk_fs_image/fd230/fs/user/res/ConfigModel/ConfigModel.json
new file mode 100644 (file)
index 0000000..7897dae
--- /dev/null
@@ -0,0 +1,3 @@
+{
+    "RaiseToBrightnessCheckBoxButtonState":true
+}
\ No newline at end of file