Files
notification_ans_standard/frameworks/ans/src/notification_picture_content.cpp
T
zero-cyc 869c9a1ea2 notification directory modify
Signed-off-by: zero-cyc <chenlien@huawei.com>
Change-Id: I5c8cb4ec680df5ad2728f721eb91d910d2353f7c
2022-05-27 00:24:46 +08:00

181 lines
5.0 KiB
C++

/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "notification_picture_content.h"
#include "ans_image_util.h"
#include "ans_log_wrapper.h"
namespace OHOS {
namespace Notification {
void NotificationPictureContent::SetExpandedTitle(const std::string &exTitle)
{
expandedTitle_ = exTitle;
}
std::string NotificationPictureContent::GetExpandedTitle() const
{
return expandedTitle_;
}
void NotificationPictureContent::SetBriefText(const std::string &briefText)
{
briefText_ = briefText;
}
std::string NotificationPictureContent::GetBriefText() const
{
return briefText_;
}
void NotificationPictureContent::SetBigPicture(const std::shared_ptr<Media::PixelMap> &bigPicture)
{
bigPicture_ = bigPicture;
}
const std::shared_ptr<Media::PixelMap> NotificationPictureContent::GetBigPicture() const
{
return bigPicture_;
}
std::string NotificationPictureContent::Dump()
{
return "NotificationPictureContent{ " + NotificationBasicContent::Dump() +
", briefText = " + briefText_ +
", expandedTitle = " + expandedTitle_ +
", bigPicture = " + (bigPicture_ ? "not null" : "null") +
" }";
}
bool NotificationPictureContent::ToJson(nlohmann::json &jsonObject) const
{
if (!NotificationBasicContent::ToJson(jsonObject)) {
ANS_LOGE("Cannot convert basicContent to JSON");
return false;
}
jsonObject["expandedTitle"] = expandedTitle_;
jsonObject["briefText"] = briefText_;
jsonObject["bigPicture"] = AnsImageUtil::PackImage(bigPicture_);
return true;
}
NotificationPictureContent *NotificationPictureContent::FromJson(const nlohmann::json &jsonObject)
{
if (jsonObject.is_null() or !jsonObject.is_object()) {
ANS_LOGE("Invalid JSON object");
return nullptr;
}
auto pContent = new (std::nothrow) NotificationPictureContent();
if (pContent == nullptr) {
ANS_LOGE("Failed to create pictureContent instance");
return nullptr;
}
pContent->ReadFromJson(jsonObject);
const auto &jsonEnd = jsonObject.cend();
if (jsonObject.find("expandedTitle") != jsonEnd) {
pContent->expandedTitle_ = jsonObject.at("expandedTitle").get<std::string>();
}
if (jsonObject.find("briefText") != jsonEnd) {
pContent->briefText_ = jsonObject.at("briefText").get<std::string>();
}
if (jsonObject.find("bigPicture") != jsonEnd) {
auto picStr = jsonObject.at("bigPicture").get<std::string>();
pContent->bigPicture_ = AnsImageUtil::UnPackImage(picStr);
}
return pContent;
}
bool NotificationPictureContent::Marshalling(Parcel &parcel) const
{
if (!NotificationBasicContent::Marshalling(parcel)) {
ANS_LOGE("Failed to write basic");
return false;
}
if (!parcel.WriteString(expandedTitle_)) {
ANS_LOGE("Failed to write expanded title");
return false;
}
if (!parcel.WriteString(briefText_)) {
ANS_LOGE("Failed to write brief text");
return false;
}
auto valid = bigPicture_ ? true : false;
if (!parcel.WriteBool(valid)) {
ANS_LOGE("Failed to write the flag which indicate whether bigPicture is null");
return false;
}
if (valid) {
if (!parcel.WriteParcelable(bigPicture_.get())) {
ANS_LOGE("Failed to write bigPicture");
return false;
}
}
return true;
}
NotificationPictureContent *NotificationPictureContent::Unmarshalling(Parcel &parcel)
{
auto pContent = new (std::nothrow) NotificationPictureContent();
if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) {
delete pContent;
pContent = nullptr;
}
return pContent;
}
bool NotificationPictureContent::ReadFromParcel(Parcel &parcel)
{
if (!NotificationBasicContent::ReadFromParcel(parcel)) {
ANS_LOGE("Failed to read basic");
return false;
}
if (!parcel.ReadString(expandedTitle_)) {
ANS_LOGE("Failed to read expanded title");
return false;
}
if (!parcel.ReadString(briefText_)) {
ANS_LOGE("Failed to read brief text");
return false;
}
auto valid = parcel.ReadBool();
if (valid) {
bigPicture_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
if (!bigPicture_) {
ANS_LOGE("Failed to read bigPicture");
return false;
}
}
return true;
}
} // namespace Notification
} // namespace OHOS