1 /*
2 * Copyright (C) 2022 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 "screenlock_manager.h"
17 #include "screenlock_manager_proxy.h"
18 #include <hitrace_meter.h>
19
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "sclock_log.h"
23 #include "screenlock_common.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace ScreenLock {
28 std::mutex ScreenLockManager::instanceLock_;
29 sptr<ScreenLockManager> ScreenLockManager::instance_;
ScreenLockManager()30 ScreenLockManager::ScreenLockManager()
31 {
32 }
33
~ScreenLockManager()34 ScreenLockManager::~ScreenLockManager()
35 {
36 }
37
GetInstance()38 sptr<ScreenLockManager> ScreenLockManager::GetInstance()
39 {
40 if (instance_ == nullptr) {
41 std::lock_guard<std::mutex> autoLock(instanceLock_);
42 if (instance_ == nullptr) {
43 instance_ = new ScreenLockManager;
44 }
45 }
46 return instance_;
47 }
48
IsLocked(bool & isLocked)49 int32_t ScreenLockManager::IsLocked(bool &isLocked)
50 {
51 auto proxy = GetProxy();
52 if (proxy == nullptr) {
53 SCLOCK_HILOGE("IsLocked quit because GetScreenLockManagerProxy failed.");
54 return E_SCREENLOCK_SENDREQUEST_FAILED;
55 }
56 return proxy->IsLocked(isLocked);
57 }
58
IsScreenLocked()59 bool ScreenLockManager::IsScreenLocked()
60 {
61 auto proxy = GetProxy();
62 if (proxy == nullptr) {
63 SCLOCK_HILOGE("IsScreenLocked quit because GetScreenLockManagerProxy failed.");
64 return false;
65 }
66 return proxy->IsScreenLocked();
67 }
68
GetSecure()69 bool ScreenLockManager::GetSecure()
70 {
71 auto proxy = GetProxy();
72 if (proxy == nullptr) {
73 SCLOCK_HILOGE("GetSecure quit because redoing GetScreenLockManagerProxy failed.");
74 return false;
75 }
76 return proxy->GetSecure();
77 }
78
Unlock(Action action,const sptr<ScreenLockCallbackInterface> & listener)79 int32_t ScreenLockManager::Unlock(Action action, const sptr<ScreenLockCallbackInterface> &listener)
80 {
81 auto proxy = GetProxy();
82 if (proxy == nullptr) {
83 SCLOCK_HILOGE("RequestUnlock quit because redoing GetScreenLockManagerProxy failed.");
84 return E_SCREENLOCK_NULLPTR;
85 }
86 if (listener == nullptr) {
87 SCLOCK_HILOGE("listener is nullptr.");
88 return E_SCREENLOCK_NULLPTR;
89 }
90 StartAsyncTrace(HITRACE_TAG_MISC, "ScreenLockManager Unlock start", HITRACE_UNLOCKSCREEN);
91 int32_t ret = 0;
92 if (action == Action::UNLOCKSCREEN) {
93 ret = proxy->UnlockScreen(listener);
94 } else {
95 ret = proxy->Unlock(listener);
96 }
97 FinishAsyncTrace(HITRACE_TAG_MISC, "ScreenLockManager Unlock end", HITRACE_UNLOCKSCREEN);
98 return ret;
99 }
100
101
Lock(const sptr<ScreenLockCallbackInterface> & listener)102 int32_t ScreenLockManager::Lock(const sptr<ScreenLockCallbackInterface> &listener)
103 {
104 auto proxy = GetProxy();
105 if (proxy == nullptr) {
106 SCLOCK_HILOGE("RequestLock quit because redoing GetScreenLockManagerProxy failed.");
107 return E_SCREENLOCK_NULLPTR;
108 }
109 if (listener == nullptr) {
110 SCLOCK_HILOGE("listener is nullptr.");
111 return E_SCREENLOCK_NULLPTR;
112 }
113 SCLOCK_HILOGD("ScreenLockManager RequestLock succeeded.");
114 return proxy->Lock(listener);
115 }
116
Lock(int32_t userId)117 int32_t ScreenLockManager::Lock(int32_t userId)
118 {
119 auto proxy = GetProxy();
120 if (proxy == nullptr) {
121 SCLOCK_HILOGE("GetProxy failed.");
122 return E_SCREENLOCK_NULLPTR;
123 }
124 return proxy->Lock(userId);
125 }
126
GetScreenLockManagerProxy()127 sptr<ScreenLockManagerInterface> ScreenLockManager::GetScreenLockManagerProxy()
128 {
129 sptr<ISystemAbilityManager> systemAbilityManager =
130 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
131 if (systemAbilityManager == nullptr) {
132 SCLOCK_HILOGE("Getting SystemAbilityManager failed.");
133 return nullptr;
134 }
135 auto systemAbility = systemAbilityManager->GetSystemAbility(SCREENLOCK_SERVICE_ID, "");
136 if (systemAbility == nullptr) {
137 SCLOCK_HILOGE("Get SystemAbility failed.");
138 return nullptr;
139 }
140 deathRecipient_ = new ScreenLockSaDeathRecipient();
141 systemAbility->AddDeathRecipient(deathRecipient_);
142 sptr<ScreenLockManagerInterface> screenlockServiceProxy = iface_cast<ScreenLockManagerInterface>(systemAbility);
143 if (screenlockServiceProxy == nullptr) {
144 SCLOCK_HILOGE("Get ScreenLockManagerProxy from SA failed.");
145 return nullptr;
146 }
147 SCLOCK_HILOGD("Getting ScreenLockManagerProxy succeeded.");
148 return screenlockServiceProxy;
149 }
150
OnRemoteSaDied(const wptr<IRemoteObject> & remote)151 void ScreenLockManager::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
152 {
153 std::lock_guard<std::mutex> autoLock(managerProxyLock_);
154 screenlockManagerProxy_ = GetScreenLockManagerProxy();
155 }
156
GetProxy()157 sptr<ScreenLockManagerInterface> ScreenLockManager::GetProxy()
158 {
159 if (screenlockManagerProxy_ != nullptr) {
160 return screenlockManagerProxy_;
161 }
162 std::lock_guard<std::mutex> autoLock(managerProxyLock_);
163 if (screenlockManagerProxy_ == nullptr) {
164 SCLOCK_HILOGW("Redo GetScreenLockManagerProxy");
165 screenlockManagerProxy_ = GetScreenLockManagerProxy();
166 }
167 return screenlockManagerProxy_;
168 }
169 } // namespace ScreenLock
170 } // namespace OHOS
171