1 /*
2 * Copyright (c) 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 "keyguard_status_listener.h"
17
18 #include "context_appstate_observer.h"
19 #include "common_event_subscribe_info.h"
20 #include "credential_info_interface.h"
21 #include "iam_logger.h"
22 #include "matching_skills.h"
23 #include "singleton.h"
24 #include "user_idm_database.h"
25 #include "want.h"
26
27 #define LOG_TAG "USER_AUTH_SA"
28
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
GetInstance()32 KeyguardStatusListenerManager &KeyguardStatusListenerManager::GetInstance()
33 {
34 static KeyguardStatusListenerManager instance;
35 return instance;
36 }
37
RegisterCommonEventListener()38 ResultCode KeyguardStatusListenerManager::RegisterCommonEventListener()
39 {
40 IAM_LOGI("start");
41 std::lock_guard<std::recursive_mutex> lock(mutex_);
42 if (commonEventListener_ != nullptr) {
43 IAM_LOGI("commonEventListener_ is not nullptr");
44 return SUCCESS;
45 }
46 commonEventListener_ = SystemAbilityListener::Subscribe("common_event_service", COMMON_EVENT_SERVICE_ID,
47 [this]() {RegisterKeyguardStatusSwitchCallback();},
48 [this]() {UnRegisterKeyguardStatusSwitchCallback();});
49 if (commonEventListener_ == nullptr) {
50 IAM_LOGE("commonEventListener_ is nullptr");
51 return GENERAL_ERROR;
52 }
53
54 IAM_LOGI("RegisterCommonEventListener success");
55 return SUCCESS;
56 }
57
UnRegisterCommonEventListener()58 ResultCode KeyguardStatusListenerManager::UnRegisterCommonEventListener()
59 {
60 IAM_LOGI("start");
61 std::lock_guard<std::recursive_mutex> lock(mutex_);
62 if (commonEventListener_ == nullptr) {
63 IAM_LOGI("commonEventListener_ is nullptr");
64 return SUCCESS;
65 }
66
67 if (SystemAbilityListener::UnSubscribe(COMMON_EVENT_SERVICE_ID, commonEventListener_) != SUCCESS) {
68 IAM_LOGE("UnRegisterCommonEventListener failed");
69 return GENERAL_ERROR;
70 }
71
72 commonEventListener_ = nullptr;
73 IAM_LOGI("UnRegisterCommonEventListener success");
74 return SUCCESS;
75 }
76
RegisterKeyguardStatusSwitchCallback()77 void KeyguardStatusListenerManager::RegisterKeyguardStatusSwitchCallback()
78 {
79 IAM_LOGI("start");
80 std::lock_guard<std::recursive_mutex> lock(mutex_);
81 if (isRegisterKeyguardStatus_) {
82 IAM_LOGI("KeyguardStatusListener already registered");
83 return;
84 }
85 EventFwk::MatchingSkills matchingSkills;
86 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
87 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
88
89 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
90 auto subscriber = std::make_shared<KeyguardStatusListener>(subscribeInfo);
91 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber)) {
92 IAM_LOGE("SubscribeCommonEvent fail");
93 return;
94 }
95 isRegisterKeyguardStatus_ = true;
96 IAM_LOGI("SubscribeCommonEvent success");
97 }
98
UnRegisterKeyguardStatusSwitchCallback()99 void KeyguardStatusListenerManager::UnRegisterKeyguardStatusSwitchCallback()
100 {
101 IAM_LOGI("start");
102 std::lock_guard<std::recursive_mutex> lock(mutex_);
103 if (!isRegisterKeyguardStatus_) {
104 IAM_LOGI("KeyguardStatusListener already registered");
105 return;
106 }
107 EventFwk::MatchingSkills matchingSkills;
108 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
109 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
110
111 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
112 auto subscriber = std::make_shared<KeyguardStatusListener>(subscribeInfo);
113 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber)) {
114 IAM_LOGE("UnSubscribeCommonEvent failed");
115 return;
116 }
117 isRegisterKeyguardStatus_ = false;
118 IAM_LOGI("UnSubscribeCommonEvent success");
119 }
120
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)121 void KeyguardStatusListener::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)
122 {
123 std::string action = data.GetWant().GetAction();
124 int32_t userId = data.GetWant().GetIntParam("userId", INVALID_USER_ID);
125 IAM_LOGI("OnReceiveEvent %{public}s, userId = %{public}d", action.c_str(), userId);
126 if (userId == INVALID_USER_ID) {
127 IAM_LOGE("Event userId invalid");
128 return;
129 }
130
131 std::vector<std::shared_ptr<CredentialInfoInterface>> credInfos;
132 int32_t ret = UserIdmDatabase::Instance().GetCredentialInfo(userId, PIN, credInfos);
133 if (ret != SUCCESS) {
134 IAM_LOGE("get credential fail, ret:%{public}d, userId:%{public}d", ret, userId);
135 return;
136 }
137 if (credInfos.empty()) {
138 IAM_LOGE("no cred enrolled, don't SetScreenLockState");
139 ContextAppStateObserverManager::GetInstance().RemoveScreenLockState(userId);
140 return;
141 }
142
143 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) {
144 ContextAppStateObserverManager::GetInstance().SetScreenLockState(true, userId);
145 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
146 ContextAppStateObserverManager::GetInstance().SetScreenLockState(false, userId);
147 }
148 };
149 } // namespace UserAuth
150 } // namespace UserIam
151 } // namespace OHOS