1 /*
2 * Copyright (c) 2021 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_slot_group.h"
17 #include "ans_log_wrapper.h"
18 #include "string_ex.h"
19
20 namespace OHOS {
21 namespace Notification {
22 const int MAX_TEXT_LENGTH = 1000;
23 const std::string LINE_SEPARATOR = "\n";
24
NotificationSlotGroup()25 NotificationSlotGroup::NotificationSlotGroup()
26 {}
27
NotificationSlotGroup(const std::string & id,const std::string & name)28 NotificationSlotGroup::NotificationSlotGroup(const std::string &id, const std::string &name) : id_(id), name_(name)
29 {
30 id_ = TruncateString(id);
31 name_ = TruncateString(name);
32 }
33
~NotificationSlotGroup()34 NotificationSlotGroup::~NotificationSlotGroup()
35 {}
36
GetDescription() const37 std::string NotificationSlotGroup::GetDescription() const
38 {
39 return description_;
40 }
41
GetId() const42 std::string NotificationSlotGroup::GetId() const
43 {
44 return id_;
45 }
46
GetName() const47 std::string NotificationSlotGroup::GetName() const
48 {
49 return name_;
50 }
51
SetSlots(const std::vector<NotificationSlot> & slots)52 void NotificationSlotGroup::SetSlots(const std::vector<NotificationSlot> &slots)
53 {
54 slots_ = slots;
55 }
56
GetSlots() const57 std::vector<NotificationSlot> NotificationSlotGroup::GetSlots() const
58 {
59 return slots_;
60 }
61
IsDisabled() const62 bool NotificationSlotGroup::IsDisabled() const
63 {
64 return isDisabled_;
65 }
66
SetDescription(const std::string & description)67 void NotificationSlotGroup::SetDescription(const std::string &description)
68 {
69 description_ = TruncateString(description);
70 }
71
Dump() const72 std::string NotificationSlotGroup::Dump() const
73 {
74 std::string contents;
75 for (auto it = slots_.begin(); it != slots_.end(); ++it) {
76 contents += it->Dump();
77 if (it != slots_.end() - 1) {
78 contents += ",";
79 }
80 }
81 return "NotificationSlotGroup{ "
82 "id = " + id_ +
83 ", name = " + name_ +
84 ", description = " + description_ +
85 ", slots = " + contents +
86 ", isDisabled = " + (isDisabled_ ? "true" : "false") +
87 " }";
88 }
89
Marshalling(Parcel & parcel) const90 bool NotificationSlotGroup::Marshalling(Parcel &parcel) const
91 {
92 if (!parcel.WriteString(id_)) {
93 ANS_LOGE("Failed to write id");
94 return false;
95 }
96
97 if (!parcel.WriteString(name_)) {
98 ANS_LOGE("Failed to write name");
99 return false;
100 }
101
102 if (!parcel.WriteString(description_)) {
103 ANS_LOGE("Failed to write description");
104 return false;
105 }
106
107 if (slots_.size() == 0) {
108 if (!parcel.WriteInt32(0)) {
109 ANS_LOGE("Failed to write the size of slots");
110 return false;
111 }
112 } else {
113 if (!parcel.WriteInt32(slots_.size())) {
114 ANS_LOGE("Failed to write the size of slots");
115 return false;
116 }
117 for (size_t it = 0; it < slots_.size(); ++it) {
118 if (!parcel.WriteParcelable(&slots_.at(it))) {
119 ANS_LOGE("Failed to write slots");
120 return false;
121 }
122 }
123 }
124
125 if (!parcel.WriteBool(isDisabled_)) {
126 ANS_LOGE("Failed to write isDisabled");
127 return false;
128 }
129
130 return true;
131 }
132
ReadFromParcel(Parcel & parcel)133 bool NotificationSlotGroup::ReadFromParcel(Parcel &parcel)
134 {
135 id_ = parcel.ReadString();
136 name_ = parcel.ReadString();
137 description_ = parcel.ReadString();
138 int32_t size = parcel.ReadInt32();
139 if (size) {
140 for (int32_t i = 0; i < size; ++i) {
141 auto slot = parcel.ReadParcelable<NotificationSlot>();
142 if (slot == nullptr) {
143 ANS_LOGE("Failed to read slot");
144 return false;
145 }
146 slots_.emplace_back(*slot);
147 }
148 }
149 isDisabled_ = parcel.ReadBool();
150 return true;
151 }
152
Unmarshalling(Parcel & parcel)153 NotificationSlotGroup *NotificationSlotGroup::Unmarshalling(Parcel &parcel)
154 {
155 NotificationSlotGroup *notificationSlotGroup = new (std::nothrow) NotificationSlotGroup();
156
157 if (notificationSlotGroup && !notificationSlotGroup->ReadFromParcel(parcel)) {
158 delete notificationSlotGroup;
159 notificationSlotGroup = nullptr;
160 }
161
162 return notificationSlotGroup;
163 }
164
TruncateString(const std::string & inPutString)165 std::string NotificationSlotGroup::TruncateString(const std::string &inPutString)
166 {
167 std::string temp = inPutString;
168 if (inPutString.length() > MAX_TEXT_LENGTH) {
169 temp = inPutString.substr(0, MAX_TEXT_LENGTH);
170 }
171 return temp;
172 }
173 } // namespace Notification
174 } // namespace OHOS