• 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_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         pContent->expandedTitle_ = jsonObject.at("expandedTitle").get<std::string>();
93     }
94 
95     if (jsonObject.find("briefText") != jsonEnd && jsonObject.at("briefText").is_string()) {
96         pContent->briefText_ = jsonObject.at("briefText").get<std::string>();
97     }
98 
99     if (jsonObject.find("bigPicture") != jsonEnd && jsonObject.at("bigPicture").is_string()) {
100         auto picStr           = jsonObject.at("bigPicture").get<std::string>();
101         pContent->bigPicture_ = AnsImageUtil::UnPackImage(picStr);
102     }
103 
104     return pContent;
105 }
106 
Marshalling(Parcel & parcel) const107 bool NotificationPictureContent::Marshalling(Parcel &parcel) const
108 {
109     if (!NotificationBasicContent::Marshalling(parcel)) {
110         ANS_LOGE("Failed to write basic");
111         return false;
112     }
113 
114     if (!parcel.WriteString(expandedTitle_)) {
115         ANS_LOGE("Failed to write expanded title");
116         return false;
117     }
118 
119     if (!parcel.WriteString(briefText_)) {
120         ANS_LOGE("Failed to write brief text");
121         return false;
122     }
123 
124     auto valid = bigPicture_ ? true : false;
125     if (!parcel.WriteBool(valid)) {
126         ANS_LOGE("Failed to write the flag which indicate whether bigPicture is null");
127         return false;
128     }
129 
130     if (valid) {
131         if (!parcel.WriteParcelable(bigPicture_.get())) {
132             ANS_LOGE("Failed to write bigPicture");
133             return false;
134         }
135     }
136 
137     return true;
138 }
139 
Unmarshalling(Parcel & parcel)140 NotificationPictureContent *NotificationPictureContent::Unmarshalling(Parcel &parcel)
141 {
142     auto pContent = new (std::nothrow) NotificationPictureContent();
143     if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) {
144         delete pContent;
145         pContent = nullptr;
146     }
147 
148     return pContent;
149 }
150 
ReadFromParcel(Parcel & parcel)151 bool NotificationPictureContent::ReadFromParcel(Parcel &parcel)
152 {
153     if (!NotificationBasicContent::ReadFromParcel(parcel)) {
154         ANS_LOGE("Failed to read basic");
155         return false;
156     }
157 
158     if (!parcel.ReadString(expandedTitle_)) {
159         ANS_LOGE("Failed to read expanded title");
160         return false;
161     }
162 
163     if (!parcel.ReadString(briefText_)) {
164         ANS_LOGE("Failed to read brief text");
165         return false;
166     }
167 
168     auto valid = parcel.ReadBool();
169     if (valid) {
170         bigPicture_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
171         if (!bigPicture_) {
172             ANS_LOGE("Failed to read bigPicture");
173             return false;
174         }
175     }
176 
177     return true;
178 }
179 }  // namespace Notification
180 }  // namespace OHOS
181