1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "pasteboard_progress.h"
17 #include "iservice_registry.h"
18 #include "pasteboard_error.h"
19 #include "pasteboard_hilog.h"
20 #include "pasteboard_time.h"
21 #include "udmf_client.h"
22
23 using namespace OHOS::UDMF;
24 namespace OHOS::MiscServices {
25 constexpr const int32_t PASTEBOARD_SA_ID = 3701;
26
27 std::mutex PasteBoardProgress::mutex_;
28 sptr<IRemoteObject> PasteBoardProgress::remoteObj_ = nullptr;
29 PasteBoardProgress *PasteBoardProgress::instance_ = nullptr;
GetInstance()30 PasteBoardProgress &PasteBoardProgress::GetInstance()
31 {
32 if (instance_ == nullptr) {
33 std::lock_guard<std::mutex> lock(mutex_);
34 if (instance_ == nullptr) {
35 instance_ = new PasteBoardProgress();
36 Initialize();
37 }
38 }
39 return *instance_;
40 }
41
Initialize()42 void PasteBoardProgress::Initialize()
43 {
44 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45 if (samgr == nullptr) {
46 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "get sa manager return nullptr");
47 return;
48 }
49 auto remoteObj = samgr->GetSystemAbility(PASTEBOARD_SA_ID);
50 if (remoteObj == nullptr) {
51 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "get system ability failed, id=%{public}d", PASTEBOARD_SA_ID);
52 return;
53 }
54 remoteObj_ = remoteObj;
55 }
56
InsertValue(std::string & key,std::string & value)57 int32_t PasteBoardProgress::InsertValue(std::string &key, std::string &value)
58 {
59 CustomOption option = {.intention = Intention::UD_INTENTION_DATA_HUB};
60 UnifiedData data;
61 auto udsObject = std::make_shared<Object>();
62 if (udsObject == nullptr) {
63 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "udsObject is nullptr");
64 return static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR);
65 }
66 auto utdId = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::PLAIN_TEXT);
67 udsObject->value_[UDMF::UNIFORM_DATA_TYPE] = utdId;
68 udsObject->value_[UDMF::CONTENT] = value;
69 udsObject->value_[UDMF::ABSTRACT] = std::to_string(PasteBoardTime::GetCurrentTimeMicros());
70 std::shared_ptr<UnifiedRecord> record = std::make_shared<UnifiedRecord>();
71 record->AddEntry(utdId, std::move(udsObject));
72 data.AddRecord(record);
73 UdmfClient::GetInstance().SetData(option, data, key);
74 return static_cast<int32_t>(PasteboardError::E_OK);
75 }
76
UpdateValue(std::string & key,std::string value)77 int32_t PasteBoardProgress::UpdateValue(std::string &key, std::string value)
78 {
79 QueryOption queryOption = { .key = key };
80 UnifiedData data;
81 auto udsObject = std::make_shared<Object>();
82 if (udsObject == nullptr) {
83 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "udsObject is nullptr");
84 return static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR);
85 }
86 auto utdId = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::PLAIN_TEXT);
87 udsObject->value_[UDMF::UNIFORM_DATA_TYPE] = utdId;
88 udsObject->value_[UDMF::CONTENT] = value;
89 udsObject->value_[UDMF::ABSTRACT] = std::to_string(PasteBoardTime::GetCurrentTimeMicros());
90 std::shared_ptr<UnifiedRecord> record = std::make_shared<UnifiedRecord>();
91 record->AddEntry(utdId, std::move(udsObject));
92 data.AddRecord(record);
93 UdmfClient::GetInstance().UpdateData(queryOption, data);
94 return static_cast<int32_t>(PasteboardError::E_OK);
95 }
96 } // namespace OHOS::MiscServices