修改部分bug
authorCaisking <3449750381@qq.com>
Tue, 9 Jan 2024 02:07:38 +0000 (10:07 +0800)
committerCaisking <3449750381@qq.com>
Tue, 9 Jan 2024 02:07:38 +0000 (10:07 +0800)
code/sdk/application/wearable/nativeabilityfwk/src/Ability.cpp
code/sdk/application/wearable/nativeapp/nativeui/dial/include/Digital_clockView.h
code/sdk/application/wearable/nativeapp/nativeui/dial/src/Digital_clockView.cpp
code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/clocks/maliao.json
code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/clocks/momo.json

index e63d37e..e4d84d2 100755 (executable)
@@ -52,17 +52,21 @@ void Ability::StartSlice(const uint16_t sliceId, const uint16_t priority)
 
     WEARABLE_LOGI(WEARABLE_LOG_MODULE_LAUNCHER_FWK, "Ability::StartSlice Size = %d", allSlices_.size());
     // 记录最多gMaxHistorySlices个页面id
+    //printf("Function: %s, Line: %d\n", __PRETTY_FUNCTION__, __LINE__);
     if (allSlices_.size() == gMaxHistorySlices) {
         allSlices_.erase(allSlices_.begin());
     }
+    //printf("Function: %s, Line: %d\n", __PRETTY_FUNCTION__, __LINE__);
     allSlices_.push_back(sliceId);
-
+    //printf("Function: %s, Line: %d\n", __PRETTY_FUNCTION__, __LINE__);
     curSlice_.sliceId = sliceId;
     curSlice_.slice = nextSlice;
     curSlicePriority_ = priority;
 
     nextSlice->OnStart();
+    //printf("Function: %s, Line: %d\n", __PRETTY_FUNCTION__, __LINE__);
     nextSlice->OnResume();
+    //printf("Function: %s, Line: %d\n", __PRETTY_FUNCTION__, __LINE__);
 }
 
 void Ability::ReleaseCurSlice() {
index cd77fd3..ac67d75 100644 (file)
@@ -219,6 +219,7 @@ namespace OHOS
         void DeInit();
         void setJsonFilePath(const std::string &filePath);
         void updateClockTime(const std::string &stringtime);
+        void FreeImagePathsArray(const char **imagePaths);
         const char **CreateImagePathsArray(const char *basePath, const char *fileExtension, int numImages);
         void addImageView(int left, int top, const char *imagePath, const char *viewId, int offsetX, int offsetY);
 
@@ -249,7 +250,7 @@ namespace OHOS
         UILabel *batteryLable = nullptr;
         UIBoxProgress *progress = nullptr;
         UISweepClock *sweepClock1_ = nullptr;
-        UIImageAnimatorView *backgroundAnimator = nullptr; 
+        UIImageAnimatorView *backgroundAnimator = nullptr;
 
         AnimatorCallback *mytimeimageCallback_ = nullptr;
         Animator *timeimageanimator_ = nullptr;
@@ -260,9 +261,9 @@ namespace OHOS
         AnimatorCallback *sweepClock1callback_ = nullptr;
         Animator *sweepClock1animator_ = nullptr;
         bool viewiInitStatus{false};
+        const char **imagePaths;
         std::string stringtime;
         std::string JosnfilePath = "/user/res/clocks/maomao.json";
-        ;
     };
 } // namespace OHOS
 #endif // Digital_clock_PRESENTER_H
index d311713..fe958d3 100644 (file)
@@ -2,6 +2,7 @@
 #include "dial/include/Digital_clockView.h"
 
 #include "common/image_cache_manager.h"
+#include "components/ui_view_group.h"
 #include "UiConfig.h"
 #include "ui_resource_image.h"
 #include <ui_scroll_view.h>
 #include "cJSON.h"
 #include <dirent.h>
 
