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