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 "iam_logger.h"
21 #include "matching_skills.h"
22 #include "singleton.h"
23 #include "want.h"
24
25 #define LOG_TAG "USER_AUTH_SA"
26
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
GetInstance()30 KeyguardStatusListenerManager &KeyguardStatusListenerManager::GetInstance()
31 {
32 static KeyguardStatusListenerManager instance;
33 return instance;
34 }
35
RegisterCommonEventListener()36 ResultCode KeyguardStatusListenerManager::RegisterCommonEventListener()
37 {
38 IAM_LOGI("start");
39 std::lock_guard<std::recursive_mutex> lock(mutex_);
40 if (commonEventListener_ != nullptr) {
41 IAM_LOGI("commonEventListener_ is not nullptr");
42 return SUCCESS;
43 }
44 commonEventListener_ = SystemAbilityListener::Subscribe("common_event_service", COMMON_EVENT_SERVICE_ID,
45 [this]() {RegisterKeyguardStatusSwitchCallback();},
46 [this]() {UnRegisterKeyguardStatusSwitchCallback();});
47 if (commonEventListener_ == nullptr) {
48 IAM_LOGE("commonEventListener_ is nullptr");
49 return GENERAL_ERROR;
50 }
51
52 IAM_LOGI("RegisterCommonEventListener success");
53 return SUCCESS;
54 }
55
UnRegisterCommonEventListener()56 ResultCode KeyguardStatusListenerManager::UnRegisterCommonEventListener()
57 {
58 IAM_LOGI("start");
59 std::lock_guard<std::recursive_mutex> lock(mutex_);
60 if (commonEventListener_ == nullptr) {
61 IAM_LOGI("commonEventListener_ is nullptr");
62 return SUCCESS;
63 }
64
65 if (SystemAbilityListener::UnSubscribe(COMMON_EVENT_SERVICE_ID, commonEventListener_) != SUCCESS) {
66 IAM_LOGE("UnRegisterCommonEventListener failed");
67 return GENERAL_ERROR;
68 }
69
70 commonEventListener_ = nullptr;
71 IAM_LOGI("UnRegisterCommonEventListener success");
72 return SUCCESS;
73 }
74
RegisterKeyguardStatusSwitchCallback()75 void KeyguardStatusListenerManager::RegisterKeyguardStatusSwitchCallback()
76 {
77 IAM_LOGI("start");
78 std::lock_guard<std::recursive_mutex> lock(mutex_);
79 if (isRegisterKeyguardStatus_) {
80 IAM_LOGI("KeyguardStatusListener already registered");
81 return;
82 }
83 EventFwk::MatchingSkills matchingSkills;
84 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
85 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
86
87 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
88 auto subscriber = std::make_shared<KeyguardStatusListener>(subscribeInfo);
89 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber)) {
90 IAM_LOGE("SubscribeCommonEvent fail");
91 return;
92 }
93 isRegisterKeyguardStatus_ = true;
94 IAM_LOGI("SubscribeCommonEvent success");
95 }
96
UnRegisterKeyguardStatusSwitchCallback()97 void KeyguardStatusListenerManager::UnRegisterKeyguardStatusSwitchCallback()
98 {
99 IAM_LOGI("start");
100 std::lock_guard<std::recursive_mutex> lock(mutex_);
101 if (!isRegisterKeyguardStatus_) {
102 IAM_LOGI("KeyguardStatusListener already registered");
103 return;
104 }
105 EventFwk::MatchingSkills matchingSkills;
106 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
107 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
108
109 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
110 auto subscriber = std::make_shared<KeyguardStatusListener>(subscribeInfo);
111 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber)) {
112 IAM_LOGE("UnSubscribeCommonEvent failed");
113 return;
114 }
115 isRegisterKeyguardStatus_ = false;
116 IAM_LOGI("UnSubscribeCommonEvent success");
117 }
118
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)119 void KeyguardStatusListener::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)
120 {
121 std::string action = data.GetWant().GetAction();
122 IAM_LOGI("OnReceiveEvent %{public}s", action.c_str());
123
124 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) {
125 ContextAppStateObserverManager::GetInstance().SetScreenLockState(true);
126 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
127 ContextAppStateObserverManager::GetInstance().SetScreenLockState(false);
128 }
129 };
130 } // namespace UserAuth
131 } // namespace UserIam
132 } // namespace OHOS