+
 #define PRINT_DEBUG_INFO printf("Function: %s, Line: %d\n", __PRETTY_FUNCTION__, __LINE__);
 
 namespace OHOS
 {
-    extern "C"
-    {
-        cJSON *cjson_json = NULL;
-    }
+    cJSON *cjson_json = NULL;
     void Digital_clockView::setJsonFilePath(const std::string &filePath)
     {
         JosnfilePath = filePath;
     }
-    void loadJsonData(const std::string &filePath)
+    bool loadJsonData(const std::string &filePath)
     {
+
         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 *new_cjson_json = cJSON_Parse(json_string.c_str());
-            if (new_cjson_json != NULL)
-            {
-                // 释放旧的 JSON 数据
-                if (cjson_json != NULL)
-                {
-                    cJSON_Delete(cjson_json);
-                }
-                // 使用新的 JSON 数据
-                cjson_json = new_cjson_json;
-            }
-            else
-            {
-                printf("Failed to parse JSON file: %s\n", filePath.c_str());
-            }
+            cjson_json = cJSON_Parse(json_string.c_str());
+            return true;
         }
         else
         {
             printf("Failed to open JSON file: %s\n", filePath.c_str());
         }
+        return false;
     }
     static Digital_clockView *g_pDigital_clockView{nullptr};
     Digital_clockView::Digital_clockView()
@@ -74,17 +62,29 @@ namespace OHOS
 
     const char **Digital_clockView::CreateImagePathsArray(const char *basePath, const char *fileExtension, int numImages)
     {
-        const char **imagePaths = new const char *[numImages];
+        const char **imagePaths = new const char *[numImages + 1]; // 多分配一个元素用于存储数组大小
         char buffer[256];
+        int total_len = 0;
         for (int i = 0; i < numImages; i++)
         {
-            snprintf(buffer, sizeof(buffer), "%s%d.%s", basePath, i + 1, fileExtension);
+            int filename_len = snprintf(buffer, sizeof(buffer), "%s%d.%s", basePath, i + 1, fileExtension);
             imagePaths[i] = strdup(buffer);
-            //printf("imagePaths[%d]:%s\n", i, imagePaths[i]);
+            total_len += filename_len;
         }
+        imagePaths[numImages] = reinterpret_cast<const char *>(numImages); // 将数组大小信息存储在最后一个元素中
         return imagePaths;
     }
 
+    void Digital_clockView::FreeImagePathsArray(const char **imagePaths)
+    {
+        int numImages = reinterpret_cast<int>(imagePaths[-1]); // 从最后一个元素中获取数组大小
+        for (int i = 0; i < numImages; i++)
+        {
+            free((void *)imagePaths[i]);
+        }
+        delete[] imagePaths;
+    }
+
     bool isDirectory(const std::string &path)
     {
         if (!path.empty() && path.back() == '/')
@@ -115,7 +115,7 @@ namespace OHOS
             {
                 p_TraingWinView = new TraingWinView();
             }
-            PRINT_DEBUG_INFO
+
             cJSON *size = cJSON_GetObjectItem(background_cjson, "size");
             cJSON *coordinates = cJSON_GetObjectItem(background_cjson, "coordinates");
             int background_count = cJSON_GetObjectItem(background_cjson, "background_count")->valueint;
@@ -124,15 +124,12 @@ namespace OHOS
             int background_top = cJSON_GetObjectItem(coordinates, "top")->valueint;
             int background_width = cJSON_GetObjectItem(size, "width")->valueint;
             int background_height = cJSON_GetObjectItem(size, "height")->valueint;
-            PRINT_DEBUG_INFO
-            const char **imagePaths = CreateImagePathsArray(backgroundimagesPath.c_str(), "bin", background_count);
-            PRINT_DEBUG_INFO
+            imagePaths = CreateImagePathsArray(backgroundimagesPath.c_str(), "bin", background_count);
             p_TraingWinView->InitImageAnimatorSource(backgroundAnimator, imagePaths, background_count, background_left, background_top,
                                                      background_width, background_height, "imageAnimator1", background_FPS);
         }
         else
         {
-            PRINT_DEBUG_INFO
             // Get the file extension
             std::string backgroundImagePathStr = backgroundimagesPath;
             std::string fileExtension = backgroundImagePathStr.substr(backgroundImagePathStr.find_last_of(".") + 1);
@@ -147,6 +144,7 @@ namespace OHOS
                 // Add code to set the background as a video
             }
         }
