• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "screen_state_monitor.h"
17 
18 #include "common_event_manager.h"
19 #include "common_event_subscriber.h"
20 #include "common_event_support.h"
21 #include "iservice_registry.h"
22 #ifdef CONFIG_USE_POWER_MANAGER_COMPONENT
23 #include "power_mgr_client.h"
24 #endif
25 #include "system_ability_definition.h"
26 #include "system_ability_status_change_stub.h"
27 #include "want.h"
28 
29 #include "iam_check.h"
30 #include "iam_logger.h"
31 #include "iam_ptr.h"
32 
33 #include "sa_command_manager.h"
34 
35 #define LOG_TAG "FINGERPRINT_AUTH_SA"
36 
37 namespace OHOS {
38 namespace UserIam {
39 namespace FingerprintAuth {
40 using namespace EventFwk;
41 using namespace AAFwk;
42 #ifdef CONFIG_USE_POWER_MANAGER_COMPONENT
43 using namespace PowerMgr;
44 #endif
45 using ResultCode = UserAuth::ResultCode;
46 
47 namespace {
GetCommonEventSubscribeInfo()48 const CommonEventSubscribeInfo GetCommonEventSubscribeInfo()
49 {
50     MatchingSkills matchingSkills;
51     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
52     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
53     return CommonEventSubscribeInfo(matchingSkills);
54 }
55 } // namespace
56 
57 typedef void(*SendEventSubscriberCallback)(const bool isOn);
58 class EventSubscriber : public CommonEventSubscriber, public NoCopyable {
59 public:
EventSubscriber(const CommonEventSubscribeInfo & subscribeInfo)60     explicit EventSubscriber(const CommonEventSubscribeInfo &subscribeInfo) : CommonEventSubscriber(subscribeInfo) {};
~EventSubscriber()61     ~EventSubscriber() override {};
62 
63     static std::shared_ptr<EventSubscriber> GetInstance();
64     static ResultCode Subscribe();
65     static void Unsubscribe();
66 
67     void OnReceiveEvent(const CommonEventData &eventData) override;
68     void SetCallback(SendEventSubscriberCallback callback);
69     SendEventSubscriberCallback GetCallback();
70 private:
71     SendEventSubscriberCallback eventSubscriberCallback_ = nullptr;
72     std::mutex eventSubscriberMutex_;
73 };
74 
GetInstance()75 std::shared_ptr<EventSubscriber> EventSubscriber::GetInstance()
76 {
77     static auto subscriber = Common::MakeShared<EventSubscriber>(GetCommonEventSubscribeInfo());
78     if (subscriber == nullptr) {
79         IAM_LOGE("EventSubscriber is null");
80     }
81     return subscriber;
82 }
83 
Subscribe()84 ResultCode EventSubscriber::Subscribe()
85 {
86     EventSubscriber::Unsubscribe();
87     auto instance = GetInstance();
88     IF_FALSE_LOGE_AND_RETURN_VAL(instance != NULL, ResultCode::GENERAL_ERROR);
89 
90     int32_t ret = CommonEventManager::NewSubscribeCommonEvent(instance);
91     if (ret != ERR_OK) {
92         IAM_LOGE("subscribe fail, ret = %{public}d", ret);
93         return ResultCode::GENERAL_ERROR;
94     }
95     IAM_LOGI("subscribe success");
96     return ResultCode::SUCCESS;
97 }
98 
Unsubscribe()99 void EventSubscriber::Unsubscribe()
100 {
101     auto instance = GetInstance();
102     IF_FALSE_LOGE_AND_RETURN(instance != NULL);
103 
104     int32_t ret = CommonEventManager::NewUnSubscribeCommonEvent(instance);
105     if (ret != ERR_OK) {
106         IAM_LOGE("unsubscribe fail, ret = %{public}d", ret);
107         return;
108     }
109     IAM_LOGI("unsubscribe success");
110 }
111 
OnReceiveEvent(const CommonEventData & eventData)112 void EventSubscriber::OnReceiveEvent(const CommonEventData &eventData)
113 {
114     const Want &want = eventData.GetWant();
115     std::string action = want.GetAction();
116     if (action != CommonEventSupport::COMMON_EVENT_SCREEN_ON && action != CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
117         return;
118     }
119     IAM_LOGI("receive event %{public}s", action.c_str());
120     auto instance = GetInstance();
121     IF_FALSE_LOGE_AND_RETURN(instance != nullptr);
122     SendEventSubscriberCallback eventSubscriberCallback = instance->GetCallback();
123     IF_FALSE_LOGE_AND_RETURN(eventSubscriberCallback != nullptr);
124     if (action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
125         eventSubscriberCallback(true);
126     } else {
127         eventSubscriberCallback(false);
128     }
129 }
130 
SetCallback(SendEventSubscriberCallback callback)131 void EventSubscriber::SetCallback(SendEventSubscriberCallback callback)
132 {
133     std::lock_guard<std::mutex> lock(eventSubscriberMutex_);
134 
135     if (eventSubscriberCallback_ == nullptr) {
136         eventSubscriberCallback_ = callback;
137     }
138 }
139 
GetCallback()140 SendEventSubscriberCallback EventSubscriber::GetCallback()
141 {
142     std::lock_guard<std::mutex> lock(eventSubscriberMutex_);
143 
144     return eventSubscriberCallback_;
145 }
146 
GetInstance()147 ScreenStateMonitor &ScreenStateMonitor::GetInstance()
148 {
149     static ScreenStateMonitor monitor;
150     return monitor;
151 }
152 
Subscribe()153 void ScreenStateMonitor::Subscribe()
154 {
155     std::lock_guard<std::mutex> lock(mutex_);
156 
157     if (isSubscribing_) {
158         IAM_LOGI("duplicate subscribe");
159         return;
160     }
161 
162     auto eventSubscriber = EventSubscriber::GetInstance();
163     IF_FALSE_LOGE_AND_RETURN(eventSubscriber != nullptr);
164     eventSubscriber->SetCallback([](bool isOn) {
165         ScreenStateMonitor::GetInstance().SetScreenOn(isOn);
166     });
167     ResultCode result = EventSubscriber::Subscribe();
168     IF_FALSE_LOGE_AND_RETURN(result == ResultCode::SUCCESS);
169     isSubscribing_ = true;
170 #ifdef CONFIG_USE_POWER_MANAGER_COMPONENT
171     isOn_ = PowerMgrClient::GetInstance().IsScreenOn();
172     IAM_LOGI("screen on %{public}d", isOn_);
173 #endif
174 }
175 
Unsubscribe()176 void ScreenStateMonitor::Unsubscribe()
177 {
178     std::lock_guard<std::mutex> lock(mutex_);
179 
180     EventSubscriber::Unsubscribe();
181     isSubscribing_ = false;
182 }
183 
SetScreenOn(bool isOn)184 void ScreenStateMonitor::SetScreenOn(bool isOn)
185 {
186     std::lock_guard<std::mutex> lock(mutex_);
187 
188     isOn_ = isOn;
189     IAM_LOGI("set screen on %{public}d", isOn_);
190 }
191 
GetScreenOn()192 bool ScreenStateMonitor::GetScreenOn()
193 {
194     std::lock_guard<std::mutex> lock(mutex_);
195 
196     return isOn_;
197 }
198 } // namespace FingerprintAuth
199 } // namespace UserIam
200 } // namespace OHOS