1 /*
2 * Copyright (c) 2021-2023 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 "mission_listener_controller.h"
17
18 #include "hilog_wrapper.h"
19
20 namespace OHOS {
21 namespace AAFwk {
22 namespace {
23 const std::string THREAD_NAME = "MissionListener";
24 }
25 using Cmd = IMissionListener::MissionListenerCmd;
26
MissionListenerController()27 MissionListenerController::MissionListenerController()
28 {
29 }
30
~MissionListenerController()31 MissionListenerController::~MissionListenerController()
32 {}
33
Init()34 void MissionListenerController::Init()
35 {
36 if (!handler_) {
37 handler_ = TaskHandlerWrap::CreateQueueHandler("mission_listener_task_queue");
38 }
39 }
40
AddMissionListener(const sptr<IMissionListener> & listener)41 int MissionListenerController::AddMissionListener(const sptr<IMissionListener> &listener)
42 {
43 if (!listener) {
44 HILOG_ERROR("listener is invalid");
45 return -1;
46 }
47
48 std::lock_guard<ffrt::mutex> guard(listenerLock_);
49 auto it = std::find_if(missionListeners_.begin(), missionListeners_.end(),
50 [&listener](const sptr<IMissionListener> &item) {
51 return (item && item->AsObject() == listener->AsObject());
52 }
53 );
54 if (it != missionListeners_.end()) {
55 HILOG_WARN("listener was already added, do not add again");
56 return 0;
57 }
58
59 if (!listenerDeathRecipient_) {
60 std::weak_ptr<MissionListenerController> thisWeakPtr(shared_from_this());
61 listenerDeathRecipient_ =
62 new ListenerDeathRecipient([thisWeakPtr](const wptr<IRemoteObject> &remote) {
63 auto controller = thisWeakPtr.lock();
64 if (controller) {
65 controller->OnListenerDied(remote);
66 }
67 });
68 }
69 auto listenerObject = listener->AsObject();
70 if (listenerObject) {
71 listenerObject->AddDeathRecipient(listenerDeathRecipient_);
72 }
73 missionListeners_.emplace_back(listener);
74
75 return 0;
76 }
77
DelMissionListener(const sptr<IMissionListener> & listener)78 void MissionListenerController::DelMissionListener(const sptr<IMissionListener> &listener)
79 {
80 if (!listener) {
81 HILOG_ERROR("listener is invalid");
82 return;
83 }
84
85 std::lock_guard<ffrt::mutex> guard(listenerLock_);
86 auto it = std::find_if(missionListeners_.begin(), missionListeners_.end(),
87 [&listener](const sptr<IMissionListener> item) {
88 return (item && item->AsObject() == listener->AsObject());
89 }
90 );
91 if (it != missionListeners_.end()) {
92 missionListeners_.erase(it);
93 }
94 }
95
NotifyMissionCreated(int32_t missionId)96 void MissionListenerController::NotifyMissionCreated(int32_t missionId)
97 {
98 if (!handler_) {
99 HILOG_ERROR("handler not init");
100 return;
101 }
102 auto task = [weak = weak_from_this(), missionId]() {
103 auto self = weak.lock();
104 if (self == nullptr) {
105 HILOG_ERROR("self is nullptr, NotifyMissionCreated failed.");
106 return;
107 }
108 HILOG_INFO("notify listeners mission is created, missionId:%{public}d.", missionId);
109 self->CallListeners(&IMissionListener::OnMissionCreated, missionId);
110 };
111 handler_->SubmitTask(task);
112 }
113
NotifyMissionDestroyed(int32_t missionId)114 void MissionListenerController::NotifyMissionDestroyed(int32_t missionId)
115 {
116 if (!handler_) {
117 HILOG_ERROR("handler not init");
118 return;
119 }
120 auto task = [weak = weak_from_this(), missionId]() {
121 auto self = weak.lock();
122 if (self == nullptr) {
123 HILOG_ERROR("self is nullptr, NotifyMissionDestroyed failed.");
124 return;
125 }
126 HILOG_INFO("notify listeners mission is destroyed, missionId:%{public}d.", missionId);
127 self->CallListeners(&IMissionListener::OnMissionDestroyed, missionId);
128 };
129 handler_->SubmitTask(task);
130 }
131
HandleUnInstallApp(const std::list<int32_t> & missions)132 void MissionListenerController::HandleUnInstallApp(const std::list<int32_t> &missions)
133 {
134 if (!handler_) {
135 HILOG_ERROR("handler not init");
136 return;
137 }
138
139 if (missions.empty()) {
140 return;
141 }
142
143 auto task = [weak = weak_from_this(), missions]() {
144 auto self = weak.lock();
145 if (self == nullptr) {
146 HILOG_ERROR("self is nullptr, NotifyMissionDestroyed failed.");
147 return;
148 }
149 for (auto id : missions) {
150 self->CallListeners(&IMissionListener::OnMissionDestroyed, id);
151 }
152 };
153 handler_->SubmitTask(task);
154 }
155
NotifyMissionSnapshotChanged(int32_t missionId)156 void MissionListenerController::NotifyMissionSnapshotChanged(int32_t missionId)
157 {
158 if (!handler_) {
159 HILOG_ERROR("handler not init");
160 return;
161 }
162
163 auto task = [weak = weak_from_this(), missionId]() {
164 auto self = weak.lock();
165 if (self == nullptr) {
166 HILOG_ERROR("self is nullptr, NotifyMissionSnapshotChanged failed.");
167 return;
168 }
169 HILOG_INFO("notify listeners mission snapshot changed, missionId:%{public}d.", missionId);
170 self->CallListeners(&IMissionListener::OnMissionSnapshotChanged, missionId);
171 };
172 handler_->SubmitTask(task);
173 }
174
NotifyMissionMovedToFront(int32_t missionId)175 void MissionListenerController::NotifyMissionMovedToFront(int32_t missionId)
176 {
177 if (!handler_) {
178 HILOG_ERROR("handler not init");
179 return;
180 }
181
182 auto task = [weak = weak_from_this(), missionId]() {
183 auto self = weak.lock();
184 if (self == nullptr) {
185 HILOG_ERROR("self is nullptr, NotifyMissionSnapshotChanged failed.");
186 return;
187 }
188 HILOG_INFO("notify listeners mission is moved to front, missionId:%{public}d.", missionId);
189 self->CallListeners(&IMissionListener::OnMissionMovedToFront, missionId);
190 };
191 handler_->SubmitTask(task);
192 }
193
NotifyMissionFocused(int32_t missionId)194 void MissionListenerController::NotifyMissionFocused(int32_t missionId)
195 {
196 if (missionId == -1) {
197 return;
198 }
199
200 if (!handler_) {
201 HILOG_ERROR("handler is null");
202 return;
203 }
204 auto task = [weak = weak_from_this(), missionId]() {
205 auto self = weak.lock();
206 if (self == nullptr) {
207 HILOG_ERROR("self is nullptr, NotifyMissionFocused failed");
208 return;
209 }
210 HILOG_INFO("NotifyMissionFocused, missionId:%{public}d.", missionId);
211 self->CallListeners(&IMissionListener::OnMissionFocused, missionId);
212 };
213 handler_->SubmitTask(task);
214 }
215
NotifyMissionUnfocused(int32_t missionId)216 void MissionListenerController::NotifyMissionUnfocused(int32_t missionId)
217 {
218 if (missionId == -1) {
219 return;
220 }
221
222 if (!handler_) {
223 HILOG_ERROR("handler is null");
224 return;
225 }
226 auto task = [weak = weak_from_this(), missionId]() {
227 auto self = weak.lock();
228 if (self == nullptr) {
229 HILOG_ERROR("self is nullptr, NotifyMissionUnfocused failed");
230 return;
231 }
232 HILOG_INFO("NotifyMissionUnfocused, missionId:%{public}d.", missionId);
233 self->CallListeners(&IMissionListener::OnMissionUnfocused, missionId);
234 };
235 handler_->SubmitTask(task);
236 }
237
238 #ifdef SUPPORT_GRAPHICS
NotifyMissionIconChanged(int32_t missionId,const std::shared_ptr<OHOS::Media::PixelMap> & icon)239 void MissionListenerController::NotifyMissionIconChanged(int32_t missionId,
240 const std::shared_ptr<OHOS::Media::PixelMap> &icon)
241 {
242 if (!handler_) {
243 HILOG_ERROR("handler not init when notify mission icon changed");
244 return;
245 }
246
247 auto task = [weak = weak_from_this(), missionId, icon]() {
248 auto self = weak.lock();
249 if (self == nullptr) {
250 HILOG_ERROR("self is nullptr, NotifyMissionIconChanged failed.");
251 return;
252 }
253 HILOG_INFO("notify listeners mission icon has changed, missionId:%{public}d.", missionId);
254 self->CallListeners(&IMissionListener::OnMissionIconUpdated, missionId, icon);
255 };
256 handler_->SubmitTask(task);
257 }
258 #endif
259
NotifyMissionClosed(int32_t missionId)260 void MissionListenerController::NotifyMissionClosed(int32_t missionId)
261 {
262 if (!handler_) {
263 HILOG_ERROR("handler not init");
264 return;
265 }
266 auto task = [weak = weak_from_this(), missionId]() {
267 auto self = weak.lock();
268 if (self == nullptr) {
269 HILOG_ERROR("self is nullptr, NotifyMissionClosed failed.");
270 return;
271 }
272 HILOG_INFO("NotifyMissionClosed, missionId:%{public}d.", missionId);
273 self->CallListeners(&IMissionListener::OnMissionClosed, missionId);
274 };
275 handler_->SubmitTask(task);
276 }
277
NotifyMissionLabelUpdated(int32_t missionId)278 void MissionListenerController::NotifyMissionLabelUpdated(int32_t missionId)
279 {
280 if (!handler_) {
281 HILOG_ERROR("NotifyMissionLabelUpdated, handler not init");
282 return;
283 }
284 auto task = [weak = weak_from_this(), missionId]() {
285 auto self = weak.lock();
286 if (self == nullptr) {
287 HILOG_ERROR("self is nullptr, NotifyMissionLabelUpdated failed.");
288 return;
289 }
290 HILOG_INFO("notify listeners mission label has updated, missionId:%{public}d.", missionId);
291 self->CallListeners(&IMissionListener::OnMissionLabelUpdated, missionId);
292 };
293 handler_->SubmitTask(task);
294 }
295
OnListenerDied(const wptr<IRemoteObject> & remote)296 void MissionListenerController::OnListenerDied(const wptr<IRemoteObject> &remote)
297 {
298 HILOG_DEBUG("On mission listener died.");
299 auto remoteObj = remote.promote();
300 if (!remoteObj) {
301 HILOG_DEBUG("invalid remote object.");
302 return;
303 }
304 remoteObj->RemoveDeathRecipient(listenerDeathRecipient_);
305
306 std::lock_guard<ffrt::mutex> guard(listenerLock_);
307 auto it = std::find_if(missionListeners_.begin(), missionListeners_.end(),
308 [&remoteObj](const sptr<IMissionListener> item) {
309 return (item && item->AsObject() == remoteObj);
310 }
311 );
312 if (it != missionListeners_.end()) {
313 missionListeners_.erase(it);
314 }
315 }
316
ListenerDeathRecipient(ListenerDiedHandler handler)317 MissionListenerController::ListenerDeathRecipient::ListenerDeathRecipient(ListenerDiedHandler handler)
318 : diedHandler_(handler)
319 {}
320
OnRemoteDied(const wptr<IRemoteObject> & remote)321 void MissionListenerController::ListenerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
322 {
323 if (diedHandler_) {
324 diedHandler_(remote);
325 }
326 }
327 } // namespace AAFwk
328 } // namespace OHOS
329