From 9e7c2c9da53178be9fa30d058d50906f0b019e1d Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Mon, 17 Jun 2024 19:18:51 +0800 Subject: [PATCH] =?utf8?q?=E4=BF=AE=E5=A4=8D=E5=9C=A8=E4=B8=BB=E7=95=8C?= =?utf8?q?=E9=9D=A2=E5=88=B7=E6=96=B0=E8=A1=A8=E7=9B=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/set_dial/src/SetDialModel.cpp | 95 ++++++++++--------- 1 file changed, 49 insertions(+), 46 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp index c0dfabfcc..de7cd96c4 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp @@ -1,3 +1,9 @@ +/*** + * @Date: 2024-06-17 19:06:05 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-06-17 19:06:53 + * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp + */ /*** * @Date: 2024-05-30 15:59:37 * @LastEditors: Caisking 3449750381@qq.com @@ -23,6 +29,7 @@ #include #include #include +#include "NativeAbility.h" namespace OHOS { // 定义特殊Id @@ -30,9 +37,9 @@ namespace OHOS constexpr uint16_t DEFAULT_DIAL_ID_1 = 1028; constexpr uint16_t DEFAULT_DIAL_ID_2 = 1052; - #define DIAL_ID_LENGTH 2 - #define DIAL_NAME_LENGTH 50 - #define DIAL_ID_PATH "user/userdata/dial_id.bin" +#define DIAL_ID_LENGTH 2 +#define DIAL_NAME_LENGTH 50 +#define DIAL_ID_PATH "user/userdata/dial_id.bin" // 表盘map[Id--dialname] 前5个ID固定 std::unordered_map registeredDials{ @@ -45,24 +52,24 @@ namespace OHOS // 表盘市场UID 对应表盘name bool SetDialModel::saveDialfile() { - FILE * fp= fopen(DIAL_ID_PATH,"wb+"); + FILE *fp = fopen(DIAL_ID_PATH, "wb+"); for (const auto &entry : registeredDials) { - char buf[DIAL_ID_LENGTH+DIAL_NAME_LENGTH]={0}; - memcpy_s(buf,DIAL_ID_LENGTH+DIAL_NAME_LENGTH,&entry.first,2); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uid %d",entry.first); - memcpy_s(buf+DIAL_ID_LENGTH,DIAL_ID_LENGTH+DIAL_NAME_LENGTH,entry.second.c_str(),entry.second.length()); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uid %s",entry.second.c_str()); - int size=fwrite(buf,1,DIAL_ID_LENGTH+DIAL_NAME_LENGTH,fp); - if(DIAL_ID_LENGTH+DIAL_NAME_LENGTH !=size ) + char buf[DIAL_ID_LENGTH + DIAL_NAME_LENGTH] = {0}; + memcpy_s(buf, DIAL_ID_LENGTH + DIAL_NAME_LENGTH, &entry.first, 2); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uid %d", entry.first); + memcpy_s(buf + DIAL_ID_LENGTH, DIAL_ID_LENGTH + DIAL_NAME_LENGTH, entry.second.c_str(), entry.second.length()); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uid %s", entry.second.c_str()); + int size = fwrite(buf, 1, DIAL_ID_LENGTH + DIAL_NAME_LENGTH, fp); + if (DIAL_ID_LENGTH + DIAL_NAME_LENGTH != size) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " fwrite is fail! %d",size); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " fwrite is fail! %d", size); return false; } else { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " fwrite is succ %d !",entry.first); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " fwrite is succ %d !", entry.first); } } fclose(fp); @@ -71,51 +78,48 @@ namespace OHOS bool SetDialModel::readDialfile() { - FILE * fp= fopen(DIAL_ID_PATH,"rb+"); - if(fp==NULL) - return false; + FILE *fp = fopen(DIAL_ID_PATH, "rb+"); + if (fp == NULL) + return false; fseek(fp, 0, SEEK_END); - int size = ftell(fp); + int size = ftell(fp); fseek(fp, 0, SEEK_SET); - //char * buf=(char *)malloc(size); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " readDialfile size is %d !",size); + // char * buf=(char *)malloc(size); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " readDialfile size is %d !", size); while (size) { - uint16_t uid=0; - if(DIAL_ID_LENGTH !=fread( (void *)&uid,1,DIAL_ID_LENGTH,fp)) + uint16_t uid = 0; + if (DIAL_ID_LENGTH != fread((void *)&uid, 1, DIAL_ID_LENGTH, fp)) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " fread is fail!"); } - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uid %d",uid); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uid %d", uid); - char name_buf[DIAL_NAME_LENGTH]={0}; - if(DIAL_NAME_LENGTH !=fread((void *)name_buf,1,DIAL_NAME_LENGTH,fp)) + char name_buf[DIAL_NAME_LENGTH] = {0}; + if (DIAL_NAME_LENGTH != fread((void *)name_buf, 1, DIAL_NAME_LENGTH, fp)) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " fread is fail!"); } - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "name_buf %s",name_buf); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "name_buf %s", name_buf); - if(uid!=0) + if (uid != 0) { - if( !IsDialInstalled(uid)) + if (!IsDialInstalled(uid)) { - RegisterDial(uid,name_buf); + RegisterDial(uid, name_buf); } - - } - size-=(DIAL_ID_LENGTH+DIAL_NAME_LENGTH); + size -= (DIAL_ID_LENGTH + DIAL_NAME_LENGTH); } - - //free(buf); + + // free(buf); fclose(fp); return true; } - - SetDialModel::SetDialModel() + SetDialModel::SetDialModel() { readDialfile(); LoadAllDial(); @@ -136,8 +140,8 @@ namespace OHOS // memcpy_s(dialname_,strlen(dialname)+1,dialname,strlen(dialname)+1); - std::string * dialname_ = new std::string(dialname); - + std::string *dialname_ = new std::string(dialname); + registeredDials[ID] = *dialname_; // 唯一性 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RegisterDial %s is ok!", dialname); @@ -176,15 +180,15 @@ namespace OHOS int length = 0; for (const auto &entry : registeredDials) { - memcpy_s(allidStr+length,512,&entry.first,2); - length+=2; + memcpy_s(allidStr + length, 512, &entry.first, 2); + length += 2; // // 格式化表盘ID为两个字节长度的字符串,不足两位则在前面补0 // std::stringstream ss; // ss << std::setw(2) << std::setfill('0') << entry.first; // allIDs += ss.str(); } - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "allidStr is %s %d", allIDs.c_str(),length); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "allidStr is %s %d", allIDs.c_str(), length); // // 将字符串拷贝到allidStr中 // int length = allIDs.size(); @@ -221,8 +225,7 @@ namespace OHOS WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UninstallDial default dial %d fail", ID); return false; } - - + /*删除文件操作*/ std::string dialFullName = APP_DIAL_PATH; dialFullName += "/"; @@ -234,7 +237,7 @@ namespace OHOS WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UninstallDial the dial %d is ok", ID); registeredDials.erase(ID); - + return true; } WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uapi_delete_files the dial %d is false", ID); @@ -259,10 +262,11 @@ namespace OHOS diagSetting.dialFullName = info.dialFullName; MainModel::GetInstance().AddDialSetting(diagSetting); // 更新表盘设置 currentDialID = ID; // 更新当前表盘Id - return; // 更新成功后直接返回 + NativeAbility::GetInstance().ChangeSlice(VIEW_MAIN); + return; // 更新成功后直接返回 } } - + NativeAbility::GetInstance().ChangeSlice(VIEW_MAIN); WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SetDialModel UpdateDialSetting: Cannot find dial with name %s", dialname.c_str()); } @@ -377,7 +381,6 @@ namespace OHOS dialInfo.description = preInfo.description; dialInfo.preImageInfo = info; - #if 0 // 需要判断表盘是否存在 不存在->新表盘注册 if (GetDialIdFromName(dialInfo.dialName) == 0) -- 2.17.1