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 #ifndef OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_H 17 #define OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_H 18 19 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h" 20 21 #include "ability_connect_callback.h" 22 #include "ability_info.h" 23 #include "caller_callback.h" 24 #include "configuration.h" 25 #include "iability_callback.h" 26 #include "native_engine/native_reference.h" 27 #include "native_engine/native_value.h" 28 #include "start_options.h" 29 #include "want.h" 30 31 #ifdef SUPPORT_GRAPHICS 32 #include "pixel_map.h" 33 #endif 34 35 namespace OHOS { 36 namespace AbilityRuntime { 37 using RuntimeTask = std::function<void(int, const AAFwk::Want&)>; 38 using PermissionRequestTask = std::function<void(const std::vector<std::string>&, const std::vector<int>&)>; 39 using RequestDialogResultTask = std::function<void(int32_t resultCode)>; 40 class LocalCallContainer; 41 class AbilityContext : public Context { 42 public: 43 virtual ~AbilityContext() = default; 44 45 /** 46 * @brief Starts a new ability. 47 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 48 * to start a specific ability. The system locates the target ability from installed abilities based on the value 49 * of the want parameter and then starts it. You can specify the ability to start using the want parameter. 50 * 51 * @param want Indicates the Want containing information about the target ability to start. 52 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 53 * template is started. You can define the request code to identify the results returned by abilities. The value 54 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 55 * template. 56 */ 57 virtual ErrCode StartAbility(const AAFwk::Want &want, int requestCode) = 0; 58 59 /** 60 * @brief Starts a new ability. 61 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 62 * to start a specific ability. The system locates the target ability from installed abilities based on the value 63 * of the want parameter and then starts it. You can specify the ability to start using the want parameter. 64 * 65 * @param want Indicates the Want containing information about the target ability to start. 66 * @param accountId ability caller accountId. 67 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 68 * template is started. You can define the request code to identify the results returned by abilities. The value 69 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 70 * template. 71 */ 72 virtual ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId, int requestCode) = 0; 73 74 /** 75 * @brief Starts a new ability. 76 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 77 * to start a specific ability. The system locates the target ability from installed abilities based on the value 78 * of the want parameter and the value of the start options and then starts it. You can specify the ability to 79 * start using the two parameters. 80 * 81 * @param want Indicates the Want containing application side information about the target ability to start. 82 * @param startOptions Indicates the StartOptions containing service side information about the target ability to 83 * start. 84 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 85 * template is started. You can define the request code to identify the results returned by abilities. The value 86 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 87 * template. 88 */ 89 virtual ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode) = 0; 90 91 /** 92 * @brief Starts a new ability. 93 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 94 * to start a specific ability. The system locates the target ability from installed abilities based on the value 95 * of the want parameter and the value of the start options and then starts it. You can specify the ability to 96 * start using the two parameters. 97 * 98 * @param want Indicates the Want containing application side information about the target ability to start. 99 * @param accountId caller userId. 100 * @param startOptions Indicates the StartOptions containing service side information about the target ability to 101 * start. 102 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 103 * template is started. You can define the request code to identify the results returned by abilities. The value 104 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 105 * template. 106 */ 107 virtual ErrCode StartAbilityWithAccount( 108 const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions, int requestCode) = 0; 109 110 virtual ErrCode StartAbilityForResult(const AAFwk::Want &Want, int requestCode, RuntimeTask &&task) = 0; 111 112 virtual ErrCode StartAbilityForResultWithAccount( 113 const AAFwk::Want &Want, int accountId, int requestCode, RuntimeTask &&task) = 0; 114 115 virtual ErrCode StartAbilityForResult(const AAFwk::Want &Want, const AAFwk::StartOptions &startOptions, 116 int requestCode, RuntimeTask &&task) = 0; 117 118 virtual ErrCode StartAbilityForResultWithAccount(const AAFwk::Want &Want, int accountId, 119 const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task) = 0; 120 121 virtual ErrCode StartServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1) = 0; 122 123 virtual ErrCode StopServiceExtensionAbility(const AAFwk::Want& want, int32_t accountId = -1) = 0; 124 125 virtual ErrCode TerminateAbilityWithResult(const AAFwk::Want &want, int resultCode) = 0; 126 127 virtual ErrCode RestoreWindowStage(NativeEngine& engine, NativeValue* contentStorage) = 0; 128 129 virtual void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData) = 0; 130 131 /** 132 * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. 133 * 134 * @param want Indicates the want containing information about the ability to connect 135 * @param connectCallback Indicates the callback object when the target ability is connected. 136 * @return True means success and false means failure 137 */ 138 virtual bool ConnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) = 0; 139 140 /** 141 * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. 142 * @param want Indicates the want containing information about the ability to connect 143 * @param accountId caller userId. 144 * @param connectCallback Indicates the callback object when the target ability is connected. 145 * @return True means success and false means failure 146 */ 147 virtual bool ConnectAbilityWithAccount(const AAFwk::Want &want, int accountId, 148 const sptr<AbilityConnectCallback> &connectCallback) = 0; 149 150 /** 151 * @brief Disconnects the current ability from an ability 152 * 153 * @param want Indicates the want containing information about the ability to disconnect 154 * @param connectCallback Indicates the callback object when the target ability is connected. 155 * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities. 156 */ 157 virtual void DisconnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) = 0; 158 159 /** 160 * @brief get ability info of the current ability 161 * 162 * @return the ability info of the current ability 163 */ 164 virtual std::shared_ptr<AppExecFwk::AbilityInfo> GetAbilityInfo() const = 0; 165 166 /** 167 * @brief Minimize the current ability. 168 * 169 * @param fromUser mark the minimize operation source. 170 */ 171 virtual void MinimizeAbility(bool fromUser = false) = 0; 172 173 virtual ErrCode TerminateSelf() = 0; 174 175 virtual ErrCode CloseAbility() = 0; 176 177 /** 178 * @brief Requests certain permissions from the system. 179 * This method is called for permission request. This is an asynchronous method. When it is executed, 180 * the task will be called back. 181 * 182 * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null. 183 * @param task The callback or promise fo js interface. 184 */ 185 virtual void RequestPermissionsFromUser(NativeEngine& engine, const std::vector<std::string> &permissions, 186 int requestCode, PermissionRequestTask &&task) = 0; 187 188 /** 189 * @brief Get ContentStorage. 190 * 191 * @return Returns the ContentStorage. 192 */ 193 virtual std::unique_ptr<NativeReference>& GetContentStorage() = 0; 194 195 /** 196 * call function by callback object 197 * 198 * @param want Request info for ability. 199 * @param callback Indicates the callback object. 200 * 201 * @return Returns zero on success, others on failure. 202 */ 203 virtual ErrCode StartAbilityByCall(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback) = 0; 204 205 /** 206 * caller release by callback object 207 * 208 * @param callback Indicates the callback object. 209 * 210 * @return Returns zero on success, others on failure. 211 */ 212 virtual ErrCode ReleaseCall(const std::shared_ptr<CallerCallBack> &callback) = 0; 213 214 /** 215 * @brief Get LocalCallContainer. 216 * 217 * @return Returns the LocalCallContainer. 218 */ 219 virtual sptr<LocalCallContainer> GetLocalCallContainer() = 0; 220 221 virtual void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config) = 0; 222 223 virtual void RegisterAbilityCallback(std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback) = 0; 224 225 virtual ErrCode GetMissionId(int32_t &missionId) = 0; 226 227 /** 228 * @brief Requests dialogService from the system. 229 * This method is called for dialog request. This is an asynchronous method. When it is executed, 230 * the task will be called back. 231 * 232 * @param engine js native engine. 233 * @param want Indicates the dialog service to be requested. 234 * @param task The callback or promise fo js interface. 235 * @return Returns ERR_OK if success. 236 */ 237 virtual ErrCode RequestDialogService(NativeEngine &engine, AAFwk::Want &want, RequestDialogResultTask &&task) = 0; 238 239 #ifdef SUPPORT_GRAPHICS 240 /** 241 * @brief Set mission label of this ability. 242 * 243 * @param label the label of this ability. 244 * @return Returns ERR_OK if success. 245 */ 246 virtual ErrCode SetMissionLabel(const std::string &label) = 0; 247 248 /** 249 * @brief Set mission icon of this ability. 250 * 251 * @param icon the icon of this ability. 252 * @return Returns ERR_OK if success. 253 */ 254 virtual ErrCode SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon) = 0; 255 256 virtual int GetCurrentWindowMode() = 0; 257 #endif 258 virtual bool IsTerminating() = 0; 259 virtual void SetTerminating(bool state) = 0; 260 using SelfType = AbilityContext; 261 static const size_t CONTEXT_TYPE_ID; 262 263 protected: IsContext(size_t contextTypeId)264 bool IsContext(size_t contextTypeId) override 265 { 266 return contextTypeId == CONTEXT_TYPE_ID || Context::IsContext(contextTypeId); 267 } 268 }; 269 } // namespace AbilityRuntime 270 } // namespace OHOS 271 #endif // OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_H 272