修复在主界面刷新表盘 304/1 304/head 305/target/1
authorCaisking <3449750381@qq.com>
Mon, 17 Jun 2024 11:18:51 +0000 (19:18 +0800)
committerCaisking <3449750381@qq.com>
Mon, 17 Jun 2024 11:18:51 +0000 (19:18 +0800)
code/sdk/application/wearable/fd230app/nativeui/set_dial/src/SetDialModel.cpp

index c0dfabf..de7cd96 100644 (file)
@@ -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 <sstream>
 #include <iomanip>
 #include <stdio.h>
+#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<uint16_t, std::string> 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)