1 /*
2 * Copyright (c) 2021-2023 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_multiline_content.h"
17
18 #include <algorithm>
19
20 #include "ans_log_wrapper.h"
21
22 namespace OHOS {
23 namespace Notification {
24 const std::vector<std::string>::size_type NotificationMultiLineContent::MAX_LINES {7};
25
SetExpandedTitle(const std::string & exTitle)26 void NotificationMultiLineContent::SetExpandedTitle(const std::string &exTitle)
27 {
28 expandedTitle_ = exTitle;
29 }
30
GetExpandedTitle() const31 std::string NotificationMultiLineContent::GetExpandedTitle() const
32 {
33 return expandedTitle_;
34 }
35
SetBriefText(const std::string & briefText)36 void NotificationMultiLineContent::SetBriefText(const std::string &briefText)
37 {
38 briefText_ = briefText;
39 }
40
GetBriefText() const41 std::string NotificationMultiLineContent::GetBriefText() const
42 {
43 return briefText_;
44 }
45
AddSingleLine(const std::string & oneLine)46 void NotificationMultiLineContent::AddSingleLine(const std::string &oneLine)
47 {
48 if (allLines_.size() >= NotificationMultiLineContent::MAX_LINES) {
49 ANS_LOGW("already added seven lines");
50 return;
51 }
52
53 allLines_.emplace_back(oneLine);
54 }
55
GetAllLines() const56 std::vector<std::string> NotificationMultiLineContent::GetAllLines() const
57 {
58 return allLines_;
59 }
60
Dump()61 std::string NotificationMultiLineContent::Dump()
62 {
63 std::string lines {};
64 std::for_each(
65 allLines_.begin(), allLines_.end(), [&lines](const std::string &line) { lines += " " + line + ","; });
66 if (!lines.empty()) {
67 lines.pop_back();
68 }
69
70 return "NotificationMultiLineContent{ " + NotificationBasicContent::Dump() +
71 ", briefText = " + briefText_ +
72 ", expandedTitle = " + expandedTitle_ +
73 ", allLines = [" + lines + "]" +
74 " }";
75 }
76
ToJson(nlohmann::json & jsonObject) const77 bool NotificationMultiLineContent::ToJson(nlohmann::json &jsonObject) const
78 {
79 if (!NotificationBasicContent::ToJson(jsonObject)) {
80 ANS_LOGE("Cannot convert basicContent to JSON");
81 return false;
82 }
83
84 jsonObject["expandedTitle"] = expandedTitle_;
85 jsonObject["briefText"] = briefText_;
86 jsonObject["allLines"] = nlohmann::json(allLines_);
87
88 return true;
89 }
90
FromJson(const nlohmann::json & jsonObject)91 NotificationMultiLineContent *NotificationMultiLineContent::FromJson(const nlohmann::json &jsonObject)
92 {
93 if (jsonObject.is_null() or !jsonObject.is_object()) {
94 ANS_LOGE("Invalid JSON object");
95 return nullptr;
96 }
97
98 auto pContent = new (std::nothrow) NotificationMultiLineContent();
99 if (pContent == nullptr) {
100 ANS_LOGE("Failed to create multiLineContent instance");
101 return nullptr;
102 }
103
104 pContent->ReadFromJson(jsonObject);
105
106 const auto &jsonEnd = jsonObject.cend();
107 if (jsonObject.find("expandedTitle") != jsonEnd && jsonObject.at("expandedTitle").is_string()) {
108 pContent->expandedTitle_ = jsonObject.at("expandedTitle").get<std::string>();
109 }
110
111 if (jsonObject.find("briefText") != jsonEnd && jsonObject.at("briefText").is_string()) {
112 pContent->briefText_ = jsonObject.at("briefText").get<std::string>();
113 }
114
115 if (jsonObject.find("allLines") != jsonEnd && jsonObject.at("allLines").is_array()) {
116 pContent->allLines_ = jsonObject.at("allLines").get<std::vector<std::string>>();
117 }
118
119 return pContent;
120 }
121
Marshalling(Parcel & parcel) const122 bool NotificationMultiLineContent::Marshalling(Parcel &parcel) const
123 {
124 if (!NotificationBasicContent::Marshalling(parcel)) {
125 ANS_LOGE("Write basic fail.");
126 return false;
127 }
128
129 if (!parcel.WriteString(expandedTitle_)) {
130 ANS_LOGE("Failed to write expanded title");
131 return false;
132 }
133
134 if (!parcel.WriteString(briefText_)) {
135 ANS_LOGE("Write brief text fail.");
136 return false;
137 }
138
139 if (!parcel.WriteStringVector(allLines_)) {
140 ANS_LOGE("Failed to write all lines");
141 return false;
142 }
143
144 return true;
145 }
146
Unmarshalling(Parcel & parcel)147 NotificationMultiLineContent *NotificationMultiLineContent::Unmarshalling(Parcel &parcel)
148 {
149 auto pContent = new (std::nothrow) NotificationMultiLineContent();
150 if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) {
151 delete pContent;
152 pContent = nullptr;
153 }
154
155 return pContent;
156 }
157
ReadFromParcel(Parcel & parcel)158 bool NotificationMultiLineContent::ReadFromParcel(Parcel &parcel)
159 {
160 if (!NotificationBasicContent::ReadFromParcel(parcel)) {
161 ANS_LOGE("Read basic failed.");
162 return false;
163 }
164
165 if (!parcel.ReadString(expandedTitle_)) {
166 ANS_LOGE("Failed to read expanded title");
167 return false;
168 }
169
170 if (!parcel.ReadString(briefText_)) {
171 ANS_LOGE("Read brief text failed.");
172 return false;
173 }
174
175 if (!parcel.ReadStringVector(&allLines_)) {
176 ANS_LOGE("Failed to read all lines");
177 return false;
178 }
179
180 return true;
181 }
182 } // namespace Notification
183 } // namespace OHOS
184