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 19 namespace OHOS { 20 namespace Notification { ~NotificationBasicContent()21NotificationBasicContent::~NotificationBasicContent() 22 {} 23 SetAdditionalText(const std::string & additionalText)24void NotificationBasicContent::SetAdditionalText(const std::string &additionalText) 25 { 26 additionalText_ = additionalText; 27 } 28 GetAdditionalText() const29std::string NotificationBasicContent::GetAdditionalText() const 30 { 31 return additionalText_; 32 } 33 SetText(const std::string & text)34void NotificationBasicContent::SetText(const std::string &text) 35 { 36 text_ = text; 37 } 38 GetText() const39std::string NotificationBasicContent::GetText() const 40 { 41 return text_; 42 } 43 SetTitle(const std::string & title)44void NotificationBasicContent::SetTitle(const std::string &title) 45 { 46 title_ = title; 47 } 48 GetTitle() const49std::string NotificationBasicContent::GetTitle() const 50 { 51 return title_; 52 } 53 Dump()54std::string NotificationBasicContent::Dump() 55 { 56 return "title = " + title_ + ", text = " + text_ + ", additionalText = " + additionalText_; 57 } 58 ToJson(nlohmann::json & jsonObject) const59bool NotificationBasicContent::ToJson(nlohmann::json &jsonObject) const 60 { 61 jsonObject["text"] = text_; 62 jsonObject["title"] = title_; 63 jsonObject["additionalText"] = additionalText_; 64 65 return true; 66 } 67 ReadFromJson(const nlohmann::json & jsonObject)68void NotificationBasicContent::ReadFromJson(const nlohmann::json &jsonObject) 69 { 70 if (jsonObject.is_null() or !jsonObject.is_object()) { 71 ANS_LOGE("Invalid JSON object"); 72 return; 73 } 74 75 const auto &jsonEnd = jsonObject.cend(); 76 if (jsonObject.find("text") != jsonEnd && jsonObject.at("text").is_string()) { 77 text_ = jsonObject.at("text").get<std::string>(); 78 } 79 80 if (jsonObject.find("title") != jsonEnd && jsonObject.at("title").is_string()) { 81 title_ = jsonObject.at("title").get<std::string>(); 82 } 83 84 if (jsonObject.find("additionalText") != jsonEnd && jsonObject.at("additionalText").is_string()) { 85 additionalText_ = jsonObject.at("additionalText").get<std::string>(); 86 } 87 } 88 Marshalling(Parcel & parcel) const89bool NotificationBasicContent::Marshalling(Parcel &parcel) const 90 { 91 if (!parcel.WriteString(text_)) { 92 ANS_LOGE("Failed to write text"); 93 return false; 94 } 95 96 if (!parcel.WriteString(title_)) { 97 ANS_LOGE("Failed to write title"); 98 return false; 99 } 100 101 if (!parcel.WriteString(additionalText_)) { 102 ANS_LOGE("Failed to write additional text"); 103 return false; 104 } 105 106 return true; 107 } 108 ReadFromParcel(Parcel & parcel)109bool NotificationBasicContent::ReadFromParcel(Parcel &parcel) 110 { 111 if (!parcel.ReadString(text_)) { 112 ANS_LOGE("Failed to read text"); 113 return false; 114 } 115 116 if (!parcel.ReadString(title_)) { 117 ANS_LOGE("Failed to read title"); 118 return false; 119 } 120 121 if (!parcel.ReadString(additionalText_)) { 122 ANS_LOGE("Failed to read additional text"); 123 return false; 124 } 125 126 return true; 127 } 128 } // namespace Notification 129 } // namespace OHOS 130