• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_USER_INPUT_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_USER_INPUT_H
18 
19 #include "notification_constant.h"
20 #include "notification_json_convert.h"
21 #include "parcel.h"
22 #include "uri.h"
23 #include "want.h"
24 
25 namespace OHOS {
26 namespace Notification {
27 class NotificationUserInput : public Parcelable, public NotificationJsonConvertionBase {
28 public:
29     /**
30      * @brief Sets the input source of this NotificationUserInput object.
31      *
32      * @param want Indicates the Want to which the input result is to be added.
33      * @param source Indicates the input source. For available values, see NotificationConstant::InputSource.
34      */
35     static void SetInputsSource(AAFwk::Want &want, NotificationConstant::InputsSource source);
36 
37     /**
38      * @brief Obtains the input source of this NotificationUserInput object.
39      *
40      * @param want Indicates the Want containing the input result.
41      * @return Returns the input source of this NotificationUserInput object, as enumerated in
42      * NotificationConstant::InputSource.
43      */
44     static NotificationConstant::InputsSource GetInputsSource(const AAFwk::Want &want);
45 
46     /**
47      * @brief Adds a list of NotificationUserInput objects to a Want. This method should only be called by user input
48      * collection services when sending input results to an WantAgent.
49      *
50      * @param userInputs Indicates the list of NotificationUserInput objects for which the input results are provided.
51      * @param want Indicates the Want to which the input results are to be added.
52      * @param additional Indicates the AAFwk::WantParams object holding the input results.
53      * The AAFwk::WantParams key must match the keys contained in the objects specified in userInputs.
54      */
55     static void AddInputsToWant(const std::vector<std::shared_ptr<NotificationUserInput>> &userInputs,
56         AAFwk::Want &want, const AAFwk::WantParams &additional);
57 
58     /**
59      * @brief Obtains the input text results from a specified Want.
60      *
61      * @param want Indicates the Want object containing one or more user input results.
62      * @return Returns the AAFwk::WantParams object containing the input text results.
63      */
64     static std::shared_ptr<AAFwk::WantParams> GetInputsFromWant(const AAFwk::Want &want);
65 
66     /**
67      * @brief Adds the given NotificationUserInput object of a specified MIME type to an Want. This method is similar to
68      * addInputsToWant(NotificationUserInput, Want, AAFwk::WantParams) except that the MIME type must be specified.
69      *
70      * @param userInput Indicates the NotificationUserInput object for which the input results are provided.
71      * @param want Indicates the Want to which the input results are to be added.
72      * @param results Indicates a map containing the MIME type and its URI result.
73      */
74     static void AddMimeInputToWant(const NotificationUserInput &userInput, AAFwk::Want &want,
75         const std::map<std::string, std::shared_ptr<Uri>> &results);
76 
77     /**
78      * @brief Obtains the input results of a particular MIME type from a specified Want. This method is similar to
79      * GetInputsFromWant(Want) but uses the key of the NotificationUserInput.
80      *
81      * @param want Indicates the Want object containing one or more user input results.
82      * @param inputKey Indicates the key of the NotificationUserInput results to obtain.
83      * @return Returns a map containing the MIME type and its URI result.
84      */
85     static std::map<std::string, std::shared_ptr<Uri>> GetMimeInputsFromWant(
86         const AAFwk::Want &want, const std::string &inputKey);
87 
88 public:
89     /**
90      * @brief A static function used to create a NotificationUserInput instance with the input parameters passed.
91      *
92      * @param inputKey Indicates the AAFwk::WantParams key used to identify this input
93      * when the input is collected from the user.
94      * @return Returns the shared_ptr object owns the created NotificationUserInput object otherwise return empty
95      * object if inputKey is empty.
96      */
97     static std::shared_ptr<NotificationUserInput> Create(const std::string &inputKey);
98 
99     /**
100      * @brief A static function used to create a NotificationUserInput instance with the input parameters passed.
101      *
102      * @param inputKey Indicates the AAFwk::WantParams key used to identify this input
103      * when the input is collected from the user.
104      * @param tag Indicates the tag to be displayed.
105      * @param options Indicates the list of predefined options to be provided for user input.
106      * @param permitFreeFormInput Specifies whether to allow arbitrary text values. The default value is true,
107      * indicating that arbitrary text values are allowed. If this parameter is set to false, you must pass a non-empty
108      * std::vector of options or a non-empty std::set of permitMimeTypes. Otherwise, return empty object.
109      * @param permitMimeTypes Indicates the MIME type allowed.
110      * @param additional Indicates the AAFwk::WantParams object containing the additional data.
111      * @param editType Indicates the edit type to set. For details about available values, see
112      * NotificationConstant::InputEditType.
113      * @return Returns the shared_ptr object owns the created NotificationUserInput object otherwise return empty
114      * object if permitFreeFormInput is false but editType is InputEditType::EDIT_ENABLED.
115      */
116     static std::shared_ptr<NotificationUserInput> Create(const std::string &inputKey, const std::string &tag,
117         const std::vector<std::string> &options, bool permitFreeFormInput, const std::set<std::string> &permitMimeTypes,
118         const std::shared_ptr<AAFwk::WantParams> &additional, NotificationConstant::InputEditType editType);
119 
120     ~NotificationUserInput() = default;
121 
122     /**
123      * @brief Obtains the key of this NotificationUserInput object.
124      *
125      * @return Returns the key of this NotificationUserInput object.
126      */
127     std::string GetInputKey() const;
128 
129     /**
130      * @brief Adds additional data to this Builder.
131      * The value of AAFwk::WantParams will replace the existing extras value in this Builder.
132      *
133      * @param additional Indicates the AAFwk::WantParams object containing the additional data.
134      */
135     void AddAdditionalData(AAFwk::WantParams &additional);
136 
137     /**
138      * @brief Obtains the additional data included in this NotificationUserInput object.
139      *
140      * @return Returns the additional data in this NotificationUserInput object.
141      */
142     const std::shared_ptr<AAFwk::WantParams> GetAdditionalData() const;
143 
144     /**
145      * @brief Sets the edit type of the options provided by this NotificationUserInput object.
146      * The edit type determines whether an option can be edited when the user taps the option.
147      *
148      * @param inputEditType Indicates the edit type to set. For details about available values,
149      * see NotificationConstant::InputEditType.
150      */
151     void SetEditType(NotificationConstant::InputEditType inputEditType);
152 
153     /**
154      * @brief Obtains the edit type of the options provided by this NotificationUserInput object.
155      *
156      * @return Returns the edit type of options.
157      */
158     NotificationConstant::InputEditType GetEditType() const;
159 
160     /**
161      * @brief Sets the options provided to users to satisfy user input needs. All options are displayed in a single
162      * line.Due to limited space, some or all of the options may be hidden if the options are too long or
163      * there are too many options.
164      *
165      * @param options Indicates the list of predefined options to be provided for user input.
166      */
167     void SetOptions(const std::vector<std::string> &options);
168 
169     /**
170      * @brief Obtains the options provided to the user to satisfy user input needs.
171      *
172      * @return Returns the list of predefined options.
173      */
174     std::vector<std::string> GetOptions() const;
175 
176     /**
177      * @brief Sets whether users can input values of the given MIME type.
178      *
179      * @param mimeType Indicates the MIME type allowed.
180      * @param doPermit Specifies whether to allow the given MIME type.
181      * The value true means to allow it, and the value false means not.
182      */
183     void SetPermitMimeTypes(const std::string &mimeType, bool doPermit);
184 
185     /**
186      * @brief Obtains the MIME types allowed by this NotificationUserInput object.
187      *
188      * @return Returns the list of allowed MIME types.
189      */
190     std::set<std::string> GetPermitMimeTypes() const;
191 
192     /**
193      * @brief Checks whether this NotificationUserInput object allows only values of particular MIME types.
194      *
195      * @return Returns true if this object allows only values of particular MIME types; returns false otherwise.
196      */
197     bool IsMimeTypeOnly() const;
198 
199     /**
200      * @brief Sets the tag to be displayed to the user when collecting this input from the user.
201      *
202      * @param tag Indicates the tag to be displayed.
203      */
204     void SetTag(const std::string tag);
205 
206     /**
207      * @brief Obtains the tag to be displayed to the user when collecting this input from the user.
208      *
209      * @return Returns the tag to be displayed to the user.
210      */
211     std::string GetTag() const;
212 
213     /**
214      * @brief Sets whether users can input arbitrary text values.
215      *
216      * @param permitFreeFormInput Specifies whether to allow arbitrary text values. The default value is true,
217      * indicating that arbitrary text values are allowed. If this parameter is set to false, you must either call
218      * SetOptions(std::vector<std::string>) to set a non-null and non-empty list or call
219      * SetPermitMimeTypes(std::string, bool) to allow a MIME type.
220      */
221     void SetPermitFreeFormInput(bool permitFreeFormInput);
222 
223     /**
224      * @brief Checks whether users can provide arbitrary values for this NotificationUserInput object.
225      *
226      * @return Returns true if this object allows arbitrary values; returns false otherwise.
227      */
228     bool IsPermitFreeFormInput() const;
229 
230     /**
231      * @brief Dumps a string representation of the object.
232      *
233      * @return Returns a string representation of the object.
234      */
235     std::string Dump();
236 
237     /**
238      * @brief Converts a NotificationUserInput object into a Json.
239      *
240      * @param jsonObject Indicates the Json object.
241      * @return Returns true if succeed; returns false otherwise.
242      */
243     bool ToJson(nlohmann::json &jsonObject) const override;
244 
245     /**
246      * @brief Creates a NotificationUserInput object from a Json.
247      *
248      * @param jsonObject Indicates the Json object.
249      * @return Returns the NotificationUserInput object.
250      */
251     static NotificationUserInput *FromJson(const nlohmann::json &jsonObject);
252 
253     /**
254      * @brief Marshal a object into a Parcel.
255      *
256      * @param parcel the object into the parcel.
257      * @return Returns true if succeed; returns false otherwise.
258      */
259     virtual bool Marshalling(Parcel &parcel) const override;
260 
261     /**
262      * @brief Unmarshal object from a Parcel.
263      *
264      * @param parcel Indicates the parcel object.
265      * @return Returns the NotificationUserInput object.
266      */
267     static NotificationUserInput *Unmarshalling(Parcel &parcel);
268 
269 private:
270     NotificationUserInput() = default;
271 
272     /**
273      * @brief A constructor used to create a NotificationUserInput instance with the input parameters passed.
274      *
275      * @param inputKey Indicates the AAFwk::WantParams key used to identify this input
276      * when the input is collected from the user.
277      */
278     explicit NotificationUserInput(const std::string &inputKey);
279 
280     /**
281      * @brief A constructor used to create a NotificationUserInput instance with the input parameters passed.
282      *
283      * @param inputKey Indicates the AAFwk::WantParams key used to identify this input
284      * when the input is collected from the user.
285      * @param tag Indicates the tag to be displayed.
286      * @param options Indicates the list of predefined options to be provided for user input.
287      * @param permitFreeFormInput Specifies whether to allow arbitrary text values. The default value is true,
288      * indicating that arbitrary text values are allowed. If this parameter is set to false, you must either call
289      * setOptions(const std::vector<std::string> &) to set a non-empty list or call
290      * setPermitMimeTypes(std::string, bool) to allow a MIME type.
291      * @param permitMimeTypes Indicates the MIME type allowed.
292      * @param additional Indicates the AAFwk::WantParams object containing the additional data.
293      * @param editType Indicates the edit type to set. For details about available values, see
294      * NotificationConstant::InputEditType.
295      */
296     NotificationUserInput(const std::string &inputKey, const std::string &tag, const std::vector<std::string> &options,
297         bool permitFreeFormInput, const std::set<std::string> &permitMimeTypes,
298         const std::shared_ptr<AAFwk::WantParams> &additional, NotificationConstant::InputEditType editType);
299 
300     /**
301      * @brief Read a NotificationUserInput object from a Parcel.
302      *
303      * @param parcel Indicates the parcel object.
304      * @return Returns true if succeed; returns false otherwise.
305      */
306     bool ReadFromParcel(Parcel &parcel);
307 
308 private:
309     std::string inputKey_ {};
310     std::string tag_ {};
311     std::vector<std::string> options_ {};
312     bool permitFreeFormInput_ {true};
313     std::set<std::string> permitMimeTypes_ {};
314     std::shared_ptr<AAFwk::WantParams> additionalData_ {};
315     NotificationConstant::InputEditType editType_ {NotificationConstant::InputEditType::EDIT_AUTO};
316 };
317 }  // namespace Notification
318 }  // namespace OHOS
319 
320 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_USER_INPUT_H
321