209 lines
7.0 KiB
C++
209 lines
7.0 KiB
C++
/*----------------------------------------------------------------------------
|
||
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
|
||
*
|
||
* Description:
|
||
*
|
||
* Author: huangshuyi
|
||
*
|
||
* Create: 2024-8
|
||
*--------------------------------------------------------------------------*/
|
||
|
||
#include "TjdUiWFCustomParse.h"
|
||
#include "TjdUiImageIds.h"
|
||
#include "cJSON.h"
|
||
#include "common/image_cache_manager.h"
|
||
#include "common/key_code.h"
|
||
#include "components/ui_image_view.h"
|
||
#include "ohos_types.h"
|
||
#include "rtc_api.h"
|
||
#include "sys_config.h"
|
||
#include <dirent.h>
|
||
#include <fstream>
|
||
#include <sstream>
|
||
|
||
using namespace OHOS;
|
||
|
||
namespace TJD {
|
||
|
||
#define ONLINE_WF_CONFIG_FILE_NAME "wf_config_0.json"
|
||
|
||
#define ENABLE_PRINT_INFO 1
|
||
#if ENABLE_PRINT_INFO
|
||
#define static_print_info(...) sys_ui_log_i(__VA_ARGS__) //一般信息打印宏控制
|
||
#define static_print_warn(...) sys_ui_log_w(__VA_ARGS__) //警告信息打印一般常开
|
||
#define static_print_error(...) sys_ui_log_e(__VA_ARGS__) //错误信息打印一般常开
|
||
#define static_print_debug(...) sys_ui_log_d(__VA_ARGS__)
|
||
#else
|
||
#define static_print_info(...)
|
||
#define static_print_warn(...)
|
||
#define static_print_error(...)
|
||
#define static_print_debug(...)
|
||
#endif
|
||
enum class PictureType : int
|
||
{
|
||
BITMAP,
|
||
MULBITMAP,
|
||
VIDEO,
|
||
};
|
||
|
||
TjdUiWFCustomParse::TjdUiWFCustomParse() {}
|
||
|
||
TjdUiWFCustomParse::~TjdUiWFCustomParse() {}
|
||
|
||
void TjdUiWFCustomParse::Unload()
|
||
{
|
||
if (VideoPage != nullptr) {
|
||
delete VideoPage;
|
||
VideoPage = nullptr;
|
||
}
|
||
|
||
if (VideoPreviewPath.size()) {
|
||
static_print_info("VideoPreviewPath:%s", VideoPreviewPath.c_str());
|
||
ImageCacheManager::GetInstance().UnloadSingleRes(VideoPreviewPath);
|
||
VideoPreviewPath.clear();
|
||
}
|
||
if (SingleImgPath.size()) {
|
||
static_print_info("SingleImgPath:%s", VideoPreviewPath.c_str());
|
||
ImageCacheManager::GetInstance().UnloadSingleRes(SingleImgPath);
|
||
SingleImgPath.clear();
|
||
}
|
||
if (MultipleImgPath.size()) {
|
||
static_print_info("MultipleImgPath:%s", VideoPreviewPath.c_str());
|
||
ImageCacheManager::GetInstance().UnloadAllInMultiRes(MultipleImgPath);
|
||
MultipleImgPath.clear();
|
||
}
|
||
}
|
||
|
||
int TjdUiWFCustomParse::FindOnlineWfConfigFile(std::string &jsonContext)
|
||
{
|
||
struct dirent *direntp;
|
||
// 打开目录
|
||
DIR *dirp = opendir(TJD_FS_DIR_WF);
|
||
// 遍历文件
|
||
if (dirp != nullptr) {
|
||
std::string DirPrePath(TJD_FS_DIR_WF);
|
||
while ((direntp = readdir(dirp)) != nullptr) {
|
||
std::string filename(direntp->d_name);
|
||
std::string fullPath = DirPrePath + "/" + filename;
|
||
if (filename.find(ONLINE_WF_CONFIG_FILE_NAME) != std::string::npos) {
|
||
static_print_debug("FindOnlineWfConfigFile::find file:%s", filename.c_str());
|
||
std::ifstream file(fullPath);
|
||
if (!file.is_open()) {
|
||
static_print_error("FindOnlineWfConfigFile::%s open file err!", __func__);
|
||
return OHOS_FAILURE;
|
||
}
|
||
// 使用 stringstream 读取文件内容
|
||
std::stringstream buffer;
|
||
buffer << file.rdbuf();
|
||
// 将文件内容存储到字符串中
|
||
jsonContext = buffer.str();
|
||
// 关闭文件
|
||
file.close();
|
||
break;
|
||
}
|
||
}
|
||
} else {
|
||
static_print_debug("FindOnlineWfConfigFile::%s opendir err!", __func__);
|
||
return OHOS_FAILURE;
|
||
}
|
||
// 关闭目录
|
||
closedir(dirp);
|
||
return OHOS_SUCCESS;
|
||
}
|
||
|
||
static bool CheckFileExtension(const std::string &filePath, const std::string &extension = "jpg")
|
||
{
|
||
// 找到最后一个点的位置
|
||
size_t dotPos = filePath.rfind('.');
|
||
if (dotPos == std::string::npos) {
|
||
// 如果没有找到点,返回false
|
||
return false;
|
||
}
|
||
|
||
// 获取扩展名
|
||
std::string extension1 = filePath.substr(dotPos + 1);
|
||
|
||
// 判断扩展名是否是jpg或bin
|
||
if (extension == extension1) {
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
int TjdUiWFCustomParse::CreateView(std::string &jsonContext, UIView *parent)
|
||
{
|
||
UIViewGroup *cont = (UIViewGroup *)parent;
|
||
cJSON *cjson_root = cJSON_Parse(jsonContext.c_str());
|
||
if (cjson_root == NULL) {
|
||
static_print_error("parse fail.");
|
||
return OHOS_FAILURE;
|
||
}
|
||
cJSON *cjsonType = cJSON_GetObjectItem(cjson_root, "wf_type");
|
||
if (cjsonType == NULL) {
|
||
static_print_error("ParseJsonFile::wf_type is null");
|
||
cJSON_Delete(cjson_root);
|
||
return OHOS_FAILURE;
|
||
}
|
||
int type = cjsonType->valueint;
|
||
cJSON *cjsonBgRes = cJSON_GetObjectItem(cjson_root, "bg_res");
|
||
if (cjsonBgRes == NULL) {
|
||
static_print_error("ParseJsonFile::cjsonBgRes is null");
|
||
cJSON_Delete(cjson_root);
|
||
return OHOS_FAILURE;
|
||
}
|
||
ImageInfo *imgInfo{nullptr};
|
||
if ((PictureType)type == PictureType::VIDEO) {
|
||
VideoPath = std::string(cJSON_GetStringValue(cjsonBgRes));
|
||
VideoPreviewPath = std::string(cJSON_GetStringValue(cJSON_GetObjectItem(cjson_root, "video_preview")));
|
||
VideoPage = new TjdUiWFCustomVideoPage(parent, VideoPath, std::string(VideoPreviewPath));
|
||
static_print_debug("VideoPreviewPath:%s", VideoPreviewPath.c_str());
|
||
} else if ((PictureType)type == PictureType::BITMAP) {
|
||
SingleImgPath = std::string(cJSON_GetStringValue(cjsonBgRes));
|
||
imgInfo = ImageCacheManager::GetInstance().LoadSingleRes(SingleImgPath);
|
||
UIImageView *backGround = new UIImageView();
|
||
cont->Add(backGround);
|
||
backGround->SetPosition(0, 0);
|
||
backGround->SetSrc(imgInfo);
|
||
} else {
|
||
cJSON *cjsonImgNumber = cJSON_GetObjectItem(cjson_root, "img_number");
|
||
if (cjsonImgNumber) {
|
||
if (cjsonImgNumber->valueint > 1) {
|
||
static_print_info("cjsonImgNumber->valueint:%d", cjsonImgNumber->valueint);
|
||
MultipleImgPath = std::string(cJSON_GetStringValue(cjsonBgRes));
|
||
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(0x100001, MultipleImgPath);
|
||
} else {
|
||
SingleImgPath = std::string(cJSON_GetStringValue(cjsonBgRes));
|
||
imgInfo = ImageCacheManager::GetInstance().LoadSingleRes(SingleImgPath);
|
||
}
|
||
}
|
||
if (imgInfo == nullptr) {
|
||
static_print_error("load bg image failed");
|
||
}
|
||
UIImageView *backGround = new UIImageView();
|
||
cont->Add(backGround);
|
||
backGround->SetPosition(0, 0);
|
||
backGround->SetSrc(imgInfo);
|
||
}
|
||
cJSON_Delete(cjson_root);
|
||
return OHOS_SUCCESS;
|
||
}
|
||
|
||
bool TjdUiWFCustomParse::ParseOnlineWfData(UIView *parent)
|
||
{
|
||
static_print_info("ParseOnlineWfData");
|
||
// 1.读取json文件
|
||
std::string jsonContext;
|
||
if (FindOnlineWfConfigFile(jsonContext) == OHOS_FAILURE) {
|
||
static_print_error("FindOnlineWfConfigFile failed");
|
||
return false;
|
||
}
|
||
if (CreateView(jsonContext, parent) == OHOS_FAILURE) {
|
||
static_print_error("CreateView failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
} // namespace TJD
|