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