#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()
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() == '/')
{
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;
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);
// Add code to set the background as a video
}
}
+ PRINT_DEBUG_INFO
}
/*初始化表盘时钟*/
// Function to create and add an image view
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)
{
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();
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()
UIView *view = static_cast<UIViewGroup *>(container_)->GetChildrenHead();
while (view != nullptr)
{
+ PRINT_DEBUG_INFO
UIView *tempView = view;
view = view->GetNextSibling();
if (tempView->GetParent())
}
delete tempView;
}
+ // FreeImagePathsArray(imagePaths);
if (timeLableanimator_ != nullptr)
{
timeLableanimator_->Stop();
}
if (sweepClock1animator_ != nullptr)
{
+ PRINT_DEBUG_INFO
+ sweepClock1_->RemoveAll();
+
+ delete sweepClock1_;
+ sweepClock1_ = nullptr;
sweepClock1animator_->Stop();
+ PRINT_DEBUG_INFO
delete sweepClock1animator_;
sweepClock1animator_ = nullptr;
}
void Digital_clockView::PreLoad(void)
{
+
if (!viewiInitStatus)
{
+
InitView();
viewiInitStatus = true;
}
}
void Digital_clockView::OnStart()
{
+ PRINT_DEBUG_INFO
container_ = new UIViewGroup();
if (container_ == nullptr)
{
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);