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 #include "screenlock_manager_proxy.h"
16
17 #include "hilog/log_cpp.h"
18 #include "iremote_broker.h"
19 #include "sclock_log.h"
20 #include "screenlock_server_ipc_interface_code.h"
21
22 namespace OHOS {
23 namespace ScreenLock {
24 using namespace OHOS::HiviewDFX;
25
ScreenLockManagerProxy(const sptr<IRemoteObject> & object)26 ScreenLockManagerProxy::ScreenLockManagerProxy(const sptr<IRemoteObject> &object)
27 : IRemoteProxy<ScreenLockManagerInterface>(object)
28 {
29 }
30
IsScreenLockedInner(MessageParcel & reply,uint32_t command)31 int32_t ScreenLockManagerProxy::IsScreenLockedInner(MessageParcel &reply, uint32_t command)
32 {
33 MessageParcel data;
34 MessageOption option;
35 if (!data.WriteInterfaceToken(GetDescriptor())) {
36 SCLOCK_HILOGE(" Failed to write parcelable ");
37 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
38 }
39 int32_t ret = Remote()->SendRequest(command, data, reply, option);
40 if (ret != ERR_NONE) {
41 SCLOCK_HILOGE("IsScreenLocked, ret = %{public}d", ret);
42 return E_SCREENLOCK_SENDREQUEST_FAILED;
43 }
44 return E_SCREENLOCK_OK;
45 }
46
IsLocked(bool & isLocked)47 int32_t ScreenLockManagerProxy::IsLocked(bool &isLocked)
48 {
49 MessageParcel reply;
50 int32_t ret = IsScreenLockedInner(reply, static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_LOCKED));
51 if (ret != E_SCREENLOCK_OK) {
52 SCLOCK_HILOGE("IsLocked, ret = %{public}d", ret);
53 return ret;
54 }
55 int errCode = reply.ReadInt32();
56 if (errCode != E_SCREENLOCK_OK) {
57 SCLOCK_HILOGE("IsLocked, errCode = %{public}d", errCode);
58 return errCode;
59 }
60 isLocked = reply.ReadBool();
61 return E_SCREENLOCK_OK;
62 }
63
IsScreenLocked()64 bool ScreenLockManagerProxy::IsScreenLocked()
65 {
66 MessageParcel reply;
67 int32_t ret = IsScreenLockedInner(reply, static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SCREEN_LOCKED));
68 if (ret != E_SCREENLOCK_OK) {
69 return false;
70 }
71 return reply.ReadBool();
72 }
73
IsLockedWithUserId(int userId,bool & isLocked)74 int32_t ScreenLockManagerProxy::IsLockedWithUserId(int userId, bool &isLocked)
75 {
76 MessageParcel data;
77 MessageParcel reply;
78 MessageOption option;
79 data.WriteInterfaceToken(GetDescriptor());
80 data.WriteInt32(userId);
81 int32_t ret = Remote()->SendRequest(
82 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_USER_SCREEN_LOCKED), data, reply, option);
83 if (ret != ERR_NONE) {
84 SCLOCK_HILOGE("ScreenLockManagerProxy IsLockedWithUserId, ret = %{public}d", ret);
85 return E_SCREENLOCK_SENDREQUEST_FAILED;
86 }
87 int32_t retCode = reply.ReadInt32();
88 isLocked = reply.ReadBool();
89 SCLOCK_HILOGD("IsLockedWithUserId end retCode is %{public}d, %{public}d.", retCode, isLocked);
90 return retCode;
91 }
92
GetSecure()93 bool ScreenLockManagerProxy::GetSecure()
94 {
95 MessageParcel data;
96 MessageParcel reply;
97 MessageOption option;
98 data.WriteInterfaceToken(ScreenLockManagerProxy::GetDescriptor());
99 SCLOCK_HILOGD("ScreenLockManagerProxy GetSecure started.");
100 bool ret = Remote()->SendRequest(
101 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SECURE_MODE), data, reply, option);
102 if (ret != ERR_NONE) {
103 SCLOCK_HILOGE("GetSecure, ret = %{public}d", ret);
104 return false;
105 }
106 return reply.ReadBool();
107 }
108
UnlockInner(MessageParcel & reply,int32_t command,const sptr<ScreenLockCallbackInterface> & listener)109 int32_t ScreenLockManagerProxy::UnlockInner(
110 MessageParcel &reply, int32_t command, const sptr<ScreenLockCallbackInterface> &listener)
111 {
112 MessageParcel data;
113 MessageOption option;
114 data.WriteInterfaceToken(GetDescriptor());
115 SCLOCK_HILOGD("started.");
116 if (listener == nullptr) {
117 SCLOCK_HILOGE("listener is nullptr");
118 return E_SCREENLOCK_NULLPTR;
119 }
120 if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
121 SCLOCK_HILOGE("write parcel failed.");
122 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
123 }
124 int32_t ret = Remote()->SendRequest(command, data, reply, option);
125 if (ret != ERR_NONE) {
126 SCLOCK_HILOGE("RequestUnlock, ret = %{public}d", ret);
127 return E_SCREENLOCK_SENDREQUEST_FAILED;
128 }
129 return E_SCREENLOCK_OK;
130 }
131
Unlock(const sptr<ScreenLockCallbackInterface> & listener)132 int32_t ScreenLockManagerProxy::Unlock(const sptr<ScreenLockCallbackInterface> &listener)
133 {
134 MessageParcel reply;
135 int ret = UnlockInner(reply, static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNLOCK), listener);
136 if (ret != E_SCREENLOCK_OK) {
137 SCLOCK_HILOGE("Unlock, ret = %{public}d", ret);
138 return ret;
139 }
140 return reply.ReadInt32();
141 }
142
UnlockScreen(const sptr<ScreenLockCallbackInterface> & listener)143 int32_t ScreenLockManagerProxy::UnlockScreen(const sptr<ScreenLockCallbackInterface> &listener)
144 {
145 MessageParcel reply;
146 int ret = UnlockInner(reply, static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNLOCK_SCREEN), listener);
147 if (ret != E_SCREENLOCK_OK) {
148 SCLOCK_HILOGE("Unlock, ret = %{public}d", ret);
149 return ret;
150 }
151 return reply.ReadInt32();
152 }
153
Lock(const sptr<ScreenLockCallbackInterface> & listener)154 int32_t ScreenLockManagerProxy::Lock(const sptr<ScreenLockCallbackInterface> &listener)
155 {
156 MessageParcel data;
157 MessageParcel reply;
158 MessageOption option;
159 if (!data.WriteInterfaceToken(GetDescriptor())) {
160 SCLOCK_HILOGE(" Failed to write parcelable ");
161 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
162 }
163 SCLOCK_HILOGD("ScreenLockManagerProxy RequestLock started.");
164 if (listener == nullptr) {
165 SCLOCK_HILOGE("listener is nullptr");
166 return E_SCREENLOCK_NULLPTR;
167 }
168 if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
169 SCLOCK_HILOGE("write parcel failed.");
170 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
171 }
172 int32_t ret =
173 Remote()->SendRequest(static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::LOCK), data, reply, option);
174 if (ret != ERR_NONE) {
175 SCLOCK_HILOGE("RequestLock, ret = %{public}d", ret);
176 return E_SCREENLOCK_SENDREQUEST_FAILED;
177 }
178 int32_t retCode = reply.ReadInt32();
179 SCLOCK_HILOGD("Lock retCode = %{public}d", retCode);
180 return retCode;
181 }
182
Lock(int32_t userId)183 int32_t ScreenLockManagerProxy::Lock(int32_t userId)
184 {
185 MessageParcel data;
186 MessageParcel reply;
187 MessageOption option;
188 if (!data.WriteInterfaceToken(GetDescriptor())) {
189 SCLOCK_HILOGE(" Failed to write parcelable ");
190 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
191 }
192 if (!data.WriteInt32(userId)) {
193 SCLOCK_HILOGE(" Failed to write userId");
194 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
195 }
196 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::LOCK_SCREEN), data,
197 reply, option);
198 if (ret != ERR_NONE) {
199 SCLOCK_HILOGE("RequestLock failed, ret = %{public}d", ret);
200 return E_SCREENLOCK_SENDREQUEST_FAILED;
201 }
202 int32_t retCode = reply.ReadInt32();
203 SCLOCK_HILOGD("Lock retCode = %{public}d", retCode);
204 return retCode;
205 }
206
OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> & listener)207 int32_t ScreenLockManagerProxy::OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener)
208 {
209 SCLOCK_HILOGD("ScreenLockManagerProxy::OnSystemEvent");
210 MessageParcel data;
211 MessageParcel reply;
212 MessageOption option;
213 if (!data.WriteInterfaceToken(GetDescriptor())) {
214 SCLOCK_HILOGE(" Failed to write parcelable ");
215 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
216 }
217 if (listener == nullptr) {
218 SCLOCK_HILOGE("listener is nullptr");
219 return E_SCREENLOCK_NULLPTR;
220 }
221 if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
222 SCLOCK_HILOGE("write parcel failed.");
223 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
224 }
225 int32_t result = Remote()->SendRequest(
226 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::ONSYSTEMEVENT), data, reply, option);
227 if (result != ERR_NONE) {
228 SCLOCK_HILOGE(" ScreenLockManagerProxy::OnSystemEvent fail, result = %{public}d ", result);
229 return E_SCREENLOCK_SENDREQUEST_FAILED;
230 }
231 int32_t status = reply.ReadInt32();
232 SCLOCK_HILOGD("ScreenLockManagerProxy::OnSystemEvent out status is :%{public}d", status);
233 return status;
234 }
235
SendScreenLockEvent(const std::string & event,int param)236 int32_t ScreenLockManagerProxy::SendScreenLockEvent(const std::string &event, int param)
237 {
238 MessageParcel data;
239 MessageParcel reply;
240 MessageOption option;
241 data.WriteInterfaceToken(GetDescriptor());
242 SCLOCK_HILOGD("ScreenLockManagerProxy SendScreenLockEvent started.");
243 data.WriteString(event);
244 data.WriteInt32(param);
245 int32_t ret = Remote()->SendRequest(
246 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SEND_SCREENLOCK_EVENT), data, reply, option);
247 if (ret != ERR_NONE) {
248 SCLOCK_HILOGE("ScreenLockManagerProxy SendScreenLockEvent, ret = %{public}d", ret);
249 return E_SCREENLOCK_SENDREQUEST_FAILED;
250 }
251 int32_t retCode = reply.ReadInt32();
252 SCLOCK_HILOGD("ScreenLockManagerProxy SendScreenLockEvent end retCode is %{public}d.", retCode);
253 return retCode;
254 }
255
IsScreenLockDisabled(int userId,bool & isDisabled)256 int32_t ScreenLockManagerProxy::IsScreenLockDisabled(int userId, bool &isDisabled)
257 {
258 MessageParcel data;
259 MessageParcel reply;
260 MessageOption option;
261 data.WriteInterfaceToken(GetDescriptor());
262 data.WriteInt32(userId);
263 SCLOCK_HILOGD("ScreenLockManagerProxy IsScreenLockDisabled started.");
264 int32_t ret = Remote()->SendRequest(
265 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SCREENLOCK_DISABLED), data, reply, option);
266 if (ret != ERR_NONE) {
267 SCLOCK_HILOGE("IsScreenLockDisabled SendRequest failed, ret = %{public}d", ret);
268 return E_SCREENLOCK_SENDREQUEST_FAILED;
269 }
270 int32_t retCode = reply.ReadInt32();
271 if (retCode != E_SCREENLOCK_OK) {
272 return retCode;
273 }
274 isDisabled = reply.ReadBool();
275 SCLOCK_HILOGD("IsScreenLockDisabled end retCode is %{public}d, %{public}d.", retCode, isDisabled);
276 return retCode;
277 }
278
SetScreenLockDisabled(bool disable,int userId)279 int32_t ScreenLockManagerProxy::SetScreenLockDisabled(bool disable, int userId)
280 {
281 MessageParcel data;
282 MessageParcel reply;
283 MessageOption option;
284 data.WriteInterfaceToken(GetDescriptor());
285 data.WriteBool(disable);
286 data.WriteInt32(userId);
287 SCLOCK_HILOGD("ScreenLockManagerProxy SetScreenLockDisabled started.");
288 int32_t ret = Remote()->SendRequest(
289 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SET_SCREENLOCK_DISABLED), data, reply, option);
290 if (ret != ERR_NONE) {
291 SCLOCK_HILOGE("SetScreenLockDisabled SendRequest failed, ret = %{public}d", ret);
292 return E_SCREENLOCK_SENDREQUEST_FAILED;
293 }
294 int32_t retCode = reply.ReadInt32();
295 SCLOCK_HILOGD("IsScreenLockDisabled end retCode is %{public}d, %{public}d.", retCode, disable);
296 return retCode;
297 }
298
SetScreenLockAuthState(int authState,int32_t userId,std::string & authToken)299 int32_t ScreenLockManagerProxy::SetScreenLockAuthState(int authState, int32_t userId, std::string &authToken)
300 {
301 MessageParcel data;
302 MessageParcel reply;
303 MessageOption option;
304 data.WriteInterfaceToken(GetDescriptor());
305 data.WriteInt32(authState);
306 data.WriteInt32(userId);
307 data.WriteString(authToken);
308
309 int32_t ret = Remote()->SendRequest(
310 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SET_SCREENLOCK_AUTHSTATE), data, reply, option);
311 if (ret != ERR_NONE) {
312 SCLOCK_HILOGE("ScreenLockManagerProxy SetScreenLockAuthState, ret = %{public}d", ret);
313 return E_SCREENLOCK_SENDREQUEST_FAILED;
314 }
315 int32_t retCode = reply.ReadInt32();
316 SCLOCK_HILOGD("ScreenLockManagerProxy SetScreenLockAuthState end retCode is %{public}d.", retCode);
317 return retCode;
318 }
319
GetScreenLockAuthState(int userId,int32_t & authState)320 int32_t ScreenLockManagerProxy::GetScreenLockAuthState(int userId, int32_t &authState)
321 {
322 MessageParcel data;
323 MessageParcel reply;
324 MessageOption option;
325 data.WriteInterfaceToken(GetDescriptor());
326 data.WriteInt32(userId);
327
328 int32_t ret = Remote()->SendRequest(
329 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::GET_SCREENLOCK_AUTHSTATE), data, reply, option);
330 if (ret != ERR_NONE) {
331 SCLOCK_HILOGE("ScreenLockManagerProxy GetScreenLockAuthState, ret = %{public}d", ret);
332 return E_SCREENLOCK_SENDREQUEST_FAILED;
333 }
334 int32_t retCode = reply.ReadInt32();
335 authState = reply.ReadInt32();
336 SCLOCK_HILOGD("GetScreenLockAuthState end retCode is %{public}d, %{public}d.", retCode, authState);
337 return retCode;
338 }
339
RequestStrongAuth(int reasonFlag,int32_t userId)340 int32_t ScreenLockManagerProxy::RequestStrongAuth(int reasonFlag, int32_t userId)
341 {
342 MessageParcel data;
343 MessageParcel reply;
344 MessageOption option;
345 data.WriteInterfaceToken(GetDescriptor());
346 data.WriteInt32(reasonFlag);
347 data.WriteInt32(userId);
348
349 int32_t ret = Remote()->SendRequest(
350 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::REQUEST_STRONG_AUTHSTATE), data, reply, option);
351 if (ret != ERR_NONE) {
352 SCLOCK_HILOGE("ScreenLockManagerProxy RequestStrongAuth, ret = %{public}d", ret);
353 return E_SCREENLOCK_SENDREQUEST_FAILED;
354 }
355 int32_t retCode = reply.ReadInt32();
356 SCLOCK_HILOGD("ScreenLockManagerProxy RequestStrongAuth end retCode is %{public}d.", retCode);
357 return retCode;
358 }
359
GetStrongAuth(int userId,int32_t & reasonFlag)360 int32_t ScreenLockManagerProxy::GetStrongAuth(int userId, int32_t &reasonFlag)
361 {
362 MessageParcel data;
363 MessageParcel reply;
364 MessageOption option;
365 data.WriteInterfaceToken(GetDescriptor());
366 data.WriteInt32(userId);
367
368 int32_t ret = Remote()->SendRequest(
369 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::GET_STRONG_AUTHSTATE), data, reply, option);
370 if (ret != ERR_NONE) {
371 SCLOCK_HILOGE("ScreenLockManagerProxy GetStrongAuth, ret = %{public}d", ret);
372 return E_SCREENLOCK_SENDREQUEST_FAILED;
373 }
374 int32_t retCode = reply.ReadInt32();
375 reasonFlag = reply.ReadInt32();
376 SCLOCK_HILOGD("GetStrongAuth end retCode is %{public}d, %{public}d.", retCode, reasonFlag);
377 return retCode;
378 }
379
IsDeviceLocked(int userId,bool & isDeviceLocked)380 int32_t ScreenLockManagerProxy::IsDeviceLocked(int userId, bool &isDeviceLocked)
381 {
382 MessageParcel data;
383 MessageParcel reply;
384 MessageOption option;
385 data.WriteInterfaceToken(GetDescriptor());
386 data.WriteInt32(userId);
387
388 int32_t ret = Remote()->SendRequest(
389 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_DEVICE_LOCKED), data, reply, option);
390 if (ret != ERR_NONE) {
391 SCLOCK_HILOGE("ScreenLockManagerProxy IsDeviceLocked, ret = %{public}d", ret);
392 return E_SCREENLOCK_SENDREQUEST_FAILED;
393 }
394 int32_t retCode = reply.ReadInt32();
395 isDeviceLocked = reply.ReadBool();
396 SCLOCK_HILOGD("IsDeviceLocked end retCode is %{public}d, %{public}d.", retCode, isDeviceLocked);
397 return retCode;
398 }
399
RegisterInnerListener(const int32_t userId,const ListenType listenType,const sptr<InnerListenerIf> & listener)400 int32_t ScreenLockManagerProxy::RegisterInnerListener(const int32_t userId, const ListenType listenType,
401 const sptr<InnerListenerIf>& listener)
402 {
403 MessageParcel data;
404 MessageParcel reply;
405 MessageOption option;
406 SCLOCK_HILOGD("RegisterInnerListener started.");
407 data.WriteInterfaceToken(GetDescriptor());
408 data.WriteInt32(userId);
409 data.WriteInt32(static_cast<int32_t>(listenType));
410 if (listener == nullptr) {
411 SCLOCK_HILOGE("listener is nullptr");
412 return E_SCREENLOCK_NULLPTR;
413 }
414 if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
415 SCLOCK_HILOGE("write parcel failed.");
416 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
417 }
418 int32_t ret = Remote()->SendRequest(
419 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::REGISTER_INNER_LISTENER), data, reply, option);
420 if (ret != ERR_NONE) {
421 SCLOCK_HILOGE("RegisterInnerListener, ret = %{public}d", ret);
422 return E_SCREENLOCK_SENDREQUEST_FAILED;
423 }
424 int32_t retCode = reply.ReadInt32();
425 SCLOCK_HILOGD("RegisterInnerListener end retCode is %{public}d.", retCode);
426 return retCode;
427 }
428
UnRegisterInnerListener(const int32_t userId,const ListenType listenType,const sptr<InnerListenerIf> & listener)429 int32_t ScreenLockManagerProxy::UnRegisterInnerListener(const int32_t userId, const ListenType listenType,
430 const sptr<InnerListenerIf>& listener)
431 {
432 MessageParcel data;
433 MessageParcel reply;
434 MessageOption option;
435 SCLOCK_HILOGD("UnRegisterInnerListener started.");
436 data.WriteInterfaceToken(GetDescriptor());
437 data.WriteInt32(userId);
438 data.WriteInt32(static_cast<int32_t>(listenType));
439 if (listener == nullptr) {
440 SCLOCK_HILOGE("listener is nullptr");
441 return E_SCREENLOCK_NULLPTR;
442 }
443 if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
444 SCLOCK_HILOGE("write parcel failed.");
445 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
446 }
447 int32_t ret = Remote()->SendRequest(
448 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNREGISTER_INNER_LISTENER), data, reply, option);
449 if (ret != ERR_NONE) {
450 SCLOCK_HILOGE("UnRegisterInnerListener, ret = %{public}d", ret);
451 return E_SCREENLOCK_SENDREQUEST_FAILED;
452 }
453 int32_t retCode = reply.ReadInt32();
454 SCLOCK_HILOGD("UnRegisterInnerListener end retCode is %{public}d.", retCode);
455 return retCode;
456 }
457 } // namespace ScreenLock
458 } // namespace OHOS