1 /*
2  * Copyright (c) 2022-2024 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 "window_focus_changed_listener.h"
17 
18 #include "app_mgr_service_inner.h"
19 #include "hilog_tag_wrapper.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 using namespace OHOS::Rosen;
24 
WindowFocusChangedListener(const std::shared_ptr<AppMgrServiceInner> & owner,const std::shared_ptr<AAFwk::TaskHandlerWrap> & handler)25 WindowFocusChangedListener::WindowFocusChangedListener(const std::shared_ptr<AppMgrServiceInner> &owner,
26     const std::shared_ptr<AAFwk::TaskHandlerWrap>& handler) : owner_(owner), taskHandler_(handler) {}
27 
~WindowFocusChangedListener()28 WindowFocusChangedListener::~WindowFocusChangedListener() {}
29 
OnFocused(const sptr<FocusChangeInfo> & focusChangeInfo)30 void WindowFocusChangedListener::OnFocused(const sptr<FocusChangeInfo> &focusChangeInfo)
31 {
32     if (!focusChangeInfo) {
33         TAG_LOGW(AAFwkTag::APPMGR, "OnFocused invalid focusChangeInfo.");
34         return;
35     }
36 
37     if (taskHandler_) {
38         auto task = [inner = owner_, focusChangeInfo] {
39             auto owner = inner.lock();
40             if (!owner) {
41                 TAG_LOGW(AAFwkTag::APPMGR, "OnUnfocused failed to get app mgr service inner.");
42                 return;
43             }
44             owner->HandleFocused(focusChangeInfo);
45         };
46         taskHandler_->SubmitTask(task, "WindowFocusChangedListener::OnFocused");
47     }
48 }
49 
OnUnfocused(const sptr<FocusChangeInfo> & focusChangeInfo)50 void WindowFocusChangedListener::OnUnfocused(const sptr<FocusChangeInfo> &focusChangeInfo)
51 {
52     if (!focusChangeInfo) {
53         TAG_LOGW(AAFwkTag::APPMGR, "OnUnfocused invalid focusChangeInfo.");
54         return;
55     }
56 
57     if (taskHandler_) {
58         auto task = [inner = owner_, focusChangeInfo] {
59             auto owner = inner.lock();
60             if (!owner) {
61                 TAG_LOGW(AAFwkTag::APPMGR, "OnUnfocused failed to get app mgr service inner.");
62                 return;
63             }
64             owner->HandleUnfocused(focusChangeInfo);
65         };
66         taskHandler_->SubmitTask(task, "WindowFocusChangedListener::OnUnfocused");
67     }
68 }
69 }  // namespace AppExecFwk
70 }  // namespace OHOS
71