216 lines
6.6 KiB
C++
216 lines
6.6 KiB
C++
/*----------------------------------------------------------------------------
|
||
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
|
||
*
|
||
* Description:
|
||
*
|
||
* Author: huangshuyi
|
||
*
|
||
* Create: 2024-8
|
||
*--------------------------------------------------------------------------*/
|
||
//brandy-native-tjd -nhso -release
|
||
|
||
#include "TjdUiAppOnlineWfParse.h"
|
||
#include "wearable_log.h"
|
||
#include "common/image_cache_manager.h"
|
||
#include <time.h>
|
||
#include <unordered_map>
|
||
#include "gfx_utils/image_info.h"
|
||
#include "gfx_utils/heap_base.h"
|
||
#include "TjdUiImageIds.h"
|
||
#include "power_display_service.h"
|
||
#include "TjdUiMemManage.h"
|
||
#include "sys_config.h"
|
||
#include "rtc_api.h"
|
||
#include "product_evb4_standard.h"
|
||
#include "common/key_code.h"
|
||
#include "sql_setting.h"
|
||
|
||
using namespace OHOS;
|
||
using namespace std;
|
||
|
||
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,
|
||
JPEG,
|
||
VIDEO,
|
||
};
|
||
|
||
TjdUiAppOnlineWfParse::TjdUiAppOnlineWfParse()
|
||
{
|
||
}
|
||
|
||
TjdUiAppOnlineWfParse::~TjdUiAppOnlineWfParse()
|
||
{
|
||
|
||
}
|
||
|
||
TjdUiAppOnlineWfParse* TjdUiAppOnlineWfParse::GetInstance(void)
|
||
{
|
||
static TjdUiAppOnlineWfParse instance;
|
||
return &instance;
|
||
}
|
||
|
||
void TjdUiAppOnlineWfParse::SetVideoBgVisible(bool visible)
|
||
{
|
||
if (videoBg != nullptr) {
|
||
videoBg->SetVisible(visible);
|
||
}
|
||
}
|
||
|
||
void TjdUiAppOnlineWfParse::Unload()
|
||
{
|
||
if (VideoPage != nullptr) {
|
||
delete VideoPage;
|
||
VideoPage = nullptr;
|
||
}
|
||
ImageCacheManager::GetInstance().UnloadSingleRes(bgResPath);
|
||
videoBg = nullptr;
|
||
}
|
||
|
||
int TjdUiAppOnlineWfParse::FindOnlineWfConfigFile(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 TjdUiAppOnlineWfParse::CreateView(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.\n");
|
||
return OHOS_FAILURE;
|
||
}
|
||
cJSON* cjsonType = cJSON_GetObjectItem(cjson_root, "wf_type");
|
||
if (cjsonType == NULL) {
|
||
static_print_error("ParseJsonFile::wf_type is null\n");
|
||
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\n");
|
||
cJSON_Delete(cjson_root);
|
||
return OHOS_FAILURE;
|
||
}
|
||
bgResPath = std::string(cJSON_GetStringValue(cjsonBgRes));
|
||
char* videoBgImagePath = cJSON_GetStringValue(cJSON_GetObjectItem(cjson_root, "video_preview"));
|
||
ImageInfo* imgInfo;
|
||
if ((PictureType)type == PictureType::VIDEO) {
|
||
VideoPage = new TjdUiAppOnlineWfVideoPage(parent, bgResPath, std::string(videoBgImagePath));
|
||
if (VideoPage == nullptr) {
|
||
static_print_error("MainMapView new fail\n");
|
||
return OHOS_FAILURE;
|
||
}
|
||
|
||
//printf("videoBgImagePath:%s\n", videoBgImagePath);
|
||
videoBg = new UIImageView();
|
||
cont->Add(videoBg);
|
||
videoBg->SetPosition(0, 0);
|
||
if (CheckFileExtension(std::string(videoBgImagePath))) {
|
||
videoBg->SetSrc(videoBgImagePath);
|
||
} else {
|
||
imgInfo = ImageCacheManager::GetInstance().LoadSingleRes(std::string(videoBgImagePath));
|
||
videoBg->SetSrc(imgInfo);
|
||
}
|
||
} else if ((PictureType)type == PictureType::BITMAP) {
|
||
imgInfo = ImageCacheManager::GetInstance().LoadSingleRes(bgResPath);
|
||
UIImageView *backGround = new UIImageView();
|
||
cont->Add(backGround);
|
||
backGround->SetPosition(0, 0);
|
||
backGround->SetSrc(imgInfo);
|
||
} else {
|
||
UIImageView *backGround = new UIImageView();
|
||
cont->Add(backGround);
|
||
backGround->SetPosition(0, 0);
|
||
backGround->SetSrc(bgResPath.c_str());
|
||
}
|
||
cJSON_Delete(cjson_root);
|
||
return OHOS_SUCCESS;
|
||
}
|
||
|
||
bool TjdUiAppOnlineWfParse::ParseOnlineWfData(UIView* parent)
|
||
{
|
||
static_print_info("ParseOnlineWfData");
|
||
// 1.读取json文件
|
||
string jsonContext;
|
||
if (FindOnlineWfConfigFile(jsonContext) == OHOS_FAILURE) {
|
||
static_print_error("FindOnlineWfConfigFile failed\n");
|
||
return false;
|
||
}
|
||
if (CreateView(jsonContext, parent) == OHOS_FAILURE) {
|
||
static_print_error("CreateView failed\n");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
}
|