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 "message_user.h"
17 #include "ans_image_util.h"
18 #include "ans_log_wrapper.h"
19
20 namespace OHOS {
21 namespace Notification {
MessageUser()22 MessageUser::MessageUser() : uri_("")
23 {}
24
~MessageUser()25 MessageUser::~MessageUser()
26 {}
27
SetKey(const std::string & key)28 void MessageUser::SetKey(const std::string &key)
29 {
30 key_ = key;
31 }
32
GetKey() const33 std::string MessageUser::GetKey() const
34 {
35 return key_;
36 }
37
SetName(const std::string & name)38 void MessageUser::SetName(const std::string &name)
39 {
40 name_ = name;
41 }
42
GetName() const43 std::string MessageUser::GetName() const
44 {
45 return name_;
46 }
47
SetPixelMap(const std::shared_ptr<Media::PixelMap> & pixelMap)48 void MessageUser::SetPixelMap(const std::shared_ptr<Media::PixelMap> &pixelMap)
49 {
50 pixelMap_ = pixelMap;
51 }
52
GetPixelMap() const53 const std::shared_ptr<Media::PixelMap> MessageUser::GetPixelMap() const
54 {
55 return pixelMap_;
56 }
57
SetUri(const Uri & uri)58 void MessageUser::SetUri(const Uri &uri)
59 {
60 uri_ = uri;
61 }
62
GetUri() const63 Uri MessageUser::GetUri() const
64 {
65 return uri_;
66 }
67
SetMachine(bool machine)68 void MessageUser::SetMachine(bool machine)
69 {
70 isMachine_ = machine;
71 }
72
IsMachine() const73 bool MessageUser::IsMachine() const
74 {
75 return isMachine_;
76 }
77
SetUserAsImportant(bool userImportant)78 void MessageUser::SetUserAsImportant(bool userImportant)
79 {
80 isUserImportant_ = userImportant;
81 }
82
IsUserImportant() const83 bool MessageUser::IsUserImportant() const
84 {
85 return isUserImportant_;
86 }
87
Dump() const88 std::string MessageUser::Dump() const
89 {
90 return "MessageUser{ "
91 "key = " + key_ +
92 ", name = " + name_ +
93 ", pixelMap = " + (pixelMap_ ? "not null" : "null") +
94 ", uri = " + uri_.ToString() +
95 ", isMachine = " + (isMachine_ ? "true" : "false") +
96 ", isUserImportant = " + (isUserImportant_ ? "true" : "false") +
97 " }";
98 }
99
ToJson(nlohmann::json & jsonObject) const100 bool MessageUser::ToJson(nlohmann::json &jsonObject) const
101 {
102 jsonObject["key"] = key_;
103 jsonObject["name"] = name_;
104 jsonObject["pixelMap"] = AnsImageUtil::PackImage(pixelMap_);
105 jsonObject["uri"] = uri_.ToString();
106 jsonObject["isMachine"] = isMachine_;
107 jsonObject["isUserImportant"] = isUserImportant_;
108
109 return true;
110 }
111
FromJson(const nlohmann::json & jsonObject)112 MessageUser *MessageUser::FromJson(const nlohmann::json &jsonObject)
113 {
114 if (jsonObject.is_null() or !jsonObject.is_object()) {
115 ANS_LOGE("Invalid JSON object");
116 return nullptr;
117 }
118
119 MessageUser *messageUser = new (std::nothrow) MessageUser();
120 if (messageUser == nullptr) {
121 ANS_LOGE("Failed to create messageUse instance");
122 return nullptr;
123 }
124
125 const auto &jsonEnd = jsonObject.cend();
126 if (jsonObject.find("key") != jsonEnd) {
127 messageUser->key_ = jsonObject.at("key").get<std::string>();
128 }
129
130 if (jsonObject.find("name") != jsonEnd) {
131 messageUser->name_ = jsonObject.at("name").get<std::string>();
132 }
133
134 if (jsonObject.find("pixelMap") != jsonEnd) {
135 auto pmStr = jsonObject.at("pixelMap").get<std::string>();
136 messageUser->pixelMap_ = AnsImageUtil::UnPackImage(pmStr);
137 }
138
139 if (jsonObject.find("uri") != jsonEnd) {
140 messageUser->uri_ = Uri(jsonObject.at("uri").get<std::string>());
141 }
142
143 if (jsonObject.find("isMachine") != jsonEnd) {
144 messageUser->isMachine_ = jsonObject.at("isMachine").get<bool>();
145 }
146
147 if (jsonObject.find("isUserImportant") != jsonEnd) {
148 messageUser->isUserImportant_ = jsonObject.at("isUserImportant").get<bool>();
149 }
150
151 return messageUser;
152 }
153
Marshalling(Parcel & parcel) const154 bool MessageUser::Marshalling(Parcel &parcel) const
155 {
156 if (!parcel.WriteString(key_)) {
157 ANS_LOGE("Failed to write key");
158 return false;
159 }
160
161 if (!parcel.WriteString(name_)) {
162 ANS_LOGE("Failed to write name");
163 return false;
164 }
165
166 if (!parcel.WriteBool(isMachine_)) {
167 ANS_LOGE("Failed to write isMachine");
168 return false;
169 }
170
171 if (!parcel.WriteBool(isUserImportant_)) {
172 ANS_LOGE("Failed to write isUserImportant");
173 return false;
174 }
175
176 if (uri_.ToString().empty()) {
177 if (!parcel.WriteInt32(VALUE_NULL)) {
178 ANS_LOGE("Failed to write VALUE_NULL");
179 return false;
180 }
181 } else {
182 if (!parcel.WriteInt32(VALUE_OBJECT)) {
183 ANS_LOGE("Failed to write VALUE_OBJECT");
184 return false;
185 }
186 if (!parcel.WriteString((uri_.ToString()))) {
187 ANS_LOGE("Failed to write uri");
188 return false;
189 }
190 }
191
192 bool valid = pixelMap_ ? true : false;
193 if (!parcel.WriteBool(valid)) {
194 ANS_LOGE("Failed to write the flag which indicate whether pixelMap is null");
195 return false;
196 }
197
198 if (valid) {
199 if (!parcel.WriteParcelable(pixelMap_.get())) {
200 ANS_LOGE("Failed to write pixelMap");
201 return false;
202 }
203 }
204
205 return true;
206 }
207
ReadFromParcel(Parcel & parcel)208 bool MessageUser::ReadFromParcel(Parcel &parcel)
209 {
210 key_ = parcel.ReadString();
211 name_ = parcel.ReadString();
212 isMachine_ = parcel.ReadBool();
213 isUserImportant_ = parcel.ReadBool();
214
215 int empty = VALUE_NULL;
216 if (!parcel.ReadInt32(empty)) {
217 ANS_LOGE("Failed to read VALUE");
218 return false;
219 }
220
221 if (empty == VALUE_OBJECT) {
222 uri_ = Uri((parcel.ReadString()));
223 }
224
225 bool valid = parcel.ReadBool();
226 if (valid) {
227 pixelMap_ = std::shared_ptr<Media::PixelMap>(parcel.ReadParcelable<Media::PixelMap>());
228 if (!pixelMap_) {
229 ANS_LOGE("Failed to read pixelMap");
230 return false;
231 }
232 }
233
234 return true;
235 }
236
Unmarshalling(Parcel & parcel)237 MessageUser *MessageUser::Unmarshalling(Parcel &parcel)
238 {
239 MessageUser *messageUser = new (std::nothrow) MessageUser();
240
241 if (messageUser && !messageUser->ReadFromParcel(parcel)) {
242 delete messageUser;
243 messageUser = nullptr;
244 }
245
246 return messageUser;
247 }
248 } // namespace Notification
249 } // namespace OHOS