1 /* 2 * Copyright (c) 2021-2023 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 "js_ui_extension_callback.h" 27 #include "mission_info.h" 28 #include "native_engine/native_reference.h" 29 #include "native_engine/native_value.h" 30 #include "start_options.h" 31 #include "want.h" 32 33 #ifdef SUPPORT_GRAPHICS 34 #include "pixel_map.h" 35 #endif 36 37 namespace OHOS { 38 namespace Ace { 39 class UIContent; 40 } 41 42 namespace AbilityRuntime { 43 using RuntimeTask = std::function<void(int, const AAFwk::Want&, bool)>; 44 using PermissionRequestTask = std::function<void(const std::vector<std::string>&, const std::vector<int>&)>; 45 using RequestDialogResultTask = std::function<void(int32_t resultCode, const AAFwk::Want&)>; 46 class LocalCallContainer; 47 constexpr int32_t DEFAULT_INVAL_VALUE = -1; 48 class AbilityContext : public Context { 49 public: 50 virtual ~AbilityContext() = default; 51 52 /** 53 * @brief Starts a new ability. 54 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 55 * to start a specific ability. The system locates the target ability from installed abilities based on the value 56 * of the want parameter and then starts it. You can specify the ability to start using the want parameter. 57 * 58 * @param want Indicates the Want containing information about the target ability to start. 59 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 60 * template is started. You can define the request code to identify the results returned by abilities. The value 61 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 62 * template. 63 */ 64 virtual ErrCode StartAbility(const AAFwk::Want &want, int requestCode) = 0; 65 66 /** 67 * @brief Starts a new ability. 68 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 69 * to start a specific ability. The system locates the target ability from installed abilities based on the value 70 * of the want parameter and then starts it. You can specify the ability to start using the want parameter. 71 * 72 * @param want Indicates the Want containing information about the target ability to start. 73 * @param accountId ability caller accountId. 74 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 75 * template is started. You can define the request code to identify the results returned by abilities. The value 76 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 77 * template. 78 */ 79 virtual ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId, int requestCode) = 0; 80 81 /** 82 * @brief Starts a new ability. 83 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 84 * to start a specific ability. The system locates the target ability from installed abilities based on the value 85 * of the want parameter and the value of the start options and then starts it. You can specify the ability to 86 * start using the two parameters. 87 * 88 * @param want Indicates the Want containing application side information about the target ability to start. 89 * @param startOptions Indicates the StartOptions containing service side information about the target ability to 90 * start. 91 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 92 * template is started. You can define the request code to identify the results returned by abilities. The value 93 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 94 * template. 95 */ 96 virtual ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode) = 0; 97 98 /** 99 * @brief Starts a new ability using the original caller information. 100 * Start a new ability as if it was started by the ability that started current ability. This is for the confirm 101 * ability and selection ability, which passthrough their want to the target. 102 * 103 * @param want Indicates the Want containing information about the target ability to start. 104 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 105 * template is started. You can define the request code to identify the results returned by abilities. The value 106 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 107 * template. 108 */ 109 virtual ErrCode StartAbilityAsCaller(const AAFwk::Want &want, int requestCode) = 0; 110 111 /** 112 * @brief Starts a new ability using the original caller information. 113 * Start a new ability as if it was started by the ability that started current ability. This is for the confirm 114 * ability and selection ability, which passthrough their want to the target. 115 * 116 * @param want Indicates the Want containing information about the target ability to start. 117 * @param startOptions Indicates the StartOptions containing service side information about the target ability to 118 * start. 119 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 120 * template is started. You can define the request code to identify the results returned by abilities. The value 121 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 122 * template. 123 */ 124 virtual ErrCode StartAbilityAsCaller(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, 125 int requestCode) = 0; 126 127 /** 128 * @brief Starts a new ability. 129 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 130 * to start a specific ability. The system locates the target ability from installed abilities based on the value 131 * of the want parameter and the value of the start options and then starts it. You can specify the ability to 132 * start using the two parameters. 133 * 134 * @param want Indicates the Want containing application side information about the target ability to start. 135 * @param accountId caller userId. 136 * @param startOptions Indicates the StartOptions containing service side information about the target ability to 137 * start. 138 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 139 * template is started. You can define the request code to identify the results returned by abilities. The value 140 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 141 * template. 142 */ 143 virtual ErrCode StartAbilityWithAccount( 144 const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions, int requestCode) = 0; 145 146 virtual ErrCode StartAbilityForResult(const AAFwk::Want &Want, int requestCode, RuntimeTask &&task) = 0; 147 148 virtual ErrCode StartAbilityForResultWithAccount( 149 const AAFwk::Want &Want, int accountId, int requestCode, RuntimeTask &&task) = 0; 150 151 virtual ErrCode StartAbilityForResult(const AAFwk::Want &Want, const AAFwk::StartOptions &startOptions, 152 int requestCode, RuntimeTask &&task) = 0; 153 154 virtual ErrCode StartAbilityForResultWithAccount(const AAFwk::Want &Want, int accountId, 155 const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task) = 0; 156 157 virtual ErrCode StartServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1) = 0; 158 159 virtual ErrCode StopServiceExtensionAbility(const AAFwk::Want& want, int32_t accountId = -1) = 0; 160 161 virtual ErrCode TerminateAbilityWithResult(const AAFwk::Want &want, int resultCode) = 0; 162 163 virtual ErrCode RestoreWindowStage(napi_env env, napi_value contentStorage) = 0; 164 165 virtual void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData) = 0; 166 167 virtual ErrCode RequestModalUIExtension(const AAFwk::Want& want) = 0; 168 169 /** 170 * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. 171 * 172 * @param want Indicates the want containing information about the ability to connect 173 * @param connectCallback Indicates the callback object when the target ability is connected. 174 * @return True means success and false means failure 175 */ 176 virtual ErrCode ConnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) = 0; 177 178 /** 179 * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. 180 * @param want Indicates the want containing information about the ability to connect 181 * @param accountId caller userId. 182 * @param connectCallback Indicates the callback object when the target ability is connected. 183 * @return True means success and false means failure 184 */ 185 virtual ErrCode ConnectAbilityWithAccount(const AAFwk::Want &want, int accountId, 186 const sptr<AbilityConnectCallback> &connectCallback) = 0; 187 188 /** 189 * @brief Disconnects the current ability from an ability 190 * 191 * @param want Indicates the want containing information about the ability to disconnect 192 * @param connectCallback Indicates the callback object when the target ability is connected. 193 * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities. 194 */ 195 virtual void DisconnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) = 0; 196 197 /** 198 * @brief get ability info of the current ability 199 * 200 * @return the ability info of the current ability 201 */ 202 virtual std::shared_ptr<AppExecFwk::AbilityInfo> GetAbilityInfo() const = 0; 203 204 /** 205 * @brief Minimize the current ability. 206 * 207 * @param fromUser mark the minimize operation source. 208 */ 209 virtual void MinimizeAbility(bool fromUser = false) = 0; 210 211 /** 212 * @brief Get OnBackPressedCallBack. 213 * @param needMoveToBackground true if ability will be moved to background; false if ability will be terminated. 214 * 215 * @return Returns ERR_OK on success, others on failure. 216 */ 217 virtual ErrCode OnBackPressedCallBack(bool &needMoveToBackground) = 0; 218 219 virtual ErrCode MoveAbilityToBackground() = 0; 220 221 virtual ErrCode TerminateSelf() = 0; 222 223 virtual ErrCode CloseAbility() = 0; 224 225 /** 226 * @brief Get ContentStorage. 227 * 228 * @return Returns the ContentStorage. 229 */ 230 virtual std::unique_ptr<NativeReference>& GetContentStorage() = 0; 231 232 /** 233 * call function by callback object 234 * 235 * @param want Request info for ability. 236 * @param callback Indicates the callback object. 237 * @param accountId Indicates the account to start. 238 * 239 * @return Returns zero on success, others on failure. 240 */ 241 virtual ErrCode StartAbilityByCall(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback, 242 int32_t accountId = DEFAULT_INVAL_VALUE) = 0; 243 244 /** 245 * caller release by callback object 246 * 247 * @param callback Indicates the callback object. 248 * 249 * @return Returns zero on success, others on failure. 250 */ 251 virtual ErrCode ReleaseCall(const std::shared_ptr<CallerCallBack> &callback) = 0; 252 253 /** 254 * clear failed call connection by callback object 255 * 256 * @param callback Indicates the callback object. 257 * 258 * @return void. 259 */ 260 virtual void ClearFailedCallConnection(const std::shared_ptr<CallerCallBack> &callback) = 0; 261 262 /** 263 * @brief Get LocalCallContainer. 264 * 265 * @return Returns the LocalCallContainer. 266 */ 267 virtual std::shared_ptr<LocalCallContainer> GetLocalCallContainer() = 0; 268 269 virtual void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config) = 0; 270 271 virtual void RegisterAbilityCallback(std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback) = 0; 272 273 virtual void SetWeakSessionToken(const wptr<IRemoteObject>& sessionToken) = 0; 274 275 /** 276 * @brief Requests dialogService from the system. 277 * This method is called for dialog request. This is an asynchronous method. When it is executed, 278 * the task will be called back. 279 * 280 * @param env js env. 281 * @param want Indicates the dialog service to be requested. 282 * @param task The callback or promise fo js interface. 283 * @return Returns ERR_OK if success. 284 */ 285 virtual ErrCode RequestDialogService(napi_env env, AAFwk::Want &want, RequestDialogResultTask &&task) = 0; 286 287 /** 288 * @brief Report drawn completed. 289 * 290 * @return Returns ERR_OK on success, others on failure. 291 */ 292 virtual ErrCode ReportDrawnCompleted() = 0; 293 294 virtual ErrCode GetMissionId(int32_t &missionId) = 0; 295 296 /** 297 * Set mission continue state of this ability. 298 * 299 * @param state the mission continuation state of this ability. 300 * @return Returns ERR_OK if success. 301 */ 302 virtual ErrCode SetMissionContinueState(const AAFwk::ContinueState &state) = 0; 303 304 #ifdef SUPPORT_GRAPHICS 305 /** 306 * @brief Set mission label of this ability. 307 * 308 * @param label the label of this ability. 309 * @return Returns ERR_OK if success. 310 */ 311 virtual ErrCode SetMissionLabel(const std::string &label) = 0; 312 313 /** 314 * @brief Set mission icon of this ability. 315 * 316 * @param icon the icon of this ability. 317 * @return Returns ERR_OK if success. 318 */ 319 virtual ErrCode SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon) = 0; 320 321 virtual int GetCurrentWindowMode() = 0; 322 323 /** 324 * @brief Get window rectangle of this ability. 325 * 326 * @param the left position of window rectangle. 327 * @param the top position of window rectangle. 328 * @param the width position of window rectangle. 329 * @param the height position of window rectangle. 330 */ 331 virtual void GetWindowRect(int32_t &left, int32_t &top, int32_t &width, int32_t &height) = 0; 332 333 /** 334 * @brief Get ui content object. 335 * 336 * @return UIContent object of ACE. 337 */ 338 virtual Ace::UIContent* GetUIContent() = 0; 339 virtual ErrCode StartAbilityByType(const std::string &type, AAFwk::WantParams &wantParam, 340 const std::shared_ptr<JsUIExtensionCallback> &uiExtensionCallbacks) = 0; 341 virtual ErrCode CreateModalUIExtensionWithApp(const AAFwk::Want &want) = 0; 342 virtual void EraseUIExtension(int32_t sessionId) = 0; 343 #endif 344 virtual bool IsTerminating() = 0; 345 virtual void SetTerminating(bool state) = 0; 346 using SelfType = AbilityContext; 347 static const size_t CONTEXT_TYPE_ID; 348 349 protected: IsContext(size_t contextTypeId)350 bool IsContext(size_t contextTypeId) override 351 { 352 return contextTypeId == CONTEXT_TYPE_ID || Context::IsContext(contextTypeId); 353 } 354 }; 355 } // namespace AbilityRuntime 356 } // namespace OHOS 357 #endif // OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_H 358