• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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     lines.pop_back();
67 
68     return "NotificationMultiLineContent{ " + NotificationBasicContent::Dump() +
69             ", briefText = " + briefText_ +
70             ", expandedTitle = " + expandedTitle_ +
71             ", allLines = [" + lines + "]" +
72             " }";
73 }
74 
ToJson(nlohmann::json & jsonObject) const75 bool NotificationMultiLineContent::ToJson(nlohmann::json &jsonObject) const
76 {
77     if (!NotificationBasicContent::ToJson(jsonObject)) {
78         ANS_LOGE("Cannot convert basicContent to JSON");
79         return false;
80     }
81 
82     jsonObject["expandedTitle"] = expandedTitle_;
83     jsonObject["briefText"]     = briefText_;
84     jsonObject["allLines"]      = nlohmann::json(allLines_);
85 
86     return true;
87 }
88 
FromJson(const nlohmann::json & jsonObject)89 NotificationMultiLineContent *NotificationMultiLineContent::FromJson(const nlohmann::json &jsonObject)
90 {
91     if (jsonObject.is_null() or !jsonObject.is_object()) {
92         ANS_LOGE("Invalid JSON object");
93         return nullptr;
94     }
95 
96     auto pContent = new (std::nothrow) NotificationMultiLineContent();
97     if (pContent == nullptr) {
98         ANS_LOGE("Failed to create multiLineContent instance");
99         return nullptr;
100     }
101 
102     pContent->ReadFromJson(jsonObject);
103 
104     const auto &jsonEnd = jsonObject.cend();
105     if (jsonObject.find("expandedTitle") != jsonEnd) {
106         pContent->expandedTitle_ = jsonObject.at("expandedTitle").get<std::string>();
107     }
108 
109     if (jsonObject.find("briefText") != jsonEnd) {
110         pContent->briefText_ = jsonObject.at("briefText").get<std::string>();
111     }
112 
113     if (jsonObject.find("allLines") != jsonEnd) {
114         pContent->allLines_ = jsonObject.at("allLines").get<std::vector<std::string>>();
115     }
116 
117     return pContent;
118 }
119 
Marshalling(Parcel & parcel) const120 bool NotificationMultiLineContent::Marshalling(Parcel &parcel) const
121 {
122     if (!NotificationBasicContent::Marshalling(parcel)) {
123         ANS_LOGE("Failed to write basic");
124         return false;
125     }
126 
127     if (!parcel.WriteString(expandedTitle_)) {
128         ANS_LOGE("Failed to write expanded title");
129         return false;
130     }
131 
132     if (!parcel.WriteString(briefText_)) {
133         ANS_LOGE("Failed to write brief text");
134         return false;
135     }
136 
137     if (!parcel.WriteStringVector(allLines_)) {
138         ANS_LOGE("Failed to write all lines");
139         return false;
140     }
141 
142     return true;
143 }
144 
Unmarshalling(Parcel & parcel)145 NotificationMultiLineContent *NotificationMultiLineContent::Unmarshalling(Parcel &parcel)
146 {
147     auto pContent = new (std::nothrow) NotificationMultiLineContent();
148     if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) {
149         delete pContent;
150         pContent = nullptr;
151     }
152 
153     return pContent;
154 }
155 
ReadFromParcel(Parcel & parcel)156 bool NotificationMultiLineContent::ReadFromParcel(Parcel &parcel)
157 {
158     if (!NotificationBasicContent::ReadFromParcel(parcel)) {
159         ANS_LOGE("Failed to read basic");
160         return false;
161     }
162 
163     if (!parcel.ReadString(expandedTitle_)) {
164         ANS_LOGE("Failed to read expanded title");
165         return false;
166     }
167 
168     if (!parcel.ReadString(briefText_)) {
169         ANS_LOGE("Failed to read brief text");
170         return false;
171     }
172 
173     if (!parcel.ReadStringVector(&allLines_)) {
174         ANS_LOGE("Failed to read all lines");
175         return false;
176     }
177 
178     return true;
179 }
180 }  // namespace Notification
181 }  // namespace OHOS
182