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