From: xue <2144439972@qq.com> Date: Thu, 16 Oct 2025 09:23:01 +0000 (+0800) Subject: 优化文本乱码问题 X-Git-Tag: V1.0.9_20251128_194606_LN~222^2~1 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=6bbae389215f8b3bde7338bc2a3ad530d2d579c5;p=mirrors%2FFD230.git 优化文本乱码问题 --- diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/uiservice/src/ui_service.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/uiservice/src/ui_service.cpp index 0449d597c..d8e4c8020 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/uiservice/src/ui_service.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/uiservice/src/ui_service.cpp @@ -570,9 +570,39 @@ void HandBindAndUnbindNotify(Request *request) // 更新文本内容通知 void HandTextUpdateNotify(Request *request) { - std::string value(static_cast(request->data), request->len); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandTextUpdateNotify msg_id = %d, len = %d", request->msgId, request->len); + + // 检查数据有效性 + if (request->data == nullptr || request->len <= 0) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandTextUpdateNotify: invalid data or length"); + return; + } + + // 确保字符串以null结尾,避免乱码 + std::string value; + if (request->len > 0) { + const char* data = static_cast(request->data); + if (data[request->len - 1] == '\0') { + value = std::string(data, request->len - 1); + } else { + value = std::string(data, request->len); + } + } + + // 验证字符串是否包含有效字符 + if (value.empty() || value.find_first_not_of(" \t\n\r") == std::string::npos) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandTextUpdateNotify: text string is empty or contains only whitespace"); + return; + } + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandTextUpdateNotify text = %s", value.c_str()); + + // 创建字符串副本,避免const_cast问题 + std::string textCopy = value; GraphicService::GetInstance()->PostGraphicEvent( - std::bind(&OHOS::ThunderBirdModel::SetTextStr,OHOS::ThunderBirdModel::GetInstance(),const_cast(value.c_str()))); + [textCopy]() { + OHOS::ThunderBirdModel::GetInstance()->SetTextStr(const_cast(textCopy.c_str())); + }); } // 下载文件通知