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
GetSecure()74 bool ScreenLockManagerProxy::GetSecure()
75 {
76 MessageParcel data;
77 MessageParcel reply;
78 MessageOption option;
79 data.WriteInterfaceToken(ScreenLockManagerProxy::GetDescriptor());
80 SCLOCK_HILOGD("ScreenLockManagerProxy GetSecure started.");
81 bool ret = Remote()->SendRequest(
82 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SECURE_MODE), data, reply, option);
83 if (ret != ERR_NONE) {
84 SCLOCK_HILOGE("GetSecure, ret = %{public}d", ret);
85 return false;
86 }
87 return reply.ReadBool();
88 }
89
UnlockInner(MessageParcel & reply,int32_t command,const sptr<ScreenLockCallbackInterface> & listener)90 int32_t ScreenLockManagerProxy::UnlockInner(
91 MessageParcel &reply, int32_t command, const sptr<ScreenLockCallbackInterface> &listener)
92 {
93 MessageParcel data;
94 MessageOption option;
95 data.WriteInterfaceToken(GetDescriptor());
96 SCLOCK_HILOGD("started.");
97 if (listener == nullptr) {
98 SCLOCK_HILOGE("listener is nullptr");
99 return E_SCREENLOCK_NULLPTR;
100 }
101 if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
102 SCLOCK_HILOGE("write parcel failed.");
103 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
104 }
105 int32_t ret = Remote()->SendRequest(command, data, reply, option);
106 if (ret != ERR_NONE) {
107 SCLOCK_HILOGE("RequestUnlock, ret = %{public}d", ret);
108 return E_SCREENLOCK_SENDREQUEST_FAILED;
109 }
110 return E_SCREENLOCK_OK;
111 }
112
Unlock(const sptr<ScreenLockCallbackInterface> & listener)113 int32_t ScreenLockManagerProxy::Unlock(const sptr<ScreenLockCallbackInterface> &listener)
114 {
115 MessageParcel reply;
116 int ret = UnlockInner(reply, static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNLOCK), listener);
117 if (ret != E_SCREENLOCK_OK) {
118 SCLOCK_HILOGE("Unlock, ret = %{public}d", ret);
119 return ret;
120 }
121 return reply.ReadInt32();
122 }
123
UnlockScreen(const sptr<ScreenLockCallbackInterface> & listener)124 int32_t ScreenLockManagerProxy::UnlockScreen(const sptr<ScreenLockCallbackInterface> &listener)
125 {
126 MessageParcel reply;
127 int ret = UnlockInner(reply, static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNLOCK_SCREEN), listener);
128 if (ret != E_SCREENLOCK_OK) {
129 SCLOCK_HILOGE("Unlock, ret = %{public}d", ret);
130 return ret;
131 }
132 return reply.ReadInt32();
133 }
134
Lock(const sptr<ScreenLockCallbackInterface> & listener)135 int32_t ScreenLockManagerProxy::Lock(const sptr<ScreenLockCallbackInterface> &listener)
136 {
137 MessageParcel data;
138 MessageParcel reply;
139 MessageOption option;
140 if (!data.WriteInterfaceToken(GetDescriptor())) {
141 SCLOCK_HILOGE(" Failed to write parcelable ");
142 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
143 }
144 SCLOCK_HILOGD("ScreenLockManagerProxy RequestLock started.");
145 if (listener == nullptr) {
146 SCLOCK_HILOGE("listener is nullptr");
147 return E_SCREENLOCK_NULLPTR;
148 }
149 if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
150 SCLOCK_HILOGE("write parcel failed.");
151 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
152 }
153 int32_t ret =
154 Remote()->SendRequest(static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::LOCK), data, reply, option);
155 if (ret != ERR_NONE) {
156 SCLOCK_HILOGE("RequestLock, ret = %{public}d", ret);
157 return E_SCREENLOCK_SENDREQUEST_FAILED;
158 }
159 int32_t retCode = reply.ReadInt32();
160 SCLOCK_HILOGD("Lock retCode = %{public}d", retCode);
161 return retCode;
162 }
163
OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> & listener)164 int32_t ScreenLockManagerProxy::OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener)
165 {
166 SCLOCK_HILOGD("ScreenLockManagerProxy::OnSystemEvent");
167 MessageParcel data;
168 MessageParcel reply;
169 MessageOption option;
170 if (!data.WriteInterfaceToken(GetDescriptor())) {
171 SCLOCK_HILOGE(" Failed to write parcelable ");
172 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
173 }
174 if (listener == nullptr) {
175 SCLOCK_HILOGE("listener is nullptr");
176 return E_SCREENLOCK_NULLPTR;
177 }
178 if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
179 SCLOCK_HILOGE("write parcel failed.");
180 return E_SCREENLOCK_WRITE_PARCEL_ERROR;
181 }
182 int32_t result = Remote()->SendRequest(
183 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::ONSYSTEMEVENT), data, reply, option);
184 if (result != ERR_NONE) {
185 SCLOCK_HILOGE(" ScreenLockManagerProxy::OnSystemEvent fail, result = %{public}d ", result);
186 return E_SCREENLOCK_SENDREQUEST_FAILED;
187 }
188 int32_t status = reply.ReadInt32();
189 SCLOCK_HILOGD("ScreenLockManagerProxy::OnSystemEvent out status is :%{public}d", status);
190 return status;
191 }
192
SendScreenLockEvent(const std::string & event,int param)193 int32_t ScreenLockManagerProxy::SendScreenLockEvent(const std::string &event, int param)
194 {
195 MessageParcel data;
196 MessageParcel reply;
197 MessageOption option;
198 data.WriteInterfaceToken(GetDescriptor());
199 SCLOCK_HILOGD("ScreenLockManagerProxy SendScreenLockEvent started.");
200 data.WriteString(event);
201 data.WriteInt32(param);
202 int32_t ret = Remote()->SendRequest(
203 static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SEND_SCREENLOCK_EVENT), data, reply, option);
204 if (ret != ERR_NONE) {
205 SCLOCK_HILOGE("ScreenLockManagerProxy SendScreenLockEvent, ret = %{public}d", ret);
206 return E_SCREENLOCK_SENDREQUEST_FAILED;
207 }
208 int32_t retCode = reply.ReadInt32();
209 SCLOCK_HILOGD("ScreenLockManagerProxy SendScreenLockEvent end retCode is %{public}d.", retCode);
210 return retCode;
211 }
212 } // namespace ScreenLock
213 } // namespace OHOS