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