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 "app_log_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 APP_LOGI("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 APP_LOGI("LifeCycle::AddObserver: called");
47
48 if (observer == nullptr) {
49 APP_LOGI("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 APP_LOGI("LifeCycle::DispatchLifecycle: called");
65
66 if ((event != LifeCycle::Event::ON_FOREGROUND) && (event != LifeCycle::Event::ON_START)) {
67 APP_LOGE("event value error: event is %{public}d", event);
68 return;
69 }
70
71 state_ = event;
72 if (callbacks_.size() != 0) {
73 for (auto &callback : callbacks_) {
74 switch (event) {
75 case ON_FOREGROUND: {
76 if (callback != nullptr) {
77 callback->OnForeground(want);
78 }
79 break;
80 }
81 case ON_START: {
82 if (callback != nullptr) {
83 callback->OnStart(want);
84 }
85 break;
86 }
87 default:
88 break;
89 }
90 if (callback != nullptr) {
91 callback->OnStateChanged(event, want);
92 }
93 }
94 }
95 }
96
97 /**
98 * @brief While Ability's lifecycle changes, dispatch lifecycle event.
99 *
100 * @param event Lifecycle state.
101 */
DispatchLifecycle(const LifeCycle::Event & event)102 void LifeCycle::DispatchLifecycle(const LifeCycle::Event &event)
103 {
104 APP_LOGI("LifeCycle::DispatchLifecycle: called");
105
106 if ((event != LifeCycle::Event::ON_ACTIVE) && (event != LifeCycle::Event::ON_BACKGROUND) &&
107 (event != LifeCycle::Event::ON_INACTIVE) && (event != LifeCycle::Event::ON_STOP)) {
108 APP_LOGE("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 case ON_BACKGROUND: {
123 if (callback != nullptr) {
124 callback->OnBackground();
125 }
126 break;
127 }
128 case ON_INACTIVE: {
129 if (callback != nullptr) {
130 callback->OnInactive();
131 }
132 break;
133 }
134 case ON_STOP: {
135 if (callback != nullptr) {
136 callback->OnStop();
137 }
138 break;
139 }
140 default:
141 break;
142 }
143 if (callback != nullptr) {
144 callback->OnStateChanged(event);
145 }
146 }
147 }
148 }
149
150 /**
151 * @brief Removes a lifecycle observer.
152 * You are advised to call this method if you no longer need to listen to lifecycle changes. This reduces the
153 * performance loss caused by observing lifecycle changes.
154 *
155 * @param observer Indicates the lifecycle observer, either LifecycleObserver or LifecycleStateObserver.
156 * The value cannot be null.
157 */
RemoveObserver(const std::shared_ptr<ILifecycleObserver> & observer)158 void LifeCycle::RemoveObserver(const std::shared_ptr<ILifecycleObserver> &observer)
159 {
160 APP_LOGI("LifeCycle::RemoveObserver: called");
161
162 if (observer == nullptr) {
163 APP_LOGI("LifeCycle::RemoveObserver: observer is null");
164 return;
165 }
166
167 callbacks_.remove(observer);
168 }
169 } // namespace AppExecFwk
170 } // namespace OHOS