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_picture_content.h"
17 #include "ans_image_util.h"
18 #include "ans_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace Notification {
SetExpandedTitle(const std::string & exTitle)22 void NotificationPictureContent::SetExpandedTitle(const std::string &exTitle)
23 {
24     expandedTitle_ = exTitle;
25 }
26 
GetExpandedTitle() const27 std::string NotificationPictureContent::GetExpandedTitle() const
28 {
29     return expandedTitle_;
30 }
31 
SetBriefText(const std::string & briefText)32 void NotificationPictureContent::SetBriefText(const std::string &briefText)
33 {
34     briefText_ = briefText;
35 }
36 
GetBriefText() const37 std::string NotificationPictureContent::GetBriefText() const
38 {
39     return briefText_;
40 }
41 
SetBigPicture(const std::shared_ptr<Media::PixelMap> & bigPicture)42 void NotificationPictureContent::SetBigPicture(const std::shared_ptr<Media::PixelMap> &bigPicture)
43 {
44     bigPicture_ = bigPicture;
45 }
46 
GetBigPicture() const47 const std::shared_ptr<Media::PixelMap> NotificationPictureContent::GetBigPicture() const
48 {
49     return bigPicture_;
50 }
51 
Dump()52 std::string NotificationPictureContent::Dump()
53 {
54     return "NotificationPictureContent{ " + NotificationBasicContent::Dump() +
55             ", briefText = " + briefText_ +
56             ", expandedTitle = " + expandedTitle_ +
57             ", bigPicture = " + (bigPicture_ ? "not null" : "null") +
58             " }";
59 }
60 
ToJson(nlohmann::json & jsonObject) const61 bool NotificationPictureContent::ToJson(nlohmann::json &jsonObject) const
62 {
63     if (!NotificationBasicContent::ToJson(jsonObject)) {
64         ANS_LOGE("Cannot convert basicContent to JSON");
65         return false;
66     }
67 
68     jsonObject["expandedTitle"] = expandedTitle_;
69     jsonObject["briefText"]     = briefText_;
70     jsonObject["bigPicture"]    = AnsImageUtil::PackImage(bigPicture_);
71 
72     return true;
73 }
74 
FromJson(const nlohmann::json & jsonObject)75 NotificationPictureContent *NotificationPictureContent::FromJson(const nlohmann::json &jsonObject)
76 {
77     if (jsonObject.is_null() or !jsonObject.is_object()) {
78         ANS_LOGE("Invalid JSON object");
79         return nullptr;
80     }
81 
82     auto pContent = new (std::nothrow) NotificationPictureContent();
83     if (pContent == nullptr) {
84         ANS_LOGE("Failed to create pictureContent instance");
85         return nullptr;
86     }
87 
88     pContent->ReadFromJson(jsonObject);
89 
90     const auto &jsonEnd = jsonObject.cend();
91     if (jsonObject.find("expandedTitle") != jsonEnd && jsonObject.at("expandedTitle").is_string()) {
92         ANS_LOGD("Find expandedTitle success.");
93         pContent->expandedTitle_ = jsonObject.at("expandedTitle").get<std::string>();
94     }
95 
96     if (jsonObject.find("briefText") != jsonEnd && jsonObject.at("briefText").is_string()) {
97         pContent->briefText_ = jsonObject.at("briefText").get<std::string>();
98     }
99 
100     if (jsonObject.find("bigPicture") != jsonEnd && jsonObject.at("bigPicture").is_string()) {
101         auto picStr           = jsonObject.at("bigPicture").get<std::string>();
102         pContent->bigPicture_ = AnsImageUtil::UnPackImage(picStr);
103     }
104 
105     return pContent;
106 }
107 
Marshalling(Parcel & parcel) const108 bool NotificationPictureContent::Marshalling(Parcel &parcel) const
109 {
110     if (!NotificationBasicContent::Marshalling(parcel)) {
111         ANS_LOGE("Failed to write basic");
112         return false;
113     }
114 
115     if (!parcel.WriteString(expandedTitle_)) {
116         ANS_LOGE("Failed to write expanded title");
117         return false;
118     }
119 
120     if (!parcel.WriteString(briefText_)) {
121         ANS_LOGE("Failed to write brief text");
122         return false;
123     }
124 
125     auto valid = bigPicture_ ? true : false;
126     if (!parcel.WriteBool(valid)) {
127         ANS_LOGE("Failed to write the flag which indicate whether bigPicture is null");
128         return false;
129     }
130 
131     if (valid) {
132         if (!parcel.WriteParcelable(bigPicture_.get())) {
133             ANS_LOGE("Failed to write bigPicture");
134             return false;
135         }
136     }
137 
138     return true;
139 }
140 
Unmarshalling(Parcel & parcel)141 NotificationPictureContent *NotificationPictureContent::Unmarshalling(Parcel &parcel)
142 {
143     auto pContent = new (std::nothrow) NotificationPictureContent();
144     if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) {
145         delete pContent;
146         pContent = nullptr;
147     }
148 
149     return pContent;
150 }
151 
ReadFromParcel(Parcel & parcel)152 bool NotificationPictureContent::ReadFromParcel(Parcel &parcel)
153 {
154     if (!NotificationBasicContent::ReadFromParcel(parcel)) {
155         ANS_LOGE("Failed to read basic");
156         return false;
157     }
158 
159     if (!parcel.ReadString(expandedTitle_)) {
160         ANS_LOGE("Failed to read expanded title");
161         return false;
162     }
163 
164     if (!parcel.ReadString(briefText_)) {
165         ANS_LOGE("Failed to read brief text");
166         return false;
167     }
168 
169     auto valid = parcel.ReadBool();
170     if (valid) {
171         bigPicture_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
172         if (!bigPicture_) {
173             ANS_LOGE("Failed to read bigPicture");
174             return false;
175         }
176     }
177 
178     return true;
179 }
180 }  // namespace Notification
181 }  // namespace OHOS
182