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 <memory>
17 #include <string>
18
19 #include "pasteboard_progress.h"
20 #include "iservice_registry.h"
21 #include "pasteboard_error.h"
22 #include "pasteboard_hilog.h"
23 #include "pasteboard_time.h"
24 #include "paste_data_entry.h"
25 #include "udmf_client.h"
26
27 using namespace OHOS::UDMF;
28 namespace OHOS::MiscServices {
29 constexpr const int32_t PASTEBOARD_SA_ID = 3701;
30
31 std::mutex PasteBoardProgress::mutex_;
32 sptr<IRemoteObject> PasteBoardProgress::remoteObj_ = nullptr;
33 PasteBoardProgress *PasteBoardProgress::instance_ = nullptr;
GetInstance()34 PasteBoardProgress &PasteBoardProgress::GetInstance()
35 {
36 if (instance_ == nullptr) {
37 std::lock_guard<std::mutex> lock(mutex_);
38 if (instance_ == nullptr) {
39 instance_ = new PasteBoardProgress();
40 Initialize();
41 }
42 }
43 return *instance_;
44 }
45
Initialize()46 void PasteBoardProgress::Initialize()
47 {
48 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
49 if (samgr == nullptr) {
50 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "get sa manager return nullptr");
51 return;
52 }
53 auto remoteObj = samgr->GetSystemAbility(PASTEBOARD_SA_ID);
54 if (remoteObj == nullptr) {
55 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "get system ability failed, id=%{public}d", PASTEBOARD_SA_ID);
56 return;
57 }
58 remoteObj_ = remoteObj;
59 }
60
InsertValue(std::string & key,std::string & value)61 int32_t PasteBoardProgress::InsertValue(std::string &key, std::string &value)
62 {
63 CustomOption option = {.intention = Intention::UD_INTENTION_DATA_HUB};
64 UnifiedData data;
65 auto udsObject = std::make_shared<Object>();
66 if (udsObject == nullptr) {
67 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "udsObject is nullptr");
68 return static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR);
69 }
70 auto utdId = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::PLAIN_TEXT);
71 udsObject->value_[UDMF::UNIFORM_DATA_TYPE] = utdId;
72 udsObject->value_[UDMF::CONTENT] = value;
73 udsObject->value_[UDMF::ABSTRACT] = std::to_string(PasteBoardTime::GetCurrentTimeMicros());
74 std::shared_ptr<UnifiedRecord> record = std::make_shared<UnifiedRecord>();
75 record->AddEntry(utdId, std::move(udsObject));
76 data.AddRecord(record);
77 UdmfClient::GetInstance().SetData(option, data, key);
78 return static_cast<int32_t>(PasteboardError::E_OK);
79 }
80
UpdateValue(std::string & key,std::string value)81 int32_t PasteBoardProgress::UpdateValue(std::string &key, std::string value)
82 {
83 QueryOption queryOption = { .key = key };
84 UnifiedData data;
85 auto udsObject = std::make_shared<Object>();
86 if (udsObject == nullptr) {
87 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "udsObject is nullptr");
88 return static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR);
89 }
90 auto utdId = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::PLAIN_TEXT);
91 udsObject->value_[UDMF::UNIFORM_DATA_TYPE] = utdId;
92 udsObject->value_[UDMF::CONTENT] = value;
93 udsObject->value_[UDMF::ABSTRACT] = std::to_string(PasteBoardTime::GetCurrentTimeMicros());
94 std::shared_ptr<UnifiedRecord> record = std::make_shared<UnifiedRecord>();
95 record->AddEntry(utdId, std::move(udsObject));
96 data.AddRecord(record);
97 UdmfClient::GetInstance().UpdateData(queryOption, data);
98 return static_cast<int32_t>(PasteboardError::E_OK);
99 }
100
GetValue(const std::string & key,std::string & value)101 int32_t PasteBoardProgress::GetValue(const std::string &key, std::string &value)
102 {
103 QueryOption option = { .key = key };
104 std::vector<UnifiedData> data;
105 UdmfClient::GetInstance().GetBatchData(option, data);
106 if (data.empty()) {
107 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "data is empty");
108 return static_cast<int32_t>(PasteboardError::INVALID_DATA_ERROR);
109 }
110 if (data[0].GetRecords().empty()) {
111 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Getrecords is empty");
112 return static_cast<int32_t>(PasteboardError::INVALID_DATA_ERROR);
113 }
114 auto outputRecord = data[0].GetRecordAt(0);
115 if (outputRecord == nullptr) {
116 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "outputRecord is nullptr");
117 return static_cast<int32_t>(PasteboardError::INVALID_DATA_ERROR);
118 }
119 auto plainText = outputRecord->GetValue();
120 auto object = std::get<std::shared_ptr<Object>>(plainText);
121 if (object == nullptr) {
122 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "object is nullptr");
123 return static_cast<int32_t>(PasteboardError::INVALID_DATA_ERROR);
124 }
125 object->GetValue(UDMF::CONTENT, value);
126 return static_cast<int32_t>(PasteboardError::E_OK);
127 }
128 } // namespace OHOS::MiscServices