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
21 namespace OHOS {
22 namespace ScreenLock {
23 using namespace OHOS::HiviewDFX;
24
ScreenLockManagerProxy(const sptr<IRemoteObject> & object)25 ScreenLockManagerProxy::ScreenLockManagerProxy(const sptr<IRemoteObject> &object)
26 : IRemoteProxy<ScreenLockManagerInterface>(object)
27 {
28 }
29
IsScreenLocked()30 bool ScreenLockManagerProxy::IsScreenLocked()
31 {
32 MessageParcel data;
33 MessageParcel reply;
34 MessageOption option;
35 data.WriteInterfaceToken(GetDescriptor());
36 SCLOCK_HILOGD("ScreenLockManagerProxy IsScreenLocked started.");
37 bool ret = Remote()->SendRequest(IS_SCREEN_LOCKED, data, reply, option);
38 if (ret != ERR_NONE) {
39 SCLOCK_HILOGE("IsScreenLocked, ret = %{public}d", ret);
40 return false;
41 }
42 SCLOCK_HILOGD("ScreenLockManagerProxy IsScreenLocked succeeded.");
43 bool result = reply.ReadBool();
44 return result;
45 }
46
GetSecure()47 bool ScreenLockManagerProxy::GetSecure()
48 {
49 MessageParcel data;
50 MessageParcel reply;
51 MessageOption option;
52 data.WriteInterfaceToken(ScreenLockManagerProxy::GetDescriptor());
53 SCLOCK_HILOGD("ScreenLockManagerProxy GetSecure started.");
54 bool ret = Remote()->SendRequest(IS_SECURE_MODE, data, reply, option);
55 if (ret != ERR_NONE) {
56 SCLOCK_HILOGE("GetSecure, ret = %{public}d", ret);
57 return false;
58 }
59 SCLOCK_HILOGD("ScreenLockManagerProxy GetSecure succeeded.");
60 bool result = reply.ReadBool();
61 return result;
62 }
63
RequestUnlock(const sptr<ScreenLockSystemAbilityInterface> & listener)64 int32_t ScreenLockManagerProxy::RequestUnlock(const sptr<ScreenLockSystemAbilityInterface> &listener)
65 {
66 MessageParcel data;
67 MessageParcel reply;
68 MessageOption option;
69 data.WriteInterfaceToken(GetDescriptor());
70 SCLOCK_HILOGD("ScreenLockManagerProxy RequestUnlock started.");
71 if (listener == nullptr) {
72 SCLOCK_HILOGE("listener is nullptr");
73 return E_SCREENLOCK_NULLPTR;
74 }
75 if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
76 SCLOCK_HILOGE("write parcel failed.");
77 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
78 }
79 int32_t ret = Remote()->SendRequest(REQUEST_UNLOCK, data, reply, option);
80 if (ret != ERR_NONE) {
81 SCLOCK_HILOGE("RequestUnlock, ret = %{public}d", ret);
82 return E_SCREENLOCK_SENDREQUEST_FAILED;
83 }
84 int32_t retCode = reply.ReadInt32();
85 SCLOCK_HILOGD("ScreenLockManagerProxy RequestUnlock end retCode is %{public}d.", retCode);
86 return retCode;
87 }
88
RequestLock(const sptr<ScreenLockSystemAbilityInterface> & listener)89 int32_t ScreenLockManagerProxy::RequestLock(const sptr<ScreenLockSystemAbilityInterface> &listener)
90 {
91 MessageParcel data;
92 MessageParcel reply;
93 MessageOption option;
94 if (!data.WriteInterfaceToken(GetDescriptor())) {
95 SCLOCK_HILOGE(" Failed to write parcelable ");
96 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
97 }
98 SCLOCK_HILOGD("ScreenLockManagerProxy RequestLock started.");
99 if (listener == nullptr) {
100 SCLOCK_HILOGE("listener is nullptr");
101 return E_SCREENLOCK_NULLPTR;
102 }
103 if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
104 SCLOCK_HILOGE("write parcel failed.");
105 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
106 }
107 int32_t ret = Remote()->SendRequest(REQUEST_LOCK, data, reply, option);
108 if (ret != ERR_NONE) {
109 SCLOCK_HILOGE("RequestLock, ret = %{public}d", ret);
110 return E_SCREENLOCK_SENDREQUEST_FAILED;
111 }
112
113 int32_t retCode = reply.ReadInt32();
114 SCLOCK_HILOGD("ScreenLockManagerProxy RequestLock end .retCode is %{public}d", retCode);
115 return retCode;
116 }
117
OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> & listener)118 int32_t ScreenLockManagerProxy::OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener)
119 {
120 SCLOCK_HILOGD("ScreenLockManagerProxy::OnSystemEvent");
121 MessageParcel data;
122 MessageParcel reply;
123 MessageOption option;
124 if (!data.WriteInterfaceToken(GetDescriptor())) {
125 SCLOCK_HILOGE(" Failed to write parcelable ");
126 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
127 }
128 if (listener == nullptr) {
129 SCLOCK_HILOGE("listener is nullptr");
130 return E_SCREENLOCK_NULLPTR;
131 }
132 if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
133 SCLOCK_HILOGE("write parcel failed.");
134 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
135 }
136 int32_t result = Remote()->SendRequest(ONSYSTEMEVENT, data, reply, option);
137 if (result != ERR_NONE) {
138 SCLOCK_HILOGE(" ScreenLockManagerProxy::OnSystemEvent fail, result = %{public}d ", result);
139 return E_SCREENLOCK_SENDREQUEST_FAILED;
140 }
141 int32_t status = reply.ReadInt32();
142 SCLOCK_HILOGD("ScreenLockManagerProxy::OnSystemEvent out status is :%{public}d", status);
143 return status;
144 }
145
SendScreenLockEvent(const std::string & event,int param)146 int32_t ScreenLockManagerProxy::SendScreenLockEvent(const std::string &event, int param)
147 {
148 MessageParcel data;
149 MessageParcel reply;
150 MessageOption option;
151 data.WriteInterfaceToken(GetDescriptor());
152 SCLOCK_HILOGD("ScreenLockManagerProxy SendScreenLockEvent started.");
153 data.WriteString(event);
154 data.WriteInt32(param);
155 int32_t ret = Remote()->SendRequest(SEND_SCREENLOCK_EVENT, data, reply, option);
156 if (ret != ERR_NONE) {
157 SCLOCK_HILOGE("ScreenLockManagerProxy SendScreenLockEvent, ret = %{public}d", ret);
158 return E_SCREENLOCK_SENDREQUEST_FAILED;
159 }
160 int32_t retCode = reply.ReadInt32();
161 SCLOCK_HILOGD("ScreenLockManagerProxy SendScreenLockEvent end retCode is %{public}d.", retCode);
162 return retCode;
163 }
164 } // namespace ScreenLock
165 } // namespace OHOS