• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     options.pop_back();
213     options.pop_back();
214 
215     std::string permitMimeTypes;
216     for (auto permitMimeType : permitMimeTypes_) {
217         permitMimeTypes += permitMimeType + ", ";
218     }
219     permitMimeTypes.pop_back();
220     permitMimeTypes.pop_back();
221 
222     return "NotificationUserInput{ "
223             "inputKey = " + inputKey_ +
224             ", tag = " + tag_ +
225             ", options = [" + options + "]" +
226             ", permitFreeFormInput = " + (permitFreeFormInput_ ? "true" : "false") +
227             ", permitMimeTypes = [" + permitMimeTypes + "]" +
228             ", editType = " + std::to_string(static_cast<int32_t>(editType_)) +
229             " }";
230 }
231 
ToJson(nlohmann::json & jsonObject) const232 bool NotificationUserInput::ToJson(nlohmann::json &jsonObject) const
233 {
234     jsonObject["inputKey"]            = inputKey_;
235     jsonObject["tag"]                 = tag_;
236     jsonObject["options"]             = nlohmann::json(options_);
237     jsonObject["permitFreeFormInput"] = permitFreeFormInput_;
238     jsonObject["permitMimeTypes"]     = nlohmann::json(permitMimeTypes_);
239     jsonObject["editType"]            = static_cast<int32_t>(editType_);
240     std::string additionalDataStr;
241     if (additionalData_) {
242         AAFwk::WantParamWrapper wWrapper(*additionalData_);
243         additionalDataStr = wWrapper.ToString();
244     }
245     jsonObject["additionalData"] = additionalDataStr;
246 
247     return true;
248 }
249 
FromJson(const nlohmann::json & jsonObject)250 NotificationUserInput *NotificationUserInput::FromJson(const nlohmann::json &jsonObject)
251 {
252     if (jsonObject.is_null() || !jsonObject.is_object()) {
253         ANS_LOGE("Invalid JSON object");
254         return nullptr;
255     }
256 
257     auto pUserInput = new (std::nothrow) NotificationUserInput();
258     if (pUserInput == nullptr) {
259         ANS_LOGE("Failed to create userInput instance");
260         return nullptr;
261     }
262 
263     const auto &jsonEnd = jsonObject.cend();
264     if (jsonObject.find("inputKey") != jsonEnd && jsonObject.at("inputKey").is_string()) {
265         pUserInput->inputKey_ = jsonObject.at("inputKey").get<std::string>();
266     }
267 
268     if (jsonObject.find("tag") != jsonEnd && jsonObject.at("tag").is_string()) {
269         pUserInput->tag_ = jsonObject.at("tag").get<std::string>();
270     }
271 
272     if (jsonObject.find("options") != jsonEnd && jsonObject.at("options").is_array()) {
273         pUserInput->options_ = jsonObject.at("options").get<std::vector<std::string>>();
274     }
275 
276     if (jsonObject.find("permitFreeFormInput") != jsonEnd && jsonObject.at("permitFreeFormInput").is_boolean()) {
277         pUserInput->permitFreeFormInput_ = jsonObject.at("permitFreeFormInput").get<bool>();
278     }
279 
280     if (jsonObject.find("permitMimeTypes") != jsonEnd && jsonObject.at("permitMimeTypes").is_array()) {
281         pUserInput->permitMimeTypes_ = jsonObject.at("permitMimeTypes").get<std::set<std::string>>();
282     }
283 
284     if (jsonObject.find("additionalData") != jsonEnd && jsonObject.at("additionalData").is_string()) {
285         auto additionalDataString = jsonObject.at("additionalData").get<std::string>();
286         if (!additionalDataString.empty()) {
287             AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParams(additionalDataString);
288             pUserInput->additionalData_ = std::make_shared<AAFwk::WantParams>(params);
289         }
290     }
291 
292     if (jsonObject.find("editType") != jsonEnd && jsonObject.at("editType").is_number_integer()) {
293         auto editTypeValue    = jsonObject.at("editType").get<int32_t>();
294         pUserInput->editType_ = static_cast<NotificationConstant::InputEditType>(editTypeValue);
295     }
296 
297     return pUserInput;
298 }
299 
Marshalling(Parcel & parcel) const300 bool NotificationUserInput::Marshalling(Parcel &parcel) const
301 {
302     if (!parcel.WriteString(inputKey_)) {
303         ANS_LOGE("Failed to write inputKey");
304         return false;
305     }
306 
307     if (!parcel.WriteString(tag_)) {
308         ANS_LOGE("Failed to write tag");
309         return false;
310     }
311 
312     if (!parcel.WriteBool(permitFreeFormInput_)) {
313         ANS_LOGE("Failed to write flag permitFreeFormInput");
314         return false;
315     }
316 
317     if (!parcel.WriteInt32(static_cast<int32_t>(editType_))) {
318         ANS_LOGE("Failed to write editType");
319         return false;
320     }
321 
322     auto valid = additionalData_ ? true : false;
323     if (!parcel.WriteBool(valid)) {
324         ANS_LOGE("Failed to write the flag which indicate whether additionalData is null");
325         return false;
326     }
327 
328     if (valid) {
329         if (!parcel.WriteParcelable(additionalData_.get())) {
330             ANS_LOGE("Failed to write additionalData");
331             return false;
332         }
333     }
334 
335     if (!parcel.WriteStringVector(options_)) {
336         ANS_LOGE("Failed to write options");
337         return false;
338     }
339 
340     if (!parcel.WriteUint64(static_cast<uint64_t>(permitMimeTypes_.size()))) {
341         ANS_LOGE("Failed to write the size of permitMimeTypes");
342         return false;
343     }
344 
345     for (auto it = permitMimeTypes_.begin(); it != permitMimeTypes_.end(); ++it) {
346         if (!parcel.WriteString(*it)) {
347             ANS_LOGE("Failed to write permitMimeTypes");
348             return false;
349         }
350     }
351 
352     return true;
353 }
354 
Unmarshalling(Parcel & parcel)355 NotificationUserInput *NotificationUserInput::Unmarshalling(Parcel &parcel)
356 {
357     auto pUserInput = new (std::nothrow) NotificationUserInput();
358     if ((pUserInput != nullptr) && !pUserInput->ReadFromParcel(parcel)) {
359         delete pUserInput;
360         pUserInput = nullptr;
361     }
362 
363     return pUserInput;
364 }
365 
ReadFromParcel(Parcel & parcel)366 bool NotificationUserInput::ReadFromParcel(Parcel &parcel)
367 {
368     if (!parcel.ReadString(inputKey_)) {
369         ANS_LOGE("Failed to read inputKey");
370         return false;
371     }
372 
373     if (!parcel.ReadString(tag_)) {
374         ANS_LOGE("Failed to read tag");
375         return false;
376     }
377 
378     permitFreeFormInput_ = parcel.ReadBool();
379 
380     editType_ = static_cast<NotificationConstant::InputEditType>(parcel.ReadInt32());
381 
382     auto valid = parcel.ReadBool();
383     if (valid) {
384         additionalData_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
385         if (!additionalData_) {
386             ANS_LOGE("Failed to read additionalData");
387             return false;
388         }
389     }
390 
391     if (!parcel.ReadStringVector(&options_)) {
392         ANS_LOGE("Failed to read options");
393         return false;
394     }
395 
396     auto ssize = parcel.ReadUint64();
397     ssize = (ssize < MAX_PERMIT_MIME_TYPE_NUM) ? ssize : MAX_PERMIT_MIME_TYPE_NUM;
398     for (uint64_t it = 0; it < ssize; ++it) {
399         std::string member {};
400         if (!parcel.ReadString(member)) {
401             ANS_LOGE("Failed to read permitMimeTypes");
402             return false;
403         }
404 
405         permitMimeTypes_.emplace(member);
406     }
407 
408     return true;
409 }
410 }  // namespace Notification
411 }  // namespace OHOS