• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_capsule.h"
17 
18 #include <string>             // for basic_string, operator+, basic_string<>...
19 #include <memory>             // for shared_ptr, shared_ptr<>::element_type
20 
21 
22 #include "ans_image_util.h"
23 #include "ans_log_wrapper.h"
24 #include "nlohmann/json.hpp"  // for json, basic_json<>::object_t, basic_json
25 #include "parcel.h"           // for Parcel
26 #include "pixel_map.h"        // for PixelMap
27 
28 namespace OHOS {
29 namespace Notification {
30 
SetTitle(const std::string & title)31 void NotificationCapsule::SetTitle(const std::string &title)
32 {
33     title_ = title;
34 }
35 
GetTitle() const36 std::string NotificationCapsule::GetTitle() const
37 {
38     return title_;
39 }
40 
SetBackgroundColor(const std::string & color)41 void NotificationCapsule::SetBackgroundColor(const std::string &color)
42 {
43     backgroundColor_ = color;
44 }
45 
GetBackgroundColor() const46 std::string NotificationCapsule::GetBackgroundColor() const
47 {
48     return backgroundColor_;
49 }
50 
SetIcon(const std::shared_ptr<Media::PixelMap> & pixelMap)51 void NotificationCapsule::SetIcon(const std::shared_ptr<Media::PixelMap> &pixelMap)
52 {
53     icon_ = pixelMap;
54 }
55 
GetIcon() const56 const std::shared_ptr<Media::PixelMap> NotificationCapsule::GetIcon() const
57 {
58     return icon_;
59 }
60 
SetContent(const std::string & content)61 void NotificationCapsule::SetContent(const std::string &content)
62 {
63     content_ = content;
64 }
65 
GetContent() const66 std::string NotificationCapsule::GetContent() const
67 {
68     return content_;
69 }
70 
GetCapsuleButton() const71 std::vector<NotificationIconButton> NotificationCapsule::GetCapsuleButton() const
72 {
73     return capsuleButton_;
74 }
75 
SetCapsuleButton(const std::vector<NotificationIconButton> & buttons)76 void NotificationCapsule::SetCapsuleButton(const std::vector<NotificationIconButton> &buttons)
77 {
78     capsuleButton_ = buttons;
79 }
80 
GetTime() const81 int32_t NotificationCapsule::GetTime() const
82 {
83     return time_;
84 }
85 
SetTime(int32_t time)86 void NotificationCapsule::SetTime(int32_t time)
87 {
88     time_ = time;
89 }
90 
Dump()91 std::string NotificationCapsule::Dump()
92 {
93     return "Capsule{ "
94             "title = " + title_ +
95             ", backgroundColor = " + backgroundColor_ +
96             ", content = " + content_ +
97             ", icon = " + (icon_ ? "not null" : "null") +
98             ", time = " + std::to_string(time_) +
99             " }";
100 }
101 
ToJson(nlohmann::json & jsonObject) const102 bool NotificationCapsule::ToJson(nlohmann::json &jsonObject) const
103 {
104     jsonObject["title"] = title_;
105     jsonObject["backgroundColor"] = backgroundColor_;
106     jsonObject["content"] = content_;
107     jsonObject["icon"] = AnsImageUtil::PackImage(icon_);
108 
109     jsonObject["time"] = time_;
110     nlohmann::json capsuleBtnArr = nlohmann::json::array();
111     for (auto btn : capsuleButton_) {
112         nlohmann::json capsuleBtnObj;
113         if (!NotificationJsonConverter::ConvertToJson(&btn, capsuleBtnObj)) {
114             ANS_LOGE("Cannot convert button to JSON");
115             return false;
116         }
117         capsuleBtnArr.emplace_back(capsuleBtnObj);
118     }
119     jsonObject["capsuleButtons"] = capsuleBtnArr;
120 
121     return true;
122 }
123 
FromJson(const nlohmann::json & jsonObject)124 NotificationCapsule *NotificationCapsule::FromJson(const nlohmann::json &jsonObject)
125 {
126     if (jsonObject.is_null() or !jsonObject.is_object()) {
127         ANS_LOGE("Invalid JSON object");
128         return nullptr;
129     }
130 
131     NotificationCapsule *capsule = new (std::nothrow) NotificationCapsule();
132     if (capsule == nullptr) {
133         ANS_LOGE("null capsule");
134         return nullptr;
135     }
136 
137     const auto &jsonEnd = jsonObject.cend();
138     if (jsonObject.find("title") != jsonEnd && jsonObject.at("title").is_string()) {
139         capsule->title_ = jsonObject.at("title").get<std::string>();
140     }
141 
142     if (jsonObject.find("backgroundColor") != jsonEnd && jsonObject.at("backgroundColor").is_string()) {
143         capsule->backgroundColor_ = jsonObject.at("backgroundColor").get<std::string>();
144     }
145 
146     if (jsonObject.find("content") != jsonEnd && jsonObject.at("content").is_string()) {
147         capsule->content_ = jsonObject.at("content").get<std::string>();
148     }
149 
150     if (jsonObject.find("icon") != jsonEnd && jsonObject.at("icon").is_string()) {
151         auto pmStr             = jsonObject.at("icon").get<std::string>();
152         capsule->icon_ = AnsImageUtil::UnPackImage(pmStr);
153     }
154 
155     if (jsonObject.find("time") != jsonEnd && jsonObject.at("time").is_number_integer()) {
156         capsule->time_ = jsonObject.at("time").get<int32_t>();
157     }
158 
159     if (jsonObject.find("capsuleButtons") != jsonEnd && jsonObject.at("capsuleButtons").is_array()) {
160         std::vector<NotificationIconButton> cardButtons;
161         for (auto &item : jsonObject.at("capsuleButtons").items()) {
162             nlohmann::json cardBtnObject = item.value();
163             auto pButton = NotificationJsonConverter::ConvertFromJson<NotificationIconButton>(cardBtnObject);
164             if (pButton != nullptr) {
165                 cardButtons.push_back(*pButton);
166                 delete pButton;
167                 pButton = nullptr;
168             }
169         }
170         capsule->capsuleButton_ = cardButtons;
171     }
172 
173     return capsule;
174 }
175 
Marshalling(Parcel & parcel) const176 bool NotificationCapsule::Marshalling(Parcel &parcel) const
177 {
178     if (!parcel.WriteString(title_)) {
179         ANS_LOGE("Failed to write title");
180         return false;
181     }
182 
183     if (!parcel.WriteString(backgroundColor_)) {
184         ANS_LOGE("Failed to write backgroundColor");
185         return false;
186     }
187 
188     if (!parcel.WriteString(content_)) {
189         ANS_LOGE("Failed to write content");
190         return false;
191     }
192 
193     bool valid = icon_ ? true : false;
194     if (!parcel.WriteBool(valid)) {
195         ANS_LOGE("Failed to write the flag which indicate whether icon pixelMap is null");
196         return false;
197     }
198 
199     if (valid) {
200         if (!parcel.WriteParcelable(icon_.get())) {
201             ANS_LOGE("Failed to write icon");
202             return false;
203         }
204     }
205 
206     if (!parcel.WriteInt32(time_)) {
207         ANS_LOGE("Write time_ fail.");
208         return false;
209     }
210 
211     parcel.WriteInt32(static_cast<int>(capsuleButton_.size()));
212     for (const auto& button : capsuleButton_) {
213         if (!parcel.WriteParcelable(&button)) {
214             ANS_LOGE("Failed to write card button");
215             return false;
216         }
217     }
218 
219     return true;
220 }
221 
ReadFromParcel(Parcel & parcel)222 bool NotificationCapsule::ReadFromParcel(Parcel &parcel)
223 {
224     title_ = parcel.ReadString();
225     backgroundColor_ = parcel.ReadString();
226     content_ = parcel.ReadString();
227 
228     bool valid = parcel.ReadBool();
229     if (valid) {
230         icon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
231         if (!icon_) {
232             ANS_LOGE("Failed to read icon pixelMap");
233             return false;
234         }
235     }
236 
237     if (!parcel.ReadInt32(time_)) {
238         ANS_LOGE("Read time_ failed.");
239         return false;
240     }
241 
242     auto vsize = static_cast<uint32_t>(parcel.ReadInt32());
243     vsize = (vsize < BUTTON_MAX_SIZE) ? vsize : BUTTON_MAX_SIZE;
244     capsuleButton_.clear();
245     for (uint32_t i = 0; i < vsize; ++i) {
246         auto btn = parcel.ReadParcelable<NotificationIconButton>();
247         if (btn == nullptr) {
248             ANS_LOGE("Failed to read card button");
249             return false;
250         }
251         capsuleButton_.push_back(*btn);
252         delete btn;
253         btn = nullptr;
254     }
255 
256     return true;
257 }
258 
Unmarshalling(Parcel & parcel)259 NotificationCapsule *NotificationCapsule::Unmarshalling(Parcel &parcel)
260 {
261     NotificationCapsule *capsule = new (std::nothrow) NotificationCapsule();
262 
263     if (capsule && !capsule->ReadFromParcel(parcel)) {
264         delete capsule;
265         capsule = nullptr;
266     }
267 
268     return capsule;
269 }
270 
ResetIcon()271 void NotificationCapsule::ResetIcon()
272 {
273     if (icon_) {
274         icon_.reset();
275     }
276 }
277 }  // namespace Notification
278 }  // namespace OHOS