+        PRINT_DEBUG_INFO
     }
     /*初始化表盘时钟*/
     // Function to create and add an image view
@@ -238,20 +236,20 @@ namespace OHOS
         cJSON *minute_coordinates = cJSON_GetObjectItem(minuteHand, "coordinates");
         cJSON *second_coordinates = cJSON_GetObjectItem(secondHand, "coordinates");
         const char *handspath = cJSON_GetObjectItem(cjson_json, "handspath")->valuestring;
-
+        PRINT_DEBUG_INFO
         signed short HOUR_POINT_POSITION_X = static_cast<signed short>(cJSON_GetObjectItem(hour_coordinates, "HOUR_POINT_POSITION_X")->valueint);
         signed short HOUR_POINT_POSITION_Y = static_cast<signed short>(cJSON_GetObjectItem(hour_coordinates, "HOUR_POINT_POSITION_Y")->valueint);
         signed short HOUR_POINT_CENTER_X = static_cast<signed short>(cJSON_GetObjectItem(hour_coordinates, "HOUR_POINT_CENTER_X")->valueint);
         signed short HOUR_POINT_CENTER_Y = static_cast<signed short>(cJSON_GetObjectItem(hour_coordinates, "HOUR_POINT_CENTER_Y")->valueint);
         signed short MINUTE_POINT_POSITION_X = static_cast<signed short>(cJSON_GetObjectItem(minute_coordinates, "MINUTE_POINT_POSITION_X")->valueint);
         signed short MINUTE_POINT_POSITION_Y = static_cast<signed short>(cJSON_GetObjectItem(minute_coordinates, "MINUTE_POINT_POSITION_Y")->valueint);
-        signed short MINUTE_POINT_CENTER_X = static_cast<signed short>(cJSON_GetObjectItem(minute_coordinates, "MINUTE_POINT_CENTER_Y")->valueint);
+        signed short MINUTE_POINT_CENTER_X = static_cast<signed short>(cJSON_GetObjectItem(minute_coordinates, "MINUTE_POINT_CENTER_X")->valueint);
         signed short MINUTE_POINT_CENTER_Y = static_cast<signed short>(cJSON_GetObjectItem(minute_coordinates, "MINUTE_POINT_CENTER_Y")->valueint);
         signed short SECOND_POINT_POSITION_X = static_cast<signed short>(cJSON_GetObjectItem(second_coordinates, "SECOND_POINT_POSITION_X")->valueint);
         signed short SECOND_POINT_POSITION_Y = static_cast<signed short>(cJSON_GetObjectItem(second_coordinates, "SECOND_POINT_POSITION_Y")->valueint);
         signed short SECOND_POINT_CENTER_X = static_cast<signed short>(cJSON_GetObjectItem(second_coordinates, "SECOND_POINT_CENTER_X")->valueint);
         signed short SECOND_POINT_CENTER_Y = static_cast<signed short>(cJSON_GetObjectItem(second_coordinates, "SECOND_POINT_CENTER_Y")->valueint);
-
+        PRINT_DEBUG_INFO
         // Add SweepClock to App
         if (sweepClock1_ == nullptr)
         {
@@ -271,7 +269,9 @@ namespace OHOS
         sweepClock1_->SetWorkMode(UISweepClock::WorkMode::NORMAL);
         Image *sweepClock1HourImage = new Image();
         std::string HourImagePath = std::string(handspath) + "hour.bin";
+        PRINT_DEBUG_INFO
         sweepClock1HourImage->SetSrc(HourImagePath.c_str());
+        PRINT_DEBUG_INFO
         sweepClock1_->SetHandImage(UISweepClock::HandType::HOUR_HAND, *sweepClock1HourImage, {HOUR_POINT_POSITION_X, HOUR_POINT_POSITION_Y}, {HOUR_POINT_CENTER_X, HOUR_POINT_CENTER_Y});
 
         Image *sweepClock1MinuteImage = new Image();
@@ -436,22 +436,23 @@ namespace OHOS
             container_->SetPosition(0, 0, 466, 466);
             container_->SetStyle(STYLE_BACKGROUND_OPA, 0);
         }
