• 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     if (executors_ != nullptr) {
98         executors_->Execute(GetTask(0));
99     }
100 }
101 
UnsubscribeScreenEvent()102 void ScreenLock::UnsubscribeScreenEvent()
103 {
104     auto res = CommonEventManager::UnSubscribeCommonEvent(eventSubscriber_);
105     if (!res) {
106         ZLOGW("unregister screen event fail res:%d", res);
107     }
108 }
109 
NotifyScreenUnlocked(int32_t user)110 void ScreenLock::NotifyScreenUnlocked(int32_t user)
111 {
112     observerMap_.ForEach([user](const auto &key, auto &val) {
113         val->OnScreenUnlocked(user);
114         return false;
115     });
116 }
117 
BindExecutor(std::shared_ptr<ExecutorPool> executors)118 void ScreenLock::BindExecutor(std::shared_ptr<ExecutorPool> executors)
119 {
120     executors_ = executors;
121 }
122 
GetTask(uint32_t retry)123 ExecutorPool::Task ScreenLock::GetTask(uint32_t retry)
124 {
125     return [this, retry] {
126         auto result = CommonEventManager::SubscribeCommonEvent(eventSubscriber_);
127         if (result) {
128             ZLOGI("success to register subscriber.");
129             return;
130         }
131         ZLOGD("fail to register subscriber, error:%{public}d, time:%{public}d", result, retry);
132 
133         if (retry + 1 > MAX_RETRY_TIME) {
134             ZLOGE("fail to register subscriber!");
135             return;
136         }
137         if (executors_ != nullptr) {
138             executors_->Schedule(std::chrono::seconds(RETRY_WAIT_TIME_S), GetTask(retry + 1));
139         }
140     };
141 }
142 } // namespace OHOS::DistributedData