1 /*
2 * Copyright (C) 2025 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 #include "screenlock_inner_listener_wapper.h"
16
17 #include "sclock_log.h"
18 #include "screenlock_common.h"
19
20
21 namespace OHOS {
22 namespace ScreenLock {
23 std::shared_ptr<AppExecFwk::EventHandler> InnerListenerWrapper::handler_{ nullptr };
24
25
InnerListenerWrapper(const sptr<InnerListener> & listener)26 InnerListenerWrapper::InnerListenerWrapper(const sptr<InnerListener>& listener)
27 : listener_(listener)
28 {
29 }
30
~InnerListenerWrapper()31 InnerListenerWrapper::~InnerListenerWrapper()
32 {
33 }
34
OnStateChanged(int userId,int state)35 void InnerListenerWrapper::OnStateChanged(int userId, int state)
36 {
37 SCLOCK_HILOGD("OnStateChanged start");
38 if (listener_ == nullptr) {
39 SCLOCK_HILOGE("eventHandler is nullptr");
40 return;
41 }
42 // 避免了 this 指针带来的潜在问题, 捕获必要的成员变量
43 sptr<InnerListener> localListener = listener_;
44 int localUserId = userId;
45 int localState = state;
46
47 auto task = [localListener, localUserId, localState]() {
48 localListener->OnStateChanged(localUserId, localState);
49 };
50
51 auto ptrTemp = GetEventHandler();
52 if (ptrTemp == nullptr) {
53 SCLOCK_HILOGE("EventHandler nullptr");
54 } else {
55 GetEventHandler()->PostTask(task, "InnerListenerWrapper");
56 }
57
58 SCLOCK_HILOGD("OnStateChanged end");
59 return;
60 }
61
GetEventHandler()62 std::shared_ptr<AppExecFwk::EventHandler> InnerListenerWrapper::GetEventHandler()
63 {
64 if (handler_ == nullptr) {
65 handler_ = AppExecFwk::EventHandler::Current();
66 }
67 return handler_;
68 }
69 }
70 }