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
16 #include "wallpaper_service_proxy.h"
17
18 #include "file_deal.h"
19 #include "hilog_wrapper.h"
20 #include "i_wallpaper_service.h"
21 #include "iremote_broker.h"
22 #include "wallpaper_common.h"
23
24 namespace OHOS {
25 namespace WallpaperMgrService {
26 using namespace OHOS::HiviewDFX;
GetColors(int32_t wallpaperType,std::vector<uint64_t> & colors)27 ErrorCode WallpaperServiceProxy::GetColors(int32_t wallpaperType, std::vector<uint64_t> &colors)
28 {
29 return GetColorsInner(wallpaperType, WallpaperServiceIpcInterfaceCode::GET_COLORS, colors);
30 }
31
GetColorsV9(int32_t wallpaperType,std::vector<uint64_t> & colors)32 ErrorCode WallpaperServiceProxy::GetColorsV9(int32_t wallpaperType, std::vector<uint64_t> &colors)
33 {
34 return GetColorsInner(wallpaperType, WallpaperServiceIpcInterfaceCode::GET_COLORS_V9, colors);
35 }
36
GetColorsInner(int32_t wallpaperType,WallpaperServiceIpcInterfaceCode code,std::vector<uint64_t> & colors)37 ErrorCode WallpaperServiceProxy::GetColorsInner(
38 int32_t wallpaperType, WallpaperServiceIpcInterfaceCode code, std::vector<uint64_t> &colors)
39 {
40 MessageParcel data;
41 MessageParcel reply;
42 MessageOption option;
43
44 if (!data.WriteInterfaceToken(GetDescriptor())) {
45 HILOG_ERROR(" Failed to write parcelable ");
46 return E_WRITE_PARCEL_ERROR;
47 }
48 if (!data.WriteInt32(wallpaperType)) {
49 HILOG_ERROR(" Failed to WriteInt32 ");
50 return E_WRITE_PARCEL_ERROR;
51 }
52
53 int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
54 if (result != ERR_NONE) {
55 HILOG_ERROR("Get color failed, result = %{public}d ", result);
56 return E_DEAL_FAILED;
57 }
58 ErrorCode wallpaperErrorCode = ConvertIntToErrorCode(reply.ReadInt32());
59 if (wallpaperErrorCode == E_OK) {
60 if (!reply.ReadUInt64Vector(&colors)) {
61 HILOG_ERROR(" Failed to ReadUInt64Vector ");
62 }
63 }
64 return wallpaperErrorCode;
65 }
66
GetFile(int32_t wallpaperType,int32_t & wallpaperFd)67 ErrorCode WallpaperServiceProxy::GetFile(int32_t wallpaperType, int32_t &wallpaperFd)
68 {
69 MessageParcel data;
70 MessageParcel reply;
71 MessageOption option;
72
73 if (!data.WriteInterfaceToken(GetDescriptor())) {
74 HILOG_ERROR(" Failed to write parcelable ");
75 return E_WRITE_PARCEL_ERROR;
76 }
77 if (!data.WriteInt32(wallpaperType)) {
78 HILOG_ERROR(" Failed to WriteInt32 ");
79 return E_WRITE_PARCEL_ERROR;
80 }
81
82 int32_t result =
83 Remote()->SendRequest(static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::GET_FILE), data, reply, option);
84 if (result != ERR_NONE) {
85 HILOG_ERROR(" get file result = %{public}d ", result);
86 return E_DEAL_FAILED;
87 }
88 ErrorCode wallpaperErrorCode = ConvertIntToErrorCode(reply.ReadInt32());
89 if (wallpaperErrorCode == E_OK) {
90 wallpaperFd = reply.ReadFileDescriptor();
91 }
92 return wallpaperErrorCode;
93 }
94
SetWallpaper(int32_t fd,int32_t wallpaperType,int32_t length)95 ErrorCode WallpaperServiceProxy::SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length)
96 {
97 return SetWallpaperInner(fd, wallpaperType, length, WallpaperServiceIpcInterfaceCode::SET_WALLPAPER);
98 }
99
SetWallpaperV9(int32_t fd,int32_t wallpaperType,int32_t length)100 ErrorCode WallpaperServiceProxy::SetWallpaperV9(int32_t fd, int32_t wallpaperType, int32_t length)
101 {
102 return SetWallpaperInner(fd, wallpaperType, length, WallpaperServiceIpcInterfaceCode::SET_WALLPAPER_V9);
103 }
104
SetWallpaperInner(int32_t fd,int32_t wallpaperType,int32_t length,WallpaperServiceIpcInterfaceCode code)105 ErrorCode WallpaperServiceProxy::SetWallpaperInner(
106 int32_t fd, int32_t wallpaperType, int32_t length, WallpaperServiceIpcInterfaceCode code)
107 {
108 HILOG_INFO(" WallpaperServiceProxy::SetWallpaper --> start ");
109 MessageParcel data;
110 MessageParcel reply;
111 MessageOption option;
112
113 if (!data.WriteInterfaceToken(GetDescriptor())) {
114 HILOG_ERROR(" Failed to write parcelable ");
115 return E_WRITE_PARCEL_ERROR;
116 }
117 if (!data.WriteFileDescriptor(fd)) {
118 HILOG_ERROR(" Failed to WriteFileDescriptor ");
119 return E_WRITE_PARCEL_ERROR;
120 }
121
122 if (!data.WriteInt32(wallpaperType)) {
123 HILOG_ERROR(" Failed to WriteInt32 ");
124 return E_WRITE_PARCEL_ERROR;
125 }
126 if (!data.WriteInt32(length)) {
127 HILOG_ERROR(" Failed to WriteInt32 ");
128 return E_WRITE_PARCEL_ERROR;
129 }
130
131 int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
132 if (result != ERR_NONE) {
133 HILOG_ERROR(" WallpaperCallbackProxy::SetWallpaper fail, result = %{public}d ", result);
134 return E_DEAL_FAILED;
135 }
136
137 return ConvertIntToErrorCode(reply.ReadInt32());
138 }
139
GetPixelMap(int32_t wallpaperType,IWallpaperService::FdInfo & fdInfo)140 ErrorCode WallpaperServiceProxy::GetPixelMap(int32_t wallpaperType, IWallpaperService::FdInfo &fdInfo)
141 {
142 return GetPixelMapInner(wallpaperType, WallpaperServiceIpcInterfaceCode::GET_PIXELMAP, fdInfo);
143 }
144
GetPixelMapV9(int32_t wallpaperType,IWallpaperService::FdInfo & fdInfo)145 ErrorCode WallpaperServiceProxy::GetPixelMapV9(int32_t wallpaperType, IWallpaperService::FdInfo &fdInfo)
146 {
147 return GetPixelMapInner(wallpaperType, WallpaperServiceIpcInterfaceCode::GET_PIXELMAP_V9, fdInfo);
148 }
149
SetVideo(int32_t fd,int32_t wallpaperType,int32_t length)150 ErrorCode WallpaperServiceProxy::SetVideo(int32_t fd, int32_t wallpaperType, int32_t length)
151 {
152 MessageParcel data;
153 MessageParcel reply;
154 MessageOption option;
155
156 if (!data.WriteInterfaceToken(GetDescriptor())) {
157 HILOG_ERROR("Failed to write parcelable");
158 return E_WRITE_PARCEL_ERROR;
159 }
160 if (!data.WriteFileDescriptor(fd)) {
161 HILOG_ERROR("Failed to WriteFileDescriptor");
162 return E_WRITE_PARCEL_ERROR;
163 }
164
165 if (!data.WriteInt32(wallpaperType)) {
166 HILOG_ERROR("Failed to WriteInt32");
167 return E_WRITE_PARCEL_ERROR;
168 }
169 if (!data.WriteInt32(length)) {
170 HILOG_ERROR("Failed to WriteInt32");
171 return E_WRITE_PARCEL_ERROR;
172 }
173
174 int32_t result =
175 Remote()->SendRequest(static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SET_VIDEO), data, reply, option);
176 if (result != ERR_NONE) {
177 HILOG_ERROR("Set video failed, result: %{public}d", result);
178 return E_DEAL_FAILED;
179 }
180
181 return ConvertIntToErrorCode(reply.ReadInt32());
182 }
183
SetCustomWallpaper(int32_t fd,int32_t wallpaperType,int32_t length)184 ErrorCode WallpaperServiceProxy::SetCustomWallpaper(int32_t fd, int32_t wallpaperType, int32_t length)
185 {
186 MessageParcel data;
187 MessageParcel reply;
188 MessageOption option;
189
190 if (!data.WriteInterfaceToken(GetDescriptor())) {
191 HILOG_ERROR("Failed to write parcelable");
192 return E_WRITE_PARCEL_ERROR;
193 }
194 if (!data.WriteFileDescriptor(fd)) {
195 HILOG_ERROR("Failed to WriteFD");
196 return E_WRITE_PARCEL_ERROR;
197 }
198 if (!data.WriteInt32(wallpaperType)) {
199 HILOG_ERROR("Failed to WriteInt32");
200 return E_WRITE_PARCEL_ERROR;
201 }
202 if (!data.WriteInt32(length)) {
203 HILOG_ERROR(" Failed to WriteInt32 ");
204 return E_WRITE_PARCEL_ERROR;
205 }
206 int32_t result = Remote()->SendRequest(static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SET_CUSTOM), data,
207 reply, option);
208 if (result != ERR_NONE) {
209 HILOG_ERROR("Set video failed, result: %{public}d", result);
210 return E_DEAL_FAILED;
211 }
212 return ConvertIntToErrorCode(reply.ReadInt32());
213 }
214
GetPixelMapInner(int32_t wallpaperType,WallpaperServiceIpcInterfaceCode code,IWallpaperService::FdInfo & fdInfo)215 ErrorCode WallpaperServiceProxy::GetPixelMapInner(
216 int32_t wallpaperType, WallpaperServiceIpcInterfaceCode code, IWallpaperService::FdInfo &fdInfo)
217 {
218 HILOG_INFO(" WallpaperServiceProxy::getPixelMap --> start ");
219 MessageParcel data;
220 MessageParcel reply;
221 MessageOption option;
222
223 if (!data.WriteInterfaceToken(GetDescriptor())) {
224 HILOG_ERROR(" Failed to write parcelable ");
225 return E_WRITE_PARCEL_ERROR;
226 }
227
228 if (!data.WriteInt32(wallpaperType)) {
229 HILOG_ERROR(" Failed to WriteInt32 ");
230 return E_DEAL_FAILED;
231 }
232 int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
233 if (result != ERR_NONE) {
234 HILOG_ERROR(" WallpaperServiceProxy::GetPixelMap fail, result = %{public}d ", result);
235 return E_DEAL_FAILED;
236 }
237 ErrorCode wallpaperErrorCode = ConvertIntToErrorCode(reply.ReadInt32());
238 if (wallpaperErrorCode == E_OK) {
239 fdInfo.size = reply.ReadInt32();
240 fdInfo.fd = reply.ReadFileDescriptor();
241 }
242 return wallpaperErrorCode;
243 }
244
GetWallpaperId(int32_t wallpaperType)245 int32_t WallpaperServiceProxy::GetWallpaperId(int32_t wallpaperType)
246 {
247 int32_t iWallpaperId = 1;
248 MessageParcel data;
249 MessageParcel reply;
250 MessageOption option;
251
252 if (!data.WriteInterfaceToken(GetDescriptor())) {
253 HILOG_ERROR(" Failed to write parcelable ");
254 return false;
255 }
256
257 data.WriteInt32(wallpaperType);
258 int32_t result = Remote()->SendRequest(
259 static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::GET_WALLPAPER_ID), data, reply, option);
260 if (result != ERR_NONE) {
261 HILOG_ERROR(" WallpaperServiceProxy::GetWallpaperId fail, result = %{public}d ", result);
262 return -1;
263 }
264
265 iWallpaperId = reply.ReadInt32();
266 HILOG_INFO(" End => iWallpaperId[%{public}d]", iWallpaperId);
267 return iWallpaperId;
268 }
269
IsChangePermitted()270 bool WallpaperServiceProxy::IsChangePermitted()
271 {
272 bool bFlag = false;
273 MessageParcel data;
274 MessageParcel reply;
275 MessageOption option;
276
277 if (!data.WriteInterfaceToken(GetDescriptor())) {
278 HILOG_ERROR(" Failed to write parcelable ");
279 return false;
280 }
281
282 int32_t result = Remote()->SendRequest(
283 static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::IS_CHANGE_PERMITTED), data, reply, option);
284 if (result != ERR_NONE) {
285 HILOG_ERROR(" WallpaperServiceProxy::IsChangePermitted fail, result = %{public}d ", result);
286 return false;
287 }
288
289 bFlag = reply.ReadBool();
290 return bFlag;
291 }
292
IsOperationAllowed()293 bool WallpaperServiceProxy::IsOperationAllowed()
294 {
295 bool bFlag = false;
296 MessageParcel data;
297 MessageParcel reply;
298 MessageOption option;
299
300 if (!data.WriteInterfaceToken(GetDescriptor())) {
301 HILOG_ERROR(" Failed to write parcelable ");
302 return false;
303 }
304
305 int32_t result = Remote()->SendRequest(
306 static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::IS_OPERATION_ALLOWED), data, reply, option);
307 if (result != ERR_NONE) {
308 HILOG_ERROR(" WallpaperServiceProxyIsOperationAllowed fail, result = %{public}d ", result);
309 return false;
310 }
311
312 bFlag = reply.ReadBool();
313 return bFlag;
314 }
315
ResetWallpaper(int32_t wallpaperType)316 ErrorCode WallpaperServiceProxy::ResetWallpaper(int32_t wallpaperType)
317 {
318 return ResetWallpaperInner(wallpaperType, WallpaperServiceIpcInterfaceCode::RESET_WALLPAPER);
319 }
320
ResetWallpaperV9(int32_t wallpaperType)321 ErrorCode WallpaperServiceProxy::ResetWallpaperV9(int32_t wallpaperType)
322 {
323 return ResetWallpaperInner(wallpaperType, WallpaperServiceIpcInterfaceCode::RESET_WALLPAPER_V9);
324 }
325
ResetWallpaperInner(int32_t wallpaperType,WallpaperServiceIpcInterfaceCode code)326 ErrorCode WallpaperServiceProxy::ResetWallpaperInner(int32_t wallpaperType, WallpaperServiceIpcInterfaceCode code)
327 {
328 MessageParcel data;
329 MessageParcel reply;
330 MessageOption option;
331
332 if (!data.WriteInterfaceToken(GetDescriptor())) {
333 HILOG_ERROR(" Failed to write parcelable ");
334 return E_WRITE_PARCEL_ERROR;
335 }
336
337 data.WriteInt32(wallpaperType);
338 int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
339 if (result != ERR_NONE) {
340 HILOG_ERROR(" WallpaperServiceProxy::ResetWallpaper fail, result = %{public}d ", result);
341 return E_DEAL_FAILED;
342 }
343 return ConvertIntToErrorCode(reply.ReadInt32());
344 }
345
SendEvent(const std::string & eventType)346 ErrorCode WallpaperServiceProxy::SendEvent(const std::string &eventType)
347 {
348 MessageParcel data;
349 MessageParcel reply;
350 MessageOption option;
351
352 if (!data.WriteInterfaceToken(GetDescriptor())) {
353 HILOG_ERROR("Failed to write parcelable");
354 return E_WRITE_PARCEL_ERROR;
355 }
356
357 if (!data.WriteString(eventType)) {
358 HILOG_ERROR("write eventType failed.");
359 return E_WRITE_PARCEL_ERROR;
360 }
361
362 int32_t result = Remote()->SendRequest(
363 static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SEND_EVENT), data, reply, option);
364 if (result != ERR_NONE) {
365 HILOG_ERROR("Failed to send event, result: %{public}d", result);
366 return E_DEAL_FAILED;
367 }
368 return ConvertIntToErrorCode(reply.ReadInt32());
369 }
370
On(const std::string & type,sptr<IWallpaperEventListener> listener)371 ErrorCode WallpaperServiceProxy::On(const std::string &type, sptr<IWallpaperEventListener> listener)
372 {
373 HILOG_DEBUG("WallpaperServiceProxy::On in");
374 MessageParcel data;
375 MessageParcel reply;
376 MessageOption option;
377 if (!data.WriteInterfaceToken(GetDescriptor())) {
378 HILOG_ERROR(" Failed to write parcelable ");
379 return E_WRITE_PARCEL_ERROR;
380 }
381 if (listener == nullptr) {
382 HILOG_ERROR("listener is nullptr");
383 return E_DEAL_FAILED;
384 }
385
386 if (!data.WriteString(type)) {
387 HILOG_ERROR("write type failed.");
388 return E_WRITE_PARCEL_ERROR;
389 }
390 if (!data.WriteRemoteObject(listener->AsObject())) {
391 HILOG_ERROR("write subscribe type or parcel failed.");
392 return E_WRITE_PARCEL_ERROR;
393 }
394 int32_t result =
395 Remote()->SendRequest(static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::ON), data, reply, option);
396 if (result != ERR_NONE) {
397 HILOG_ERROR(" WallpaperServiceProxy::On fail, result = %{public}d ", result);
398 return E_DEAL_FAILED;
399 }
400
401 HILOG_DEBUG("WallpaperServiceProxy::On out");
402 return ConvertIntToErrorCode(reply.ReadInt32());
403 }
404
Off(const std::string & type,sptr<IWallpaperEventListener> listener)405 ErrorCode WallpaperServiceProxy::Off(const std::string &type, sptr<IWallpaperEventListener> listener)
406 {
407 HILOG_DEBUG("WallpaperServiceProxy::Off in");
408 MessageParcel data;
409 MessageParcel reply;
410 MessageOption option;
411 if (!data.WriteInterfaceToken(GetDescriptor())) {
412 HILOG_ERROR(" Failed to write parcelable ");
413 return E_WRITE_PARCEL_ERROR;
414 }
415 if (!data.WriteString(type)) {
416 HILOG_ERROR("write type failed.");
417 return E_WRITE_PARCEL_ERROR;
418 }
419 if (listener != nullptr) {
420 if (!data.WriteRemoteObject(listener->AsObject())) {
421 HILOG_ERROR("write subscribe type or parcel failed.");
422 return E_WRITE_PARCEL_ERROR;
423 }
424 }
425
426 int32_t result =
427 Remote()->SendRequest(static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::OFF), data, reply, option);
428 if (result != ERR_NONE) {
429 HILOG_ERROR(" WallpaperServiceProxy::Off fail, result = %{public}d ", result);
430 return E_DEAL_FAILED;
431 }
432
433 HILOG_DEBUG("WallpaperServiceProxy::Off out");
434 return ConvertIntToErrorCode(reply.ReadInt32());
435 }
436
RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback)437 bool WallpaperServiceProxy::RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback)
438 {
439 HILOG_DEBUG("WallpaperServiceProxy::RegisterWallpaperCallback in");
440 MessageParcel data;
441 MessageParcel reply;
442 MessageOption option;
443 if (!data.WriteInterfaceToken(GetDescriptor())) {
444 HILOG_ERROR(" Failed to write parcelable ");
445 return false;
446 }
447 if (callback == nullptr) {
448 HILOG_ERROR("callback is nullptr");
449 return false;
450 }
451 if (!data.WriteRemoteObject(callback->AsObject())) {
452 HILOG_ERROR("write subscribe type or parcel failed.");
453 return false;
454 }
455 HILOG_INFO(" Remote()->SendRequest");
456 int32_t result = Remote()->SendRequest(
457 static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::REGISTER_CALLBACK), data, reply, option);
458 if (result != ERR_NONE) {
459 HILOG_ERROR(" WallpaperServiceProxy::REGISTER_CALLBACK fail, result = %{public}d ", result);
460 return false;
461 }
462 int32_t status = reply.ReadInt32();
463 bool ret = status == static_cast<int32_t>(E_OK);
464 HILOG_DEBUG("WallpaperServiceProxy::REGISTER_CALLBACK out");
465 return ret;
466 }
467
ConvertIntToErrorCode(int32_t errorCode)468 ErrorCode WallpaperServiceProxy::ConvertIntToErrorCode(int32_t errorCode)
469 {
470 ErrorCode wallpaperErrorCode = E_UNKNOWN;
471 switch (errorCode) {
472 case static_cast<int32_t>(E_OK):
473 case static_cast<int32_t>(E_SA_DIED):
474 case static_cast<int32_t>(E_READ_PARCEL_ERROR):
475 case static_cast<int32_t>(E_WRITE_PARCEL_ERROR):
476 case static_cast<int32_t>(E_PUBLISH_FAIL):
477 case static_cast<int32_t>(E_TRANSACT_ERROR):
478 case static_cast<int32_t>(E_DEAL_FAILED):
479 case static_cast<int32_t>(E_PARAMETERS_INVALID):
480 case static_cast<int32_t>(E_SET_RTC_FAILED):
481 case static_cast<int32_t>(E_NOT_FOUND):
482 case static_cast<int32_t>(E_NO_PERMISSION):
483 case static_cast<int32_t>(E_FILE_ERROR):
484 case static_cast<int32_t>(E_IMAGE_ERRCODE):
485 case static_cast<int32_t>(E_NO_MEMORY):
486 case static_cast<int32_t>(E_NOT_SYSTEM_APP):
487 case static_cast<int32_t>(E_USER_IDENTITY_ERROR):
488 wallpaperErrorCode = static_cast<ErrorCode>(errorCode);
489 break;
490 default:
491 break;
492 }
493 return wallpaperErrorCode;
494 }
495 } // namespace WallpaperMgrService
496 } // namespace OHOS
497