• 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 
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 
IsScreenLockedInner(MessageParcel & reply,int32_t command)30 int32_t ScreenLockManagerProxy::IsScreenLockedInner(MessageParcel &reply, int32_t command)
31 {
32     MessageParcel data;
33     MessageOption option;
34     if (!data.WriteInterfaceToken(GetDescriptor())) {
35         SCLOCK_HILOGE(" Failed to write parcelable ");
36         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
37     }
38     int32_t ret = Remote()->SendRequest(command, data, reply, option);
39     if (ret != ERR_NONE) {
40         SCLOCK_HILOGE("IsScreenLocked, ret = %{public}d", ret);
41         return E_SCREENLOCK_SENDREQUEST_FAILED;
42     }
43     return E_SCREENLOCK_OK;
44 }
45 
IsLocked(bool & isLocked)46 int32_t ScreenLockManagerProxy::IsLocked(bool &isLocked)
47 {
48     MessageParcel reply;
49     int32_t ret = IsScreenLockedInner(reply, IS_LOCKED);
50     if (ret != E_SCREENLOCK_OK) {
51         SCLOCK_HILOGE("IsLocked, ret = %{public}d", ret);
52         return ret;
53     }
54     int errCode = reply.ReadInt32();
55     if (errCode != E_SCREENLOCK_OK) {
56         SCLOCK_HILOGE("IsLocked, errCode = %{public}d", errCode);
57         return errCode;
58     }
59     isLocked = reply.ReadBool();
60     return E_SCREENLOCK_OK;
61 }
62 
IsScreenLocked()63 bool ScreenLockManagerProxy::IsScreenLocked()
64 {
65     MessageParcel reply;
66     int32_t ret = IsScreenLockedInner(reply, IS_SCREEN_LOCKED);
67     if (ret != E_SCREENLOCK_OK) {
68         return false;
69     }
70     return reply.ReadBool();
71 }
72 
GetSecure()73 bool ScreenLockManagerProxy::GetSecure()
74 {
75     MessageParcel data;
76     MessageParcel reply;
77     MessageOption option;
78     data.WriteInterfaceToken(ScreenLockManagerProxy::GetDescriptor());
79     SCLOCK_HILOGD("ScreenLockManagerProxy GetSecure started.");
80     bool ret = Remote()->SendRequest(IS_SECURE_MODE, data, reply, option);
81     if (ret != ERR_NONE) {
82         SCLOCK_HILOGE("GetSecure, ret = %{public}d", ret);
83         return false;
84     }
85     return reply.ReadBool();
86 }
87 
UnlockInner(MessageParcel & reply,int32_t command,const sptr<ScreenLockSystemAbilityInterface> & listener)88 int32_t ScreenLockManagerProxy::UnlockInner(
89     MessageParcel &reply, int32_t command, const sptr<ScreenLockSystemAbilityInterface> &listener)
90 {
91     MessageParcel data;
92     MessageOption option;
93     data.WriteInterfaceToken(GetDescriptor());
94     SCLOCK_HILOGD("started.");
95     if (listener == nullptr) {
96         SCLOCK_HILOGE("listener is nullptr");
97         return E_SCREENLOCK_NULLPTR;
98     }
99     if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
100         SCLOCK_HILOGE("write parcel failed.");
101         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
102     }
103     int32_t ret = Remote()->SendRequest(command, data, reply, option);
104     if (ret != ERR_NONE) {
105         SCLOCK_HILOGE("RequestUnlock, ret = %{public}d", ret);
106         return E_SCREENLOCK_SENDREQUEST_FAILED;
107     }
108     return E_SCREENLOCK_OK;
109 }
110 
Unlock(const sptr<ScreenLockSystemAbilityInterface> & listener)111 int32_t ScreenLockManagerProxy::Unlock(const sptr<ScreenLockSystemAbilityInterface> &listener)
112 {
113     MessageParcel reply;
114     int ret = UnlockInner(reply, UNLOCK, listener);
115     if (ret != E_SCREENLOCK_OK) {
116         SCLOCK_HILOGE("Unlock, ret = %{public}d", ret);
117         return ret;
118     }
119     return reply.ReadInt32();
120 }
121 
UnlockScreen(const sptr<ScreenLockSystemAbilityInterface> & listener)122 int32_t ScreenLockManagerProxy::UnlockScreen(const sptr<ScreenLockSystemAbilityInterface> &listener)
123 {
124     MessageParcel reply;
125     int ret = UnlockInner(reply, UNLOCK_SCREEN, listener);
126     if (ret != E_SCREENLOCK_OK) {
127         SCLOCK_HILOGE("UnlockScreen, ret = %{public}d", ret);
128         return ret;
129     }
130     return reply.ReadInt32();
131 }
132 
Lock(const sptr<ScreenLockSystemAbilityInterface> & listener)133 int32_t ScreenLockManagerProxy::Lock(const sptr<ScreenLockSystemAbilityInterface> &listener)
134 {
135     MessageParcel data;
136     MessageParcel reply;
137     MessageOption option;
138     if (!data.WriteInterfaceToken(GetDescriptor())) {
139         SCLOCK_HILOGE(" Failed to write parcelable ");
140         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
141     }
142     SCLOCK_HILOGD("ScreenLockManagerProxy RequestLock started.");
143     if (listener == nullptr) {
144         SCLOCK_HILOGE("listener is nullptr");
145         return E_SCREENLOCK_NULLPTR;
146     }
147     if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
148         SCLOCK_HILOGE("write parcel failed.");
149         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
150     }
151     int32_t ret = Remote()->SendRequest(LOCK, data, reply, option);
152     if (ret != ERR_NONE) {
153         SCLOCK_HILOGE("RequestLock, ret = %{public}d", ret);
154         return E_SCREENLOCK_SENDREQUEST_FAILED;
155     }
156 
157     int32_t retCode = reply.ReadInt32();
158     SCLOCK_HILOGD("ScreenLockManagerProxy RequestLock end .retCode is %{public}d", retCode);
159     return retCode;
160 }
161 
OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> & listener)162 int32_t ScreenLockManagerProxy::OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener)
163 {
164     SCLOCK_HILOGD("ScreenLockManagerProxy::OnSystemEvent");
165     MessageParcel data;
166     MessageParcel reply;
167     MessageOption option;
168     if (!data.WriteInterfaceToken(GetDescriptor())) {
169         SCLOCK_HILOGE(" Failed to write parcelable ");
170         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
171     }
172     if (listener == nullptr) {
173         SCLOCK_HILOGE("listener is nullptr");
174         return E_SCREENLOCK_NULLPTR;
175     }
176     if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
177         SCLOCK_HILOGE("write parcel failed.");
178         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
179     }
180     int32_t result = Remote()->SendRequest(ONSYSTEMEVENT, data, reply, option);
181     if (result != ERR_NONE) {
182         SCLOCK_HILOGE(" ScreenLockManagerProxy::OnSystemEvent fail, result = %{public}d ", result);
183         return E_SCREENLOCK_SENDREQUEST_FAILED;
184     }
185     int32_t status = reply.ReadInt32();
186     SCLOCK_HILOGD("ScreenLockManagerProxy::OnSystemEvent out status is :%{public}d", status);
187     return status;
188 }
189 
SendScreenLockEvent(const std::string & event,int param)190 int32_t ScreenLockManagerProxy::SendScreenLockEvent(const std::string &event, int param)
191 {
192     MessageParcel data;
193     MessageParcel reply;
194     MessageOption option;
195     data.WriteInterfaceToken(GetDescriptor());
196     SCLOCK_HILOGD("ScreenLockManagerProxy SendScreenLockEvent started.");
197     data.WriteString(event);
198     data.WriteInt32(param);
199     int32_t ret = Remote()->SendRequest(SEND_SCREENLOCK_EVENT, data, reply, option);
200     if (ret != ERR_NONE) {
201         SCLOCK_HILOGE("ScreenLockManagerProxy SendScreenLockEvent, ret = %{public}d", ret);
202         return E_SCREENLOCK_SENDREQUEST_FAILED;
203     }
204     int32_t retCode = reply.ReadInt32();
205     SCLOCK_HILOGD("ScreenLockManagerProxy SendScreenLockEvent end retCode is %{public}d.", retCode);
206     return retCode;
207 }
208 } // namespace ScreenLock
209 } // namespace OHOS