• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     MessageParcel data;
210     MessageParcel reply;
211     MessageOption option;
212     if (!data.WriteInterfaceToken(GetDescriptor())) {
213         SCLOCK_HILOGE(" Failed to write parcelable ");
214         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
215     }
216     if (listener == nullptr) {
217         SCLOCK_HILOGE("listener is nullptr");
218         return E_SCREENLOCK_NULLPTR;
219     }
220     if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
221         SCLOCK_HILOGE("write parcel failed.");
222         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
223     }
224     int32_t result = Remote()->SendRequest(
225         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::ONSYSTEMEVENT), data, reply, option);
226     if (result != ERR_NONE) {
227         SCLOCK_HILOGE(" ScreenLockManagerProxy::OnSystemEvent fail, result = %{public}d ", result);
228         return E_SCREENLOCK_SENDREQUEST_FAILED;
229     }
230     int32_t status = reply.ReadInt32();
231     SCLOCK_HILOGD("ScreenLockManagerProxy::OnSystemEvent out status is :%{public}d", status);
232     return status;
233 }
234 
SendScreenLockEvent(const std::string & event,int param)235 int32_t ScreenLockManagerProxy::SendScreenLockEvent(const std::string &event, int param)
236 {
237     MessageParcel data;
238     MessageParcel reply;
239     MessageOption option;
240     data.WriteInterfaceToken(GetDescriptor());
241     data.WriteString(event);
242     data.WriteInt32(param);
243     int32_t ret = Remote()->SendRequest(
244         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SEND_SCREENLOCK_EVENT), data, reply, option);
245     if (ret != ERR_NONE) {
246         SCLOCK_HILOGE("ScreenLockManagerProxy SendScreenLockEvent, ret = %{public}d", ret);
247         return E_SCREENLOCK_SENDREQUEST_FAILED;
248     }
249     int32_t retCode = reply.ReadInt32();
250     SCLOCK_HILOGD("ScreenLockManagerProxy SendScreenLockEvent end retCode is %{public}d.", retCode);
251     return retCode;
252 }
253 
IsScreenLockDisabled(int userId,bool & isDisabled)254 int32_t ScreenLockManagerProxy::IsScreenLockDisabled(int userId, bool &isDisabled)
255 {
256     MessageParcel data;
257     MessageParcel reply;
258     MessageOption option;
259     data.WriteInterfaceToken(GetDescriptor());
260     data.WriteInt32(userId);
261     int32_t ret = Remote()->SendRequest(
262         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SCREENLOCK_DISABLED), data, reply, option);
263     if (ret != ERR_NONE) {
264         SCLOCK_HILOGE("IsScreenLockDisabled SendRequest failed, ret = %{public}d", ret);
265         return E_SCREENLOCK_SENDREQUEST_FAILED;
266     }
267     int32_t retCode = reply.ReadInt32();
268     if (retCode != E_SCREENLOCK_OK) {
269         return retCode;
270     }
271     isDisabled = reply.ReadBool();
272     SCLOCK_HILOGD("IsScreenLockDisabled end retCode is %{public}d, %{public}d.", retCode, isDisabled);
273     return retCode;
274 }
275 
SetScreenLockDisabled(bool disable,int userId)276 int32_t ScreenLockManagerProxy::SetScreenLockDisabled(bool disable, int userId)
277 {
278     MessageParcel data;
279     MessageParcel reply;
280     MessageOption option;
281     data.WriteInterfaceToken(GetDescriptor());
282     data.WriteBool(disable);
283     data.WriteInt32(userId);
284     int32_t ret = Remote()->SendRequest(
285         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SET_SCREENLOCK_DISABLED), data, reply, option);
286     if (ret != ERR_NONE) {
287         SCLOCK_HILOGE("SetScreenLockDisabled SendRequest failed, ret = %{public}d", ret);
288         return E_SCREENLOCK_SENDREQUEST_FAILED;
289     }
290     int32_t retCode = reply.ReadInt32();
291     SCLOCK_HILOGD("IsScreenLockDisabled end retCode is %{public}d, %{public}d.", retCode, disable);
292     return retCode;
293 }
294 
SetScreenLockAuthState(int authState,int32_t userId,std::string & authToken)295 int32_t ScreenLockManagerProxy::SetScreenLockAuthState(int authState, int32_t userId, std::string &authToken)
296 {
297     MessageParcel data;
298     MessageParcel reply;
299     MessageOption option;
300     data.WriteInterfaceToken(GetDescriptor());
301     data.WriteInt32(authState);
302     data.WriteInt32(userId);
303     data.WriteString(authToken);
304 
305     int32_t ret = Remote()->SendRequest(
306         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SET_SCREENLOCK_AUTHSTATE), data, reply, option);
307     if (ret != ERR_NONE) {
308         SCLOCK_HILOGE("ScreenLockManagerProxy SetScreenLockAuthState, ret = %{public}d", ret);
309         return E_SCREENLOCK_SENDREQUEST_FAILED;
310     }
311     int32_t retCode = reply.ReadInt32();
312     SCLOCK_HILOGD("ScreenLockManagerProxy SetScreenLockAuthState end retCode is %{public}d.", retCode);
313     return retCode;
314 }
315 
GetScreenLockAuthState(int userId,int32_t & authState)316 int32_t ScreenLockManagerProxy::GetScreenLockAuthState(int userId, int32_t &authState)
317 {
318     MessageParcel data;
319     MessageParcel reply;
320     MessageOption option;
321     data.WriteInterfaceToken(GetDescriptor());
322     data.WriteInt32(userId);
323 
324     int32_t ret = Remote()->SendRequest(
325         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::GET_SCREENLOCK_AUTHSTATE), data, reply, option);
326     if (ret != ERR_NONE) {
327         SCLOCK_HILOGE("ScreenLockManagerProxy GetScreenLockAuthState, ret = %{public}d", ret);
328         return E_SCREENLOCK_SENDREQUEST_FAILED;
329     }
330     int32_t retCode = reply.ReadInt32();
331     authState = reply.ReadInt32();
332     SCLOCK_HILOGD("GetScreenLockAuthState end retCode is %{public}d, %{public}d.", retCode, authState);
333     return retCode;
334 }
335 
RequestStrongAuth(int reasonFlag,int32_t userId)336 int32_t ScreenLockManagerProxy::RequestStrongAuth(int reasonFlag, int32_t userId)
337 {
338     MessageParcel data;
339     MessageParcel reply;
340     MessageOption option;
341     data.WriteInterfaceToken(GetDescriptor());
342     data.WriteInt32(reasonFlag);
343     data.WriteInt32(userId);
344 
345     int32_t ret = Remote()->SendRequest(
346         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::REQUEST_STRONG_AUTHSTATE), data, reply, option);
347     if (ret != ERR_NONE) {
348         SCLOCK_HILOGE("ScreenLockManagerProxy RequestStrongAuth, ret = %{public}d", ret);
349         return E_SCREENLOCK_SENDREQUEST_FAILED;
350     }
351     int32_t retCode = reply.ReadInt32();
352     SCLOCK_HILOGD("ScreenLockManagerProxy RequestStrongAuth end retCode is %{public}d.", retCode);
353     return retCode;
354 }
355 
GetStrongAuth(int userId,int32_t & reasonFlag)356 int32_t ScreenLockManagerProxy::GetStrongAuth(int userId, int32_t &reasonFlag)
357 {
358     MessageParcel data;
359     MessageParcel reply;
360     MessageOption option;
361     data.WriteInterfaceToken(GetDescriptor());
362     data.WriteInt32(userId);
363 
364     int32_t ret = Remote()->SendRequest(
365         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::GET_STRONG_AUTHSTATE), data, reply, option);
366     if (ret != ERR_NONE) {
367         SCLOCK_HILOGE("ScreenLockManagerProxy GetStrongAuth, ret = %{public}d", ret);
368         return E_SCREENLOCK_SENDREQUEST_FAILED;
369     }
370     int32_t retCode = reply.ReadInt32();
371     reasonFlag = reply.ReadInt32();
372     SCLOCK_HILOGD("GetStrongAuth end retCode is %{public}d, %{public}d.", retCode, reasonFlag);
373     return retCode;
374 }
375 
IsDeviceLocked(int userId,bool & isDeviceLocked)376 int32_t ScreenLockManagerProxy::IsDeviceLocked(int userId, bool &isDeviceLocked)
377 {
378     MessageParcel data;
379     MessageParcel reply;
380     MessageOption option;
381     data.WriteInterfaceToken(GetDescriptor());
382     data.WriteInt32(userId);
383 
384     int32_t ret = Remote()->SendRequest(
385         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_DEVICE_LOCKED), data, reply, option);
386     if (ret != ERR_NONE) {
387         SCLOCK_HILOGE("ScreenLockManagerProxy IsDeviceLocked, ret = %{public}d", ret);
388         return E_SCREENLOCK_SENDREQUEST_FAILED;
389     }
390     int32_t retCode = reply.ReadInt32();
391     isDeviceLocked = reply.ReadBool();
392     SCLOCK_HILOGD("IsDeviceLocked end retCode is %{public}d, %{public}d.", retCode, isDeviceLocked);
393     return retCode;
394 }
395 
RegisterInnerListener(const int32_t userId,const ListenType listenType,const sptr<InnerListenerIf> & listener)396 int32_t ScreenLockManagerProxy::RegisterInnerListener(const int32_t userId, const ListenType listenType,
397                                                       const sptr<InnerListenerIf>& listener)
398 {
399     MessageParcel data;
400     MessageParcel reply;
401     MessageOption option;
402     data.WriteInterfaceToken(GetDescriptor());
403     data.WriteInt32(userId);
404     data.WriteInt32(static_cast<int32_t>(listenType));
405     if (listener == nullptr) {
406         SCLOCK_HILOGE("listener is nullptr");
407         return E_SCREENLOCK_NULLPTR;
408     }
409     if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
410         SCLOCK_HILOGE("write parcel failed.");
411         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
412     }
413     int32_t ret = Remote()->SendRequest(
414         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::REGISTER_INNER_LISTENER), data, reply, option);
415     if (ret != ERR_NONE) {
416         SCLOCK_HILOGE("RegisterInnerListener, ret = %{public}d", ret);
417         return E_SCREENLOCK_SENDREQUEST_FAILED;
418     }
419     int32_t retCode = reply.ReadInt32();
420     SCLOCK_HILOGD("RegisterInnerListener end retCode is %{public}d.", retCode);
421     return retCode;
422 }
423 
UnRegisterInnerListener(const int32_t userId,const ListenType listenType,const sptr<InnerListenerIf> & listener)424 int32_t ScreenLockManagerProxy::UnRegisterInnerListener(const int32_t userId, const ListenType listenType,
425                                                         const sptr<InnerListenerIf>& listener)
426 {
427     MessageParcel data;
428     MessageParcel reply;
429     MessageOption option;
430     data.WriteInterfaceToken(GetDescriptor());
431     data.WriteInt32(userId);
432     data.WriteInt32(static_cast<int32_t>(listenType));
433     if (listener == nullptr) {
434         SCLOCK_HILOGE("listener is nullptr");
435         return E_SCREENLOCK_NULLPTR;
436     }
437     if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
438         SCLOCK_HILOGE("write parcel failed.");
439         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
440     }
441     int32_t ret = Remote()->SendRequest(
442         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNREGISTER_INNER_LISTENER), data, reply, option);
443     if (ret != ERR_NONE) {
444         SCLOCK_HILOGE("UnRegisterInnerListener, ret = %{public}d", ret);
445         return E_SCREENLOCK_SENDREQUEST_FAILED;
446     }
447     int32_t retCode = reply.ReadInt32();
448     SCLOCK_HILOGD("UnRegisterInnerListener end retCode is %{public}d.", retCode);
449     return retCode;
450 }
451 } // namespace ScreenLock
452 } // namespace OHOS