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
SetLocalDeviceId(const std::string & deviceId)39 bool NotificationSyncBox::SetLocalDeviceId(const std::string& deviceId)
40 {
41 if (box_ == nullptr) {
42 return false;
43 }
44 return box_->PutValue(std::make_shared<TlvItem>(LOCAL_DEVICE_ID, deviceId));
45 }
46
SetNotificationList(const std::vector<std::string> & notificationList)47 bool NotificationSyncBox::SetNotificationList(const std::vector<std::string>& notificationList)
48 {
49 if (box_ == nullptr) {
50 return false;
51 }
52 std::ostringstream listStream;
53 for (auto& notification : notificationList) {
54 listStream << notification << ' ';
55 }
56 std::string result = listStream.str();
57 ANS_LOGI("SetNotificationList %{public}s", result.c_str());
58 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_HASHCODE, result));
59 }
60
SetNotificationEmpty(const bool empty)61 bool NotificationSyncBox::SetNotificationEmpty(const bool empty)
62 {
63 if (box_ == nullptr) {
64 return false;
65 }
66 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_HASHCODE + 1, empty));
67 }
68
GetLocalDeviceId(std::string & deviceId) const69 bool NotificationSyncBox::GetLocalDeviceId(std::string& deviceId) const
70 {
71 if (box_ == nullptr) {
72 return false;
73 }
74 return box_->GetStringValue(LOCAL_DEVICE_ID, deviceId);
75 }
76
GetNotificationList(std::unordered_set<std::string> & notificationList) const77 bool NotificationSyncBox::GetNotificationList(std::unordered_set<std::string>& notificationList) const
78 {
79 if (box_ == nullptr) {
80 return false;
81 }
82 std::string notificationContent;
83 if (!box_->GetStringValue(NOTIFICATION_HASHCODE, notificationContent)) {
84 return false;
85 }
86
87 ANS_LOGI("SetNotificationList %{public}s", notificationContent.c_str());
88 std::istringstream listStream(notificationContent);
89 std::string hashCode;
90 while (listStream >> hashCode) {
91 if (!hashCode.empty()) {
92 notificationList.insert(hashCode);
93 }
94 }
95 return true;
96 }
97
GetNotificationEmpty(bool & empty) const98 bool NotificationSyncBox::GetNotificationEmpty(bool& empty) const
99 {
100 if (box_ == nullptr) {
101 return false;
102 }
103 return box_->GetBoolValue(NOTIFICATION_HASHCODE + 1, empty);
104 }
105 }
106 }
107