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_basic_content.h" 17 #include "ans_log_wrapper.h" 18 #include "ans_image_util.h" 19 20 namespace OHOS { 21 namespace Notification { ~NotificationBasicContent()22NotificationBasicContent::~NotificationBasicContent() 23 {} 24 SetAdditionalText(const std::string & additionalText)25void NotificationBasicContent::SetAdditionalText(const std::string &additionalText) 26 { 27 additionalText_ = additionalText; 28 } 29 GetAdditionalText() const30std::string NotificationBasicContent::GetAdditionalText() const 31 { 32 return additionalText_; 33 } 34 SetText(const std::string & text)35void NotificationBasicContent::SetText(const std::string &text) 36 { 37 text_ = text; 38 } 39 GetText() const40std::string NotificationBasicContent::GetText() const 41 { 42 return text_; 43 } 44 SetTitle(const std::string & title)45void NotificationBasicContent::SetTitle(const std::string &title) 46 { 47 title_ = title; 48 } 49 GetTitle() const50std::string NotificationBasicContent::GetTitle() const 51 { 52 return title_; 53 } 54 SetLockScreenPicture(const std::shared_ptr<Media::PixelMap> & lockScreenPicture)55void NotificationBasicContent::SetLockScreenPicture(const std::shared_ptr<Media::PixelMap> &lockScreenPicture) 56 { 57 lockScreenPicture_ = lockScreenPicture; 58 } 59 GetLockScreenPicture() const60std::shared_ptr<Media::PixelMap> NotificationBasicContent::GetLockScreenPicture() const 61 { 62 return lockScreenPicture_; 63 } 64 Dump()65std::string NotificationBasicContent::Dump() 66 { 67 return "title = " + title_ + ", text = " + text_ + ", additionalText = " + additionalText_ + 68 ", lockScreenPicture = " + (lockScreenPicture_ ? "not null" : "null"); 69 } 70 ToJson(nlohmann::json & jsonObject) const71bool NotificationBasicContent::ToJson(nlohmann::json &jsonObject) const 72 { 73 jsonObject["text"] = text_; 74 jsonObject["title"] = title_; 75 jsonObject["additionalText"] = additionalText_; 76 jsonObject["lockscreenPicture"] = AnsImageUtil::PackImage(lockScreenPicture_); 77 78 return true; 79 } 80 ReadFromJson(const nlohmann::json & jsonObject)81void NotificationBasicContent::ReadFromJson(const nlohmann::json &jsonObject) 82 { 83 if (jsonObject.is_null() or !jsonObject.is_object()) { 84 ANS_LOGE("Invalid JSON object"); 85 return; 86 } 87 88 const auto &jsonEnd = jsonObject.cend(); 89 if (jsonObject.find("text") != jsonEnd && jsonObject.at("text").is_string()) { 90 text_ = jsonObject.at("text").get<std::string>(); 91 } 92 93 if (jsonObject.find("title") != jsonEnd && jsonObject.at("title").is_string()) { 94 title_ = jsonObject.at("title").get<std::string>(); 95 } 96 97 if (jsonObject.find("additionalText") != jsonEnd && jsonObject.at("additionalText").is_string()) { 98 additionalText_ = jsonObject.at("additionalText").get<std::string>(); 99 } 100 101 if (jsonObject.find("lockscreenPicture") != jsonEnd && jsonObject.at("lockscreenPicture").is_string()) { 102 auto lockScreenPictureStr = jsonObject.at("lockscreenPicture").get<std::string>(); 103 lockScreenPicture_ = AnsImageUtil::UnPackImage(lockScreenPictureStr); 104 } 105 } 106 Marshalling(Parcel & parcel) const107bool NotificationBasicContent::Marshalling(Parcel &parcel) const 108 { 109 if (!parcel.WriteString(text_)) { 110 ANS_LOGE("Failed to write text"); 111 return false; 112 } 113 114 if (!parcel.WriteString(title_)) { 115 ANS_LOGE("Failed to write title"); 116 return false; 117 } 118 119 if (!parcel.WriteString(additionalText_)) { 120 ANS_LOGE("Failed to write additional text"); 121 return false; 122 } 123 124 auto valid = lockScreenPicture_ ? true : false; 125 if (!parcel.WriteBool(valid)) { 126 ANS_LOGE("Failed to write the flag which indicate whether lockScreenPicture is null"); 127 return false; 128 } 129 130 if (valid) { 131 if (!parcel.WriteParcelable(lockScreenPicture_.get())) { 132 ANS_LOGE("Failed to write lockScreenPicture"); 133 return false; 134 } 135 } 136 137 return true; 138 } 139 ReadFromParcel(Parcel & parcel)140bool NotificationBasicContent::ReadFromParcel(Parcel &parcel) 141 { 142 if (!parcel.ReadString(text_)) { 143 ANS_LOGE("Failed to read text"); 144 return false; 145 } 146 147 if (!parcel.ReadString(title_)) { 148 ANS_LOGE("Failed to read title"); 149 return false; 150 } 151 152 if (!parcel.ReadString(additionalText_)) { 153 ANS_LOGE("Failed to read additional text"); 154 return false; 155 } 156 157 auto valid = parcel.ReadBool(); 158 if (valid) { 159 lockScreenPicture_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>()); 160 if (!lockScreenPicture_) { 161 ANS_LOGE("Failed to read lockScreenPicture"); 162 return false; 163 } 164 } 165 166 return true; 167 } 168 } // namespace Notification 169 } // namespace OHOS 170