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 
Dump()71 std::string NotificationCapsule::Dump()
72 {
73     return "Capsule{ "
74             "title = " + title_ +
75             ", backgroundColor = " + backgroundColor_ +
76             ", content = " + content_ +
77             ", icon = " + (icon_ ? "not null" : "null") +
78             " }";
79 }
80 
ToJson(nlohmann::json & jsonObject) const81 bool NotificationCapsule::ToJson(nlohmann::json &jsonObject) const
82 {
83     jsonObject["title"] = title_;
84     jsonObject["backgroundColor"] = backgroundColor_;
85     jsonObject["content"] = content_;
86     jsonObject["icon"] = AnsImageUtil::PackImage(icon_);
87 
88     return true;
89 }
90 
FromJson(const nlohmann::json & jsonObject)91 NotificationCapsule *NotificationCapsule::FromJson(const nlohmann::json &jsonObject)
92 {
93     if (jsonObject.is_null() or !jsonObject.is_object()) {
94         ANS_LOGE("Invalid JSON object");
95         return nullptr;
96     }
97 
98     NotificationCapsule *capsule = new (std::nothrow) NotificationCapsule();
99     if (capsule == nullptr) {
100         ANS_LOGE("Failed to create capsule instance");
101         return nullptr;
102     }
103 
104     const auto &jsonEnd = jsonObject.cend();
105     if (jsonObject.find("title") != jsonEnd && jsonObject.at("title").is_string()) {
106         capsule->title_ = jsonObject.at("title").get<std::string>();
107     }
108 
109     if (jsonObject.find("backgroundColor") != jsonEnd && jsonObject.at("backgroundColor").is_string()) {
110         capsule->backgroundColor_ = jsonObject.at("backgroundColor").get<std::string>();
111     }
112 
113     if (jsonObject.find("content") != jsonEnd && jsonObject.at("content").is_string()) {
114         capsule->content_ = jsonObject.at("content").get<std::string>();
115     }
116 
117     if (jsonObject.find("icon") != jsonEnd && jsonObject.at("icon").is_string()) {
118         auto pmStr             = jsonObject.at("icon").get<std::string>();
119         capsule->icon_ = AnsImageUtil::UnPackImage(pmStr);
120     }
121 
122     return capsule;
123 }
124 
Marshalling(Parcel & parcel) const125 bool NotificationCapsule::Marshalling(Parcel &parcel) const
126 {
127     if (!parcel.WriteString(title_)) {
128         ANS_LOGE("Failed to write title");
129         return false;
130     }
131 
132     if (!parcel.WriteString(backgroundColor_)) {
133         ANS_LOGE("Failed to write backgroundColor");
134         return false;
135     }
136 
137     if (!parcel.WriteString(content_)) {
138         ANS_LOGE("Failed to write content");
139         return false;
140     }
141 
142     bool valid = icon_ ? true : false;
143     if (!parcel.WriteBool(valid)) {
144         ANS_LOGE("Failed to write the flag which indicate whether icon pixelMap is null");
145         return false;
146     }
147 
148     if (valid) {
149         if (!parcel.WriteParcelable(icon_.get())) {
150             ANS_LOGE("Failed to write icon");
151             return false;
152         }
153     }
154 
155     return true;
156 }
157 
ReadFromParcel(Parcel & parcel)158 bool NotificationCapsule::ReadFromParcel(Parcel &parcel)
159 {
160     title_ = parcel.ReadString();
161     backgroundColor_ = parcel.ReadString();
162     content_ = parcel.ReadString();
163 
164     bool valid = parcel.ReadBool();
165     if (valid) {
166         icon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
167         if (!icon_) {
168             ANS_LOGE("Failed to read icon pixelMap");
169             return false;
170         }
171     }
172 
173     return true;
174 }
175 
Unmarshalling(Parcel & parcel)176 NotificationCapsule *NotificationCapsule::Unmarshalling(Parcel &parcel)
177 {
178     NotificationCapsule *capsule = new (std::nothrow) NotificationCapsule();
179 
180     if (capsule && !capsule->ReadFromParcel(parcel)) {
181         delete capsule;
182         capsule = nullptr;
183     }
184 
185     return capsule;
186 }
187 
ResetIcon()188 void NotificationCapsule::ResetIcon()
189 {
190     if (icon_) {
191         icon_.reset();
192     }
193 }
194 }  // namespace Notification
195 }  // namespace OHOS