1 /*
2 * Copyright (c) 2021-2022 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_user_input.h"
17
18 #include "ans_const_define.h"
19 #include "ans_log_wrapper.h"
20 #include "want_params_wrapper.h"
21
22 namespace OHOS {
23 namespace Notification {
SetInputsSource(AAFwk::Want & want,NotificationConstant::InputsSource source)24 void NotificationUserInput::SetInputsSource(AAFwk::Want &want, NotificationConstant::InputsSource source)
25 {
26 want.SetParam(NotificationConstant::EXTRA_INPUTS_SOURCE, static_cast<int32_t>(source));
27 }
28
GetInputsSource(const AAFwk::Want & want)29 NotificationConstant::InputsSource NotificationUserInput::GetInputsSource(const AAFwk::Want &want)
30 {
31 auto inputsSource = want.GetIntParam(NotificationConstant::EXTRA_INPUTS_SOURCE,
32 static_cast<int32_t>(NotificationConstant::InputsSource::FREE_FORM_INPUT));
33 return static_cast<NotificationConstant::InputsSource>(inputsSource);
34 }
35
AddInputsToWant(const std::vector<std::shared_ptr<NotificationUserInput>> & userInputs,AAFwk::Want & want,const AAFwk::WantParams & additional)36 void NotificationUserInput::AddInputsToWant(const std::vector<std::shared_ptr<NotificationUserInput>> &userInputs,
37 AAFwk::Want &want, const AAFwk::WantParams &additional)
38 {}
39
GetInputsFromWant(const AAFwk::Want & want)40 std::shared_ptr<AAFwk::WantParams> NotificationUserInput::GetInputsFromWant(const AAFwk::Want &want)
41 {
42 return {};
43 }
44
AddMimeInputToWant(const NotificationUserInput & userInput,AAFwk::Want & want,const std::map<std::string,std::shared_ptr<Uri>> & results)45 void NotificationUserInput::AddMimeInputToWant(const NotificationUserInput &userInput, AAFwk::Want &want,
46 const std::map<std::string, std::shared_ptr<Uri>> &results)
47 {}
48
GetMimeInputsFromWant(const AAFwk::Want & want,const std::string & inputKey)49 std::map<std::string, std::shared_ptr<Uri>> NotificationUserInput::GetMimeInputsFromWant(
50 const AAFwk::Want &want, const std::string &inputKey)
51 {
52 return {};
53 }
54
Create(const std::string & inputKey)55 std::shared_ptr<NotificationUserInput> NotificationUserInput::Create(const std::string &inputKey)
56 {
57 if (inputKey.empty()) {
58 ANS_LOGE("The param of inputKey is empty");
59 return {};
60 }
61
62 auto pUserInput = new (std::nothrow) NotificationUserInput(inputKey);
63 if (pUserInput == nullptr) {
64 ANS_LOGE("create NotificationUserInput object failed");
65 return {};
66 }
67
68 return std::shared_ptr<NotificationUserInput>(pUserInput);
69 }
70
Create(const std::string & inputKey,const std::string & tag,const std::vector<std::string> & options,bool permitFreeFormInput,const std::set<std::string> & permitMimeTypes,const std::shared_ptr<AAFwk::WantParams> & additional,NotificationConstant::InputEditType editType)71 std::shared_ptr<NotificationUserInput> NotificationUserInput::Create(const std::string &inputKey,
72 const std::string &tag, const std::vector<std::string> &options, bool permitFreeFormInput,
73 const std::set<std::string> &permitMimeTypes, const std::shared_ptr<AAFwk::WantParams> &additional,
74 NotificationConstant::InputEditType editType)
75 {
76 if (inputKey.empty()) {
77 ANS_LOGE("The param of inputKey is empty");
78 return {};
79 }
80
81 if (!permitFreeFormInput) {
82 if (editType == NotificationConstant::InputEditType::EDIT_ENABLED) {
83 ANS_LOGE("Setting editType to enable requires permitFreeFormInput to be set to true");
84 return {};
85 }
86
87 if (options.empty() && permitMimeTypes.empty()) {
88 ANS_LOGE("options and permitMimeTypes cannot be empty at the same time");
89 return {};
90 }
91 }
92
93 auto realAdditional = additional;
94 if (!realAdditional) {
95 realAdditional = std::make_shared<AAFwk::WantParams>();
96 }
97
98 auto pUserInput = new (std::nothrow)
99 NotificationUserInput(inputKey, tag, options, permitFreeFormInput, permitMimeTypes, realAdditional, editType);
100 if (pUserInput == nullptr) {
101 ANS_LOGE("create NotificationUserInput object failed");
102 return {};
103 }
104
105 return std::shared_ptr<NotificationUserInput>(pUserInput);
106 }
107
NotificationUserInput(const std::string & inputKey)108 NotificationUserInput::NotificationUserInput(const std::string &inputKey)
109 : inputKey_(inputKey), additionalData_(std::make_shared<AAFwk::WantParams>())
110 {}
111
NotificationUserInput(const std::string & inputKey,const std::string & tag,const std::vector<std::string> & options,bool permitFreeFormInput,const std::set<std::string> & permitMimeTypes,const std::shared_ptr<AAFwk::WantParams> & additional,NotificationConstant::InputEditType editType)112 NotificationUserInput::NotificationUserInput(const std::string &inputKey, const std::string &tag,
113 const std::vector<std::string> &options, bool permitFreeFormInput, const std::set<std::string> &permitMimeTypes,
114 const std::shared_ptr<AAFwk::WantParams> &additional, NotificationConstant::InputEditType editType)
115 : inputKey_(inputKey),
116 tag_(tag),
117 options_(options),
118 permitFreeFormInput_(permitFreeFormInput),
119 permitMimeTypes_(permitMimeTypes),
120 additionalData_(additional),
121 editType_(editType)
122 {}
123
GetInputKey() const124 std::string NotificationUserInput::GetInputKey() const
125 {
126 return inputKey_;
127 }
128
AddAdditionalData(AAFwk::WantParams & additional)129 void NotificationUserInput::AddAdditionalData(AAFwk::WantParams &additional)
130 {
131 if (additionalData_) {
132 *additionalData_ = additional;
133 }
134 }
135
GetAdditionalData() const136 const std::shared_ptr<AAFwk::WantParams> NotificationUserInput::GetAdditionalData() const
137 {
138 return additionalData_;
139 }
140
SetEditType(NotificationConstant::InputEditType inputEditType)141 void NotificationUserInput::SetEditType(NotificationConstant::InputEditType inputEditType)
142 {
143 editType_ = inputEditType;
144 }
145
GetEditType() const146 NotificationConstant::InputEditType NotificationUserInput::GetEditType() const
147 {
148 return editType_;
149 }
150
SetOptions(const std::vector<std::string> & options)151 void NotificationUserInput::SetOptions(const std::vector<std::string> &options)
152 {
153 options_ = options;
154 }
155
GetOptions() const156 std::vector<std::string> NotificationUserInput::GetOptions() const
157 {
158 return options_;
159 }
160
SetPermitMimeTypes(const std::string & mimeType,bool doPermit)161 void NotificationUserInput::SetPermitMimeTypes(const std::string &mimeType, bool doPermit)
162 {
163 if (mimeType.empty()) {
164 ANS_LOGE("The mimeType is invalid.");
165 return;
166 }
167
168 if (doPermit) {
169 permitMimeTypes_.emplace(mimeType);
170 return;
171 }
172
173 permitMimeTypes_.erase(mimeType);
174 }
175
GetPermitMimeTypes() const176 std::set<std::string> NotificationUserInput::GetPermitMimeTypes() const
177 {
178 return permitMimeTypes_;
179 }
180
IsMimeTypeOnly() const181 bool NotificationUserInput::IsMimeTypeOnly() const
182 {
183 return !permitFreeFormInput_ && options_.empty() && !permitMimeTypes_.empty();
184 }
185
SetTag(const std::string tag)186 void NotificationUserInput::SetTag(const std::string tag)
187 {
188 tag_ = tag;
189 }
190
GetTag() const191 std::string NotificationUserInput::GetTag() const
192 {
193 return tag_;
194 }
195
SetPermitFreeFormInput(bool permitFreeFormInput)196 void NotificationUserInput::SetPermitFreeFormInput(bool permitFreeFormInput)
197 {
198 permitFreeFormInput_ = permitFreeFormInput;
199 }
200
IsPermitFreeFormInput() const201 bool NotificationUserInput::IsPermitFreeFormInput() const
202 {
203 return permitFreeFormInput_;
204 }
205
Dump()206 std::string NotificationUserInput::Dump()
207 {
208 std::string options;
209 for (std::string option : options_) {
210 options += option + ", ";
211 }
212 if (!options.empty()) {
213 options.pop_back();
214 }
215 if (!options.empty()) {
216 options.pop_back();
217 }
218
219 std::string permitMimeTypes;
220 for (auto permitMimeType : permitMimeTypes_) {
221 permitMimeTypes += permitMimeType + ", ";
222 }
223 if (!permitMimeTypes.empty()) {
224 permitMimeTypes.pop_back();
225 }
226 if (!permitMimeTypes.empty()) {
227 permitMimeTypes.pop_back();
228 }
229
230 return "NotificationUserInput{ "
231 "inputKey = " + inputKey_ +
232 ", tag = " + tag_ +
233 ", options = [" + options + "]" +
234 ", permitFreeFormInput = " + (permitFreeFormInput_ ? "true" : "false") +
235 ", permitMimeTypes = [" + permitMimeTypes + "]" +
236 ", editType = " + std::to_string(static_cast<int32_t>(editType_)) +
237 " }";
238 }
239
ToJson(nlohmann::json & jsonObject) const240 bool NotificationUserInput::ToJson(nlohmann::json &jsonObject) const
241 {
242 jsonObject["inputKey"] = inputKey_;
243 jsonObject["tag"] = tag_;
244 jsonObject["options"] = nlohmann::json(options_);
245 jsonObject["permitFreeFormInput"] = permitFreeFormInput_;
246 jsonObject["permitMimeTypes"] = nlohmann::json(permitMimeTypes_);
247 jsonObject["editType"] = static_cast<int32_t>(editType_);
248 std::string additionalDataStr;
249 if (additionalData_) {
250 AAFwk::WantParamWrapper wWrapper(*additionalData_);
251 additionalDataStr = wWrapper.ToString();
252 }
253 jsonObject["additionalData"] = additionalDataStr;
254
255 return true;
256 }
257
FromJson(const nlohmann::json & jsonObject)258 NotificationUserInput *NotificationUserInput::FromJson(const nlohmann::json &jsonObject)
259 {
260 if (jsonObject.is_null() || !jsonObject.is_object()) {
261 ANS_LOGE("Invalid JSON object");
262 return nullptr;
263 }
264
265 auto pUserInput = new (std::nothrow) NotificationUserInput();
266 if (pUserInput == nullptr) {
267 ANS_LOGE("Failed to create userInput instance");
268 return nullptr;
269 }
270
271 const auto &jsonEnd = jsonObject.cend();
272 if (jsonObject.find("inputKey") != jsonEnd && jsonObject.at("inputKey").is_string()) {
273 pUserInput->inputKey_ = jsonObject.at("inputKey").get<std::string>();
274 }
275
276 if (jsonObject.find("tag") != jsonEnd && jsonObject.at("tag").is_string()) {
277 pUserInput->tag_ = jsonObject.at("tag").get<std::string>();
278 }
279
280 if (jsonObject.find("options") != jsonEnd && jsonObject.at("options").is_array()) {
281 pUserInput->options_ = jsonObject.at("options").get<std::vector<std::string>>();
282 }
283
284 if (jsonObject.find("permitFreeFormInput") != jsonEnd && jsonObject.at("permitFreeFormInput").is_boolean()) {
285 pUserInput->permitFreeFormInput_ = jsonObject.at("permitFreeFormInput").get<bool>();
286 }
287
288 if (jsonObject.find("permitMimeTypes") != jsonEnd && jsonObject.at("permitMimeTypes").is_array()) {
289 pUserInput->permitMimeTypes_ = jsonObject.at("permitMimeTypes").get<std::set<std::string>>();
290 }
291
292 if (jsonObject.find("additionalData") != jsonEnd && jsonObject.at("additionalData").is_string()) {
293 auto additionalDataString = jsonObject.at("additionalData").get<std::string>();
294 if (!additionalDataString.empty()) {
295 AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParams(additionalDataString);
296 pUserInput->additionalData_ = std::make_shared<AAFwk::WantParams>(params);
297 }
298 }
299
300 if (jsonObject.find("editType") != jsonEnd && jsonObject.at("editType").is_number_integer()) {
301 auto editTypeValue = jsonObject.at("editType").get<int32_t>();
302 pUserInput->editType_ = static_cast<NotificationConstant::InputEditType>(editTypeValue);
303 }
304
305 return pUserInput;
306 }
307
Marshalling(Parcel & parcel) const308 bool NotificationUserInput::Marshalling(Parcel &parcel) const
309 {
310 if (!parcel.WriteString(inputKey_)) {
311 ANS_LOGE("Failed to write inputKey");
312 return false;
313 }
314
315 if (!parcel.WriteString(tag_)) {
316 ANS_LOGE("Failed to write tag");
317 return false;
318 }
319
320 if (!parcel.WriteBool(permitFreeFormInput_)) {
321 ANS_LOGE("Failed to write flag permitFreeFormInput");
322 return false;
323 }
324
325 if (!parcel.WriteInt32(static_cast<int32_t>(editType_))) {
326 ANS_LOGE("Failed to write editType");
327 return false;
328 }
329
330 auto valid = additionalData_ ? true : false;
331 if (!parcel.WriteBool(valid)) {
332 ANS_LOGE("Failed to write the flag which indicate whether additionalData is null");
333 return false;
334 }
335
336 if (valid) {
337 if (!parcel.WriteParcelable(additionalData_.get())) {
338 ANS_LOGE("Failed to write additionalData");
339 return false;
340 }
341 }
342
343 if (!parcel.WriteStringVector(options_)) {
344 ANS_LOGE("Failed to write options");
345 return false;
346 }
347
348 if (!parcel.WriteUint64(static_cast<uint64_t>(permitMimeTypes_.size()))) {
349 ANS_LOGE("Failed to write the size of permitMimeTypes");
350 return false;
351 }
352
353 for (auto it = permitMimeTypes_.begin(); it != permitMimeTypes_.end(); ++it) {
354 if (!parcel.WriteString(*it)) {
355 ANS_LOGE("Failed to write permitMimeTypes");
356 return false;
357 }
358 }
359
360 return true;
361 }
362
Unmarshalling(Parcel & parcel)363 NotificationUserInput *NotificationUserInput::Unmarshalling(Parcel &parcel)
364 {
365 auto pUserInput = new (std::nothrow) NotificationUserInput();
366 if ((pUserInput != nullptr) && !pUserInput->ReadFromParcel(parcel)) {
367 delete pUserInput;
368 pUserInput = nullptr;
369 }
370
371 return pUserInput;
372 }
373
ReadFromParcel(Parcel & parcel)374 bool NotificationUserInput::ReadFromParcel(Parcel &parcel)
375 {
376 if (!parcel.ReadString(inputKey_)) {
377 ANS_LOGE("Failed to read inputKey");
378 return false;
379 }
380
381 if (!parcel.ReadString(tag_)) {
382 ANS_LOGE("Failed to read tag");
383 return false;
384 }
385
386 permitFreeFormInput_ = parcel.ReadBool();
387
388 editType_ = static_cast<NotificationConstant::InputEditType>(parcel.ReadInt32());
389
390 auto valid = parcel.ReadBool();
391 if (valid) {
392 additionalData_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
393 if (!additionalData_) {
394 ANS_LOGE("Failed to read additionalData");
395 return false;
396 }
397 }
398
399 if (!parcel.ReadStringVector(&options_)) {
400 ANS_LOGE("Failed to read options");
401 return false;
402 }
403
404 auto ssize = parcel.ReadUint64();
405 ssize = (ssize < MAX_PERMIT_MIME_TYPE_NUM) ? ssize : MAX_PERMIT_MIME_TYPE_NUM;
406 for (uint64_t it = 0; it < ssize; ++it) {
407 std::string member {};
408 if (!parcel.ReadString(member)) {
409 ANS_LOGE("Failed to read permitMimeTypes");
410 return false;
411 }
412
413 permitMimeTypes_.emplace(member);
414 }
415
416 return true;
417 }
418 } // namespace Notification
419 } // namespace OHOS
420