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_long_text_content.h"
17
18 #include <string> // for basic_string, operator+
19 #include <algorithm> // for min
20
21 #include "ans_log_wrapper.h"
22 #include "nlohmann/json.hpp" // for json, basic_json<>::obje...
23 #include "notification_basic_content.h" // for NotificationBasicContent
24 #include "parcel.h" // for Parcel
25
26 namespace OHOS {
27 namespace Notification {
28 const std::size_t NotificationLongTextContent::MAX_LONGTEXT_LENGTH {1024};
29
NotificationLongTextContent(const std::string & longText)30 NotificationLongTextContent::NotificationLongTextContent(const std::string &longText)
31 {
32 SetLongText(longText);
33 }
34
SetExpandedTitle(const std::string & exTitle)35 void NotificationLongTextContent::SetExpandedTitle(const std::string &exTitle)
36 {
37 expandedTitle_ = exTitle;
38 }
39
GetExpandedTitle() const40 std::string NotificationLongTextContent::GetExpandedTitle() const
41 {
42 return expandedTitle_;
43 }
44
SetBriefText(const std::string & briefText)45 void NotificationLongTextContent::SetBriefText(const std::string &briefText)
46 {
47 briefText_ = briefText;
48 }
49
GetBriefText() const50 std::string NotificationLongTextContent::GetBriefText() const
51 {
52 return briefText_;
53 }
54
SetLongText(const std::string & longText)55 void NotificationLongTextContent::SetLongText(const std::string &longText)
56 {
57 if (longText.empty()) {
58 longText_.clear();
59 return;
60 }
61
62 auto length = std::min(NotificationLongTextContent::MAX_LONGTEXT_LENGTH, longText.length());
63 longText_.assign(longText.begin(), longText.begin() + length);
64 }
65
GetLongText() const66 std::string NotificationLongTextContent::GetLongText() const
67 {
68 return longText_;
69 }
70
Dump()71 std::string NotificationLongTextContent::Dump()
72 {
73 return "NotificationLongTextContent{ " + NotificationBasicContent::Dump() +
74 ", longText = " + longText_ +
75 ", briefText = " + briefText_ +
76 ", expandedTitle = " + expandedTitle_ +
77 " }";
78 }
79
ToJson(nlohmann::json & jsonObject) const80 bool NotificationLongTextContent::ToJson(nlohmann::json &jsonObject) const
81 {
82 if (!NotificationBasicContent::ToJson(jsonObject)) {
83 ANS_LOGE("Cannot convert basicContent to JSON");
84 return false;
85 }
86
87 jsonObject["longText"] = longText_;
88 jsonObject["expandedTitle"] = expandedTitle_;
89 jsonObject["briefText"] = briefText_;
90
91 return true;
92 }
93
FromJson(const nlohmann::json & jsonObject)94 NotificationLongTextContent *NotificationLongTextContent::FromJson(const nlohmann::json &jsonObject)
95 {
96 if (jsonObject.is_null() or !jsonObject.is_object()) {
97 ANS_LOGE("Invalid JSON object");
98 return nullptr;
99 }
100
101 auto pContent = new (std::nothrow) NotificationLongTextContent();
102 if (pContent == nullptr) {
103 ANS_LOGE("Failed to create longTextContent instance");
104 return nullptr;
105 }
106
107 pContent->ReadFromJson(jsonObject);
108
109 const auto &jsonEnd = jsonObject.cend();
110 if (jsonObject.find("longText") != jsonEnd && jsonObject.at("longText").is_string()) {
111 pContent->longText_ = jsonObject.at("longText").get<std::string>();
112 }
113
114 if (jsonObject.find("expandedTitle") != jsonEnd && jsonObject.at("expandedTitle").is_string()) {
115 pContent->expandedTitle_ = jsonObject.at("expandedTitle").get<std::string>();
116 }
117
118 if (jsonObject.find("briefText") != jsonEnd && jsonObject.at("briefText").is_string()) {
119 pContent->briefText_ = jsonObject.at("briefText").get<std::string>();
120 }
121
122 return pContent;
123 }
124
Marshalling(Parcel & parcel) const125 bool NotificationLongTextContent::Marshalling(Parcel &parcel) const
126 {
127 if (!NotificationBasicContent::Marshalling(parcel)) {
128 ANS_LOGE("Failed to write basic");
129 return false;
130 }
131
132 if (!parcel.WriteString(expandedTitle_)) {
133 ANS_LOGE("Failed to write expanded title");
134 return false;
135 }
136
137 if (!parcel.WriteString(briefText_)) {
138 ANS_LOGE("Failed to write brief text");
139 return false;
140 }
141
142 if (!parcel.WriteString(longText_)) {
143 ANS_LOGE("Failed to write longText");
144 return false;
145 }
146
147 return true;
148 }
149
Unmarshalling(Parcel & parcel)150 NotificationLongTextContent *NotificationLongTextContent::Unmarshalling(Parcel &parcel)
151 {
152 auto pContent = new (std::nothrow) NotificationLongTextContent();
153 if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) {
154 delete pContent;
155 pContent = nullptr;
156 }
157
158 return pContent;
159 }
160
ReadFromParcel(Parcel & parcel)161 bool NotificationLongTextContent::ReadFromParcel(Parcel &parcel)
162 {
163 if (!NotificationBasicContent::ReadFromParcel(parcel)) {
164 ANS_LOGE("Failed to read basic");
165 return false;
166 }
167
168 if (!parcel.ReadString(expandedTitle_)) {
169 ANS_LOGE("Failed to read expanded title");
170 return false;
171 }
172
173 if (!parcel.ReadString(briefText_)) {
174 ANS_LOGE("Failed to read brief text");
175 return false;
176 }
177
178 if (!parcel.ReadString(longText_)) {
179 ANS_LOGE("Failed to read longtext");
180 return false;
181 }
182
183 return true;
184 }
185 } // namespace Notification
186 } // namespace OHOS
187