• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "ScreenLock"
17 #include "screen_lock.h"
18 
19 #include "account/account_delegate.h"
20 #include "log_print.h"
21 #include "screenlock_manager.h"
22 
23 namespace OHOS::DistributedData {
24 using namespace OHOS::ScreenLock;
25 __attribute__((used)) static bool g_init =
26     ScreenManager::RegisterInstance(std::static_pointer_cast<ScreenManager>(std::make_shared<ScreenLock>()));
IsLocked()27 bool ScreenLock::IsLocked()
28 {
29     auto manager = ScreenLockManager::GetInstance();
30     if (manager == nullptr) {
31         return false;
32     }
33     return manager->IsScreenLocked();
34 }
35 
EventSubscriber(const CommonEventSubscribeInfo & info)36 EventSubscriber::EventSubscriber(const CommonEventSubscribeInfo &info) : CommonEventSubscriber(info)
37 {
38 }
39 
OnReceiveEvent(const CommonEventData & event)40 void EventSubscriber::OnReceiveEvent(const CommonEventData &event)
41 {
42     const auto want = event.GetWant();
43     const auto action = want.GetAction();
44     if (action != CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
45         return;
46     }
47     ZLOGI("Want Action is %{public}s", action.c_str());
48     auto user = want.GetIntParam(USER_ID, INVALID_USER);
49     if (user == INVALID_USER) {
50         std::vector<int32_t> users;
51         AccountDelegate::GetInstance()->QueryForegroundUsers(users);
52         if (users.empty()) {
53             return;
54         }
55         user = users[0];
56     }
57     eventCallback_(user);
58 }
59 
SetEventCallback(EventCallback callback)60 void EventSubscriber::SetEventCallback(EventCallback callback)
61 {
62     eventCallback_ = callback;
63 }
64 
Subscribe(std::shared_ptr<Observer> observer)65 void ScreenLock::Subscribe(std::shared_ptr<Observer> observer)
66 {
67     if (observer == nullptr || observer->GetName().empty() || observerMap_.Contains(observer->GetName())) {
68         return;
69     }
70     if (!observerMap_.Insert(observer->GetName(), observer)) {
71         ZLOGE("fail, name is %{public}s", observer->GetName().c_str());
72     }
73 }
74 
Unsubscribe(std::shared_ptr<Observer> observer)75 void ScreenLock::Unsubscribe(std::shared_ptr<Observer> observer)
76 {
77     if (observer == nullptr || observer->GetName().empty() || !observerMap_.Contains(observer->GetName())) {
78         return;
79     }
80     if (!observerMap_.Erase(observer->GetName())) {
81         ZLOGD("fail, name is %{public}s", observer->GetName().c_str());
82     }
83 }
84 
SubscribeScreenEvent()85 void ScreenLock::SubscribeScreenEvent()
86 {
87     ZLOGI("Subscribe screen event listener start.");
88     if (eventSubscriber_ == nullptr) {
89         MatchingSkills matchingSkills;
90         matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
91         CommonEventSubscribeInfo info(matchingSkills);
92         eventSubscriber_ = std::make_shared<EventSubscriber>(info);
93         eventSubscriber_->SetEventCallback([this](int32_t user) {
94             NotifyScreenUnlocked(user);
95         });
96     }
97     executors_->Execute(GetTask(0));
98 }
99 
UnsubscribeScreenEvent()100 void ScreenLock::UnsubscribeScreenEvent()
101 {
102     auto res = CommonEventManager::UnSubscribeCommonEvent(eventSubscriber_);
103     if (!res) {
104         ZLOGW("unregister screen event fail res:%d", res);
105     }
106 }
107 
NotifyScreenUnlocked(int32_t user)108 void ScreenLock::NotifyScreenUnlocked(int32_t user)
109 {
110     observerMap_.ForEach([user](const auto &key, auto &val) {
111         val->OnScreenUnlocked(user);
112         return false;
113     });
114 }
115 
BindExecutor(std::shared_ptr<ExecutorPool> executors)116 void ScreenLock::BindExecutor(std::shared_ptr<ExecutorPool> executors)
117 {
118     executors_ = executors;
119 }
120 
GetTask(uint32_t retry)121 ExecutorPool::Task ScreenLock::GetTask(uint32_t retry)
122 {
123     return [this, retry] {
124         auto result = CommonEventManager::SubscribeCommonEvent(eventSubscriber_);
125         if (result) {
126             ZLOGI("success to register subscriber.");
127             return;
128         }
129         ZLOGD("fail to register subscriber, error:%{public}d, time:%{public}d", result, retry);
130 
131         if (retry + 1 > MAX_RETRY_TIME) {
132             ZLOGE("fail to register subscriber!");
133             return;
134         }
135         executors_->Schedule(std::chrono::seconds(RETRY_WAIT_TIME_S), GetTask(retry + 1));
136     };
137 }
138 } // namespace OHOS::DistributedData