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_media_content.h"
17
18 #include "ans_log_wrapper.h"
19
20 namespace OHOS {
21 namespace Notification {
SetAVToken(const std::shared_ptr<AVToken> & avToken)22 void NotificationMediaContent::SetAVToken(const std::shared_ptr<AVToken> &avToken)
23 {
24 avToken_ = avToken;
25 }
26
GetAVToken() const27 const std::shared_ptr<AVToken> NotificationMediaContent::GetAVToken() const
28 {
29 return avToken_;
30 }
31
SetShownActions(const std::vector<uint32_t> & actions)32 void NotificationMediaContent::SetShownActions(const std::vector<uint32_t> &actions)
33 {
34 sequenceNumbers_ = actions;
35 }
36
GetShownActions() const37 std::vector<uint32_t> NotificationMediaContent::GetShownActions() const
38 {
39 return sequenceNumbers_;
40 }
41
Dump()42 std::string NotificationMediaContent::Dump()
43 {
44 std::string numbers {};
45 std::for_each(sequenceNumbers_.begin(), sequenceNumbers_.end(), [&numbers](int32_t num) {
46 numbers += std::to_string(num) + ", ";
47 });
48
49 return "NotificationMediaContent{ " + NotificationBasicContent::Dump() +
50 ", avToken = " + (avToken_ ? "not null" : "null") +
51 ", sequenceNumbers = " + numbers +
52 " }";
53 }
54
ToJson(nlohmann::json & jsonObject) const55 bool NotificationMediaContent::ToJson(nlohmann::json &jsonObject) const
56 {
57 if (!NotificationBasicContent::ToJson(jsonObject)) {
58 ANS_LOGE("Cannot convert basicContent to JSON");
59 return false;
60 }
61
62 jsonObject["sequenceNumbers"] = nlohmann::json(sequenceNumbers_);
63
64 return true;
65 }
66
FromJson(const nlohmann::json & jsonObject)67 NotificationMediaContent *NotificationMediaContent::FromJson(const nlohmann::json &jsonObject)
68 {
69 if (jsonObject.is_null() or !jsonObject.is_object()) {
70 ANS_LOGE("Invalid JSON object");
71 return nullptr;
72 }
73
74 auto pContent = new (std::nothrow) NotificationMediaContent();
75 if (pContent == nullptr) {
76 ANS_LOGE("Failed to create mediaContent instance");
77 return nullptr;
78 }
79
80 pContent->ReadFromJson(jsonObject);
81
82 const auto& jsonEnd = jsonObject.cend();
83 if (jsonObject.find("sequenceNumbers") != jsonEnd && jsonObject.at("sequenceNumbers").is_array()) {
84 pContent->sequenceNumbers_ = jsonObject.at("sequenceNumbers").get<std::vector<uint32_t>>();
85 }
86
87 return pContent;
88 }
89
Marshalling(Parcel & parcel) const90 bool NotificationMediaContent::Marshalling(Parcel &parcel) const
91 {
92 if (!NotificationBasicContent::Marshalling(parcel)) {
93 ANS_LOGE("Failed to write basic");
94 return false;
95 }
96
97 if (!parcel.WriteUInt32Vector(sequenceNumbers_)) {
98 ANS_LOGE("Failed to write sequence numbers");
99 return false;
100 }
101
102 return true;
103 }
104
Unmarshalling(Parcel & parcel)105 NotificationMediaContent *NotificationMediaContent::Unmarshalling(Parcel &parcel)
106 {
107 auto pContent = new (std::nothrow) NotificationMediaContent();
108 if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) {
109 delete pContent;
110 pContent = nullptr;
111 }
112
113 return pContent;
114 }
115
ReadFromParcel(Parcel & parcel)116 bool NotificationMediaContent::ReadFromParcel(Parcel &parcel)
117 {
118 if (!NotificationBasicContent::ReadFromParcel(parcel)) {
119 ANS_LOGE("Failed to read basic");
120 return false;
121 }
122
123 if (!parcel.ReadUInt32Vector(&sequenceNumbers_)) {
124 ANS_LOGE("Failed to read sequence numbers");
125 return false;
126 }
127
128 return true;
129 }
130 } // namespace Notification
131 } // namespace OHOS
132