• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SetLineWantAgents(std::vector<std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>> lineWantAgents)61 void NotificationMultiLineContent::SetLineWantAgents(
62     std::vector<std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>> lineWantAgents)
63 {
64     lineWantAgents_ = lineWantAgents;
65 }
66 
GetLineWantAgents()67 std::vector<std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>> NotificationMultiLineContent::GetLineWantAgents()
68 {
69     return lineWantAgents_;
70 }
71 
Dump()72 std::string NotificationMultiLineContent::Dump()
73 {
74     std::string lines {};
75     std::for_each(
76         allLines_.begin(), allLines_.end(), [&lines](const std::string &line) { lines += " " + line + ","; });
77     if (!lines.empty()) {
78         lines.pop_back();
79     }
80 
81     return "NotificationMultiLineContent{ " + NotificationBasicContent::Dump() +
82             ", briefText = " + briefText_ +
83             ", expandedTitle = " + expandedTitle_ +
84             ", allLines = [" + lines + "]" +
85             " }";
86 }
87 
ToJson(nlohmann::json & jsonObject) const88 bool NotificationMultiLineContent::ToJson(nlohmann::json &jsonObject) const
89 {
90     if (!NotificationBasicContent::ToJson(jsonObject)) {
91         ANS_LOGE("Cannot convert basicContent to JSON");
92         return false;
93     }
94 
95     jsonObject["expandedTitle"] = expandedTitle_;
96     jsonObject["briefText"]     = briefText_;
97     jsonObject["allLines"]      = nlohmann::json(allLines_);
98 
99     return true;
100 }
101 
FromJson(const nlohmann::json & jsonObject)102 NotificationMultiLineContent *NotificationMultiLineContent::FromJson(const nlohmann::json &jsonObject)
103 {
104     if (jsonObject.is_null() or !jsonObject.is_object()) {
105         ANS_LOGE("Invalid JSON object");
106         return nullptr;
107     }
108 
109     auto pContent = new (std::nothrow) NotificationMultiLineContent();
110     if (pContent == nullptr) {
111         ANS_LOGE("null pContent");
112         return nullptr;
113     }
114 
115     pContent->ReadFromJson(jsonObject);
116 
117     const auto &jsonEnd = jsonObject.cend();
118     if (jsonObject.find("expandedTitle") != jsonEnd && jsonObject.at("expandedTitle").is_string()) {
119         pContent->expandedTitle_ = jsonObject.at("expandedTitle").get<std::string>();
120     }
121 
122     if (jsonObject.find("briefText") != jsonEnd && jsonObject.at("briefText").is_string()) {
123         pContent->briefText_ = jsonObject.at("briefText").get<std::string>();
124     }
125 
126     if (jsonObject.find("allLines") != jsonEnd && jsonObject.at("allLines").is_array()) {
127         pContent->allLines_ = jsonObject.at("allLines").get<std::vector<std::string>>();
128     }
129 
130     return pContent;
131 }
132 
Marshalling(Parcel & parcel) const133 bool NotificationMultiLineContent::Marshalling(Parcel &parcel) const
134 {
135     if (!NotificationBasicContent::Marshalling(parcel)) {
136         ANS_LOGE("Write basic fail.");
137         return false;
138     }
139 
140     if (!parcel.WriteString(expandedTitle_)) {
141         ANS_LOGE("Failed to write expanded title");
142         return false;
143     }
144 
145     if (!parcel.WriteString(briefText_)) {
146         ANS_LOGE("Write brief text fail.");
147         return false;
148     }
149 
150     if (!parcel.WriteStringVector(allLines_)) {
151         ANS_LOGE("Failed to write all lines");
152         return false;
153     }
154 
155     std::uint8_t lineWantAgentsLength = lineWantAgents_.size();
156     if (!parcel.WriteUint8(lineWantAgentsLength)) {
157         ANS_LOGE("Failed to write lineWantAgentsLength");
158         return false;
159     }
160     for (auto it = lineWantAgents_.begin(); it != lineWantAgents_.end(); ++it) {
161         if (!parcel.WriteParcelable(it->get())) {
162             ANS_LOGE("Fail to write wantAgent of lineWantAgent.");
163             return false;
164         }
165     }
166 
167     return true;
168 }
169 
Unmarshalling(Parcel & parcel)170 NotificationMultiLineContent *NotificationMultiLineContent::Unmarshalling(Parcel &parcel)
171 {
172     auto pContent = new (std::nothrow) NotificationMultiLineContent();
173     if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) {
174         delete pContent;
175         pContent = nullptr;
176     }
177 
178     return pContent;
179 }
180 
ReadFromParcel(Parcel & parcel)181 bool NotificationMultiLineContent::ReadFromParcel(Parcel &parcel)
182 {
183     if (!NotificationBasicContent::ReadFromParcel(parcel)) {
184         ANS_LOGE("Read basic failed.");
185         return false;
186     }
187 
188     if (!parcel.ReadString(expandedTitle_)) {
189         ANS_LOGE("Failed to read expanded title");
190         return false;
191     }
192 
193     if (!parcel.ReadString(briefText_)) {
194         ANS_LOGE("Read brief text failed.");
195         return false;
196     }
197 
198     if (!parcel.ReadStringVector(&allLines_)) {
199         ANS_LOGE("Failed to read all lines");
200         return false;
201     }
202     std::uint8_t lineWantAgentsLength = 0;
203     if (!parcel.ReadUint8(lineWantAgentsLength)) {
204         ANS_LOGE("Failed to read lineWantAgentsLength");
205         return false;
206     }
207     for (std::uint8_t i = 0; i < lineWantAgentsLength; i++) {
208         auto wantAgent = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
209             parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
210         lineWantAgents_.push_back(wantAgent);
211     }
212 
213     return true;
214 }
215 }  // namespace Notification
216 }  // namespace OHOS
217