-        loadJsonData(JosnfilePath);
-        std::string Dialtype = cJSON_GetObjectItem(cjson_json, "Dialtype")->valuestring;
-        Initbackground();
-        PRINT_DEBUG_INFO
-        if (Dialtype == "pointer")
+        if (loadJsonData(JosnfilePath))
         {
-            Initpointer();
-        }
-        else
-        {
-            Initdigit();
+            std::string Dialtype = cJSON_GetObjectItem(cjson_json, "Dialtype")->valuestring;
+            Initbackground();
+            if (Dialtype == "pointer")
+            {
+                Initpointer();
+            }
+            else
+            {
+                Initdigit();
+            }
+            Initdata();
+            Initweek();
+            Initbattery();
+            cJSON_Delete(cjson_json);
         }
-        Initdata();
-        Initweek();
-        Initbattery();
-        cJSON_Delete(cjson_json);
     }
     /* DeInit:删除组件*/
     void Digital_clockView::DeInit()
@@ -459,6 +460,7 @@ namespace OHOS
         UIView *view = static_cast<UIViewGroup *>(container_)->GetChildrenHead();
         while (view != nullptr)
         {
+            PRINT_DEBUG_INFO
             UIView *tempView = view;
             view = view->GetNextSibling();
             if (tempView->GetParent())
@@ -467,6 +469,7 @@ namespace OHOS
             }
             delete tempView;
         }
+        // FreeImagePathsArray(imagePaths);
         if (timeLableanimator_ != nullptr)
         {
             timeLableanimator_->Stop();
@@ -487,7 +490,13 @@ namespace OHOS
         }
         if (sweepClock1animator_ != nullptr)
         {
+            PRINT_DEBUG_INFO
+            sweepClock1_->RemoveAll();
+    
+            delete sweepClock1_;
+            sweepClock1_ = nullptr;
             sweepClock1animator_->Stop();
+            PRINT_DEBUG_INFO
             delete sweepClock1animator_;
             sweepClock1animator_ = nullptr;
         }
@@ -498,14 +507,17 @@ namespace OHOS
 
     void Digital_clockView::PreLoad(void)
     {
+
         if (!viewiInitStatus)
         {
+
             InitView();
             viewiInitStatus = true;
         }
     }
     void Digital_clockView::OnStart()
     {
+        PRINT_DEBUG_INFO
         container_ = new UIViewGroup();
         if (container_ == nullptr)
         {
@@ -514,15 +526,14 @@ namespace OHOS
         container_->SetPosition(0, 0);
         container_->SetWidth(Screen::GetInstance().GetWidth());
         container_->SetHeight(Screen::GetInstance().GetHeight());
-
         // 创建 draglistener 实例并设置为拖动监听器
         UIView::OnDragListener *draglistener = static_cast<UIView::OnDragListener *>(Digital_clockPresenter::GetInstance());
+
         if (draglistener == nullptr)
         {
             return;
         }
         container_->SetOnDragListener(draglistener);
-
         container_->SetTouchable(true);
         container_->SetDraggable(true);
         BaseView::RootView(Color::Black().full);
index 052f437..4e02537 100644 (file)
@@ -3,7 +3,7 @@
     "digitspath": "/user/res/clocks/maliao/",\r
     "digits": {\r
         "timeformat": "24format",\r
-        "playformat": "picturesdisplay",\r
+        "displayformat": "picturesdisplay",\r
         "digitspath": "/user/res/clocks/maliao/",\r
         "digitsize": 40,\r
         "picturesdisplay": {\r
index b68eb54..175c666 100644 (file)
@@ -65,7 +65,7 @@
                 "height": 19\r
             },\r
             "coordinates": {\r
-                "left": 177,\r
+                "left": 179,\r
                 "top": 58\r
             }\r
         },\r