1 /*
2 * Copyright (C) 2025 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 "notification_sync_box.h"
17
18 #include <sstream>
19 #include "ans_log_wrapper.h"
20
21 namespace OHOS {
22 namespace Notification {
23
NotificationSyncBox()24 NotificationSyncBox::NotificationSyncBox()
25 {
26 if (box_ == nullptr) {
27 return;
28 }
29 box_->SetMessageType(SYNC_NOTIFICATION);
30 }
31
~NotificationSyncBox()32 NotificationSyncBox::~NotificationSyncBox()
33 {}
34
NotificationSyncBox(std::shared_ptr<TlvBox> box)35 NotificationSyncBox::NotificationSyncBox(std::shared_ptr<TlvBox> box) : BoxBase(box)
36 {
37 }
38
39 #ifdef DISTRIBUTED_FEATURE_MASTER
SetLocalDeviceId(const std::string & deviceId)40 bool NotificationSyncBox::SetLocalDeviceId(const std::string& deviceId)
41 {
42 if (box_ == nullptr) {
43 return false;
44 }
45 return box_->PutValue(std::make_shared<TlvItem>(LOCAL_DEVICE_ID, deviceId));
46 }
47
SetNotificationList(const std::vector<std::string> & notificationList)48 bool NotificationSyncBox::SetNotificationList(const std::vector<std::string>& notificationList)
49 {
50 if (box_ == nullptr) {
51 return false;
52 }
53 std::ostringstream listStream;
54 for (auto& notification : notificationList) {
55 listStream << notification << ' ';
56 }
57 std::string result = listStream.str();
58 ANS_LOGI("SetNotificationList %{public}s", result.c_str());
59 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_HASHCODE, result));
60 }
61
SetNotificationEmpty(const bool empty)62 bool NotificationSyncBox::SetNotificationEmpty(const bool empty)
63 {
64 if (box_ == nullptr) {
65 return false;
66 }
67 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_HASHCODE + 1, empty));
68 }
69
70 #else
GetLocalDeviceId(std::string & deviceId) const71 bool NotificationSyncBox::GetLocalDeviceId(std::string& deviceId) const
72 {
73 if (box_ == nullptr) {
74 return false;
75 }
76 return box_->GetStringValue(LOCAL_DEVICE_ID, deviceId);
77 }
78
GetNotificationList(std::unordered_set<std::string> & notificationList) const79 bool NotificationSyncBox::GetNotificationList(std::unordered_set<std::string>& notificationList) const
80 {
81 if (box_ == nullptr) {
82 return false;
83 }
84 std::string notificationContent;
85 if (!box_->GetStringValue(NOTIFICATION_HASHCODE, notificationContent)) {
86 return false;
87 }
88
89 ANS_LOGI("SetNotificationList %{public}s", notificationContent.c_str());
90 std::istringstream listStream(notificationContent);
91 std::string hashCode;
92 while (listStream >> hashCode) {
93 if (!hashCode.empty()) {
94 notificationList.insert(hashCode);
95 }
96 }
97 return true;
98 }
99
GetNotificationEmpty(bool & empty) const100 bool NotificationSyncBox::GetNotificationEmpty(bool& empty) const
101 {
102 if (box_ == nullptr) {
103 return false;
104 }
105 return box_->GetBoolValue(NOTIFICATION_HASHCODE + 1, empty);
106 }
107 #endif
108 }
109 }
110