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_OBSERVER_INTERFACE_H
17 #define OHOS_ABILITY_RUNTIME_ABILITY_LIFECYCLE_OBSERVER_INTERFACE_H
18 
19 #include "want.h"
20 #include "ability_lifecycle.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 using Want = OHOS::AAFwk::Want;
25 class ILifecycleObserver {
26 public:
27     ILifecycleObserver() = default;
28     virtual ~ILifecycleObserver() = default;
29 
30     /**
31      * @brief Called back in response to an ON_ACTIVE event.
32      * When an ON_ACTIVE event is received, the ability or ability slice is in the foreground and is interactive.
33      */
34     virtual void OnActive() = 0;
35 
36 #ifdef SUPPORT_GRAPHICS
37     /**
38      * @brief Called back in response to an ON_BACKGROUND event.
39      * When an ON_BACKGROUND event is received, the ability or ability slice is invisible. You are advised to
40      * suspend the threads related to this ability or ability slice and clear resources for more system memory.
41      *
42      */
43     virtual void OnBackground() = 0;
44 
45     /**
46      * @brief Called back in response to an ON_FOREGROUND event, where information for the
47      * ability or ability slice to go back to the ACTIVE state is carried in the want parameter.
48      * When an ON_FOREGROUND event is received, the ability or ability slice returns to the foreground. You can use
49      * this method to implement re-initialization or adjust the UI display by using the want parameter.
50      *
51      * @param want Indicates the information for the ability or ability slice to go back to the ACTIVE state.
52      */
53     virtual void OnForeground(const Want &want) = 0;
54 #endif
55 
56     /**
57      * @brief Called back in response to an ON_INACTIVE event.
58      * When an ON_INACTIVE event is received, the ability or ability slice is in the INACTIVE state. INACTIVE is an
59      * intermediate state before the state changes to ACTIVE or BACKGROUND. In this state, the UI may be visible but is
60      * not interactive. You are advised not to use this method to invoke complex service logic.
61      *
62      */
63     virtual void OnInactive() = 0;
64 
65     /**
66      * @brief Called back in response to an ON_START event, where the startup information
67      * is carried in the want parameter.
68      * This method initializes an Ability or AbilitySlice and is called back only once during the entire lifecycle.
69      * You are advised to implement some initialization logic using this method, for example, you can initialize a
70      * timer or define some global objects.
71      *
72      * @param want Indicates the startup information.
73      */
74     virtual void OnStart(const Want &want) = 0;
75 
76     /**
77      * @brief Called back in response to an ON_STOP event.
78      * This method is called back when the lifecycle of the ability or ability slice is destroyed. You can reclaim
79      * resources using this method.
80      *
81      */
82     virtual void OnStop() = 0;
83 
84     /**
85      * @brief Called back in response to a lifecycle change. This method is triggered by a registered LifecycleObserver
86      * each time the lifecycle state changes.
87      *
88      * @param event Indicates the lifecycle event.
89      * @param want Indicates the state change information.
90      */
91     virtual void OnStateChanged(LifeCycle::Event event, const Want &want) = 0;
92 
93     /**
94      * @brief Called back in response to a lifecycle change. This method is triggered by a registered LifecycleObserver
95      * each time the lifecycle state changes.
96      *
97      * @param event Indicates the lifecycle event.
98      */
99     virtual void OnStateChanged(LifeCycle::Event event) = 0;
100 };
101 }  // namespace AppExecFwk
102 }  // namespace OHOS
103 #endif  // OHOS_ABILITY_RUNTIME_ABILITY_LIFECYCLE_OBSERVER_INTERFACE_H
104