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 #include "power_mgr_client.h"
23 #include "system_ability_definition.h"
24 #include "system_ability_status_change_stub.h"
25 #include "want.h"
26 
27 #include "iam_check.h"
28 #include "iam_logger.h"
29 #include "iam_ptr.h"
30 
31 #include "sa_command_manager.h"
32 
33 #define LOG_TAG "FINGERPRINT_AUTH_SA"
34 
35 namespace OHOS {
36 namespace UserIam {
37 namespace FingerprintAuth {
38 using namespace EventFwk;
39 using namespace AAFwk;
40 using namespace PowerMgr;
41 using ResultCode = UserAuth::ResultCode;
42 
43 namespace {
GetCommonEventSubscribeInfo()44 const CommonEventSubscribeInfo GetCommonEventSubscribeInfo()
45 {
46     MatchingSkills matchingSkills;
47     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
48     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
49     return CommonEventSubscribeInfo(matchingSkills);
50 }
51 } // namespace
52 
53 class EventSubscriber : public CommonEventSubscriber, public NoCopyable {
54 public:
EventSubscriber(const CommonEventSubscribeInfo & subscribeInfo)55     explicit EventSubscriber(const CommonEventSubscribeInfo &subscribeInfo) : CommonEventSubscriber(subscribeInfo) {};
~EventSubscriber()56     ~EventSubscriber() override {};
57 
58     static std::shared_ptr<EventSubscriber> GetInstance();
59     static ResultCode Subscribe();
60     static void Unsubscribe();
61 
62     void OnReceiveEvent(const CommonEventData &eventData) override;
63 };
64 
GetInstance()65 std::shared_ptr<EventSubscriber> EventSubscriber::GetInstance()
66 {
67     static auto subscriber = Common::MakeShared<EventSubscriber>(GetCommonEventSubscribeInfo());
68     if (subscriber == nullptr) {
69         IAM_LOGE("EventSubscriber is null");
70     }
71     return subscriber;
72 }
73 
Subscribe()74 ResultCode EventSubscriber::Subscribe()
75 {
76     EventSubscriber::Unsubscribe();
77     auto instance = GetInstance();
78     IF_FALSE_LOGE_AND_RETURN_VAL(instance != NULL, ResultCode::GENERAL_ERROR);
79 
80     int32_t ret = CommonEventManager::NewSubscribeCommonEvent(instance);
81     if (ret != ERR_OK) {
82         IAM_LOGE("subscribe fail, ret = %{public}d", ret);
83         return ResultCode::GENERAL_ERROR;
84     }
85     IAM_LOGI("subscribe success");
86     return ResultCode::SUCCESS;
87 }
88 
Unsubscribe()89 void EventSubscriber::Unsubscribe()
90 {
91     auto instance = GetInstance();
92     IF_FALSE_LOGE_AND_RETURN(instance != NULL);
93 
94     int32_t ret = CommonEventManager::NewUnSubscribeCommonEvent(instance);
95     if (ret != ERR_OK) {
96         IAM_LOGE("unsubscribe fail, ret = %{public}d", ret);
97         return;
98     }
99     IAM_LOGI("unsubscribe success");
100 }
101 
OnReceiveEvent(const CommonEventData & eventData)102 void EventSubscriber::OnReceiveEvent(const CommonEventData &eventData)
103 {
104     const Want &want = eventData.GetWant();
105     std::string action = want.GetAction();
106     if (action != CommonEventSupport::COMMON_EVENT_SCREEN_ON && action != CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
107         return;
108     }
109     IAM_LOGI("receive event %{public}s", action.c_str());
110     if (action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
111         ScreenStateMonitor::GetInstance().SetScreenOn(true);
112     } else {
113         ScreenStateMonitor::GetInstance().SetScreenOn(false);
114     }
115 }
116 
GetInstance()117 ScreenStateMonitor &ScreenStateMonitor::GetInstance()
118 {
119     static ScreenStateMonitor monitor;
120     return monitor;
121 }
122 
Subscribe()123 void ScreenStateMonitor::Subscribe()
124 {
125     std::lock_guard<std::mutex> lock(mutex_);
126 
127     if (isSubscribing_) {
128         IAM_LOGI("duplicate subscribe");
129         return;
130     }
131 
132     ResultCode result = EventSubscriber::Subscribe();
133     IF_FALSE_LOGE_AND_RETURN(result == ResultCode::SUCCESS);
134     isSubscribing_ = true;
135     isOn_ = PowerMgrClient::GetInstance().IsScreenOn();
136     IAM_LOGI("screen on %{public}d", isOn_);
137 }
138 
Unsubscribe()139 void ScreenStateMonitor::Unsubscribe()
140 {
141     std::lock_guard<std::mutex> lock(mutex_);
142 
143     EventSubscriber::Unsubscribe();
144     isSubscribing_ = false;
145 }
146 
SetScreenOn(bool isOn)147 void ScreenStateMonitor::SetScreenOn(bool isOn)
148 {
149     std::lock_guard<std::mutex> lock(mutex_);
150 
151     isOn_ = isOn;
152     IAM_LOGI("set screen on %{public}d", isOn_);
153 }
154 
GetScreenOn()155 bool ScreenStateMonitor::GetScreenOn()
156 {
157     std::lock_guard<std::mutex> lock(mutex_);
158 
159     return isOn_;
160 }
161 } // namespace FingerprintAuth
162 } // namespace UserIam
163 } // namespace OHOS