1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_ABILITY_RUNTIME_ABILITY_LIFECYCLE_H 17 #define OHOS_ABILITY_RUNTIME_ABILITY_LIFECYCLE_H 18 19 #include <vector> 20 #include <string> 21 #include <list> 22 #include <refbase.h> 23 #include <memory> 24 #include "want.h" 25 26 namespace OHOS { 27 namespace AppExecFwk { 28 using Want = OHOS::AAFwk::Want; 29 class ILifecycleObserver; 30 class LifeCycle { 31 public: 32 LifeCycle() = default; 33 virtual ~LifeCycle() = default; 34 35 enum Event { ON_ACTIVE, ON_BACKGROUND, ON_FOREGROUND, ON_INACTIVE, ON_START, ON_STOP, UNDEFINED }; 36 37 /** 38 * @brief Obtains the current lifecycle event. 39 * Lifecycle events drive lifecycle state changes. Therefore, you are able to know the lifecycle state 40 * once you obtain the lifecycle event. For example, if the ON_ACTIVE event is received, the ability or 41 * ability slice is in the ACTIVE state; if the ON_FOREGROUND event is received, the ability or ability 42 * slice is changing from the BACKGROUND state to INACTIVE. 43 * 44 * @return Returns the current lifecycle event. 45 */ 46 LifeCycle::Event GetLifecycleState(); 47 48 /** 49 * @brief Adds a lifecycle observer. 50 * The observer will be notified of lifecycle changes. 51 * 52 * @param observer Indicates the lifecycle observer, either LifecycleObserver or LifecycleStateObserver. 53 * The value cannot be null. 54 * 55 */ 56 void AddObserver(const std::shared_ptr<ILifecycleObserver> &observer); 57 58 /** 59 * @brief While Ability's lifecycle changes, dispatch lifecycle event. 60 * 61 * @param event Lifecycle state. 62 * @param want Indicates the Want containing information about the target ability to change lifecycle state. 63 */ 64 void DispatchLifecycle(const LifeCycle::Event &event, const Want &want); 65 66 /** 67 * @brief While Ability's lifecycle changes, dispatch lifecycle event. 68 * 69 * @param event Lifecycle state. 70 * @param want Indicates the Want containing information about the target ability to change lifecycle state. 71 */ 72 void DispatchLifecycle(const LifeCycle::Event &event); 73 74 /** 75 * @brief Removes a lifecycle observer. 76 * You are advised to call this method if you no longer need to listen to lifecycle changes. This reduces the 77 * performance loss caused by observing lifecycle changes. 78 * 79 * @param observer Indicates the lifecycle observer, either LifecycleObserver or LifecycleStateObserver. 80 * The value cannot be null. 81 */ 82 void RemoveObserver(const std::shared_ptr<ILifecycleObserver> &observer); 83 84 private: 85 LifeCycle::Event state_ = UNDEFINED; 86 87 std::list<std::shared_ptr<ILifecycleObserver>> callbacks_; 88 }; 89 } // namespace AppExecFwk 90 } // namespace OHOS 91 #endif // OHOS_ABILITY_RUNTIME_ABILITY_LIFECYCLE_H 92