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
16 #include "screenlock_status_listener.h"
17
18 #include "common_event_subscribe_info.h"
19 #include "matching_skills.h"
20 #include "singleton.h"
21 #include "want.h"
22
23 #include "context_appstate_observer.h"
24 #include "credential_info_interface.h"
25 #include "risk_event_manager.h"
26 #include "user_idm_database.h"
27
28 #define LOG_TAG "USER_AUTH_SA"
29
30 namespace OHOS {
31 namespace UserIam {
32 namespace UserAuth {
GetInstance()33 ScreenlockStatusListenerManager &ScreenlockStatusListenerManager::GetInstance()
34 {
35 static ScreenlockStatusListenerManager instance;
36 return instance;
37 }
38
RegisterCommonEventListener()39 ResultCode ScreenlockStatusListenerManager::RegisterCommonEventListener()
40 {
41 IAM_LOGI("start");
42 std::lock_guard<std::recursive_mutex> lock(mutex_);
43 if (commonEventSaStatusListener_ != nullptr) {
44 return SUCCESS;
45 }
46 commonEventSaStatusListener_ = SystemAbilityListener::Subscribe(
47 "common_event_service", COMMON_EVENT_SERVICE_ID,
48 []() { ScreenlockStatusListenerManager::GetInstance().RegisterScreenLockedCallback(); },
49 []() { ScreenlockStatusListenerManager::GetInstance().UnRegisterScreenLockedCallback(); });
50 IF_FALSE_LOGE_AND_RETURN_VAL(commonEventSaStatusListener_ != nullptr, GENERAL_ERROR);
51
52 return SUCCESS;
53 }
54
RegisterScreenLockedCallback()55 void ScreenlockStatusListenerManager::RegisterScreenLockedCallback()
56 {
57 IAM_LOGI("start");
58 std::lock_guard<std::recursive_mutex> lock(mutex_);
59 if (subscriber_ != nullptr) {
60 return;
61 }
62 EventFwk::MatchingSkills matchingSkills;
63 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
64
65 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
66 auto subscriber = Common::MakeShared<ScreenlockStatusListener>(subscribeInfo);
67 IF_FALSE_LOGE_AND_RETURN(subscriber != nullptr);
68 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber)) {
69 IAM_LOGE("SubscribeCommonEvent fail");
70 return;
71 }
72 IF_FALSE_LOGE_AND_RETURN(subscriber != nullptr);
73 subscriber_ = subscriber;
74 SyncScreenlockStatus();
75 }
76
UnRegisterScreenLockedCallback()77 void ScreenlockStatusListenerManager::UnRegisterScreenLockedCallback()
78 {
79 IAM_LOGI("start");
80 std::lock_guard<std::recursive_mutex> lock(mutex_);
81 if (subscriber_ == nullptr) {
82 return;
83 }
84
85 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
86 IAM_LOGE("UnSubscribeCommonEvent failed");
87 }
88 subscriber_ = nullptr;
89 }
90
SyncScreenlockStatus()91 void ScreenlockStatusListenerManager::SyncScreenlockStatus()
92 {
93 IAM_LOGI("start");
94 bool screenLockState = ContextAppStateObserverManager::GetInstance().IsScreenLocked();
95 if (!screenLockState) {
96 IAM_LOGI("screen is not locked");
97 return;
98 }
99 RiskEventManager::GetInstance().OnScreenLock();
100 }
101
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)102 void ScreenlockStatusListener::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)
103 {
104 std::string action = data.GetWant().GetAction();
105 int32_t userId = data.GetWant().GetIntParam("userId", INVALID_USER_ID);
106 IAM_LOGI("OnReceiveEvent %{public}s, userId = %{public}d", action.c_str(), userId);
107 if (userId == INVALID_USER_ID) {
108 IAM_LOGE("Event userId invalid");
109 return;
110 }
111 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) {
112 RiskEventManager::GetInstance().OnScreenLock();
113 }
114 }
115 } // namespace UserAuth
116 } // namespace UserIam
117 } // namespace OHOS