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 FOUNDATION_APPEXECFWK_OHOS_ABILITY_H 17 #define FOUNDATION_APPEXECFWK_OHOS_ABILITY_H 18 19 #include <functional> 20 #include <string> 21 #include <unistd.h> 22 23 #include "ability_context.h" 24 #include "ability_continuation_interface.h" 25 #include "ability_event_interface.h" 26 #include "ability_lifecycle_executor.h" 27 #include "ability_lifecycle_interface.h" 28 #ifdef SUPPORT_GRAPHICS 29 #include "ability_window.h" 30 #endif 31 #include "appexecfwk_errors.h" 32 #include "configuration.h" 33 #include "context.h" 34 #include "continuation_handler.h" 35 #include "continuation_state.h" 36 #ifdef SUPPORT_GRAPHICS 37 #include "display_manager.h" 38 #endif 39 #include "dummy_ability_package.h" 40 #include "dummy_component_container.h" 41 #include "dummy_notification_request.h" 42 #ifdef SUPPORT_GRAPHICS 43 #include "form_callback_interface.h" 44 #include "form_constants.h" 45 #include "form_death_callback.h" 46 #include "form_info.h" 47 #include "form_provider_info.h" 48 #include "form_state_info.h" 49 #include "foundation/multimodalinput/input/interfaces/native/innerkits/event/include/key_event.h" 50 #include "foundation/multimodalinput/input/interfaces/native/innerkits/event/include/pointer_event.h" 51 #endif 52 #include "iability_callback.h" 53 #include "iremote_object.h" 54 #include "pac_map.h" 55 #include "want.h" 56 #include "want_agent.h" 57 #ifdef SUPPORT_GRAPHICS 58 #include "window_option.h" 59 #include "window_scene.h" 60 #include "wm_common.h" 61 #endif 62 #include "../../ability_runtime/include/ability_context.h" 63 64 using Uri = OHOS::Uri; 65 66 namespace OHOS { 67 namespace NativeRdb { 68 class AbsSharedResultSet; 69 class DataAbilityPredicates; 70 class ValuesBucket; 71 } // namespace NativeRdb 72 namespace AbilityRuntime { 73 class Runtime; 74 } 75 #ifdef SUPPORT_GRAPHICS 76 class KeyEvent; 77 #endif 78 namespace AppExecFwk { 79 using FeatureAbilityTask = std::function<void(int, const AAFwk::Want&)>; 80 class DataAbilityResult; 81 class DataAbilityOperation; 82 class AbilityPostEventTimeout; 83 class OHOSApplication; 84 class AbilityHandler; 85 #ifdef SUPPORT_GRAPHICS 86 class AbilityWindow; 87 #endif 88 class ILifeCycle; 89 class ContinuationManager; 90 class ContinuationRegisterManager; 91 class IContinuationRegisterManager; 92 class Ability : public IAbilityEvent, 93 public ILifeCycle, 94 public AbilityContext, 95 #ifdef SUPPORT_GRAPHICS 96 public FormCallbackInterface, 97 public OHOS::Rosen::DisplayManager::IDisplayListener, 98 public OHOS::Rosen::IDisplayMoveListener, 99 #endif 100 public IAbilityContinuation, 101 public IAbilityCallback, 102 public std::enable_shared_from_this<Ability> { 103 public: 104 #ifdef SUPPORT_GRAPHICS 105 friend class PageAbilityImpl; 106 #endif 107 friend class NewAbilityImpl; 108 109 static Ability* Create(const std::unique_ptr<AbilityRuntime::Runtime>& runtime); 110 111 Ability() = default; 112 virtual ~Ability() = default; 113 114 /** 115 * @brief Obtains the AbilityContext object of the ability. 116 * 117 * @return Returns the AbilityContext object of the ability. 118 */ GetAbilityContext()119 inline std::shared_ptr<AbilityRuntime::AbilityContext> GetAbilityContext() 120 { 121 return abilityContext_; 122 } 123 124 /** 125 * @brief Destroys ability. 126 * 127 * @param want Indicates the want containing information about TerminateAbility 128 * 129 * @return Returns the result of TerminateAbility 130 */ 131 using AbilityContext::TerminateAbility; 132 int TerminateAbility(Want &want); 133 134 /** 135 * @brief By binding an action, you can set different action parameters in want to present different initial 136 * pages. You must register actions in the profile file. 137 * 138 * @param action Indicates the action to bind. 139 * 140 * @param entry Indicates the entry, which is the fully qualified name of your AbilitySlice class. 141 * 142 * @return Returns the result of AddActionRoute 143 */ 144 virtual void AddActionRoute(const std::string &action, const std::string &entry) final; 145 146 #ifdef SUPPORT_GRAPHICS 147 /** 148 * @brief Sets the background color of the window in RGB color mode. 149 * 150 * @param red The value ranges from 0 to 255. 151 * 152 * @param green The value ranges from 0 to 255. 153 * 154 * @param blue The value ranges from 0 to 255. 155 * 156 * @return Returns the result of SetWindowBackgroundColor 157 */ 158 virtual int SetWindowBackgroundColor(int red, int green, int blue) final; 159 #endif 160 161 /** 162 * @brief Destroys this Page or Service ability. 163 * After a Page or Service ability performs all operations, it can use this method to destroy itself 164 * to free up memory. This method can be called only after the ability is initialized. 165 * 166 * @return errCode ERR_OK on success, others on failure. 167 */ 168 virtual ErrCode TerminateAbility() final; 169 170 /** 171 * @brief Obtains the Lifecycle object of the current ability. 172 * 173 * @return Returns the Lifecycle object. 174 */ 175 virtual std::shared_ptr<LifeCycle> GetLifecycle() override final; 176 177 /** 178 * @brief Obtains a resource manager. 179 * 180 * @return Returns a ResourceManager object. 181 */ 182 std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override; 183 184 /** 185 * @brief Checks whether the configuration of this ability is changing. 186 * 187 * @return Returns true if the configuration of this ability is changing and false otherwise. 188 */ 189 bool IsUpdatingConfigurations() override; 190 191 #ifdef SUPPORT_GRAPHICS 192 /** 193 * @brief Informs the system of the time required for drawing this Page ability. 194 * 195 * @return Returns the notification is successful or fail 196 */ 197 bool PrintDrawnCompleted() override; 198 199 /** 200 * @brief Inflates UI controls by using ComponentContainer. 201 * You can create a ComponentContainer instance that contains multiple components. 202 * 203 * @param componentContainer Indicates a set of customized components. 204 */ 205 virtual void SetUIContent(const ComponentContainer &componentContainer); 206 207 /** 208 * @brief Inflates layout resources by using the layout resource ID. 209 * 210 * @param layoutRes Indicates the layout resource ID, which cannot be a negative number. 211 */ 212 virtual void SetUIContent(int layoutRes) final; 213 #endif 214 215 /** 216 * Start other ability for result. 217 * 218 * @param want information of other ability 219 * @param requestCode request code for abilityMS to return result 220 * 221 * @return errCode ERR_OK on success, others on failure. 222 */ 223 virtual ErrCode StartAbilityForResult(const Want &want, int requestCode) final; 224 225 /** 226 * Starts an ability with specific start settings and returns the execution result when the ability is destroyed. 227 * When the ability is destroyed, onAbilityResult(int,int,ohos.aafwk.content.Want) is called and the returned 228 * requestCode is transferred to the current method. The given requestCode is customized and cannot be a negative 229 * number. 230 * 231 * @param want Indicates the ability to start. 232 * @param requestCode Indicates the request code returned after the ability is started. You can define the request 233 * code to identify the results returned by abilities. The value ranges from 0 to 65535. 234 * @param abilityStartSetting Indicates the setting ability used to start. 235 * 236 * @return errCode ERR_OK on success, others on failure. 237 */ 238 virtual ErrCode StartAbilityForResult( 239 const Want &want, int requestCode, AbilityStartSetting abilityStartSetting) final; 240 241 /** 242 * Starts a new ability with specific start settings. 243 * A Page or Service ability uses this method to start a specific ability. 244 * The system locates the target ability from installed abilities based on 245 * the value of the want parameter and then starts it. You can specify the 246 * ability to start using the want parameter. 247 * 248 * @param want Indicates the ability to start. 249 * @param abilityStartSetting Indicates the setting ability used to start. 250 * 251 * @return errCode ERR_OK on success, others on failure. 252 */ 253 ErrCode StartAbility(const Want &want, AbilityStartSetting abilityStartSetting); 254 255 ErrCode StartFeatureAbilityForResult(const Want &want, int requestCode, FeatureAbilityTask &&task); 256 257 // lifecycle callback 258 virtual void Init(const std::shared_ptr<AbilityInfo> &abilityInfo, 259 const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler, 260 const sptr<IRemoteObject> &token); 261 262 void AttachAbilityContext(const std::shared_ptr<AbilityRuntime::AbilityContext> &abilityContext); 263 264 /** 265 * @brief Called when this ability is started. You must override this function if you want to perform some 266 * initialization operations during ability startup. 267 * 268 * This function can be called only once in the entire lifecycle of an ability. 269 * @param Want Indicates the {@link Want} structure containing startup information about the ability. 270 */ 271 virtual void OnStart(const Want &want); 272 273 /** 274 * @brief Called when this ability enters the <b>STATE_STOP</b> state. 275 * 276 * The ability in the <b>STATE_STOP</b> is being destroyed. 277 * You can override this function to implement your own processing logic. 278 */ 279 virtual void OnStop(); 280 281 /** 282 * @brief Called when this ability enters the <b>STATE_ACTIVE</b> state. 283 * 284 * The ability in the <b>STATE_ACTIVE</b> state is visible and has focus. 285 * You can override this function to implement your own processing logic. 286 * 287 * @param Want Indicates the {@link Want} structure containing activation information about the ability. 288 */ 289 virtual void OnActive(); 290 291 /** 292 * @brief Called when this ability enters the <b>STATE_INACTIVE</b> state. 293 * 294 * <b>STATE_INACTIVE</b> is an instantaneous state. The ability in this state may be visible but does not have 295 * focus.You can override this function to implement your own processing logic. 296 */ 297 virtual void OnInactive(); 298 299 #ifdef SUPPORT_GRAPHICS 300 /** 301 * @brief Called after instantiating WindowScene. 302 * 303 * 304 * You can override this function to implement your own processing logic. 305 */ 306 virtual void OnSceneCreated(); 307 308 /** 309 * @brief Called after ability stoped. 310 * 311 * 312 * You can override this function to implement your own processing logic. 313 */ 314 virtual void onSceneDestroyed(); 315 316 /** 317 * @brief Called after ability restored. 318 * 319 * 320 * You can override this function to implement your own processing logic. 321 */ 322 virtual void OnSceneRestored(); 323 324 /** 325 * @brief Called when this ability enters the <b>STATE_FOREGROUND</b> state. 326 * 327 * 328 * The ability in the <b>STATE_FOREGROUND</b> state is visible. 329 * You can override this function to implement your own processing logic. 330 */ 331 virtual void OnForeground(const Want &want); 332 333 /** 334 * @brief Called when this ability enters the <b>STATE_BACKGROUND</b> state. 335 * 336 * 337 * The ability in the <b>STATE_BACKGROUND</b> state is invisible. 338 * You can override this function to implement your own processing logic. 339 */ 340 virtual void OnBackground(); 341 342 /** 343 * @brief Called when a key is pressed. When any component in the Ability gains focus, the key-down event for 344 * the component will be handled first. This callback will not be invoked if the callback triggered for the 345 * key-down event of the component returns true. The default implementation of this callback does nothing 346 * and returns false. 347 * 348 * @param keyEvent Indicates the key-down event. 349 * 350 * @return Returns true if this event is handled and will not be passed further; returns false if this event 351 * is not handled and should be passed to other handlers. 352 */ 353 virtual void OnKeyDown(const std::shared_ptr<MMI::KeyEvent>& keyEvent); 354 355 /** 356 * @brief Called when a key is released. When any component in the Ability gains focus, the key-up event for 357 * the component will be handled first. This callback will not be invoked if the callback triggered for the 358 * key-up event of the component returns true. The default implementation of this callback does nothing and 359 * returns false. 360 * 361 * @param keyEvent Indicates the key-up event. 362 * 363 * @return Returns true if this event is handled and will not be passed further; returns false if this event 364 * is not handled and should be passed to other handlers. 365 */ 366 virtual void OnKeyUp(const std::shared_ptr<MMI::KeyEvent>& keyEvent); 367 368 /** 369 * @brief Called when a touch event is dispatched to this ability. The default implementation of this callback 370 * does nothing and returns false. 371 * 372 * @param event Indicates information about the touch event. 373 * 374 * @return Returns true if the event is handled; returns false otherwise. 375 */ 376 virtual void OnPointerEvent(std::shared_ptr<MMI::PointerEvent>& pointerEvent); 377 #endif 378 379 /** 380 * @brief Called when this Service ability is connected for the first time. 381 * 382 * You can override this function to implement your own processing logic. 383 * 384 * @param want Indicates the {@link Want} structure containing connection information about the Service ability. 385 * @return Returns a pointer to the <b>sid</b> of the connected Service ability. 386 */ 387 virtual sptr<IRemoteObject> OnConnect(const Want &want); 388 389 /** 390 * @brief Called when all abilities connected to this Service ability are disconnected. 391 * 392 * You can override this function to implement your own processing logic. 393 * 394 */ 395 virtual void OnDisconnect(const Want &want); 396 397 #ifdef SUPPORT_GRAPHICS 398 /** 399 * @brief Inflates UI controls by using ComponentContainer. 400 * You can create a ComponentContainer instance that contains multiple components. 401 * 402 * @param componentContainer Indicates the component layout defined by the user. 403 * @param context Indicates the context to use. 404 * @param typeFlag Indicates the window type. 405 */ 406 virtual void SetUIContent( 407 const ComponentContainer &componentContainer, std::shared_ptr<Context> &context, int typeFlag); 408 409 /** 410 * @brief Inflates layout resources by using the layout resource ID. 411 * 412 * @param layoutRes Indicates the layout resource ID, which cannot be a negative number. 413 * @param context Indicates the context to use. 414 * @param typeFlag Indicates the window type. 415 */ 416 virtual void SetUIContent(int layoutRes, std::shared_ptr<Context> &context, int typeFlag); 417 418 /** 419 * @brief Called when this ability gains or loses window focus. 420 * 421 * @param hasFocus Specifies whether this ability has focus. 422 */ 423 virtual void OnWindowFocusChanged(bool hasFocus); 424 425 /** 426 * @brief Called when this ability is moved to or removed from the top of the stack. 427 * 428 * @param topActive Specifies whether this ability is moved to or removed from the top of the stack. The value true 429 * indicates that it is moved to the top, and false indicates that it is removed from the top of the stack. 430 */ 431 virtual void OnTopActiveAbilityChanged(bool topActive); 432 433 /** 434 * @brief Inflates UI controls by using windowOption. 435 * 436 * @param windowOption Indicates the window option defined by the user. 437 */ 438 virtual void InitWindow(Rosen::WindowType winType, int32_t displayId, sptr<Rosen::WindowOption> option); 439 440 /** 441 * @brief Get the window belong to the ability. 442 * 443 * @return Returns a Window object pointer. 444 */ 445 virtual const sptr<Rosen::Window> GetWindow(); 446 447 /** 448 * @brief get the scene belong to the ability. 449 * 450 * @return Returns a WindowScene object pointer. 451 */ 452 std::shared_ptr<Rosen::WindowScene> GetScene(); 453 454 void SetShowOnLockScreen(bool showOnLockScreen); 455 456 /** 457 * @brief Checks whether the main window of this ability has window focus. 458 * 459 * @return Returns true if this ability currently has window focus; returns false otherwise. 460 */ 461 bool HasWindowFocus(); 462 463 /** 464 * @brief When the ability starts, set whether to wake up the screen. 465 * 466 * @param wakeUp Set true to wake up, false to not wake up. 467 */ 468 void SetWakeUpScreen(bool wakeUp); 469 470 /** 471 * @brief Set the display orientation of the main window. 472 * 473 * @param orientation Indicates the display orientation of the window. 474 */ 475 void SetDisplayOrientation(int orientation); 476 477 /** 478 * @brief Get the display orientation of the main window. 479 * 480 * @return Returns the display orientation of the window. 481 */ 482 int GetDisplayOrientation() override; 483 #endif 484 485 /** 486 * @description: Obtains api version based on ability. 487 * @return api version. 488 */ 489 int GetCompatibleVersion(); 490 491 /** 492 * @description: Set api version in an ability. 493 * @param compatibleVersion api version 494 * @return None. 495 */ 496 void SetCompatibleVersion(int compatibleVersion); 497 498 #ifdef SUPPORT_GRAPHICS 499 /** 500 * @brief Called when a key is lone pressed. 501 * 502 * @param keyCode Indicates the code of the key long pressed. 503 * @param keyEvent Indicates the key-long-press event. 504 * 505 * @return Returns true if this event is handled and will not be passed further; returns false if this event 506 * is not handled and should be passed to other handlers. 507 */ 508 virtual bool OnKeyPressAndHold(int keyCode, const std::shared_ptr<KeyEvent> &keyEvent); 509 #endif 510 511 /** 512 * @brief Called back after permissions are requested by using 513 * AbilityContext.requestPermissionsFromUser(java.lang.String[],int). 514 * 515 * @param requestCode Indicates the request code passed to this method from 516 * AbilityContext.requestPermissionsFromUser(java.lang.String[],int). 517 * @param permissions Indicates the list of permissions requested by using 518 * AbilityContext.requestPermissionsFromUser(java.lang.String[],int). This parameter cannot be null. 519 * @param grantResults Indicates the granting results of the corresponding permissions requested using 520 * AbilityContext.requestPermissionsFromUser(java.lang.String[],int). The value 0 indicates that a 521 * permission is granted, and the value -1 indicates not. 522 * 523 */ 524 virtual void OnRequestPermissionsFromUserResult( 525 int requestCode, const std::vector<std::string> &permissions, const std::vector<int> &grantResults); 526 527 #ifdef SUPPORT_GRAPHICS 528 /** 529 * @brief Called when this ability is about to leave the foreground and enter the background due to a user 530 * operation, for example, when the user touches the Home key. 531 * 532 */ 533 virtual void OnLeaveForeground(); 534 #endif 535 536 /** 537 * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be 538 * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG. 539 * 540 * @param uri Indicates the URI of the data. 541 * 542 * @return Returns the MIME type that matches the data specified by uri. 543 */ 544 virtual std::string GetType(const Uri &uri); 545 546 /** 547 * @brief Inserts a data record into the database. This method should be implemented by a Data ability. 548 * 549 * @param uri Indicates the position where the data is to insert. 550 * @param value Indicates the data to insert. 551 * 552 * @return Returns the index of the newly inserted data record. 553 */ 554 virtual int Insert(const Uri &uri, const NativeRdb::ValuesBucket &value); 555 556 virtual std::shared_ptr<AppExecFwk::PacMap> Call( 557 const Uri &uri, const std::string &method, const std::string &arg, const AppExecFwk::PacMap &pacMap); 558 559 /** 560 * @brief Called when the system configuration is updated. 561 * 562 * @param configuration Indicates the updated configuration information. 563 */ 564 virtual void OnConfigurationUpdated(const Configuration &configuration); 565 566 /** 567 * @brief Called when the system configuration is updated. 568 * 569 * @param configuration Indicates the updated configuration information. 570 */ 571 void OnConfigurationUpdatedNotify(const Configuration &configuration); 572 573 /** 574 * @brief Update context.config when configuration is updated. 575 * 576 */ UpdateContextConfiguration()577 virtual void UpdateContextConfiguration() {}; 578 579 /** 580 * @brief Called when the system configuration is updated. 581 * 582 * @param level Indicates the memory trim level, which shows the current memory usage status. 583 * 584 */ 585 virtual void OnMemoryLevel(int level); 586 587 /** 588 * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets 589 * inside of their .hap. 590 * 591 * @param uri Indicates the path of the file to open. 592 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access 593 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 594 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing 595 * data, or "rwt" for read and write access that truncates any existing file. 596 * 597 * @return Returns the RawFileDescriptor object containing file descriptor. 598 */ 599 virtual int OpenRawFile(const Uri &uri, const std::string &mode); 600 601 /** 602 * @brief Updates one or more data records in the database. This method should be implemented by a Data ability. 603 * 604 * @param uri Indicates the database table storing the data to update. 605 * @param value Indicates the data to update. This parameter can be null. 606 * @param predicates Indicates filter criteria. If this parameter is null, all data records will be updated by 607 * default. 608 * 609 * @return Returns the number of data records updated. 610 */ 611 virtual int Update( 612 const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates); 613 614 /** 615 * @brief get application witch the ability belong 616 * 617 * @return Returns the application ptr 618 */ 619 std::shared_ptr<OHOSApplication> GetApplication(); 620 621 /** 622 * @brief Obtains the class name in this ability name, without the prefixed bundle name. 623 * 624 * @return Returns the class name of this ability. 625 */ 626 std::string GetAbilityName(); 627 628 /** 629 * @brief OChecks whether the current ability is being destroyed. 630 * An ability is being destroyed if you called terminateAbility() on it or someone else requested to destroy it. 631 * 632 * @return Returns true if the current ability is being destroyed; returns false otherwise. 633 */ 634 bool IsTerminating(); 635 636 /** 637 * @brief Called when startAbilityForResult(ohos.aafwk.content.Want,int) is called to start an ability and the 638 * result is returned. This method is called only on Page abilities. You can start a new ability to perform some 639 * calculations and use setResult (int,ohos.aafwk.content.Want) to return the calculation result. Then the system 640 * calls back the current method to use the returned data to execute its own logic. 641 * 642 * @param requestCode Indicates the request code returned after the ability is started. You can define the request 643 * code to identify the results returned by abilities. The value ranges from 0 to 65535. 644 * @param resultCode Indicates the result code returned after the ability is started. You can define the result code 645 * to identify an error. 646 * @param resultData Indicates the data returned after the ability is started. You can define the data returned. The 647 * value can be null. 648 * 649 */ 650 virtual void OnAbilityResult(int requestCode, int resultCode, const Want &resultData); 651 652 virtual void OnFeatureAbilityResult(int requestCode, int resultCode, const Want &resultData); 653 654 /** 655 * @brief Called back when the Back key is pressed. 656 * The default implementation destroys the ability. You can override this method. 657 * 658 */ 659 virtual void OnBackPressed() override; 660 661 /** 662 * @brief Called when the launch mode of an ability is set to singleInstance. This happens when you re-launch an 663 * ability that has been at the top of the ability stack. 664 * 665 * @param want Indicates the new Want containing information about the ability. 666 */ 667 virtual void OnNewWant(const Want &want); 668 669 /** 670 * @brief Restores data and states of an ability when it is restored by the system. This method should be 671 * implemented by a Page ability. This method is called if an ability was destroyed at a certain time due to 672 * resource reclaim or was unexpectedly destroyed and the onSaveAbilityState(ohos.utils.PacMap) method was called to 673 * save its user data and states. Generally, this method is called after the onStart(ohos.aafwk.content.Want) 674 * method. 675 * 676 * @param inState Indicates the PacMap object used for storing data and states. This parameter can not be null. 677 * 678 */ 679 virtual void OnRestoreAbilityState(const PacMap &inState); 680 681 /** 682 * @brief Saves temporary data and states of this ability. This method should be implemented by a Page ability. 683 * This method is called when the system determines that the ability may be destroyed in an unexpected situation, 684 * for example, when the screen orientation changes or the user touches the Home key. Generally, this method is used 685 * only to save temporary states. 686 * 687 * @param outState Indicates the PacMap object used for storing user data and states. This parameter cannot be 688 * null. 689 * 690 */ 691 virtual void OnSaveAbilityState(PacMap &outState); 692 693 /** 694 * @brief Called every time a key, touch, or trackball event is dispatched to this ability. 695 * You can override this callback method if you want to know that the user has interacted with 696 * the device in a certain way while this ability is running. This method, together with onLeaveForeground(), 697 * is designed to help abilities intelligently manage status bar notifications. Specifically, they help 698 * abilities determine when to cancel a notification. 699 * 700 */ 701 virtual void OnEventDispatch(); 702 703 /** 704 * @brief Sets the want object that can be obtained by calling getWant(). 705 * 706 * @param Want information of other ability 707 */ 708 void SetWant(const AAFwk::Want &want); 709 710 /** 711 * @brief Obtains the Want object that starts this ability. 712 * 713 * @return Returns the Want object that starts this ability. 714 */ 715 std::shared_ptr<AAFwk::Want> GetWant(); 716 717 /** 718 * @brief Sets the result code and data to be returned by this Page ability to the caller. 719 * When a Page ability is destroyed, the caller overrides the AbilitySlice#onAbilityResult(int, int, Want) method to 720 * receive the result set in the current method. This method can be called only after the ability has been 721 * initialized. 722 * 723 * @param resultCode Indicates the result code returned after the ability is destroyed. You can define the result 724 * code to identify an error. 725 * @param resultData Indicates the data returned after the ability is destroyed. You can define the data returned. 726 * This parameter can be null. 727 */ 728 virtual void SetResult(int resultCode, const Want &resultData) final; 729 730 #ifdef SUPPORT_GRAPHICS 731 /** 732 * @brief Sets the type of audio whose volume will be adjusted by the volume button. 733 * 734 * @param volumeType Indicates the AudioManager.AudioVolumeType to set. 735 */ 736 virtual void SetVolumeTypeAdjustedByKey(int volumeType); 737 #endif 738 739 /** 740 * @brief Called back when Service is started. 741 * This method can be called only by Service. You can use the StartAbility(ohos.aafwk.content.Want) method to start 742 * Service. Then the system calls back the current method to use the transferred want parameter to execute its own 743 * logic. 744 * 745 * @param want Indicates the want of Service to start. 746 * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being 747 * destroyed, and the value false indicates a normal startup. 748 * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by 749 * 1 every time the ability is started. For example, if the ability has been started for six times, the value of 750 * startId is 6. 751 */ 752 virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId); 753 754 /** 755 * @brief dump ability info 756 * 757 * @param extra dump ability info 758 */ 759 virtual void Dump(const std::string &extra); 760 761 /** 762 * @brief dump ability info 763 * 764 * @param params dump params that indicate different dump targets 765 * @param info dump ability info 766 */ 767 virtual void Dump(const std::vector<std::string> ¶ms, std::vector<std::string> &info); 768 769 /** 770 * @brief Keeps this Service ability in the background and displays a notification bar. 771 * To use this method, you need to request the ohos.permission.KEEP_BACKGROUND_RUNNING permission from the system. 772 * The ohos.permission.KEEP_BACKGROUND_RUNNING permission is of the normal level. 773 * This method can be called only by Service abilities after the onStart(ohos.aafwk.content.Want) method is called. 774 * 775 * @param id Identifies the notification bar information. 776 * @param notificationRequest Indicates the NotificationRequest instance containing information for displaying a 777 * notification bar. 778 */ 779 virtual void KeepBackgroundRunning(int id, const NotificationRequest ¬ificationRequest) final; 780 781 /** 782 * @brief Cancels background running of this ability to free up system memory. 783 * This method can be called only by Service abilities when the onStop() method is called. 784 * 785 */ 786 virtual void CancelBackgroundRunning() final; 787 788 /** 789 * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used 790 * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the 791 * context has changed. If you implement URI normalization for a Data ability, you must also implement 792 * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to 793 * any method that is called on the Data ability must require normalization verification and denormalization. The 794 * default implementation of this method returns null, indicating that this Data ability does not support URI 795 * normalization. 796 * 797 * @param uri Indicates the Uri object to normalize. 798 * 799 * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise. 800 */ 801 virtual Uri NormalizeUri(const Uri &uri); 802 803 /** 804 * @brief Deletes one or more data records. This method should be implemented by a Data ability. 805 * 806 * @param uri Indicates the database table storing the data to delete. 807 * @param predicates Indicates filter criteria. If this parameter is null, all data records will be deleted by 808 * default. 809 * 810 * @return Returns the number of data records deleted. 811 */ 812 virtual int Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates); 813 814 /** 815 * @brief Obtains the MIME type of files. This method should be implemented by a Data ability. 816 * 817 * @param uri Indicates the path of the files to obtain. 818 * @param mimeTypeFilter Indicates the MIME type of the files to obtain. This parameter cannot be set to null. 819 * 1. * / *: Obtains all types supported by a Data ability. 820 * 2. image/ *: Obtains files whose main type is image of any subtype. 821 * 3. * /jpg: Obtains files whose subtype is JPG of any main type. 822 * 823 * @return Returns the MIME type of the matched files; returns null if there is no type that matches the Data 824 * ability. 825 */ 826 virtual std::vector<std::string> GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter); 827 828 /** 829 * @brief Opens a file. This method should be implemented by a Data ability. 830 * 831 * @param uri Indicates the path of the file to open. 832 * @param mode Indicates the open mode, which can be "r" for read-only access, "w" for write-only access 833 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 834 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data, 835 * or "rwt" for read and write access that truncates any existing file. 836 * 837 * @return Returns the FileDescriptor object of the file descriptor. 838 */ 839 virtual int OpenFile(const Uri &uri, const std::string &mode); 840 841 /** 842 * @brief Queries one or more data records in the database. This method should be implemented by a Data ability. 843 * 844 * @param uri Indicates the database table storing the data to query. 845 * @param columns Indicates the columns to be queried, in array, for example, {"name","age"}. You should define the 846 * processing logic when this parameter is null. 847 * @param predicates Indicates filter criteria. If this parameter is null, all data records will be queried by 848 * default. 849 * 850 * @return Returns the queried data. 851 */ 852 virtual std::shared_ptr<NativeRdb::AbsSharedResultSet> Query( 853 const Uri &uri, const std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates); 854 855 /** 856 * @brief Sets the main route for this ability. 857 * 858 * The main route, also called main entry, refers to the default <b>AbilitySlice</b> to present for this ability. 859 * This function should be called only on Feature Abilities. If this function is not called in the 860 * {@link OnStart(const Want &want)} function for a Feature Ability, the Feature Ability will fail to start. 861 * 862 * @param entry Indicates the main entry, which is the class name of the <b>AbilitySlice</b> instance to start. 863 * 864 * @return Returns the result of SetMainRoute 865 */ 866 virtual void SetMainRoute(const std::string &entry) final; 867 868 /** 869 * @brief Migrates this ability to the given device on the same distributed network in a reversible way that allows 870 * this ability to be migrated back to the local device through reverseContinueAbility(). The ability to migrate and 871 * its ability slices must implement the IAbilityContinuation interface. Otherwise, an exception is thrown, 872 * indicating that the ability does not support migration. 873 * 874 * @param deviceId Indicates the ID of the target device where this ability will be migrated to. If this parameter 875 * is null, this method has the same effect as ContinueAbilityReversibly(). 876 * 877 */ 878 virtual void ContinueAbilityReversibly(const std::string &deviceId) final; 879 880 /** 881 * @brief Obtains the ID of the source device from which this ability is migrated. 882 * 883 * @return Returns the source device ID. 884 */ 885 virtual std::string GetOriginalDeviceId() final; 886 887 /** 888 * @brief Obtains the migration state of this ability. 889 * @return Returns the migration state. 890 */ 891 virtual ContinuationState GetContinuationState() final; 892 893 /** 894 * @brief Obtains the singleton AbilityPackage object to which this ability belongs. 895 * 896 * @return Returns the singleton AbilityPackage object to which this ability belongs. 897 */ 898 virtual std::shared_ptr<AbilityPackage> GetAbilityPackage(); 899 900 /** 901 * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one. 902 * The default implementation of this method returns the original URI passed to it. 903 * 904 * @param uri uri Indicates the Uri object to denormalize. 905 * 906 * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed 907 * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found 908 * in the current environment. 909 */ 910 virtual Uri DenormalizeUri(const Uri &uri); 911 912 /** 913 * @brief Reloads data in the database. 914 * 915 * @param uri Indicates the position where the data is to reload. This parameter is mandatory. 916 * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This 917 * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across 918 * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object. 919 * 920 * @return Returns true if the data is successfully reloaded; returns false otherwise. 921 */ 922 virtual bool Reload(const Uri &uri, const PacMap &extras); 923 924 /** 925 * @brief Inserts multiple data records into the database. 926 * 927 * @param uri Indicates the path of the data to operate. 928 * @param values Indicates the data records to insert. 929 * 930 * @return Returns the number of data records inserted. 931 */ 932 virtual int BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values); 933 934 #ifdef SUPPORT_GRAPHICS 935 /** 936 * @brief Obtains the type of audio whose volume is adjusted by the volume button. 937 * 938 * @return Returns the AudioManager.AudioVolumeType. 939 */ 940 int GetVolumeTypeAdjustedByKey(); 941 #endif 942 943 /** 944 * @brief Obtains the lifecycle state of this ability. 945 * 946 * @return Returns the lifecycle state of this ability. 947 */ 948 virtual AbilityLifecycleExecutor::LifecycleState GetState() final; 949 950 /** 951 * @brief A Page or Service ability uses this method to start a specific ability. The system locates the target 952 * ability from installed abilities based on the value of the want parameter and then starts it. You can specify 953 * the ability to start using the want parameter. 954 * 955 * @param want Indicates the ability to start. 956 * 957 * @return errCode ERR_OK on success, others on failure. 958 */ 959 using AbilityContext::StartAbility; 960 virtual ErrCode StartAbility(const Want &want) final; 961 962 /** 963 * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. 964 * 965 * @param want Indicates the want containing information about the ability to connect 966 * 967 * @param conn Indicates the callback object when the target ability is connected. 968 * 969 * @return True means success and false means failure 970 */ 971 bool ConnectAbility(const Want &want, const sptr<AAFwk::IAbilityConnection> &conn) override; 972 973 /** 974 * @brief Disconnects the current ability from an ability 975 * 976 * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection 977 * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities. 978 * 979 * @return errCode ERR_OK on success, others on failure. 980 */ 981 ErrCode DisconnectAbility(const sptr<AAFwk::IAbilityConnection> &conn) override; 982 983 /** 984 * @brief Destroys another ability that uses the AbilityInfo.AbilityType.SERVICE template. 985 * The current ability using either the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE 986 * template can call this method to destroy another ability that uses the AbilityInfo.AbilityType.SERVICE 987 * template. The current ability itself can be destroyed by calling the terminateAbility() method. 988 * 989 * @param want Indicates the Want containing information about the ability to destroy. 990 * 991 * @return Returns true if the ability is destroyed successfully; returns false otherwise. 992 */ 993 bool StopAbility(const AAFwk::Want &want) override; 994 995 /** 996 * @brief Release the ability instance. 997 */ 998 void DestroyInstance(); 999 1000 /** 1001 * @brief Posts a scheduled Runnable task to a new non-UI thread. 1002 * The task posted via this method will be executed in a new thread, which allows you to perform certain 1003 * time-consuming operations. To use this method, you must also override the supportHighPerformanceUI() method. 1004 * Additionally, the usage of this method must comply with the following constraints: 1、This method can only be 1005 * used to initialize the component tree in parallel mode. 2、The task can only be processed during system cold 1006 * start. 3、If the parallel loading mechanism is used, the component tree-related operations to be performed in 1007 * onActive() and onStop() of the ability slice must also be added to the parallel thread. 4、You must run 1008 * setUIContent(ohos.agp.components.ComponentContainer) to unlock the UI thread and wait until the component tree is 1009 * ready. 5、Other time-consuming operations, such as I/O and network processing, cannot be added to the parallel 1010 * task queue. 1011 * 1012 * @param task Indicates the Runnable task to post. 1013 * 1014 * @param delayTime Indicates the number of milliseconds after which the task will be executed. 1015 * 1016 * @return - 1017 */ 1018 void PostTask(std::function<void()> task, long delayTime); 1019 1020 /** 1021 * @brief Called to set caller information for the application. The default implementation returns null. 1022 * 1023 * @return Returns the caller information. 1024 */ 1025 virtual Uri OnSetCaller(); 1026 1027 /** 1028 * @brief Call this when your ability should be closed and the mission should be completely removed as a part of 1029 * finishing the root ability of the mission. 1030 */ 1031 void TerminateAndRemoveMission() override; 1032 1033 /** 1034 * @brief Create a PostEvent timeout task. The default delay is 5000ms 1035 * 1036 * @return Return a smart pointer to a timeout object 1037 */ 1038 std::shared_ptr<AbilityPostEventTimeout> CreatePostEventTimeouter(std::string taskstr); 1039 1040 #ifdef SUPPORT_GRAPHICS 1041 /** 1042 * Releases an obtained form by its ID. 1043 * 1044 * <p>After this method is called, the form won't be available for use by the application, but the Form Manager 1045 * Service still keeps the cache information about the form, so that the application can quickly obtain it based on 1046 * the {@code formId}.</p> 1047 * <p><b>Permission: </b>{@link ohos.security.SystemPermission#REQUIRE_FORM}</p> 1048 * 1049 * @param formId Indicates the form ID. 1050 * @return Returns {@code true} if the form is successfully released; returns {@code false} otherwise. 1051 * 1052 * <ul> 1053 * <li>The passed {@code formId} is invalid. Its value must be larger than 0.</li> 1054 * <li>The specified form has not been added by the application.</li> 1055 * <li>An error occurred when connecting to the Form Manager Service.</li> 1056 * <li>The application is not granted with the {@link ohos.security.SystemPermission#REQUIRE_FORM} permission.</li> 1057 * <li>The form has been obtained by another application and cannot be released by the current application.</li> 1058 * <li>The form is being restored.</li> 1059 * </ul> 1060 */ 1061 ErrCode ReleaseForm(const int64_t formId); 1062 1063 /** 1064 * @brief Releases an obtained form by its ID. 1065 * 1066 * <p>After this method is called, the form won't be available for use by the application, if isReleaseCache is 1067 * false, this method is same as {@link #releaseForm(int)}, otherwise the Form Manager Service still store this 1068 * form in the cache.</p> 1069 * <p><b>Permission: </b>{@link ohos.security.SystemPermission#REQUIRE_FORM}</p> 1070 * 1071 * @param formId Indicates the form ID. 1072 * @param isReleaseCache Indicates whether to clear cache in service. 1073 * @return Returns {@code true} if the form is successfully released; returns {@code false} otherwise. 1074 * 1075 * <ul> 1076 * <li>The passed {@code formId} is invalid. Its value must be larger than 0.</li> 1077 * <li>The specified form has not been added by the application.</li> 1078 * <li>An error occurred when connecting to the Form Manager Service.</li> 1079 * <li>The application is not granted with the {@link ohos.security.SystemPermission#REQUIRE_FORM} permission.</li> 1080 * <li>The form has been obtained by another application and cannot be released by the current application.</li> 1081 * <li>The form is being restored.</li> 1082 * </ul> 1083 */ 1084 ErrCode ReleaseForm(const int64_t formId, const bool isReleaseCache); 1085 1086 /** 1087 * @brief Deletes an obtained form by its ID. 1088 * 1089 * <p>After this method is called, the form won't be available for use by the application and the Form Manager 1090 * Service no longer keeps the cache information about the form.</p> 1091 * <p><b>Permission: </b>{@link ohos.security.SystemPermission#REQUIRE_FORM}</p> 1092 * 1093 * @param formId Indicates the form ID. 1094 * @return Returns {@code true} if the form is successfully deleted; returns {@code false} otherwise. 1095 * 1096 * <ul> 1097 * <li>The passed {@code formId} is invalid. Its value must be larger than 0.</li> 1098 * <li>The specified form has not been added by the application.</li> 1099 * <li>An error occurred when connecting to the Form Manager Service.</li> 1100 * <li>The application is not granted with the {@link ohos.security.SystemPermission#REQUIRE_FORM} permission.</li> 1101 * <li>The form has been obtained by another application and cannot be deleted by the current application.</li> 1102 * <li>The form is being restored.</li> 1103 * </ul> 1104 */ 1105 ErrCode DeleteForm(const int64_t formId); 1106 #endif 1107 1108 /** 1109 * @brief Keep this Service ability in the background and displays a notification bar. 1110 * 1111 * @param wantAgent Indicates which ability to start when user click the notification bar. 1112 * @return the method result code, 0 means succeed 1113 */ 1114 virtual int StartBackgroundRunning(const AbilityRuntime::WantAgent::WantAgent &wantAgent) final; 1115 1116 /** 1117 * @brief Cancel background running of this ability to free up system memory. 1118 * 1119 * @return the method result code, 0 means succeed 1120 */ 1121 virtual int StopBackgroundRunning() final; 1122 1123 #ifdef SUPPORT_GRAPHICS 1124 /** 1125 * @brief The form callback. 1126 */ 1127 class FormCallback { 1128 public: 1129 static const int32_t OHOS_FORM_ACQUIRE_SUCCESS = 0; 1130 static const int32_t OHOS_FORM_UPDATE_SUCCESS = 0; 1131 static const int32_t OHOS_FORM_PREVIEW_FAILURE = 1; 1132 static const int32_t OHOS_FORM_RESTORE_FAILURE = 2; 1133 1134 /** 1135 * @brief Called to notify the application that the {@code FormJsInfo} instance has been obtained after 1136 * the application called the asynchronous method {@link Ability#acquireForm(Want, FormCallback)}. 1137 * The application must present the form information on a specific page in this callback. 1138 * 1139 * @param result Specifies whether the asynchronous form acquisition process is successful. 1140 * {@link FormCallback#OHOS_FORM_ACQUIRE_SUCCESS} indicates that the form 1141 * is successfully obtained, and other values indicate that the process fails. 1142 * @param formJsInfo Indicates the obtained {@code FormJsInfo} instance. 1143 */ 1144 virtual void OnAcquired(const int32_t result, const FormJsInfo &formJsInfo) const = 0; 1145 1146 /** 1147 * @brief Called to notify the application that the {@code FormJsInfo} instance has been obtained after 1148 * the application called the asynchronous method {@link Ability#acquireForm(Want, FormCallback)}. 1149 * The application must present the form information on a specific page in this callback. 1150 * 1151 * @param result Specifies whether the asynchronous form acquisition process is successful. 1152 * {@link FormCallback#OHOS_FORM_UPDATE_SUCCESS} indicates that the form is 1153 * successfully obtained, and other values indicate that the process fails. 1154 * @param formJsInfo Indicates the obtained {@code FormJsInfo} instance. 1155 */ 1156 virtual void OnUpdate(const int32_t result, const FormJsInfo &formJsInfo) const = 0; 1157 1158 /** 1159 * @brief Called to notify the application that the {@code Form} provider has been uninstalled and the 1160 * corresponding 1161 * {@code Form} instance is no longer available. 1162 * 1163 * @param formId Indicates the ID of the {@code Form} instance provided by the uninstalled form provider. 1164 */ 1165 virtual void OnFormUninstall(const int64_t formId) const = 0; 1166 }; 1167 1168 /** 1169 * @brief Obtains a specified form that matches the application bundle name, module name, form name, and 1170 * other related information specified in the passed {@code Want}. 1171 * 1172 * <p>This method is asynchronous. After the {@link FormJsInfo} instance is obtained. 1173 * 1174 * @param formId Indicates the form ID. 1175 * @param want Indicates the detailed information about the form to be obtained, including the bundle name, 1176 * module name, ability name, form name, form id, tempForm flag, form dimension, and form customize data. 1177 * @param callback Indicates the callback to be invoked whenever the {@link FormJsInfo} instance is obtained. 1178 * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. 1179 */ 1180 bool AcquireForm(const int64_t formId, const Want &want, const std::shared_ptr<FormCallback> callback); 1181 1182 /** 1183 * @brief Updates the content of a specified JS form. 1184 * 1185 * <p>This method is called by a form provider to update JS form data as needed. 1186 * 1187 * @param formId Indicates the form ID. 1188 * @param formProviderData The data used to update the JS form displayed on the client. 1189 * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. 1190 */ 1191 ErrCode UpdateForm(const int64_t formId, const FormProviderData &formProviderData); 1192 1193 /** 1194 * @brief Cast temp form with formId. 1195 * 1196 * @param formId Indicates the form's ID. 1197 * 1198 * @return Returns {@code true} if the form is successfully casted; returns {@code false} otherwise. 1199 */ 1200 ErrCode CastTempForm(const int64_t formId); 1201 1202 /** 1203 * @brief Sends a notification to the form framework to make the specified forms visible. 1204 * 1205 * <p>After this method is successfully called, {@link Ability#OnVisibilityChanged(std::map<int64_t, int>)} 1206 * will be called to notify the form provider of the form visibility change event.</p> 1207 * 1208 * @param formIds Indicates the IDs of the forms to be made visible. 1209 * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. 1210 */ 1211 ErrCode NotifyVisibleForms(const std::vector<int64_t> &formIds); 1212 1213 /** 1214 * @brief Sends a notification to the form framework to make the specified forms invisible. 1215 * 1216 * <p>After this method is successfully called, {@link Ability#OnVisibilityChanged(std::map<int64_t, int>)} 1217 * will be called to notify the form provider of the form visibility change event.</p> 1218 * 1219 * @param formIds Indicates the IDs of the forms to be made invisible. 1220 * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. 1221 */ 1222 ErrCode NotifyInvisibleForms(const std::vector<int64_t> &formIds); 1223 1224 /** 1225 * @brief Set form next refresh time. 1226 * 1227 * <p>This method is called by a form provider to set refresh time. 1228 * 1229 * @param formId Indicates the ID of the form to set refresh time. 1230 * @param nextTime Indicates the next time gap now in seconds, can not be litter than 300 seconds. 1231 * @return Returns {@code true} if seting succeed; returns {@code false} otherwise. 1232 */ 1233 1234 ErrCode SetFormNextRefreshTime(const int64_t formId, const int64_t nextTime); 1235 1236 /** 1237 * @brief Update form. 1238 * 1239 * @param formJsInfo Indicates the obtained {@code FormJsInfo} instance. 1240 */ 1241 void ProcessFormUpdate(const FormJsInfo &formJsInfo) override; 1242 1243 /** 1244 * @brief Uninstall form. 1245 * 1246 * @param formId Indicates the ID of the form to uninstall. 1247 */ 1248 void ProcessFormUninstall(const int64_t formId) override; 1249 1250 /** 1251 * @brief Called to reacquire form and update the form host after the death callback is received. 1252 * 1253 */ 1254 void OnDeathReceived() override; 1255 1256 /** 1257 * @brief Called to return a FormProviderInfo object. 1258 * 1259 * <p>You must override this method if your ability will serve as a form provider to provide a form for clients. 1260 * The default implementation returns nullptr. </p> 1261 * 1262 * @param want Indicates the detailed information for creating a FormProviderInfo. 1263 * The Want object must include the form ID, form name of the form, 1264 * which can be obtained from Ability#PARAM_FORM_IDENTITY_KEY, 1265 * Ability#PARAM_FORM_NAME_KEY, and Ability#PARAM_FORM_DIMENSION_KEY, 1266 * respectively. Such form information must be managed as persistent data for further form 1267 * acquisition, update, and deletion. 1268 * 1269 * @return Returns the created FormProviderInfo object. 1270 */ 1271 virtual FormProviderInfo OnCreate(const Want &want); 1272 1273 /** 1274 * @brief Called to notify the form provider that a specified form has been deleted. Override this method if 1275 * you want your application, as the form provider, to be notified of form deletion. 1276 * 1277 * @param formId Indicates the ID of the deleted form. 1278 * @return None. 1279 */ 1280 virtual void OnDelete(const int64_t formId); 1281 1282 /** 1283 * @brief Called when the form provider is notified that a temporary form is successfully converted to 1284 * a normal form. 1285 * 1286 * @param formId Indicates the ID of the form. 1287 * @return None. 1288 */ 1289 virtual void OnCastTemptoNormal(const int64_t formId); 1290 1291 /** 1292 * @brief Called to notify the form provider to update a specified form. 1293 * 1294 * @param formId Indicates the ID of the form to update. 1295 * @return none. 1296 */ 1297 virtual void OnUpdate(const int64_t formId); 1298 1299 /** 1300 * @brief Called when the form provider receives form events from the fms. 1301 * 1302 * @param formEventsMap Indicates the form events occurred. The key in the Map object indicates the form ID, 1303 * and the value indicates the event type, which can be either FORM_VISIBLE 1304 * or FORM_INVISIBLE. FORM_VISIBLE means that the form becomes visible, 1305 * and FORM_INVISIBLE means that the form becomes invisible. 1306 * @return none. 1307 */ 1308 virtual void OnVisibilityChanged(const std::map<int64_t, int32_t> &formEventsMap); 1309 /** 1310 * @brief Called to notify the form provider to update a specified form. 1311 * 1312 * @param formId Indicates the ID of the form to update. 1313 * @param message Form event message. 1314 */ 1315 virtual void OnTriggerEvent(const int64_t formId, const std::string &message); 1316 /** 1317 * @brief Called to notify the form supplier to acquire form state. 1318 * 1319 * @param want Indicates the detailed information about the form to be obtained, including 1320 * the bundle name, module name, ability name, form name and form dimension. 1321 */ 1322 virtual FormState OnAcquireFormState(const Want &want); 1323 /** 1324 * @brief Requests for form data update. 1325 * 1326 * This method must be called when the application has detected that a system setting item (such as the language, 1327 * resolution, or screen orientation) being listened for has changed. Upon receiving the update request, the form 1328 * provider automatically updates the form data (if there is any update) through the form framework, with the update 1329 * process being unperceivable by the application. 1330 * 1331 * @param formId Indicates the ID of the form to update. 1332 * @return Returns true if the update request is successfully initiated, returns false otherwise. 1333 */ 1334 ErrCode RequestForm(const int64_t formId); 1335 1336 /** 1337 * @brief Requests for form data update, by passing a set of parameters (using Want) to the form provider. 1338 * 1339 * This method must be called when the application has detected that a system setting item (such as the language, 1340 * resolution, or screen orientation) being listened for has changed. Upon receiving the update request, the form 1341 * provider automatically updates the form data (if there is any update) through the form framework, with the update 1342 * process being unperceivable by the application. 1343 * 1344 * @param formId Indicates the ID of the form to update. 1345 * @param want Indicates a set of parameters to be transparently passed to the form provider. 1346 * @return Returns true if the update request is successfully initiated, returns false otherwise. 1347 */ 1348 ErrCode RequestForm(const int64_t formId, const Want &want); 1349 /** 1350 * @brief Enable form update. 1351 * 1352 * @param formIds formIds of hostclient. 1353 */ 1354 ErrCode EnableUpdateForm(const std::vector<int64_t> &formIds); 1355 1356 /** 1357 * @brief Disable form update. 1358 * 1359 * @param formIds formIds of hostclient. 1360 */ 1361 ErrCode DisableUpdateForm(const std::vector<int64_t> &formIds); 1362 1363 /** 1364 * @brief Check form manager service ready. 1365 * 1366 * @return Returns true if form manager service ready; returns false otherwise. 1367 */ 1368 bool CheckFMSReady(); 1369 1370 /** 1371 * @brief Delete the invalid forms. 1372 * 1373 * @param formIds Indicates the ID of the valid forms. 1374 * @param numFormsDeleted Returns the number of the deleted forms. 1375 * @return Returns true if the request is successfully initiated; returns false otherwise. 1376 */ 1377 ErrCode DeleteInvalidForms(const std::vector<int64_t> &formIds, int32_t &numFormsDeleted); 1378 1379 /** 1380 * @brief Acquire form state info by passing a set of parameters (using Want) to the form provider. 1381 * 1382 * @param want Indicates a set of parameters to be transparently passed to the form provider. 1383 * @param stateInfo Returns the form's state info of the specify. 1384 * @return Returns true if the request is successfully initiated; returns false otherwise. 1385 */ 1386 ErrCode AcquireFormState(const Want &want, FormStateInfo &stateInfo); 1387 1388 /** 1389 * @brief Notify the forms is visible to FMS. 1390 * 1391 * @param formIds Indicates the ID of the forms. 1392 * @param isVisible Visible or not. 1393 * @return Returns true if the request is successfully initiated; returns false otherwise. 1394 */ 1395 ErrCode NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible); 1396 1397 /** 1398 * @brief Notify the forms is enable update to FMS. 1399 * 1400 * @param formIds Indicates the ID of the forms. 1401 * @param isEnableUpdate enable update or not. 1402 * @return Returns true if the request is successfully initiated; returns false otherwise. 1403 */ 1404 ErrCode NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds, bool isEnableUpdate); 1405 1406 /** 1407 * @brief Get All FormsInfo. 1408 * 1409 * @param formInfos Returns the forms' information of all forms provided. 1410 * @return Returns true if the request is successfully initiated; returns false otherwise. 1411 */ 1412 ErrCode GetAllFormsInfo(std::vector<FormInfo> &formInfos); 1413 1414 /** 1415 * @brief Get forms info by application name. 1416 * 1417 * @param bundleName Application name. 1418 * @param formInfos Returns the forms' information of the specify application name. 1419 * @return Returns true if the request is successfully initiated; returns false otherwise. 1420 */ 1421 ErrCode GetFormsInfoByApp(std::string &bundleName, std::vector<FormInfo> &formInfos); 1422 1423 /** 1424 * @brief Get forms info by application name and module name. 1425 * 1426 * @param bundleName Application name. 1427 * @param moduleName Module name of hap. 1428 * @param formInfos Returns the forms' information of the specify application name and module name. 1429 * @return Returns true if the request is successfully initiated; returns false otherwise. 1430 */ 1431 ErrCode GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector<FormInfo> &formInfos); 1432 #endif 1433 /** 1434 * @brief Get the error message by error code. 1435 * @param errorCode the error code return form fms. 1436 * @return Returns the error message detail. 1437 */ 1438 std::string GetErrorMsg(const ErrCode errorCode); 1439 1440 /** 1441 * @brief Acquire a bundle manager, if it not existed, 1442 * @return returns the bundle manager ipc object, or nullptr for failed. 1443 */ 1444 sptr<IBundleMgr> GetBundleMgr(); 1445 1446 /** 1447 * @brief Add the bundle manager instance for debug. 1448 * @param bundleManager the bundle manager ipc object. 1449 */ 1450 void SetBundleManager(const sptr<IBundleMgr> &bundleManager); 1451 1452 /** 1453 * @brief You can use the IContinuationRegisterManager object to interact with the Device+ control center, 1454 * including registering and unregistering the ability to migrate, updating the device connection state, and 1455 * showing the list of devices that can be selected for ability migration. 1456 * 1457 * @return Returns true if the migration request is successful; returns false otherwise. 1458 */ 1459 std::weak_ptr<IContinuationRegisterManager> GetContinuationRegisterManager(); 1460 1461 /** 1462 * @brief Prepare user data of local Ability. 1463 * 1464 * @param wantParams Indicates the user data to be saved. 1465 * @return If the ability is willing to continue and data saved successfully, it returns 0; 1466 * otherwise, it returns errcode. 1467 */ 1468 virtual int32_t OnContinue(WantParams &wantParams); 1469 1470 #ifdef SUPPORT_GRAPHICS 1471 /** 1472 * @brief Get page ability stack info. 1473 * 1474 * @return A string represents page ability stack info, empty if failed; 1475 */ 1476 virtual std::string GetContentInfo(); 1477 #endif 1478 1479 /** 1480 * @brief Migrates this ability to the given device on the same distributed network. The ability to migrate and its 1481 * ability slices must implement the IAbilityContinuation interface. 1482 * 1483 * @param deviceId Indicates the ID of the target device where this ability will be migrated to. 1484 * 1485 */ 1486 virtual void ContinueAbilityWithStack(const std::string &deviceId) final; 1487 1488 /** 1489 * @brief Migrates this ability to the given device on the same distributed network. The ability to migrate and its 1490 * ability slices must implement the IAbilityContinuation interface. 1491 * 1492 * @param deviceId Indicates the ID of the target device where this ability will be migrated to. If this parameter 1493 * is null, this method has the same effect as continueAbility(). 1494 * 1495 */ 1496 virtual void ContinueAbility(const std::string &deviceId) final; 1497 1498 /** 1499 * @brief Callback function to ask the user whether to start the migration . 1500 * 1501 * @return If the user allows migration, it returns true; otherwise, it returns false. 1502 */ 1503 virtual bool OnStartContinuation() override; 1504 1505 /** 1506 * @brief Performs batch operations on the database. 1507 * 1508 * @param operations Indicates a list of database operations on the database. 1509 * @return Returns the result of each operation, in array. 1510 */ 1511 virtual std::vector<std::shared_ptr<DataAbilityResult>> ExecuteBatch( 1512 const std::vector<std::shared_ptr<DataAbilityOperation>> &operations); 1513 1514 /** 1515 * @brief Executes an operation among the batch operations to be executed. 1516 * 1517 * @param operation Indicates the operation to execute. 1518 * @param results Indicates a set of results of the batch operations. 1519 * @param index Indicates the index of the current operation result in the batch operation results. 1520 */ 1521 void ExecuteOperation(std::shared_ptr<DataAbilityOperation> &operation, 1522 std::vector<std::shared_ptr<DataAbilityResult>> &results, int index); 1523 1524 /** 1525 * @brief Save user data of local Ability generated at runtime. 1526 * 1527 * @param saveData Indicates the user data to be saved. 1528 * @return If the data is saved successfully, it returns true; otherwise, it returns false. 1529 */ 1530 virtual bool OnSaveData(WantParams &saveData) override; 1531 1532 /** 1533 * @brief After creating the Ability on the remote device, 1534 * immediately restore the user data saved during the migration of the Ability on the remote device. 1535 * @param restoreData Indicates the user data to be restored. 1536 * @return If the data is restored successfully, it returns true; otherwise, it returns false . 1537 */ 1538 virtual bool OnRestoreData(WantParams &restoreData) override; 1539 1540 /** 1541 * @brief This function can be used to implement the processing logic after the migration is completed. 1542 * 1543 * @param result Migration result code. 0 means the migration was successful, -1 means the migration failed. 1544 * @return None. 1545 */ 1546 virtual void OnCompleteContinuation(int result) override; 1547 1548 /** 1549 * @brief Used to notify the local Ability that the remote Ability has been destroyed. 1550 * 1551 * @return None. 1552 */ 1553 virtual void OnRemoteTerminated() override; 1554 1555 #ifdef SUPPORT_GRAPHICS 1556 /** 1557 * @brief Set WindowScene listener 1558 * 1559 * @param listener WindowScene listener 1560 * @return None. 1561 */ 1562 void SetSceneListener(const sptr<Rosen::IWindowLifeCycle> &listener); 1563 #endif 1564 1565 /** 1566 * @brief request a remote object of callee from this ability. 1567 * @return Returns the remote object of callee. 1568 */ 1569 virtual sptr<IRemoteObject> CallRequest(); 1570 1571 #ifdef SUPPORT_GRAPHICS 1572 /** 1573 * @brief Called back at ability context. 1574 */ 1575 virtual int GetCurrentWindowMode() override; 1576 1577 uint32_t sceneFlag_ = 0; 1578 #endif 1579 protected: 1580 #ifdef SUPPORT_GRAPHICS 1581 /** 1582 * @brief Acquire a form provider remote object. 1583 * @return Returns form provider remote object. 1584 */ 1585 sptr<IRemoteObject> GetFormRemoteObject(); 1586 #endif 1587 1588 /** 1589 * @brief Acquire the launch parameter. 1590 * @return launch parameter. 1591 */ 1592 const AAFwk::LaunchParam& GetLaunchParam() const; 1593 1594 #ifdef SUPPORT_GRAPHICS 1595 /** 1596 * @brief process when foreground executed. 1597 * 1598 * You can override this function to implement your own processing logic 1599 */ 1600 virtual void DoOnForeground(const Want& want); 1601 1602 /** 1603 * @brief requeset focus for current window. 1604 * 1605 * You can override this function to implement your own processing logic 1606 */ 1607 virtual void RequsetFocus(const Want &want); 1608 1609 /** 1610 * @brief Acquire the window option. 1611 * @return window option. 1612 */ 1613 sptr<Rosen::WindowOption> GetWindowOption(const Want &want); 1614 #endif 1615 1616 /** 1617 * @brief judge where invoke resoreWindowStage in continuation 1618 * @return true if invoked resoreWindowStage in continuation. 1619 */ 1620 bool IsRestoredInContinuation() const; 1621 1622 /** 1623 * @brief wait for distributed object to complete sync 1624 * 1625 * @param want the want param. 1626 */ 1627 void WaitingDistributedObjectSyncComplete(const Want& want); 1628 1629 /** 1630 * @brief Notify continuation 1631 * 1632 * @param want the want param. 1633 * @param success whether continuation success. 1634 */ 1635 void NotityContinuationResult(const Want& want, bool success); 1636 1637 #ifdef SUPPORT_GRAPHICS 1638 /** 1639 * @brief override Rosen::DisplayManager::IDisplayListener virtual callback function 1640 * 1641 * @param displayId displayId 1642 */ 1643 void OnCreate(Rosen::DisplayId displayId) override; 1644 void OnDestroy(Rosen::DisplayId displayId) override; 1645 void OnChange(Rosen::DisplayId displayId) override; 1646 1647 /** 1648 * @brief override Rosen::IDisplayMoveListener virtual callback function 1649 * 1650 * @param from the displayId before display move 1651 * @param to the displayId after display move 1652 */ 1653 void OnDisplayMove(Rosen::DisplayId from, Rosen::DisplayId to) override; 1654 #endif 1655 protected: 1656 std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext_ = nullptr; 1657 std::shared_ptr<AbilityStartSetting> setting_ = nullptr; 1658 LaunchParam launchParam_; 1659 #ifdef SUPPORT_GRAPHICS 1660 std::shared_ptr<Rosen::WindowScene> scene_ = nullptr; 1661 sptr<Rosen::IWindowLifeCycle> sceneListener_ = nullptr; 1662 #endif 1663 private: 1664 std::shared_ptr<NativeRdb::DataAbilityPredicates> ParsePredictionArgsReference( 1665 std::vector<std::shared_ptr<DataAbilityResult>> &results, std::shared_ptr<DataAbilityOperation> &operation, 1666 int numRefs); 1667 1668 std::shared_ptr<NativeRdb::ValuesBucket> ParseValuesBucketReference( 1669 std::vector<std::shared_ptr<DataAbilityResult>> &results, std::shared_ptr<DataAbilityOperation> &operation, 1670 int numRefs); 1671 1672 int ChangeRef2Value(std::vector<std::shared_ptr<DataAbilityResult>> &results, int numRefs, int index); 1673 1674 bool CheckAssertQueryResult(std::shared_ptr<NativeRdb::AbsSharedResultSet> &queryResult, 1675 std::shared_ptr<NativeRdb::ValuesBucket> &&valuesBucket); 1676 1677 void DispatchLifecycleOnForeground(const Want &want); 1678 1679 friend class AbilityImpl; 1680 bool VerifySupportForContinuation(); 1681 void HandleCreateAsContinuation(const Want &want); 1682 bool IsFlagExists(unsigned int flag, unsigned int flagSet); 1683 /** 1684 * @brief Set the start ability setting. 1685 * @param setting the start ability setting. 1686 */ 1687 void SetStartAbilitySetting(std::shared_ptr<AbilityStartSetting> setting); 1688 1689 /** 1690 * @brief Set the launch param. 1691 * 1692 * @param launchParam the launch param. 1693 */ 1694 void SetLaunchParam(const AAFwk::LaunchParam &launchParam); 1695 1696 private: 1697 std::shared_ptr<ContinuationHandler> continuationHandler_ = nullptr; 1698 std::shared_ptr<ContinuationManager> continuationManager_ = nullptr; 1699 std::shared_ptr<ContinuationRegisterManager> continuationRegisterManager_ = nullptr; 1700 std::shared_ptr<AbilityInfo> abilityInfo_ = nullptr; 1701 std::shared_ptr<AbilityHandler> handler_ = nullptr; 1702 std::shared_ptr<LifeCycle> lifecycle_ = nullptr; 1703 std::shared_ptr<AbilityLifecycleExecutor> abilityLifecycleExecutor_ = nullptr; 1704 std::shared_ptr<OHOSApplication> application_ = nullptr; 1705 std::vector<std::string> types_; 1706 std::map<int, FeatureAbilityTask> resultCallbacks_; 1707 #ifdef SUPPORT_GRAPHICS 1708 std::shared_ptr<AbilityWindow> abilityWindow_ = nullptr; 1709 #endif 1710 std::shared_ptr<AAFwk::Want> setWant_ = nullptr; 1711 sptr<IRemoteObject> reverseContinuationSchedulerReplica_ = nullptr; 1712 #ifdef SUPPORT_GRAPHICS 1713 bool bWindowFocus_ = false; 1714 bool showOnLockScreen_ = false; 1715 #endif 1716 int compatibleVersion_ = 0; 1717 1718 static const std::string SYSTEM_UI; 1719 static const std::string STATUS_BAR; 1720 static const std::string NAVIGATION_BAR; 1721 static const std::string KEYGUARD; 1722 sptr<IRemoteObject> providerRemoteObject_ = nullptr; 1723 // Keep consistent with DMS defines. Used to callback to DMS. 1724 static const std::string DMS_SESSION_ID; 1725 1726 // The originating deviceId passed by DMS using want param. 1727 static const std::string DMS_ORIGIN_DEVICE_ID; 1728 1729 // If session id cannot get from want, assign it as default. 1730 static const int DEFAULT_DMS_SESSION_ID; 1731 #ifdef SUPPORT_GRAPHICS 1732 std::vector<int64_t> lostedByReconnectTempForms_; 1733 std::map<int64_t, std::shared_ptr<FormCallback>> appCallbacks_; 1734 #endif 1735 std::map<int64_t, Want> userReqParams_; 1736 sptr<IBundleMgr> iBundleMgr_; 1737 #ifdef SUPPORT_GRAPHICS 1738 static const int32_t OHOS_FORM_ACQUIRE_FORM = 0; 1739 static const int32_t OHOS_FORM_UPDATE_FORM = 1; 1740 1741 static const int32_t DELETE_FORM = 3; 1742 static const int32_t ENABLE_FORM_UPDATE = 5; 1743 static const int32_t DISABLE_FORM_UPDATE = 6; 1744 static const int32_t RELEASE_FORM = 8; 1745 static const int32_t RELEASE_CACHED_FORM = 9; 1746 #endif 1747 static const int64_t MIN_NEXT_TIME = 5; 1748 1749 private: 1750 #ifdef SUPPORT_GRAPHICS 1751 /** 1752 * @brief Delete or release form with formId. 1753 * 1754 * @param formId Indicates the form's ID. 1755 * @param deleteType Indicates the type of delete or release. 1756 * @return Returns {@code true} if the form is successfully deleted; returns {@code false} otherwise. 1757 */ 1758 ErrCode DeleteForm(const int64_t formId, const int32_t deleteType); 1759 1760 /** 1761 * @brief Clean form resource with formId. 1762 * 1763 * @param formId Indicates the form's ID. 1764 */ 1765 void CleanFormResource(const int64_t formId); 1766 1767 /** 1768 * @brief Handle acquire result of the obtained form instance. 1769 * 1770 * @param want Indicates the detailed information about the form to be obtained, including the bundle name, 1771 * module name, ability name, form name, form id, tempForm flag, form dimension, and form customize data. 1772 * @param formJsInfo Indicates the obtained {@code FormJsInfo} instance. 1773 * @param callback Indicates the callback to be invoked whenever the {@link FormJsInfo} instance is obtained. 1774 */ 1775 void HandleAcquireResult( 1776 const Want &want, 1777 const FormJsInfo &formJsInfo, 1778 const std::shared_ptr<FormCallback> callback 1779 ); 1780 1781 /** 1782 * @brief Handle acquire message of the obtained form instance. 1783 * 1784 * @param msgCode Indicates the code of message type. 1785 * @param formJsInfo Indicates the obtained {@code FormJsInfo} instance. 1786 */ 1787 void HandleFormMessage(const int32_t msgCode, const FormJsInfo &formJsInfo); 1788 1789 /** 1790 * @brief Notify the forms visibility change event. 1791 * 1792 * @param formIds Indicates the IDs of the forms to be made visible or invisible. 1793 * @param eventType Indicates the form events occurred. FORM_VISIBLE means that the form becomes visible, 1794 * and FORM_INVISIBLE means that the form becomes invisible. 1795 * @return none. 1796 */ 1797 ErrCode NotifyWhetherVisibleForms(const std::vector<int64_t> &formIds, int32_t eventType); 1798 1799 /** 1800 * @brief Check the param of want. 1801 * 1802 * @param formId Indicates the form's ID. 1803 * @param want Indicates the detailed information about the form to be obtained, including the bundle name, 1804 * module name, ability name, form name, form id, tempForm flag, form dimension, and form customize data. 1805 * @return Returns {@code true} if the check result is ok; returns {@code false} ng. 1806 */ 1807 bool CheckWantValid(const int64_t formId, const Want &want); 1808 1809 /** 1810 * @brief Handle enable/disable form update. 1811 * 1812 * @param formIds Indicates the IDs of the forms to be made visible. 1813 * @param updateType Update type. 1814 * @return Returns true if the result is ok; returns false otherwise. 1815 */ 1816 ErrCode LifecycleUpdate(std::vector<int64_t> formIds, int32_t updateType); 1817 1818 /** 1819 * @brief Reacquire a specified form when the death callback is received. 1820 * 1821 * @param formId Indicates the form ID. 1822 * @param want Indicates the detailed information about the form to be obtained. 1823 * @return Returns true if the request is successfully initiated; returns false otherwise. 1824 */ 1825 bool ReAcquireForm(const int64_t formId, const Want &want); 1826 #endif 1827 }; 1828 } // namespace AppExecFwk 1829 } // namespace OHOS 1830 #endif // FOUNDATION_APPEXECFWK_OHOS_ABILITY_H 1831