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
18 #include "system_ability_definition.h"
19 #include "iservice_registry.h"
20
21 #include "sclock_log.h"
22
23 namespace OHOS {
24 namespace ScreenLock {
25 std::mutex ScreenLockManager::instanceLock_;
26 sptr<ScreenLockManager> ScreenLockManager::instance_;
27 sptr<ScreenLockManagerInterface> ScreenLockManager::screenlockManagerProxy_;
28 sptr<ScreenLockSaDeathRecipient> ScreenLockManager::deathRecipient_;
29
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 screenlockManagerProxy_ = GetScreenLockManagerProxy();
45 }
46 }
47 return instance_;
48 }
49
IsScreenLocked()50 bool ScreenLockManager::IsScreenLocked()
51 {
52 if (screenlockManagerProxy_ == nullptr) {
53 SCLOCK_HILOGW("Redo GetScreenLockManagerProxy");
54 screenlockManagerProxy_ = GetScreenLockManagerProxy();
55 }
56 if (screenlockManagerProxy_ == nullptr) {
57 SCLOCK_HILOGE("IsScreenLocked quit because redoing GetScreenLockManagerProxy failed.");
58 return false;
59 }
60 SCLOCK_HILOGD("ScreenLockManager IsScreenLocked succeeded.");
61 return screenlockManagerProxy_->IsScreenLocked();
62 }
63
GetSecure()64 bool ScreenLockManager::GetSecure()
65 {
66 if (screenlockManagerProxy_ == nullptr) {
67 SCLOCK_HILOGW("Redo GetScreenLockManagerProxy");
68 screenlockManagerProxy_ = GetScreenLockManagerProxy();
69 }
70 if (screenlockManagerProxy_ == nullptr) {
71 SCLOCK_HILOGE("GetSecure quit because redoing GetScreenLockManagerProxy failed.");
72 return false;
73 }
74 SCLOCK_HILOGD("ScreenLockManager GetSecure succeeded.");
75 return screenlockManagerProxy_->GetSecure();
76 }
77
RequestUnlock(const sptr<ScreenLockSystemAbilityInterface> & listener)78 void ScreenLockManager::RequestUnlock(const sptr<ScreenLockSystemAbilityInterface> &listener)
79 {
80 if (screenlockManagerProxy_ == nullptr) {
81 SCLOCK_HILOGW("Redo GetScreenLockManagerProxy");
82 screenlockManagerProxy_ = GetScreenLockManagerProxy();
83 }
84 if (screenlockManagerProxy_ == nullptr) {
85 SCLOCK_HILOGE("RequestUnlock quit because redoing GetScreenLockManagerProxy failed.");
86 return;
87 }
88 if (listener == nullptr) {
89 SCLOCK_HILOGE("listener is nullptr.");
90 return;
91 }
92 SCLOCK_HILOGD("ScreenLockManager RequestUnlock succeeded.");
93 screenlockManagerProxy_->RequestUnlock(listener);
94 }
95
Test_SetScreenLocked(bool isScreenlocked)96 bool ScreenLockManager::Test_SetScreenLocked(bool isScreenlocked)
97 {
98 bool flag = false;
99 if (screenlockManagerProxy_ == nullptr) {
100 SCLOCK_HILOGW("Redo GetScreenLockManagerProxy");
101 screenlockManagerProxy_ = GetScreenLockManagerProxy();
102 }
103 if (screenlockManagerProxy_ == nullptr) {
104 SCLOCK_HILOGE("ScreenLockManager::Test_SetScreenLocked quit because redoing GetScreenLockManagerProxy failed.");
105 return false;
106 }
107 SCLOCK_HILOGD("ScreenLockManager::Test_SetScreenLocked succeeded.");
108 flag = screenlockManagerProxy_->Test_SetScreenLocked(isScreenlocked);
109 return flag;
110 }
111
Test_RuntimeNotify(const std::string & event,int param)112 bool ScreenLockManager::Test_RuntimeNotify(const std::string &event, int param)
113 {
114 bool flag = false;
115 if (screenlockManagerProxy_ == nullptr) {
116 SCLOCK_HILOGW("Redo GetScreenLockManagerProxy");
117 screenlockManagerProxy_ = GetScreenLockManagerProxy();
118 }
119 if (screenlockManagerProxy_ == nullptr) {
120 SCLOCK_HILOGE("ScreenLockManager::Test_RuntimeNotify quit because redoing GetScreenLockManagerProxy failed.");
121 return false;
122 }
123 SCLOCK_HILOGD("ScreenLockManager::Test_RuntimeNotify succeeded. event=%{public}s", event.c_str());
124 SCLOCK_HILOGD("ScreenLockManager::Test_RuntimeNotify succeeded. param=%{public}d", param);
125 flag = screenlockManagerProxy_->Test_RuntimeNotify(event, param);
126 return flag;
127 }
128
Test_GetRuntimeState(const std::string & event)129 int ScreenLockManager::Test_GetRuntimeState(const std::string &event)
130 {
131 int flag = -100;
132 if (screenlockManagerProxy_ == nullptr) {
133 SCLOCK_HILOGW("Redo GetScreenLockManagerProxy");
134 screenlockManagerProxy_ = GetScreenLockManagerProxy();
135 }
136 if (screenlockManagerProxy_ == nullptr) {
137 SCLOCK_HILOGE("ScreenLockManager::Test_GetRuntimeState quit because redoing GetScreenLockManagerProxy failed.");
138 return false;
139 }
140 SCLOCK_HILOGD("ScreenLockManager::Test_GetRuntimeState succeeded. event=%{public}s", event.c_str());
141 flag = screenlockManagerProxy_->Test_GetRuntimeState(event);
142 return flag;
143 }
144
GetScreenLockManagerProxy()145 sptr<ScreenLockManagerInterface> ScreenLockManager::GetScreenLockManagerProxy()
146 {
147 sptr<ISystemAbilityManager> systemAbilityManager =
148 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
149 if (systemAbilityManager == nullptr) {
150 SCLOCK_HILOGE("Getting SystemAbilityManager failed.");
151 return nullptr;
152 }
153 auto systemAbility = systemAbilityManager->GetSystemAbility(SCREENLOCK_SERVICE_ID, "");
154 if (systemAbility == nullptr) {
155 SCLOCK_HILOGE("Get SystemAbility failed.");
156 return nullptr;
157 }
158 deathRecipient_ = new ScreenLockSaDeathRecipient();
159 systemAbility->AddDeathRecipient(deathRecipient_);
160 sptr<ScreenLockManagerInterface> screenlockServiceProxy = iface_cast<ScreenLockManagerInterface>(systemAbility);
161 if (screenlockServiceProxy == nullptr) {
162 SCLOCK_HILOGE("Get ScreenLockManagerProxy from SA failed.");
163 return nullptr;
164 }
165 SCLOCK_HILOGD("Getting ScreenLockManagerProxy succeeded.");
166 return screenlockServiceProxy;
167 }
168
OnRemoteSaDied(const wptr<IRemoteObject> & remote)169 void ScreenLockManager::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
170 {
171 screenlockManagerProxy_ = GetScreenLockManagerProxy();
172 }
173
ScreenLockSaDeathRecipient()174 ScreenLockSaDeathRecipient::ScreenLockSaDeathRecipient()
175 {
176 }
177
OnRemoteDied(const wptr<IRemoteObject> & object)178 void ScreenLockSaDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
179 {
180 SCLOCK_HILOGE("ScreenLockSaDeathRecipient on remote systemAbility died.");
181 ScreenLockManager::GetInstance()->OnRemoteSaDied(object);
182 }
183 } // namespace ScreenLock
184 } // namespace OHOS
185