• 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 
Dump()61 std::string NotificationCapsule::Dump()
62 {
63     return "Capsule{ "
64             "title = " + title_ +
65             ", backgroundColor = " + backgroundColor_ +
66             ", icon = " + (icon_ ? "not null" : "null") +
67             " }";
68 }
69 
ToJson(nlohmann::json & jsonObject) const70 bool NotificationCapsule::ToJson(nlohmann::json &jsonObject) const
71 {
72     jsonObject["title"] = title_;
73     jsonObject["backgroundColor"] = backgroundColor_;
74     jsonObject["icon"] = AnsImageUtil::PackImage(icon_);
75 
76     return true;
77 }
78 
FromJson(const nlohmann::json & jsonObject)79 NotificationCapsule *NotificationCapsule::FromJson(const nlohmann::json &jsonObject)
80 {
81     if (jsonObject.is_null() or !jsonObject.is_object()) {
82         ANS_LOGE("Invalid JSON object");
83         return nullptr;
84     }
85 
86     NotificationCapsule *capsule = new (std::nothrow) NotificationCapsule();
87     if (capsule == nullptr) {
88         ANS_LOGE("Failed to create capsule instance");
89         return nullptr;
90     }
91 
92     const auto &jsonEnd = jsonObject.cend();
93     if (jsonObject.find("title") != jsonEnd && jsonObject.at("title").is_string()) {
94         capsule->title_ = jsonObject.at("title").get<std::string>();
95     }
96 
97     if (jsonObject.find("backgroundColor") != jsonEnd && jsonObject.at("backgroundColor").is_string()) {
98         capsule->backgroundColor_ = jsonObject.at("backgroundColor").get<std::string>();
99     }
100 
101     if (jsonObject.find("icon") != jsonEnd && jsonObject.at("icon").is_string()) {
102         auto pmStr             = jsonObject.at("icon").get<std::string>();
103         capsule->icon_ = AnsImageUtil::UnPackImage(pmStr);
104     }
105 
106     return capsule;
107 }
108 
Marshalling(Parcel & parcel) const109 bool NotificationCapsule::Marshalling(Parcel &parcel) const
110 {
111     if (!parcel.WriteString(title_)) {
112         ANS_LOGE("Failed to write title");
113         return false;
114     }
115 
116     if (!parcel.WriteString(backgroundColor_)) {
117         ANS_LOGE("Failed to write backgroundColor");
118         return false;
119     }
120 
121     bool valid = icon_ ? true : false;
122     if (!parcel.WriteBool(valid)) {
123         ANS_LOGE("Failed to write the flag which indicate whether icon pixelMap is null");
124         return false;
125     }
126 
127     if (valid) {
128         if (!parcel.WriteParcelable(icon_.get())) {
129             ANS_LOGE("Failed to write icon");
130             return false;
131         }
132     }
133 
134     return true;
135 }
136 
ReadFromParcel(Parcel & parcel)137 bool NotificationCapsule::ReadFromParcel(Parcel &parcel)
138 {
139     title_ = parcel.ReadString();
140     backgroundColor_ = parcel.ReadString();
141 
142     bool valid = parcel.ReadBool();
143     if (valid) {
144         icon_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
145         if (!icon_) {
146             ANS_LOGE("Failed to read icon pixelMap");
147             return false;
148         }
149     }
150 
151     return true;
152 }
153 
Unmarshalling(Parcel & parcel)154 NotificationCapsule *NotificationCapsule::Unmarshalling(Parcel &parcel)
155 {
156     NotificationCapsule *capsule = new (std::nothrow) NotificationCapsule();
157 
158     if (capsule && !capsule->ReadFromParcel(parcel)) {
159         delete capsule;
160         capsule = nullptr;
161     }
162 
163     return capsule;
164 }
165 }  // namespace Notification
166 }  // namespace OHOS