• 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 "iremote_broker.h"
18 
19 #include "sclock_log.h"
20 #include "screenlock_common.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 
IsScreenLocked()31 bool ScreenLockManagerProxy::IsScreenLocked()
32 {
33     MessageParcel data, 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, reply;
50     MessageOption option;
51     data.WriteInterfaceToken(ScreenLockManagerProxy::GetDescriptor());
52     SCLOCK_HILOGD("ScreenLockManagerProxy GetSecure started.");
53     bool ret = Remote()->SendRequest(IS_SECURE_MODE, data, reply, option);
54     if (ret != ERR_NONE) {
55         SCLOCK_HILOGE("GetSecure, ret = %{public}d", ret);
56         return false;
57     }
58     SCLOCK_HILOGD("ScreenLockManagerProxy GetSecure succeeded.");
59     bool result = reply.ReadBool();
60     return result;
61 }
62 
RequestUnlock(const sptr<ScreenLockSystemAbilityInterface> & listener)63 void ScreenLockManagerProxy::RequestUnlock(const sptr<ScreenLockSystemAbilityInterface> &listener)
64 {
65     MessageParcel data, reply;
66     MessageOption option;
67     data.WriteInterfaceToken(GetDescriptor());
68     SCLOCK_HILOGD("ScreenLockManagerProxy RequestUnlock started.");
69     if (listener == nullptr) {
70         SCLOCK_HILOGE("listener is nullptr");
71         return;
72     }
73     if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
74         SCLOCK_HILOGE("write parcel failed.");
75         return;
76     }
77     int32_t ret = Remote()->SendRequest(REQUEST_UNLOCK, data, reply, option);
78     if (ret != ERR_NONE) {
79         SCLOCK_HILOGE("RequestUnlock, ret = %{public}d", ret);
80         return;
81     }
82     SCLOCK_HILOGD("ScreenLockManagerProxy RequestUnlock succeeded.");
83 }
84 
On(const sptr<ScreenLockSystemAbilityInterface> & listener,const std::string & type)85 bool ScreenLockManagerProxy::On(const sptr<ScreenLockSystemAbilityInterface> &listener, const std::string &type)
86 {
87     SCLOCK_HILOGD("ScreenLockManagerProxy::On listener=%{public}p", listener.GetRefPtr());
88     MessageParcel data, reply;
89     MessageOption option;
90     if (!data.WriteInterfaceToken(GetDescriptor())) {
91         SCLOCK_HILOGE(" Failed to write parcelable ");
92         return false;
93     }
94     if (listener == nullptr) {
95         SCLOCK_HILOGE("listener is nullptr");
96         return false;
97     }
98     SCLOCK_HILOGD("ScreenLockManagerProxy::On type=%{public}s", type.c_str());
99     if (type.empty()) {
100         SCLOCK_HILOGE("ScreenLockManagerProxy::On type is null.");
101         return false;
102     }
103     if (!data.WriteString(type)) {
104         SCLOCK_HILOGE("write type failed.");
105         return false;
106     }
107     if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
108         SCLOCK_HILOGE("write parcel failed.");
109         return false;
110     }
111     int32_t result = Remote()->SendRequest(ON, data, reply, option);
112     if (result != ERR_NONE) {
113         SCLOCK_HILOGE(" ScreenLockManagerProxy::On fail, result = %{public}d ", result);
114         return false;
115     }
116     int32_t status = reply.ReadInt32();
117     bool ret = (status == 0) ? true : false;
118     SCLOCK_HILOGD("ScreenLockManagerProxy::On out");
119     return ret;
120 }
121 
Off(const std::string & type)122 bool ScreenLockManagerProxy::Off(const std::string &type)
123 {
124     SCLOCK_HILOGD("ScreenLockManagerProxy::Off in");
125     MessageParcel data, reply;
126     MessageOption option;
127     if (!data.WriteInterfaceToken(GetDescriptor())) {
128         SCLOCK_HILOGE(" Failed to write parcelable ");
129         return false;
130     }
131     if (!data.WriteString(type)) {
132         SCLOCK_HILOGE("write type=%{public}s fail", type.c_str());
133         return false;
134     }
135     int32_t result = Remote()->SendRequest(OFF, data, reply, option);
136     if (result != ERR_NONE) {
137         SCLOCK_HILOGE(" ScreenLockManagerProxy::Off fail, result = %{public}d ", result);
138         return false;
139     }
140     int32_t status = reply.ReadInt32();
141     bool ret = (status == 0) ? true : false;
142     SCLOCK_HILOGD("ScreenLockManagerProxy::Off out");
143     return ret;
144 }
145 
SendScreenLockEvent(const std::string & event,int param)146 bool ScreenLockManagerProxy::SendScreenLockEvent(const std::string &event, int param)
147 {
148     MessageParcel data, reply;
149     MessageOption option;
150     bool bFlag = false;
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 false;
159     }
160     bFlag = reply.ReadBool();
161     SCLOCK_HILOGD("ScreenLockManagerProxy SendScreenLockEvent succeeded.");
162     return bFlag;
163 }
164 
Test_SetScreenLocked(const bool isScreenlocked)165 bool ScreenLockManagerProxy::Test_SetScreenLocked(const bool isScreenlocked)
166 {
167     MessageParcel data, reply;
168     MessageOption option;
169     bool bFlag = false;
170     data.WriteInterfaceToken(GetDescriptor());
171     SCLOCK_HILOGD("ScreenLockManagerProxy Test_SetScreenLocked started.");
172     data.WriteBool(isScreenlocked);
173     int32_t ret = Remote()->SendRequest(TEST_SET_SCREENLOCKED, data, reply, option);
174     if (ret != ERR_NONE) {
175         SCLOCK_HILOGE("ScreenLockManagerProxy Test_SetScreenLocked, ret = %{public}d", ret);
176         return false;
177     }
178     bFlag = reply.ReadBool();
179     SCLOCK_HILOGD("ScreenLockManagerProxy Test_SetScreenLocked succeeded.");
180     return bFlag;
181 }
182 
Test_RuntimeNotify(const std::string & event,int param)183 bool ScreenLockManagerProxy::Test_RuntimeNotify(const std::string &event, int param)
184 {
185     MessageParcel data, reply;
186     MessageOption option;
187     bool bFlag = false;
188     data.WriteInterfaceToken(GetDescriptor());
189     SCLOCK_HILOGD("ScreenLockManagerProxy Test_RuntimeNotify started.  event=%{public}s", event.c_str());
190     SCLOCK_HILOGD("ScreenLockManagerProxy Test_RuntimeNotify started.  param=%{public}d", param);
191     data.WriteString(event);
192     data.WriteInt32(param);
193     int32_t ret = Remote()->SendRequest(TEST_RUNTIME_NOTIFY, data, reply, option);
194     if (ret != ERR_NONE) {
195         SCLOCK_HILOGE("ScreenLockManagerProxy SendScreenLockEvent, ret = %{public}d", ret);
196         return false;
197     }
198     bFlag = reply.ReadBool();
199     SCLOCK_HILOGD("ScreenLockManagerProxy Test_RuntimeNotify succeeded.");
200     return bFlag;
201 }
202 
Test_GetRuntimeState(const std::string & event)203 int ScreenLockManagerProxy::Test_GetRuntimeState(const std::string &event)
204 {
205     MessageParcel data, reply;
206     MessageOption option;
207     int bFlag = -100;
208     data.WriteInterfaceToken(GetDescriptor());
209     SCLOCK_HILOGD("ScreenLockManagerProxy Test_GetRuntimeState started.  event=%{public}s", event.c_str());
210     data.WriteString(event);
211     int32_t ret = Remote()->SendRequest(TEST_GET_RUNTIME_STATE, data, reply, option);
212     if (ret != ERR_NONE) {
213         SCLOCK_HILOGE("ScreenLockManagerProxy Test_GetRuntimeState, ret = %{public}d", ret);
214         return false;
215     }
216     bFlag = reply.ReadInt32();
217     SCLOCK_HILOGD("ScreenLockManagerProxy Test_GetRuntimeState succeeded.");
218     return bFlag;
219 }
220 } // namespace ScreenLock
221 } // namespace OHOS