• 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 #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_LABEL UserIam::Common::LABEL_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 
107     if (action != CommonEventSupport::COMMON_EVENT_SCREEN_ON && action != CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
108         return;
109     }
110     IAM_LOGI("receive event %{public}s", action.c_str());
111     if (action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
112         ScreenStateMonitor::GetInstance().SetScreenOn(true);
113     } else {
114         ScreenStateMonitor::GetInstance().SetScreenOn(false);
115     }
116 }
117 
GetInstance()118 ScreenStateMonitor &ScreenStateMonitor::GetInstance()
119 {
120     static ScreenStateMonitor monitor;
121     return monitor;
122 }
123 
Subscribe()124 void ScreenStateMonitor::Subscribe()
125 {
126     std::lock_guard<std::mutex> lock(mutex_);
127 
128     if (isSubscribing_) {
129         IAM_LOGI("duplicate subscribe");
130         return;
131     }
132 
133     ResultCode result = EventSubscriber::Subscribe();
134     IF_FALSE_LOGE_AND_RETURN(result == ResultCode::SUCCESS);
135     isSubscribing_ = true;
136     isOn_ = PowerMgrClient::GetInstance().IsScreenOn();
137     IAM_LOGI("screen on %{public}d", isOn_);
138 }
139 
Unsubscribe()140 void ScreenStateMonitor::Unsubscribe()
141 {
142     std::lock_guard<std::mutex> lock(mutex_);
143 
144     EventSubscriber::Unsubscribe();
145     isSubscribing_ = false;
146 }
147 
SetScreenOn(bool isOn)148 void ScreenStateMonitor::SetScreenOn(bool isOn)
149 {
150     std::lock_guard<std::mutex> lock(mutex_);
151 
152     isOn_ = isOn;
153     IAM_LOGI("set screen on %{public}d", isOn_);
154 }
155 
GetScreenOn()156 bool ScreenStateMonitor::GetScreenOn()
157 {
158     std::lock_guard<std::mutex> lock(mutex_);
159 
160     return isOn_;
161 }
162 } // namespace FingerprintAuth
163 } // namespace UserIam
164 } // namespace OHOS