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 #include "ability_lifecycle_observer_interface.h"
17 #include "hilog_wrapper.h"
18
19 namespace OHOS {
20 namespace AppExecFwk {
21 /**
22 * @brief Obtains the current lifecycle event.
23 * Lifecycle events drive lifecycle state changes. Therefore, you are able to know the lifecycle state
24 * once you obtain the lifecycle event. For example, if the ON_ACTIVE event is received, the ability or
25 * ability slice is in the ACTIVE state; if the ON_FOREGROUND event is received, the ability or ability
26 * slice is changing from the BACKGROUND state to INACTIVE.
27 *
28 * @return Returns the current lifecycle event.
29 */
GetLifecycleState()30 LifeCycle::Event LifeCycle::GetLifecycleState()
31 {
32 HILOG_INFO("LifeCycle::GetLifecycleState: called");
33 return state_;
34 }
35
36 /**
37 * @brief Adds a lifecycle observer.
38 * The observer will be notified of lifecycle changes.
39 *
40 * @param observer Indicates the lifecycle observer, either LifecycleObserver or LifecycleStateObserver.
41 * The value cannot be null.
42 *
43 */
AddObserver(const std::shared_ptr<ILifecycleObserver> & observer)44 void LifeCycle::AddObserver(const std::shared_ptr<ILifecycleObserver> &observer)
45 {
46 HILOG_INFO("LifeCycle::AddObserver: called");
47
48 if (observer == nullptr) {
49 HILOG_INFO("LifeCycle::AddObserver: observer is null");
50 return;
51 }
52
53 callbacks_.emplace_back(observer);
54 }
55
56 /**
57 * @brief While Ability's lifecycle changes, dispatch lifecycle event.
58 *
59 * @param event Lifecycle state.
60 * @param want Indicates the Want containing information about the target ability to change lifecycle state.
61 */
DispatchLifecycle(const LifeCycle::Event & event,const Want & want)62 void LifeCycle::DispatchLifecycle(const LifeCycle::Event &event, const Want &want)
63 {
64 HILOG_INFO("LifeCycle::DispatchLifecycle: event:%{public}d", event);
65 if ((event != LifeCycle::Event::ON_FOREGROUND) && (event != LifeCycle::Event::ON_START)) {
66 HILOG_ERROR("event value error: event is %{public}d", event);
67 return;
68 }
69
70 state_ = event;
71 if (callbacks_.size() != 0) {
72 for (auto &callback : callbacks_) {
73 switch (event) {
74 #ifdef SUPPORT_GRAPHICS
75 case ON_FOREGROUND: {
76 if (callback != nullptr) {
77 callback->OnForeground(want);
78 }
79 break;
80 }
81 #endif
82 case ON_START: {
83 if (callback != nullptr) {
84 callback->OnStart(want);
85 }
86 break;
87 }
88 default:
89 break;
90 }
91 if (callback != nullptr) {
92 callback->OnStateChanged(event, want);
93 }
94 }
95 }
96 }
97
98 /**
99 * @brief While Ability's lifecycle changes, dispatch lifecycle event.
100 *
101 * @param event Lifecycle state.
102 */
DispatchLifecycle(const LifeCycle::Event & event)103 void LifeCycle::DispatchLifecycle(const LifeCycle::Event &event)
104 {
105 HILOG_INFO("LifeCycle::DispatchLifecycle: event:%{public}d", event);
106 if ((event != LifeCycle::Event::ON_ACTIVE) && (event != LifeCycle::Event::ON_BACKGROUND) &&
107 (event != LifeCycle::Event::ON_INACTIVE) && (event != LifeCycle::Event::ON_STOP)) {
108 HILOG_ERROR("event value error: event is %{public}d", event);
109 return;
110 }
111
112 state_ = event;
113 if (callbacks_.size() != 0) {
114 for (auto &callback : callbacks_) {
115 switch (event) {
116 case ON_ACTIVE: {
117 if (callback != nullptr) {
118 callback->OnActive();
119 }
120 break;
121 }
122 #ifdef SUPPORT_GRAPHICS
123 case ON_BACKGROUND: {
124 if (callback != nullptr) {
125 callback->OnBackground();
126 }
127 break;
128 }
129 #endif
130 case ON_INACTIVE: {
131 if (callback != nullptr) {
132 callback->OnInactive();
133 }
134 break;
135 }
136 case ON_STOP: {
137 if (callback != nullptr) {
138 callback->OnStop();
139 }
140 break;
141 }
142 default:
143 break;
144 }
145 if (callback != nullptr) {
146 callback->OnStateChanged(event);
147 }
148 }
149 }
150 }
151
152 /**
153 * @brief Removes a lifecycle observer.
154 * You are advised to call this method if you no longer need to listen to lifecycle changes. This reduces the
155 * performance loss caused by observing lifecycle changes.
156 *
157 * @param observer Indicates the lifecycle observer, either LifecycleObserver or LifecycleStateObserver.
158 * The value cannot be null.
159 */
RemoveObserver(const std::shared_ptr<ILifecycleObserver> & observer)160 void LifeCycle::RemoveObserver(const std::shared_ptr<ILifecycleObserver> &observer)
161 {
162 HILOG_INFO("LifeCycle::RemoveObserver: called");
163
164 if (observer == nullptr) {
165 HILOG_INFO("LifeCycle::RemoveObserver: observer is null");
166 return;
167 }
168
169 callbacks_.remove(observer);
170 }
171 } // namespace AppExecFwk
172 } // namespace OHOS