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 OHOS_ABILITY_RUNTIME_TRIGGER_INFO_H 17 #define OHOS_ABILITY_RUNTIME_TRIGGER_INFO_H 18 19 #include <string> 20 #include <memory> 21 #include "want.h" 22 #include "want_params.h" 23 24 namespace OHOS::AbilityRuntime::WantAgent { 25 class TriggerInfo final : public std::enable_shared_from_this<TriggerInfo> { 26 public: 27 /** 28 * Default constructor used to create a {@code TriggerInfo} instance. 29 * 30 */ 31 TriggerInfo(); 32 virtual ~TriggerInfo() = default; 33 34 /** 35 * A constructor used to create a {@code TriggerInfo} instance based on the input parameters. 36 * 37 * @param permission Indicates the permission required for an {@link WantAgent} recipient. 38 * This parameter is valid only when the {@link WantAgent} is triggered to send common events. 39 * @param extraInfo Indicates the custom extra data you want to add for triggering an {@link WantAgent}. 40 * @param want Indicates the extra {@link ohos.aafwk.content.Want}. 41 * If {@code flags} in {@link WantAgentInfo} contains {@link WantAgentConstant.Flags#CONSTANT_FLAG}, 42 * this parameter is invalid. If flags contains {@link WantAgentConstant.Flags#REPLACE_ELEMENT}, 43 * {@link WantAgentConstant.Flags#REPLACE_ACTION}, {@link WantAgentConstant.Flags#REPLACE_URI}, 44 * {@link WantAgentConstant.Flags#REPLACE_ENTITIES}, and {@link WantAgentConstant.Flags#REPLACE_BUNDLE}, 45 * the {@code element}, {@code action}, {@code uri}, {@code entities}, and {@code bundleName} attributes of the 46 * {@link ohos.aafwk.content.Want} specified in this parameter will be used to replace the 47 * corresponding attributes in the original {@link ohos.aafwk.content.Want}, respectively. 48 * If this parameter is null, the original {@link ohos.aafwk.content.Want} remains unchanged. 49 * @param code Indicates the result code provided for the target of the {@link WantAgent}. 50 */ 51 TriggerInfo(const std::string &permission, const std::shared_ptr<AAFwk::WantParams> &extraInfo, 52 const std::shared_ptr<AAFwk::Want> &want, int resultCode); 53 54 /** 55 * A constructor used to create a {@code TriggerInfo} instance by copying parameters from an existing one. 56 * 57 * @param paramInfo Indicates the existing {@code TriggerInfo} object. 58 */ 59 explicit TriggerInfo(const TriggerInfo ¶mInfo); 60 61 /** 62 * A copy assignment operator used to create a {@code TriggerInfo} instance by copying parameters from an existing 63 * one. 64 * 65 * @param paramInfo Indicates the existing {@code TriggerInfo} object. 66 */ 67 const TriggerInfo &operator=(const TriggerInfo ¶mInfo); 68 69 /** 70 * Obtains the permission from the current {@code TriggerInfo} object. 71 * 72 * @return Returns the permission name. 73 */ 74 std::string GetPermission() const; 75 76 /** 77 * Obtains the extra data from the {@code TriggerInfo} object. 78 * 79 * @return Returns the extra data. 80 */ 81 std::shared_ptr<AAFwk::WantParams> GetExtraInfo() const; 82 83 /** 84 * Obtains the {@link ohos.aafwk.content.Want} used for triggering an {@link WantAgent}. 85 * 86 * @return Returns an {@link ohos.aafwk.content.Want} object. 87 */ 88 std::shared_ptr<AAFwk::Want> GetWant() const; 89 90 /** 91 * Obtains the result code provided for the target of the {@link WantAgent}. 92 * 93 * @return Returns the result code provided for the target of the {@link WantAgent}. 94 */ 95 int GetResultCode() const; 96 97 /** 98 * A builder class for {@link TriggerInfo} objects. 99 * 100 */ 101 public: 102 class Builder final : public std::enable_shared_from_this<Builder> { 103 public: 104 /** 105 * Default constructor used to create a {@code Builder} instance. 106 * 107 */ 108 Builder(); 109 virtual ~Builder() = default; 110 111 /** 112 * Sets the permission that the {@link WantAgent} recipient must have. 113 * 114 * @param permission Indicates the permission to set. This parameter is valid only when the {@link WantAgent} 115 * to trigger is intended to send a common event. 116 * @return Returns this {@code Builder} object with the specified permission. 117 */ 118 std::shared_ptr<Builder> SetPermission(const std::string &permission); 119 120 /** 121 * Sets custom data. 122 * 123 * @param params Indicates the custom data to set. 124 * @return Returns this {@code Builder} object with the custom data. 125 */ 126 std::shared_ptr<Builder> SetWantParams(const std::shared_ptr<AAFwk::WantParams> ¶ms); 127 128 /** 129 * Sets a custom {@link ohos.aafwk.content.Want}. 130 * 131 * @param want Indicates the custom {@code Want} to set. If the member variable {@code flags} of the 132 * {@link WantAgentInfo} contains {@link WantAgentConstant.Flags#CONSTANT_FLAG}, this parameter does not 133 * take effect. If {@code flags} contains {@link WantAgentConstant.Flags#REPLACE_ELEMENT}, 134 * {@link WantAgentConstant.Flags#REPLACE_ACTION}, {@link WantAgentConstant.Flags#REPLACE_URI}, 135 * {@link WantAgentConstant.Flags#REPLACE_ENTITIES}, and {@link WantAgentConstant.Flags#REPLACE_BUNDLE}, 136 * the {@code element}, {@code action}, {@code uri}, {@code entities}, and {@code bundleName} attributes of the 137 * {@link ohos.aafwk.content.Want} specified in this parameter will be used to replace the corresponding 138 * attributes in the original {@link ohos.aafwk.content.Want}, respectively. If this parameter is null, the 139 * original {@link ohos.aafwk.content.Want} remains unchanged. 140 * @return Returns this {@code Builder} object with the custom {@code Want}. 141 */ 142 std::shared_ptr<Builder> SetWant(const std::shared_ptr<AAFwk::Want> &want); 143 144 /** 145 * Sets the result code provided for the target of the {@link WantAgent}. 146 * 147 * @param code Indicates the result code provided for the target of the {@link WantAgent}. 148 * @return Returns this {@code Builder} object with the specified result code. 149 */ 150 std::shared_ptr<Builder> SetResultCode(int resultCode); 151 152 /** 153 * Creates a {@link TriggerInfo} object using all of the settings. 154 * 155 * @return Returns the created {@code TriggerInfo} object. 156 */ 157 std::shared_ptr<TriggerInfo> Build(); 158 159 private: 160 std::string permission_; 161 std::shared_ptr<AAFwk::WantParams> params_; 162 std::shared_ptr<AAFwk::Want> want_; 163 int resultCode_ = 0; 164 }; 165 166 private: 167 std::string permission_; 168 std::shared_ptr<AAFwk::WantParams> extraInfo_; 169 std::shared_ptr<AAFwk::Want> want_; 170 int resultCode_ = 0; 171 }; 172 } // namespace OHOS::AbilityRuntime::WantAgent 173 #endif // OHOS_ABILITY_RUNTIME_TRIGGER_INFO_H 174