123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "udmf_impl.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include <unordered_map>
1923b3eb3cSopenharmony_ci#include <variant>
2023b3eb3cSopenharmony_ci
2123b3eb3cSopenharmony_ci#include "html.h"
2223b3eb3cSopenharmony_ci#include "image.h"
2323b3eb3cSopenharmony_ci#include "link.h"
2423b3eb3cSopenharmony_ci#include "summary_napi.h"
2523b3eb3cSopenharmony_ci#include "system_defined_form.h"
2623b3eb3cSopenharmony_ci#include "system_defined_pixelmap.h"
2723b3eb3cSopenharmony_ci#include "text.h"
2823b3eb3cSopenharmony_ci#include "plain_text.h"
2923b3eb3cSopenharmony_ci#include "udmf_client.h"
3023b3eb3cSopenharmony_ci#include "application_defined_record.h"
3123b3eb3cSopenharmony_ci#include "unified_data.h"
3223b3eb3cSopenharmony_ci#include "unified_data_napi.h"
3323b3eb3cSopenharmony_ci#include "unified_types.h"
3423b3eb3cSopenharmony_ci#include "video.h"
3523b3eb3cSopenharmony_ci#include "native_engine/native_engine.h"
3623b3eb3cSopenharmony_ci#include "frameworks/bridge/common/utils/engine_helper.h"
3723b3eb3cSopenharmony_ci#include "frameworks/bridge/common/utils/utils.h"
3823b3eb3cSopenharmony_ci#include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
3923b3eb3cSopenharmony_ci#include "js_native_api_types.h"
4023b3eb3cSopenharmony_ci
4123b3eb3cSopenharmony_ci#include "base/image/file_uri_helper.h"
4223b3eb3cSopenharmony_ci#include "base/utils/utils.h"
4323b3eb3cSopenharmony_ci#include "core/common/udmf/unified_data.h"
4423b3eb3cSopenharmony_ci#include "ndk_data_conversion.h"
4523b3eb3cSopenharmony_cinamespace OHOS::Ace {
4623b3eb3cSopenharmony_ciUdmfClient* UdmfClient::GetInstance()
4723b3eb3cSopenharmony_ci{
4823b3eb3cSopenharmony_ci    static UdmfClientImpl instance;
4923b3eb3cSopenharmony_ci    return &instance;
5023b3eb3cSopenharmony_ci}
5123b3eb3cSopenharmony_ci
5223b3eb3cSopenharmony_ciRefPtr<UnifiedData> UdmfClientImpl::CreateUnifiedData()
5323b3eb3cSopenharmony_ci{
5423b3eb3cSopenharmony_ci    return AceType::DynamicCast<UnifiedData>(AceType::MakeRefPtr<UnifiedDataImpl>());
5523b3eb3cSopenharmony_ci}
5623b3eb3cSopenharmony_ci
5723b3eb3cSopenharmony_ciRefPtr<UnifiedData> UdmfClientImpl::TransformUnifiedData(napi_value napiValue)
5823b3eb3cSopenharmony_ci{
5923b3eb3cSopenharmony_ci    auto engine = EngineHelper::GetCurrentEngine();
6023b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(engine, nullptr);
6123b3eb3cSopenharmony_ci    NativeEngine* nativeEngine = engine->GetNativeEngine();
6223b3eb3cSopenharmony_ci    napi_env env = reinterpret_cast<napi_env>(nativeEngine);
6323b3eb3cSopenharmony_ci    void* native = nullptr;
6423b3eb3cSopenharmony_ci    napi_unwrap(env, napiValue, &native);
6523b3eb3cSopenharmony_ci    auto* unifiedData = reinterpret_cast<UDMF::UnifiedDataNapi*>(native);
6623b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(unifiedData, nullptr);
6723b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(unifiedData->value_, nullptr);
6823b3eb3cSopenharmony_ci    auto udData = AceType::MakeRefPtr<UnifiedDataImpl>();
6923b3eb3cSopenharmony_ci    udData->SetUnifiedData(unifiedData->value_);
7023b3eb3cSopenharmony_ci    return udData;
7123b3eb3cSopenharmony_ci}
7223b3eb3cSopenharmony_ci
7323b3eb3cSopenharmony_cinapi_value UdmfClientImpl::TransformUdmfUnifiedData(RefPtr<UnifiedData>& UnifiedData)
7423b3eb3cSopenharmony_ci{
7523b3eb3cSopenharmony_ci    auto engine = EngineHelper::GetCurrentEngine();
7623b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(engine, nullptr);
7723b3eb3cSopenharmony_ci    NativeEngine* nativeEngine = engine->GetNativeEngine();
7823b3eb3cSopenharmony_ci    napi_env env = reinterpret_cast<napi_env>(nativeEngine);
7923b3eb3cSopenharmony_ci    auto unifiedData = AceType::DynamicCast<UnifiedDataImpl>(UnifiedData)->GetUnifiedData();
8023b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(unifiedData, nullptr);
8123b3eb3cSopenharmony_ci    napi_value dataVal = nullptr;
8223b3eb3cSopenharmony_ci    UDMF::UnifiedDataNapi::NewInstance(env, unifiedData, dataVal);
8323b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(dataVal, nullptr);
8423b3eb3cSopenharmony_ci    return dataVal;
8523b3eb3cSopenharmony_ci}
8623b3eb3cSopenharmony_ci
8723b3eb3cSopenharmony_civoid* UdmfClientImpl::TransformUnifiedDataPtr(RefPtr<UnifiedData>& unifiedDataImpl)
8823b3eb3cSopenharmony_ci{
8923b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(unifiedDataImpl, nullptr);
9023b3eb3cSopenharmony_ci    std::shared_ptr<UDMF::UnifiedData> unifiedData =
9123b3eb3cSopenharmony_ci        AceType::DynamicCast<UnifiedDataImpl>(unifiedDataImpl)->GetUnifiedData();
9223b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(unifiedData, nullptr);
9323b3eb3cSopenharmony_ci    return unifiedData.get();
9423b3eb3cSopenharmony_ci}
9523b3eb3cSopenharmony_ci
9623b3eb3cSopenharmony_ciRefPtr<UnifiedData> UdmfClientImpl::TransformUnifiedDataForNative(void* rawData)
9723b3eb3cSopenharmony_ci{
9823b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(rawData, nullptr);
9923b3eb3cSopenharmony_ci    auto udData = AceType::MakeRefPtr<UnifiedDataImpl>();
10023b3eb3cSopenharmony_ci    auto udmfData = static_cast<OH_UdmfData*>(rawData);
10123b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udmfData, nullptr);
10223b3eb3cSopenharmony_ci    auto unifiedData = std::make_shared<UDMF::UnifiedData>();
10323b3eb3cSopenharmony_ci    auto status = OHOS::UDMF::NdkDataConversion::GetNativeUnifiedData(udmfData, unifiedData);
10423b3eb3cSopenharmony_ci    if (status) {
10523b3eb3cSopenharmony_ci        return nullptr;
10623b3eb3cSopenharmony_ci    }
10723b3eb3cSopenharmony_ci
10823b3eb3cSopenharmony_ci    udData->SetUnifiedData(unifiedData);
10923b3eb3cSopenharmony_ci    return udData;
11023b3eb3cSopenharmony_ci}
11123b3eb3cSopenharmony_ci
11223b3eb3cSopenharmony_cinapi_value UdmfClientImpl::TransformSummary(std::map<std::string, int64_t>& summary)
11323b3eb3cSopenharmony_ci{
11423b3eb3cSopenharmony_ci    auto engine = EngineHelper::GetCurrentEngine();
11523b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(engine, nullptr);
11623b3eb3cSopenharmony_ci    NativeEngine* nativeEngine = engine->GetNativeEngine();
11723b3eb3cSopenharmony_ci    napi_env env = reinterpret_cast<napi_env>(nativeEngine);
11823b3eb3cSopenharmony_ci    std::shared_ptr<UDMF::Summary> udmfSummary = std::make_shared<UDMF::Summary>();
11923b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udmfSummary, nullptr);
12023b3eb3cSopenharmony_ci    udmfSummary->totalSize = 0;
12123b3eb3cSopenharmony_ci    for (auto element : summary) {
12223b3eb3cSopenharmony_ci        udmfSummary->totalSize += element.second;
12323b3eb3cSopenharmony_ci    }
12423b3eb3cSopenharmony_ci    udmfSummary->summary = std::move(summary);
12523b3eb3cSopenharmony_ci    napi_value dataVal = nullptr;
12623b3eb3cSopenharmony_ci    UDMF::SummaryNapi::NewInstance(env, udmfSummary, dataVal);
12723b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(dataVal, nullptr);
12823b3eb3cSopenharmony_ci    return dataVal;
12923b3eb3cSopenharmony_ci}
13023b3eb3cSopenharmony_ci
13123b3eb3cSopenharmony_ciint32_t UdmfClientImpl::SetData(const RefPtr<UnifiedData>& unifiedData, std::string& key)
13223b3eb3cSopenharmony_ci{
13323b3eb3cSopenharmony_ci    auto& client = UDMF::UdmfClient::GetInstance();
13423b3eb3cSopenharmony_ci    UDMF::CustomOption udCustomOption;
13523b3eb3cSopenharmony_ci    udCustomOption.intention = UDMF::Intention::UD_INTENTION_DRAG;
13623b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
13723b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData, UDMF::E_ERROR);
13823b3eb3cSopenharmony_ci    int32_t ret = client.SetData(udCustomOption, *udData->GetUnifiedData(), key);
13923b3eb3cSopenharmony_ci    return ret;
14023b3eb3cSopenharmony_ci}
14123b3eb3cSopenharmony_ci
14223b3eb3cSopenharmony_ciint32_t UdmfClientImpl::GetData(const RefPtr<UnifiedData>& unifiedData, const std::string& key)
14323b3eb3cSopenharmony_ci{
14423b3eb3cSopenharmony_ci    auto& client = UDMF::UdmfClient::GetInstance();
14523b3eb3cSopenharmony_ci    UDMF::QueryOption queryOption;
14623b3eb3cSopenharmony_ci    queryOption.key = key;
14723b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
14823b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData, UDMF::E_ERROR);
14923b3eb3cSopenharmony_ci    int ret = client.GetData(queryOption, *udData->GetUnifiedData());
15023b3eb3cSopenharmony_ci    return ret;
15123b3eb3cSopenharmony_ci}
15223b3eb3cSopenharmony_ci
15323b3eb3cSopenharmony_ciint32_t UdmfClientImpl::GetSummary(std::string& key, std::map<std::string, int64_t>& summaryMap)
15423b3eb3cSopenharmony_ci{
15523b3eb3cSopenharmony_ci    auto& client = UDMF::UdmfClient::GetInstance();
15623b3eb3cSopenharmony_ci    UDMF::Summary summary;
15723b3eb3cSopenharmony_ci    UDMF::QueryOption queryOption;
15823b3eb3cSopenharmony_ci    queryOption.key = key;
15923b3eb3cSopenharmony_ci    int32_t ret = client.GetSummary(queryOption, summary);
16023b3eb3cSopenharmony_ci    summaryMap = summary.summary;
16123b3eb3cSopenharmony_ci    return ret;
16223b3eb3cSopenharmony_ci}
16323b3eb3cSopenharmony_ci
16423b3eb3cSopenharmony_cibool UdmfClientImpl::GetRemoteStatus(std::string& key)
16523b3eb3cSopenharmony_ci{
16623b3eb3cSopenharmony_ci    auto& client = UDMF::UdmfClient::GetInstance();
16723b3eb3cSopenharmony_ci    bool isRemoteData = false;
16823b3eb3cSopenharmony_ci    UDMF::QueryOption queryOption;
16923b3eb3cSopenharmony_ci    queryOption.key = key;
17023b3eb3cSopenharmony_ci    int32_t ret = client.IsRemoteData(queryOption, isRemoteData);
17123b3eb3cSopenharmony_ci    if (ret != 0) {
17223b3eb3cSopenharmony_ci        // if ret is not 0, udmf client has not been sync, so return true to use remote getData.
17323b3eb3cSopenharmony_ci        return true;
17423b3eb3cSopenharmony_ci    }
17523b3eb3cSopenharmony_ci    return isRemoteData;
17623b3eb3cSopenharmony_ci}
17723b3eb3cSopenharmony_ci
17823b3eb3cSopenharmony_ciint64_t UnifiedDataImpl::GetSize()
17923b3eb3cSopenharmony_ci{
18023b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(unifiedData_, 0);
18123b3eb3cSopenharmony_ci    return unifiedData_->GetRecords().size();
18223b3eb3cSopenharmony_ci}
18323b3eb3cSopenharmony_ci
18423b3eb3cSopenharmony_cistd::shared_ptr<UDMF::UnifiedData> UnifiedDataImpl::GetUnifiedData()
18523b3eb3cSopenharmony_ci{
18623b3eb3cSopenharmony_ci    if (unifiedData_ == nullptr) {
18723b3eb3cSopenharmony_ci        unifiedData_ = std::make_shared<UDMF::UnifiedData>();
18823b3eb3cSopenharmony_ci    }
18923b3eb3cSopenharmony_ci    return unifiedData_;
19023b3eb3cSopenharmony_ci}
19123b3eb3cSopenharmony_ci
19223b3eb3cSopenharmony_civoid UnifiedDataImpl::SetUnifiedData(std::shared_ptr<UDMF::UnifiedData> unifiedData)
19323b3eb3cSopenharmony_ci{
19423b3eb3cSopenharmony_ci    unifiedData_ = unifiedData;
19523b3eb3cSopenharmony_ci}
19623b3eb3cSopenharmony_ci
19723b3eb3cSopenharmony_civoid UdmfClientImpl::AddFormRecord(
19823b3eb3cSopenharmony_ci    const RefPtr<UnifiedData>& unifiedData, int32_t formId, const RequestFormInfo& cardInfo)
19923b3eb3cSopenharmony_ci{
20023b3eb3cSopenharmony_ci    auto formRecord = std::make_shared<UDMF::SystemDefinedForm>();
20123b3eb3cSopenharmony_ci    formRecord->SetFormId(formId);
20223b3eb3cSopenharmony_ci    formRecord->SetFormName(cardInfo.cardName);
20323b3eb3cSopenharmony_ci    formRecord->SetBundleName(cardInfo.bundleName);
20423b3eb3cSopenharmony_ci    formRecord->SetAbilityName(cardInfo.abilityName);
20523b3eb3cSopenharmony_ci    formRecord->SetModule(cardInfo.moduleName);
20623b3eb3cSopenharmony_ci    formRecord->SetType(UDMF::UDType::SYSTEM_DEFINED_FORM);
20723b3eb3cSopenharmony_ci
20823b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
20923b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData);
21023b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData->GetUnifiedData());
21123b3eb3cSopenharmony_ci    udData->GetUnifiedData()->AddRecord(formRecord);
21223b3eb3cSopenharmony_ci}
21323b3eb3cSopenharmony_ci
21423b3eb3cSopenharmony_civoid UdmfClientImpl::AddLinkRecord(
21523b3eb3cSopenharmony_ci    const RefPtr<UnifiedData>& unifiedData, const std::string& url, const std::string& description)
21623b3eb3cSopenharmony_ci{
21723b3eb3cSopenharmony_ci    auto record = std::make_shared<UDMF::Link>(url, description);
21823b3eb3cSopenharmony_ci
21923b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
22023b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData);
22123b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData->GetUnifiedData());
22223b3eb3cSopenharmony_ci    udData->GetUnifiedData()->AddRecord(record);
22323b3eb3cSopenharmony_ci}
22423b3eb3cSopenharmony_ci
22523b3eb3cSopenharmony_civoid UdmfClientImpl::GetLinkRecord(
22623b3eb3cSopenharmony_ci    const RefPtr<UnifiedData>& unifiedData, std::string& url, std::string& description)
22723b3eb3cSopenharmony_ci{
22823b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
22923b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData);
23023b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData->GetUnifiedData());
23123b3eb3cSopenharmony_ci    auto records = udData->GetUnifiedData()->GetRecords();
23223b3eb3cSopenharmony_ci    for (auto record : records) {
23323b3eb3cSopenharmony_ci        UDMF::UDType type = record->GetType();
23423b3eb3cSopenharmony_ci        if (type == UDMF::UDType::HYPERLINK) {
23523b3eb3cSopenharmony_ci            UDMF::Link* link = reinterpret_cast<UDMF::Link*>(record.get());
23623b3eb3cSopenharmony_ci            url = link->GetUrl();
23723b3eb3cSopenharmony_ci            description = link->GetDescription();
23823b3eb3cSopenharmony_ci            return;
23923b3eb3cSopenharmony_ci        }
24023b3eb3cSopenharmony_ci    }
24123b3eb3cSopenharmony_ci}
24223b3eb3cSopenharmony_ci
24323b3eb3cSopenharmony_civoid UdmfClientImpl::AddHtmlRecord(
24423b3eb3cSopenharmony_ci    const RefPtr<UnifiedData>& unifiedData, const std::string& htmlContent, const std::string& plainContent)
24523b3eb3cSopenharmony_ci{
24623b3eb3cSopenharmony_ci    auto htmlRecord = std::make_shared<UDMF::Html>(htmlContent, plainContent);
24723b3eb3cSopenharmony_ci
24823b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
24923b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData);
25023b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData->GetUnifiedData());
25123b3eb3cSopenharmony_ci    if (!plainContent.empty() || !htmlContent.empty()) {
25223b3eb3cSopenharmony_ci        udData->GetUnifiedData()->AddRecord(htmlRecord);
25323b3eb3cSopenharmony_ci    }
25423b3eb3cSopenharmony_ci}
25523b3eb3cSopenharmony_ci
25623b3eb3cSopenharmony_civoid UdmfClientImpl::GetHtmlRecord(
25723b3eb3cSopenharmony_ci    const RefPtr<UnifiedData>& unifiedData, std::string& htmlContent, std::string& plainContent)
25823b3eb3cSopenharmony_ci{
25923b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
26023b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData);
26123b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData->GetUnifiedData());
26223b3eb3cSopenharmony_ci    auto records = udData->GetUnifiedData()->GetRecords();
26323b3eb3cSopenharmony_ci    for (auto record : records) {
26423b3eb3cSopenharmony_ci        UDMF::UDType type = record->GetType();
26523b3eb3cSopenharmony_ci        if (type == UDMF::UDType::HTML) {
26623b3eb3cSopenharmony_ci            UDMF::Html* html = reinterpret_cast<UDMF::Html*>(record.get());
26723b3eb3cSopenharmony_ci            plainContent = html->GetPlainContent();
26823b3eb3cSopenharmony_ci            htmlContent = html->GetHtmlContent();
26923b3eb3cSopenharmony_ci            return;
27023b3eb3cSopenharmony_ci        }
27123b3eb3cSopenharmony_ci    }
27223b3eb3cSopenharmony_ci}
27323b3eb3cSopenharmony_ci
27423b3eb3cSopenharmony_civoid UdmfClientImpl::AddPixelMapRecord(const RefPtr<UnifiedData>& unifiedData, std::vector<uint8_t>& data,
27523b3eb3cSopenharmony_ci    PixelMapRecordDetails& details)
27623b3eb3cSopenharmony_ci{
27723b3eb3cSopenharmony_ci    auto record = std::make_shared<UDMF::SystemDefinedPixelMap>(data);
27823b3eb3cSopenharmony_ci    UDMF::UDDetails uDetails = {
27923b3eb3cSopenharmony_ci        { "width", details.width },
28023b3eb3cSopenharmony_ci        { "height", details.height },
28123b3eb3cSopenharmony_ci        { "pixel-format", static_cast<int32_t>(details.pixelFormat) },
28223b3eb3cSopenharmony_ci        { "alpha-type", static_cast<int32_t>(details.alphaType) } };
28323b3eb3cSopenharmony_ci    record->SetDetails(uDetails);
28423b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
28523b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData);
28623b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData->GetUnifiedData());
28723b3eb3cSopenharmony_ci    udData->GetUnifiedData()->AddRecord(record);
28823b3eb3cSopenharmony_ci}
28923b3eb3cSopenharmony_ci
29023b3eb3cSopenharmony_civoid UdmfClientImpl::AddImageRecord(const RefPtr<UnifiedData>& unifiedData, const std::string& uri)
29123b3eb3cSopenharmony_ci{
29223b3eb3cSopenharmony_ci    auto record = std::make_shared<UDMF::Image>(uri);
29323b3eb3cSopenharmony_ci
29423b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
29523b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData);
29623b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData->GetUnifiedData());
29723b3eb3cSopenharmony_ci    udData->GetUnifiedData()->AddRecord(record);
29823b3eb3cSopenharmony_ci}
29923b3eb3cSopenharmony_ci
30023b3eb3cSopenharmony_civoid UdmfClientImpl::AddPlainTextRecord(const RefPtr<UnifiedData>& unifiedData, const std::string& selectedStr)
30123b3eb3cSopenharmony_ci{
30223b3eb3cSopenharmony_ci    auto record = std::make_shared<UDMF::PlainText>(selectedStr, "");
30323b3eb3cSopenharmony_ci
30423b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
30523b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData);
30623b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData->GetUnifiedData());
30723b3eb3cSopenharmony_ci    udData->GetUnifiedData()->AddRecord(record);
30823b3eb3cSopenharmony_ci}
30923b3eb3cSopenharmony_ci
31023b3eb3cSopenharmony_cistd::string UdmfClientImpl::GetSinglePlainTextRecord(const RefPtr<UnifiedData>& unifiedData)
31123b3eb3cSopenharmony_ci{
31223b3eb3cSopenharmony_ci    std::string str = "";
31323b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
31423b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData, str);
31523b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData->GetUnifiedData(), str);
31623b3eb3cSopenharmony_ci    auto records = udData->GetUnifiedData()->GetRecords();
31723b3eb3cSopenharmony_ci    if (records.size() >= 1 && records[0]->GetType() == UDMF::UDType::PLAIN_TEXT) {
31823b3eb3cSopenharmony_ci        UDMF::PlainText* plainText = reinterpret_cast<UDMF::PlainText*>(records[0].get());
31923b3eb3cSopenharmony_ci        str = plainText->GetContent();
32023b3eb3cSopenharmony_ci    }
32123b3eb3cSopenharmony_ci    return str;
32223b3eb3cSopenharmony_ci}
32323b3eb3cSopenharmony_ci
32423b3eb3cSopenharmony_cibool UdmfClientImpl::AddFileUriRecord(const RefPtr<UnifiedData>& unifiedData, std::vector<std::string>& uri)
32523b3eb3cSopenharmony_ci{
32623b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
32723b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData, false);
32823b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData->GetUnifiedData(), false);
32923b3eb3cSopenharmony_ci
33023b3eb3cSopenharmony_ci    for (std::string u : uri) {
33123b3eb3cSopenharmony_ci        LOGI("DragDrop event AddFileUriRecord, uri:%{public}s", u.c_str());
33223b3eb3cSopenharmony_ci        auto record = std::make_shared<UDMF::Image>(u);
33323b3eb3cSopenharmony_ci        udData->GetUnifiedData()->AddRecord(record);
33423b3eb3cSopenharmony_ci    }
33523b3eb3cSopenharmony_ci
33623b3eb3cSopenharmony_ci    return true;
33723b3eb3cSopenharmony_ci}
33823b3eb3cSopenharmony_ci
33923b3eb3cSopenharmony_cibool UdmfClientImpl::GetFileUriRecord(const RefPtr<UnifiedData>& unifiedData, std::vector<std::string>& uri)
34023b3eb3cSopenharmony_ci{
34123b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
34223b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData, false);
34323b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData->GetUnifiedData(), false);
34423b3eb3cSopenharmony_ci    auto records = udData->GetUnifiedData()->GetRecords();
34523b3eb3cSopenharmony_ci
34623b3eb3cSopenharmony_ci    for (auto record : records) {
34723b3eb3cSopenharmony_ci        UDMF::UDType type = record->GetType();
34823b3eb3cSopenharmony_ci        if (type == UDMF::UDType::IMAGE || type == UDMF::UDType::AUDIO ||
34923b3eb3cSopenharmony_ci            type == UDMF::UDType::VIDEO || type == UDMF::UDType::FILE) {
35023b3eb3cSopenharmony_ci            UDMF::File* file = reinterpret_cast<UDMF::File*>(record.get());
35123b3eb3cSopenharmony_ci            if (file) {
35223b3eb3cSopenharmony_ci                uri.emplace_back(file->GetUri());
35323b3eb3cSopenharmony_ci                LOGI("DragDrop event GetFileUri, uri:%{public}s", file->GetUri().c_str());
35423b3eb3cSopenharmony_ci            } else {
35523b3eb3cSopenharmony_ci                LOGE("DragDrop event GetFileUri file is null");
35623b3eb3cSopenharmony_ci            }
35723b3eb3cSopenharmony_ci        }
35823b3eb3cSopenharmony_ci    }
35923b3eb3cSopenharmony_ci    return true;
36023b3eb3cSopenharmony_ci}
36123b3eb3cSopenharmony_ci
36223b3eb3cSopenharmony_cistd::vector<std::string> UdmfClientImpl::GetPlainTextRecords(const RefPtr<UnifiedData>& unifiedData)
36323b3eb3cSopenharmony_ci{
36423b3eb3cSopenharmony_ci    std::vector<std::string> textRecords;
36523b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
36623b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData, textRecords);
36723b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData->GetUnifiedData(), textRecords);
36823b3eb3cSopenharmony_ci    auto records = udData->GetUnifiedData()->GetRecords();
36923b3eb3cSopenharmony_ci    for (const auto& record : records) {
37023b3eb3cSopenharmony_ci        UDMF::UDType type = record->GetType();
37123b3eb3cSopenharmony_ci        if (type == UDMF::UDType::PLAIN_TEXT) {
37223b3eb3cSopenharmony_ci            UDMF::PlainText* plainText = reinterpret_cast<UDMF::PlainText*>(record.get());
37323b3eb3cSopenharmony_ci            std::string str = plainText->GetContent();
37423b3eb3cSopenharmony_ci            textRecords.emplace_back(str);
37523b3eb3cSopenharmony_ci        }
37623b3eb3cSopenharmony_ci    }
37723b3eb3cSopenharmony_ci    return textRecords;
37823b3eb3cSopenharmony_ci}
37923b3eb3cSopenharmony_ci
38023b3eb3cSopenharmony_ciint32_t UdmfClientImpl::GetVideoRecordUri(const RefPtr<UnifiedData>& unifiedData, std::string& uri)
38123b3eb3cSopenharmony_ci{
38223b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
38323b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData, UDMF::E_ERROR);
38423b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData->GetUnifiedData(), UDMF::E_ERROR);
38523b3eb3cSopenharmony_ci    auto records = udData->GetUnifiedData()->GetRecords();
38623b3eb3cSopenharmony_ci    if (records.size() == 0) {
38723b3eb3cSopenharmony_ci        return UDMF::E_ERROR;
38823b3eb3cSopenharmony_ci    }
38923b3eb3cSopenharmony_ci    auto video = static_cast<UDMF::Video*>(records[0].get());
39023b3eb3cSopenharmony_ci    uri = video->GetUri();
39123b3eb3cSopenharmony_ci    return 0;
39223b3eb3cSopenharmony_ci}
39323b3eb3cSopenharmony_ci
39423b3eb3cSopenharmony_cistd::pair<int32_t, std::string> UdmfClientImpl::GetErrorInfo(int32_t errorCode)
39523b3eb3cSopenharmony_ci{
39623b3eb3cSopenharmony_ci    switch (errorCode) {
39723b3eb3cSopenharmony_ci        case UDMF::E_NOT_FOUND:
39823b3eb3cSopenharmony_ci            return { ERROR_CODE_DRAG_DATA_NOT_FOUND, "GetData failed, data not found." };
39923b3eb3cSopenharmony_ci        default:
40023b3eb3cSopenharmony_ci            return { ERROR_CODE_DRAG_DATA_ERROR, "GetData failed, data error." };
40123b3eb3cSopenharmony_ci    }
40223b3eb3cSopenharmony_ci}
40323b3eb3cSopenharmony_ci
40423b3eb3cSopenharmony_civoid UdmfClientImpl::AddSpanStringRecord(
40523b3eb3cSopenharmony_ci    const RefPtr<UnifiedData>& unifiedData, std::vector<uint8_t>& data)
40623b3eb3cSopenharmony_ci{
40723b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
40823b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData);
40923b3eb3cSopenharmony_ci    CHECK_NULL_VOID(udData->GetUnifiedData());
41023b3eb3cSopenharmony_ci    auto record = std::make_shared<UDMF::ApplicationDefinedRecord>("OPENHARMONY_STYLED_STRING_UDMF", data);
41123b3eb3cSopenharmony_ci    udData->GetUnifiedData()->AddRecord(record);
41223b3eb3cSopenharmony_ci}
41323b3eb3cSopenharmony_ci
41423b3eb3cSopenharmony_cistd::vector<uint8_t> UdmfClientImpl::GetSpanStringRecord(const RefPtr<UnifiedData>& unifiedData)
41523b3eb3cSopenharmony_ci{
41623b3eb3cSopenharmony_ci    std::vector<uint8_t> arr;
41723b3eb3cSopenharmony_ci    auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
41823b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData, arr);
41923b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(udData->GetUnifiedData(), arr);
42023b3eb3cSopenharmony_ci    auto records = udData->GetUnifiedData()->GetRecords();
42123b3eb3cSopenharmony_ci    for (auto record: records) {
42223b3eb3cSopenharmony_ci        UDMF::UDType type = record->GetType();
42323b3eb3cSopenharmony_ci        if (type == UDMF::UDType::APPLICATION_DEFINED_RECORD) {
42423b3eb3cSopenharmony_ci            UDMF::ApplicationDefinedRecord* app = reinterpret_cast<UDMF::ApplicationDefinedRecord*>(record.get());
42523b3eb3cSopenharmony_ci            if (app->GetApplicationDefinedType() == "OPENHARMONY_STYLED_STRING_UDMF") {
42623b3eb3cSopenharmony_ci                arr = app->GetRawData();
42723b3eb3cSopenharmony_ci                return arr;
42823b3eb3cSopenharmony_ci            }
42923b3eb3cSopenharmony_ci        }
43023b3eb3cSopenharmony_ci    }
43123b3eb3cSopenharmony_ci    return arr;
43223b3eb3cSopenharmony_ci}
43323b3eb3cSopenharmony_ci} // namespace OHOS::Ace
434