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_ACTION_BUTTON_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_ACTION_BUTTON_H 18 19 #include "notification_constant.h" 20 #include "notification_json_convert.h" 21 #include "notification_user_input.h" 22 #include "parcel.h" 23 #include "pixel_map.h" 24 #include "want_agent.h" 25 26 namespace OHOS { 27 namespace Notification { 28 class NotificationActionButton : public Parcelable, public NotificationJsonConvertionBase { 29 public: 30 /** 31 * @brief A static function used to create a NotificationActionButton instance with the input parameters passed. 32 * 33 * @param icon Indicates the icon to represent this NotificationActionButton. 34 * @param title Indicates the title of this NotificationActionButton. 35 * @param wantAgent Indicates the wantAgent to be triggered when this NotificationActionButton is triggered. 36 * @param extras Indicates the AAFwk::WantParams object containing the additional data. 37 * @param semanticActionButton Indicates the semantic action to add. 38 * @param autoCreatedReplies Indicates whether to allow the platform to automatically generate possible replies. 39 * @param mimeTypeOnlyInputs Indicates the NotificationUserInput object to add which allows only values of 40 * particular MIME types. 41 * @param userInputs Indicates the NotificationUserInput object to add. 42 * @param isContextual Indicates whether this NotificationActionButton is a contextual action, that is, whether this 43 * NotificationActionButton is dependent on the notification message body. 44 * @return Returns the shared_ptr object owns the created NotificationActionButton object otherwise returns empty 45 * object if isContextual is true but icon or wantAgent is empty. 46 */ 47 static std::shared_ptr<NotificationActionButton> Create(const std::shared_ptr<Media::PixelMap> &icon, 48 const std::string &title, const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent, 49 const std::shared_ptr<AAFwk::WantParams> &extras = {}, 50 NotificationConstant::SemanticActionButton semanticActionButton = 51 NotificationConstant::SemanticActionButton::NONE_ACTION_BUTTON, 52 bool autoCreatedReplies = true, 53 const std::vector<std::shared_ptr<NotificationUserInput>> &mimeTypeOnlyInputs = {}, 54 const std::shared_ptr<NotificationUserInput> &userInput = {}, bool isContextual = false); 55 56 /** 57 * @brief A static function used to create a NotificationActionButton instance by copying parameters from an 58 * existing NotificationActionButton object. 59 * 60 * @param actionButton Indicates the existing NotificationActionButton object. 61 * @return Returns the shared_ptr object owns the created NotificationActionButton object otherwise returns 62 * empty object. 63 */ 64 static std::shared_ptr<NotificationActionButton> Create( 65 const std::shared_ptr<NotificationActionButton> &actionButton); 66 67 ~NotificationActionButton() = default; 68 69 /** 70 * @brief Obtains the icon of this NotificationActionButton. 71 * 72 * @return Returns the icon of this NotificationActionButton. 73 */ 74 const std::shared_ptr<Media::PixelMap> GetIcon() const; 75 76 /** 77 * @brief Obtains the title of this NotificationActionButton. 78 * 79 * @return Returns the title of this NotificationActionButton. 80 */ 81 std::string GetTitle() const; 82 83 /** 84 * @brief Obtains the WantAgent of this NotificationActionButton. 85 * 86 * @return Returns the WantAgent of this NotificationActionButton. 87 */ 88 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> GetWantAgent() const; 89 90 /** 91 * Adds additional data to this NotificationActionButton. 92 * @param extras Indicates the AAFwk::WantParams object containing the additional data. 93 */ 94 void AddAdditionalData(AAFwk::WantParams &extras); 95 96 /** 97 * @brief Obtains the additional data included in this NotificationActionButton. 98 * 99 * @return Returns the additional data included in this NotificationActionButton. 100 */ 101 const std::shared_ptr<AAFwk::WantParams> GetAdditionalData() const; 102 103 /** 104 * @brief Sets a semantic action for this NotificationActionButton. 105 * 106 * @param semanticActionButton Indicates the semantic action to add.For available values, 107 * see NotificationConstant::SemanticActionButton. 108 */ 109 void SetSemanticActionButton(NotificationConstant::SemanticActionButton semanticActionButton); 110 111 /** 112 * @brief Obtains the semantic action of this NotificationActionButton. 113 * 114 * @return Returns the semantic action of this NotificationActionButton, as enumerated in 115 * NotificationConstant::SemanticActionButton. 116 */ 117 NotificationConstant::SemanticActionButton GetSemanticActionButton() const; 118 119 /** 120 * @brief Adds a NotificationUserInput object that only allows values of particular MIME types. 121 * 122 * @param userInput Indicates the NotificationUserInput object to add. 123 */ 124 void AddMimeTypeOnlyUserInput(const std::shared_ptr<NotificationUserInput> &userInput); 125 126 /** 127 * @brief Obtains the NotificationUserInput objects that only allow values of particular MIME types 128 * when this NotificationActionButton is sent. 129 * 130 * @return Returns the list of NotificationUserInput objects allowing only values of particular MIME types. 131 */ 132 std::vector<std::shared_ptr<NotificationUserInput>> GetMimeTypeOnlyUserInputs() const; 133 134 /** 135 * @brief Adds a NotificationUserInput object used to collect user input. 136 * 137 * @param userInput Indicates the NotificationUserInput object to add. 138 */ 139 void AddNotificationUserInput(const std::shared_ptr<NotificationUserInput> &userInput); 140 141 /** 142 * @brief Obtains the NotificationUserInput object to be collected from the user when this NotificationActionButton 143 * is sent. 144 * 145 * @return Returns the NotificationUserInput object. 146 */ 147 const std::shared_ptr<NotificationUserInput> GetUserInput() const; 148 149 /** 150 * @brief Sets whether to allow the platform to automatically generate possible replies and add them to 151 * NotificationUserInput::getOptions(). 152 * 153 * @param autoCreatedReplies Specifies whether to allow the platform to automatically generate possible replies. 154 * The value true means to allow generated replies; and the value false means not. 155 */ 156 void SetAutoCreatedReplies(bool autoCreatedReplies); 157 158 /** 159 * @brief Checks whether the platform can automatically generate possible replies for this NotificationActionButton. 160 * 161 * @return Returns true if the platform can generate replies; returns false otherwise. 162 */ 163 bool IsAutoCreatedReplies() const; 164 165 /** 166 * @brief Sets whether this NotificationActionButton is a contextual action, that is, whether this 167 * NotificationActionButton is dependent on the notification message body. For example, a contextual 168 * NotificationActionButton provides an address in the notification for users to open a map application. 169 * 170 * @param isContextual Specifies whether this NotificationActionButton is a contextual action. 171 * The value true indicates a contextual action, and the value false indicates not. 172 */ 173 void SetContextDependent(bool isContextual); 174 175 /** 176 * @brief Checks whether this NotificationActionButton is a contextual action, that is, whether this 177 * NotificationActionButton is dependent on the notification message body. For example, a contextual 178 * NotificationActionButton provides an address in the notification for users to open a map application. 179 * 180 * @return Returns true if this NotificationActionButton is a contextual action; returns false otherwise. 181 */ 182 bool IsContextDependent() const; 183 184 /** 185 * @brief Returns a string representation of the object. 186 * 187 * @return Returns a string representation of the object. 188 */ 189 std::string Dump(); 190 191 /** 192 * @brief Converts a NotificationActionButton object into a Json. 193 * 194 * @param jsonObject Indicates the Json object. 195 * @return Returns true if succeed; returns false otherwise. 196 */ 197 bool ToJson(nlohmann::json &jsonObject) const override; 198 199 /** 200 * @brief Creates a NotificationActionButton object from a Json. 201 * 202 * @param jsonObject Indicates the Json object. 203 * @return Returns the NotificationActionButton. 204 */ 205 static NotificationActionButton *FromJson(const nlohmann::json &jsonObject); 206 207 /** 208 * @brief Marshal a object into a Parcel. 209 * 210 * @param parcel the object into the parcel. 211 * @return Returns true if succeed; returns false otherwise. 212 */ 213 virtual bool Marshalling(Parcel &parcel) const override; 214 215 /** 216 * @brief Unmarshal object from a Parcel. 217 * 218 * @param parcel Indicates the parcel object. 219 * @return Returns the NotificationActionButton. 220 */ 221 static NotificationActionButton *Unmarshalling(Parcel &parcel); 222 223 private: 224 NotificationActionButton() = default; 225 226 /** 227 * @brief A constructor used to create a NotificationActionButton instance with the input parameters passed. 228 * 229 * @param icon Indicates the icon to represent this NotificationActionButton. 230 * @param title Indicates the title of this NotificationActionButton. 231 * @param wantAgent Indicates the WantAgent to be triggered when this NotificationActionButton is triggered. 232 * @param extras Indicates the AAFwk::WantParams object containing the additional data. 233 * @param semanticActionButton Indicates the semantic action to add. 234 * @param autoCreatedReplies Indicates whether to allow the platform to automatically generate possible replies. 235 * @param mimeTypeOnlyInputs Indicates the NotificationUserInput object to add which allows only values of 236 * particular MIME types. 237 * @param userInputs Indicates the NotificationUserInput object to add. 238 * @param isContextual Indicates whether this NotificationActionButton is a contextual action. 239 */ 240 NotificationActionButton(const std::shared_ptr<Media::PixelMap> &icon, const std::string &title, 241 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent, 242 const std::shared_ptr<AAFwk::WantParams> &extras, 243 NotificationConstant::SemanticActionButton semanticActionButton, bool autoCreatedReplies, 244 const std::vector<std::shared_ptr<NotificationUserInput>> &mimeTypeOnlyInputs, 245 const std::shared_ptr<NotificationUserInput> &userInput, bool isContextual); 246 247 /** 248 * @brief Read a NotificationActionButton object from a Parcel. 249 * 250 * @param parcel Indicates the parcel object. 251 * @return Returns true if succeed; returns false otherwise. 252 */ 253 bool ReadFromParcel(Parcel &parcel); 254 255 private: 256 std::shared_ptr<Media::PixelMap> icon_ {}; 257 std::string title_ {}; 258 std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent_ {}; 259 std::shared_ptr<AAFwk::WantParams> extras_ {}; 260 NotificationConstant::SemanticActionButton semanticActionButton_ { 261 NotificationConstant::SemanticActionButton::NONE_ACTION_BUTTON 262 }; 263 bool autoCreatedReplies_ {true}; 264 std::vector<std::shared_ptr<NotificationUserInput>> mimeTypeOnlyUserInputs_ {}; 265 std::shared_ptr<NotificationUserInput> userInput_ {}; 266 bool isContextual_ {false}; 267 }; 268 } // namespace Notification 269 } // namespace OHOS 270 271 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_ACTION_BUTTON_H