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 "file_deal.h"
17 #include "hilog_wrapper.h"
18 #include "i_wallpaper_service.h"
19 #include "iremote_broker.h"
20 #include "wallpaper_common.h"
21 #include "wallpaper_service_proxy.h"
22 
23 namespace OHOS {
24 namespace WallpaperMgrService {
25 using namespace OHOS::HiviewDFX;
GetColors(int32_t wallpaperType,std::vector<uint64_t> & colors)26 ErrorCode WallpaperServiceProxy::GetColors(int32_t wallpaperType, std::vector<uint64_t> &colors)
27 {
28     return GetColorsInner(wallpaperType, WallpaperServiceIpcInterfaceCode::GET_COLORS, colors);
29 }
30 
GetColorsV9(int32_t wallpaperType,std::vector<uint64_t> & colors)31 ErrorCode WallpaperServiceProxy::GetColorsV9(int32_t wallpaperType, std::vector<uint64_t> &colors)
32 {
33     return GetColorsInner(wallpaperType, WallpaperServiceIpcInterfaceCode::GET_COLORS_V9, colors);
34 }
35 
GetColorsInner(int32_t wallpaperType,WallpaperServiceIpcInterfaceCode code,std::vector<uint64_t> & colors)36 ErrorCode WallpaperServiceProxy::GetColorsInner(
37     int32_t wallpaperType, WallpaperServiceIpcInterfaceCode code, std::vector<uint64_t> &colors)
38 {
39     MessageParcel data;
40     MessageParcel reply;
41     MessageOption option;
42 
43     if (!data.WriteInterfaceToken(GetDescriptor())) {
44         HILOG_ERROR("Failed to write parcelable!");
45         return E_WRITE_PARCEL_ERROR;
46     }
47     if (!data.WriteInt32(wallpaperType)) {
48         HILOG_ERROR("Failed to WriteInt32!");
49         return E_WRITE_PARCEL_ERROR;
50     }
51 
52     int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
53     if (result != ERR_NONE) {
54         HILOG_ERROR("Get color failed, result = %{public}d!", result);
55         return E_DEAL_FAILED;
56     }
57     ErrorCode wallpaperErrorCode = ConvertIntToErrorCode(reply.ReadInt32());
58     if (wallpaperErrorCode == E_OK) {
59         if (!reply.ReadUInt64Vector(&colors)) {
60             HILOG_ERROR("Failed to ReadUInt64Vector!");
61         }
62     }
63     return wallpaperErrorCode;
64 }
65 
GetFile(int32_t wallpaperType,int32_t & wallpaperFd)66 ErrorCode WallpaperServiceProxy::GetFile(int32_t wallpaperType, int32_t &wallpaperFd)
67 {
68     MessageParcel data;
69     MessageParcel reply;
70     MessageOption option;
71 
72     if (!data.WriteInterfaceToken(GetDescriptor())) {
73         HILOG_ERROR("Failed to write parcelable!");
74         return E_WRITE_PARCEL_ERROR;
75     }
76     if (!data.WriteInt32(wallpaperType)) {
77         HILOG_ERROR("Failed to WriteInt32!");
78         return E_WRITE_PARCEL_ERROR;
79     }
80 
81     int32_t result =
82         Remote()->SendRequest(static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::GET_FILE), data, reply, option);
83     if (result != ERR_NONE) {
84         HILOG_ERROR("get file result = %{public}d.", result);
85         return E_DEAL_FAILED;
86     }
87     ErrorCode wallpaperErrorCode = ConvertIntToErrorCode(reply.ReadInt32());
88     if (wallpaperErrorCode == E_OK) {
89         wallpaperFd = reply.ReadFileDescriptor();
90     }
91     return wallpaperErrorCode;
92 }
93 
SetWallpaper(int32_t fd,int32_t wallpaperType,int32_t length)94 ErrorCode WallpaperServiceProxy::SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length)
95 {
96     return SetWallpaperInner(fd, wallpaperType, length, WallpaperServiceIpcInterfaceCode::SET_WALLPAPER);
97 }
98 
SetWallpaperV9(int32_t fd,int32_t wallpaperType,int32_t length)99 ErrorCode WallpaperServiceProxy::SetWallpaperV9(int32_t fd, int32_t wallpaperType, int32_t length)
100 {
101     return SetWallpaperInner(fd, wallpaperType, length, WallpaperServiceIpcInterfaceCode::SET_WALLPAPER_V9);
102 }
103 
SetWallpaperInner(int32_t fd,int32_t wallpaperType,int32_t length,WallpaperServiceIpcInterfaceCode code)104 ErrorCode WallpaperServiceProxy::SetWallpaperInner(
105     int32_t fd, int32_t wallpaperType, int32_t length, WallpaperServiceIpcInterfaceCode code)
106 {
107     HILOG_DEBUG("WallpaperServiceProxy::SetWallpaper --> start.");
108     MessageParcel data;
109     MessageParcel reply;
110     MessageOption option;
111 
112     if (!data.WriteInterfaceToken(GetDescriptor())) {
113         HILOG_ERROR("Failed to write parcelable!");
114         return E_WRITE_PARCEL_ERROR;
115     }
116     if (!data.WriteFileDescriptor(fd)) {
117         HILOG_ERROR("Failed to WriteFileDescriptor!");
118         return E_WRITE_PARCEL_ERROR;
119     }
120 
121     if (!data.WriteInt32(wallpaperType)) {
122         HILOG_ERROR("Failed to WriteInt32!");
123         return E_WRITE_PARCEL_ERROR;
124     }
125     if (!data.WriteInt32(length)) {
126         HILOG_ERROR("Failed to WriteInt32!");
127         return E_WRITE_PARCEL_ERROR;
128     }
129 
130     int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
131     if (result != ERR_NONE) {
132         HILOG_ERROR("WallpaperCallbackProxy::SetWallpaper fail, result = %{public}d.", result);
133         return E_DEAL_FAILED;
134     }
135 
136     return ConvertIntToErrorCode(reply.ReadInt32());
137 }
138 
SetWallpaperByPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap,int32_t wallpaperType)139 ErrorCode WallpaperServiceProxy::SetWallpaperByPixelMap(
140     std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType)
141 {
142     return SetWallpaperInnerByPixelMap(
143         pixelMap, wallpaperType, WallpaperServiceIpcInterfaceCode::SET_WALLPAPER_PIXELMAP);
144 }
145 
SetWallpaperV9ByPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap,int32_t wallpaperType)146 ErrorCode WallpaperServiceProxy::SetWallpaperV9ByPixelMap(
147     std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType)
148 {
149     return SetWallpaperInnerByPixelMap(
150         pixelMap, wallpaperType, WallpaperServiceIpcInterfaceCode::SET_WALLPAPER_PIXELMAP_V9);
151 }
152 
SetWallpaperInnerByPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap,int32_t wallpaperType,WallpaperServiceIpcInterfaceCode code)153 ErrorCode WallpaperServiceProxy::SetWallpaperInnerByPixelMap(
154     std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType, WallpaperServiceIpcInterfaceCode code)
155 {
156     HILOG_DEBUG("WallpaperServiceProxy::SetWallpaper --> start.");
157     MessageParcel data;
158     MessageParcel reply;
159     MessageOption option;
160 
161     if (!data.WriteInterfaceToken(GetDescriptor())) {
162         HILOG_ERROR("Failed to write parcelable!");
163         return E_WRITE_PARCEL_ERROR;
164     }
165     std::vector<std::uint8_t> value = PixelMapToVector(pixelMap);
166     if (!data.WriteInt32(value.size())) {
167         HILOG_ERROR("Failed to WriteInt32!");
168         return E_WRITE_PARCEL_ERROR;
169     }
170     if (!data.WriteRawData(value.data(), value.size())) {
171         HILOG_ERROR("Failed to WriteUInt8Vector!");
172         return E_WRITE_PARCEL_ERROR;
173     }
174 
175     if (!data.WriteInt32(wallpaperType)) {
176         HILOG_ERROR("Failed to WriteInt32!");
177         return E_WRITE_PARCEL_ERROR;
178     }
179 
180     int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
181     if (result != ERR_NONE) {
182         HILOG_ERROR("WallpaperCallbackProxy::SetWallpaper fail, result = %{public}d!", result);
183         return E_DEAL_FAILED;
184     }
185 
186     return ConvertIntToErrorCode(reply.ReadInt32());
187 }
188 
GetPixelMap(int32_t wallpaperType,IWallpaperService::FdInfo & fdInfo)189 ErrorCode WallpaperServiceProxy::GetPixelMap(int32_t wallpaperType, IWallpaperService::FdInfo &fdInfo)
190 {
191     return GetPixelMapInner(wallpaperType, WallpaperServiceIpcInterfaceCode::GET_PIXELMAP, fdInfo);
192 }
193 
GetPixelMapV9(int32_t wallpaperType,IWallpaperService::FdInfo & fdInfo)194 ErrorCode WallpaperServiceProxy::GetPixelMapV9(int32_t wallpaperType, IWallpaperService::FdInfo &fdInfo)
195 {
196     return GetPixelMapInner(wallpaperType, WallpaperServiceIpcInterfaceCode::GET_PIXELMAP_V9, fdInfo);
197 }
198 
SetVideo(int32_t fd,int32_t wallpaperType,int32_t length)199 ErrorCode WallpaperServiceProxy::SetVideo(int32_t fd, int32_t wallpaperType, int32_t length)
200 {
201     MessageParcel data;
202     MessageParcel reply;
203     MessageOption option;
204 
205     if (!data.WriteInterfaceToken(GetDescriptor())) {
206         HILOG_ERROR("Failed to write parcelable!");
207         return E_WRITE_PARCEL_ERROR;
208     }
209     if (!data.WriteFileDescriptor(fd)) {
210         HILOG_ERROR("Failed to WriteFileDescriptor!");
211         return E_WRITE_PARCEL_ERROR;
212     }
213 
214     if (!data.WriteInt32(wallpaperType)) {
215         HILOG_ERROR("Failed to WriteInt32!");
216         return E_WRITE_PARCEL_ERROR;
217     }
218     if (!data.WriteInt32(length)) {
219         HILOG_ERROR("Failed to WriteInt32!");
220         return E_WRITE_PARCEL_ERROR;
221     }
222 
223     int32_t result =
224         Remote()->SendRequest(static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SET_VIDEO), data, reply, option);
225     if (result != ERR_NONE) {
226         HILOG_ERROR("Set video failed, result: %{public}d!", result);
227         return E_DEAL_FAILED;
228     }
229 
230     return ConvertIntToErrorCode(reply.ReadInt32());
231 }
232 
SetCustomWallpaper(int32_t fd,int32_t wallpaperType,int32_t length)233 ErrorCode WallpaperServiceProxy::SetCustomWallpaper(int32_t fd, int32_t wallpaperType, int32_t length)
234 {
235     MessageParcel data;
236     MessageParcel reply;
237     MessageOption option;
238 
239     if (!data.WriteInterfaceToken(GetDescriptor())) {
240         HILOG_ERROR("Failed to write parcelable!");
241         return E_WRITE_PARCEL_ERROR;
242     }
243     if (!data.WriteFileDescriptor(fd)) {
244         HILOG_ERROR("Failed to WriteFD!");
245         return E_WRITE_PARCEL_ERROR;
246     }
247     if (!data.WriteInt32(wallpaperType)) {
248         HILOG_ERROR("Failed to WriteInt32!");
249         return E_WRITE_PARCEL_ERROR;
250     }
251     if (!data.WriteInt32(length)) {
252         HILOG_ERROR("Failed to WriteInt32!");
253         return E_WRITE_PARCEL_ERROR;
254     }
255     int32_t result = Remote()->SendRequest(
256         static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SET_CUSTOM), data, reply, option);
257     if (result != ERR_NONE) {
258         HILOG_ERROR("Set video failed, result: %{public}d!", result);
259         return E_DEAL_FAILED;
260     }
261     return ConvertIntToErrorCode(reply.ReadInt32());
262 }
263 
GetPixelMapInner(int32_t wallpaperType,WallpaperServiceIpcInterfaceCode code,IWallpaperService::FdInfo & fdInfo)264 ErrorCode WallpaperServiceProxy::GetPixelMapInner(
265     int32_t wallpaperType, WallpaperServiceIpcInterfaceCode code, IWallpaperService::FdInfo &fdInfo)
266 {
267     HILOG_DEBUG("WallpaperServiceProxy::getPixelMap --> start.");
268     MessageParcel data;
269     MessageParcel reply;
270     MessageOption option;
271 
272     if (!data.WriteInterfaceToken(GetDescriptor())) {
273         HILOG_ERROR("Failed to write parcelable!");
274         return E_WRITE_PARCEL_ERROR;
275     }
276 
277     if (!data.WriteInt32(wallpaperType)) {
278         HILOG_ERROR("Failed to WriteInt32!");
279         return E_DEAL_FAILED;
280     }
281     int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
282     if (result != ERR_NONE) {
283         HILOG_ERROR("WallpaperServiceProxy::GetPixelMap fail, result = %{public}d!", result);
284         return E_DEAL_FAILED;
285     }
286     ErrorCode wallpaperErrorCode = ConvertIntToErrorCode(reply.ReadInt32());
287     if (wallpaperErrorCode == E_OK) {
288         fdInfo.size = reply.ReadInt32();
289         fdInfo.fd = reply.ReadFileDescriptor();
290     }
291     return wallpaperErrorCode;
292 }
293 
GetWallpaperId(int32_t wallpaperType)294 int32_t WallpaperServiceProxy::GetWallpaperId(int32_t wallpaperType)
295 {
296     int32_t iWallpaperId = 1;
297     MessageParcel data;
298     MessageParcel reply;
299     MessageOption option;
300 
301     if (!data.WriteInterfaceToken(GetDescriptor())) {
302         HILOG_ERROR("Failed to write parcelable!");
303         return false;
304     }
305 
306     data.WriteInt32(wallpaperType);
307     int32_t result = Remote()->SendRequest(
308         static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::GET_WALLPAPER_ID), data, reply, option);
309     if (result != ERR_NONE) {
310         HILOG_ERROR("WallpaperServiceProxy::GetWallpaperId fail, result = %{public}d!", result);
311         return -1;
312     }
313 
314     iWallpaperId = reply.ReadInt32();
315     HILOG_INFO("End => iWallpaperId[%{public}d]", iWallpaperId);
316     return iWallpaperId;
317 }
318 
IsChangePermitted()319 bool WallpaperServiceProxy::IsChangePermitted()
320 {
321     bool bFlag = false;
322     MessageParcel data;
323     MessageParcel reply;
324     MessageOption option;
325 
326     if (!data.WriteInterfaceToken(GetDescriptor())) {
327         HILOG_ERROR("Failed to write parcelable!");
328         return false;
329     }
330 
331     int32_t result = Remote()->SendRequest(
332         static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::IS_CHANGE_PERMITTED), data, reply, option);
333     if (result != ERR_NONE) {
334         HILOG_ERROR("WallpaperServiceProxy::IsChangePermitted fail, result = %{public}d!", result);
335         return false;
336     }
337 
338     bFlag = reply.ReadBool();
339     return bFlag;
340 }
341 
IsOperationAllowed()342 bool WallpaperServiceProxy::IsOperationAllowed()
343 {
344     bool bFlag = false;
345     MessageParcel data;
346     MessageParcel reply;
347     MessageOption option;
348 
349     if (!data.WriteInterfaceToken(GetDescriptor())) {
350         HILOG_ERROR("Failed to write parcelable!");
351         return false;
352     }
353 
354     int32_t result = Remote()->SendRequest(
355         static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::IS_OPERATION_ALLOWED), data, reply, option);
356     if (result != ERR_NONE) {
357         HILOG_ERROR("WallpaperServiceProxyIsOperationAllowed fail, result = %{public}d!", result);
358         return false;
359     }
360 
361     bFlag = reply.ReadBool();
362     return bFlag;
363 }
364 
ResetWallpaper(int32_t wallpaperType)365 ErrorCode WallpaperServiceProxy::ResetWallpaper(int32_t wallpaperType)
366 {
367     return ResetWallpaperInner(wallpaperType, WallpaperServiceIpcInterfaceCode::RESET_WALLPAPER);
368 }
369 
ResetWallpaperV9(int32_t wallpaperType)370 ErrorCode WallpaperServiceProxy::ResetWallpaperV9(int32_t wallpaperType)
371 {
372     return ResetWallpaperInner(wallpaperType, WallpaperServiceIpcInterfaceCode::RESET_WALLPAPER_V9);
373 }
374 
ResetWallpaperInner(int32_t wallpaperType,WallpaperServiceIpcInterfaceCode code)375 ErrorCode WallpaperServiceProxy::ResetWallpaperInner(int32_t wallpaperType, WallpaperServiceIpcInterfaceCode code)
376 {
377     MessageParcel data;
378     MessageParcel reply;
379     MessageOption option;
380 
381     if (!data.WriteInterfaceToken(GetDescriptor())) {
382         HILOG_ERROR("Failed to write parcelable!");
383         return E_WRITE_PARCEL_ERROR;
384     }
385 
386     data.WriteInt32(wallpaperType);
387     int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
388     if (result != ERR_NONE) {
389         HILOG_ERROR("WallpaperServiceProxy::ResetWallpaper fail, result = %{public}d", result);
390         return E_DEAL_FAILED;
391     }
392     return ConvertIntToErrorCode(reply.ReadInt32());
393 }
394 
SendEvent(const std::string & eventType)395 ErrorCode WallpaperServiceProxy::SendEvent(const std::string &eventType)
396 {
397     MessageParcel data;
398     MessageParcel reply;
399     MessageOption option;
400 
401     if (!data.WriteInterfaceToken(GetDescriptor())) {
402         HILOG_ERROR("Failed to write parcelable!");
403         return E_WRITE_PARCEL_ERROR;
404     }
405 
406     if (!data.WriteString(eventType)) {
407         HILOG_ERROR("write eventType failed!");
408         return E_WRITE_PARCEL_ERROR;
409     }
410 
411     int32_t result = Remote()->SendRequest(
412         static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SEND_EVENT), data, reply, option);
413     if (result != ERR_NONE) {
414         HILOG_ERROR("Failed to send event, result: %{public}d!", result);
415         return E_DEAL_FAILED;
416     }
417     return ConvertIntToErrorCode(reply.ReadInt32());
418 }
419 
On(const std::string & type,sptr<IWallpaperEventListener> listener)420 ErrorCode WallpaperServiceProxy::On(const std::string &type, sptr<IWallpaperEventListener> listener)
421 {
422     HILOG_DEBUG("WallpaperServiceProxy::On in.");
423     MessageParcel data;
424     MessageParcel reply;
425     MessageOption option;
426     if (!data.WriteInterfaceToken(GetDescriptor())) {
427         HILOG_ERROR("Failed to write parcelable!");
428         return E_WRITE_PARCEL_ERROR;
429     }
430     if (listener == nullptr) {
431         HILOG_ERROR("listener is nullptr.");
432         return E_DEAL_FAILED;
433     }
434 
435     if (!data.WriteString(type)) {
436         HILOG_ERROR("write type failed!");
437         return E_WRITE_PARCEL_ERROR;
438     }
439     if (!data.WriteRemoteObject(listener->AsObject())) {
440         HILOG_ERROR("write subscribe type or parcel failed!");
441         return E_WRITE_PARCEL_ERROR;
442     }
443     int32_t result =
444         Remote()->SendRequest(static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::ON), data, reply, option);
445     if (result != ERR_NONE) {
446         HILOG_ERROR("WallpaperServiceProxy::On fail, result = %{public}d!", result);
447         return E_DEAL_FAILED;
448     }
449 
450     return ConvertIntToErrorCode(reply.ReadInt32());
451 }
452 
Off(const std::string & type,sptr<IWallpaperEventListener> listener)453 ErrorCode WallpaperServiceProxy::Off(const std::string &type, sptr<IWallpaperEventListener> listener)
454 {
455     HILOG_DEBUG("WallpaperServiceProxy::Off in.");
456     MessageParcel data;
457     MessageParcel reply;
458     MessageOption option;
459     if (!data.WriteInterfaceToken(GetDescriptor())) {
460         HILOG_ERROR("Failed to write parcelable!");
461         return E_WRITE_PARCEL_ERROR;
462     }
463     if (!data.WriteString(type)) {
464         HILOG_ERROR("write type failed!");
465         return E_WRITE_PARCEL_ERROR;
466     }
467     if (listener != nullptr) {
468         if (!data.WriteRemoteObject(listener->AsObject())) {
469             HILOG_ERROR("write subscribe type or parcel failed!");
470             return E_WRITE_PARCEL_ERROR;
471         }
472     }
473 
474     int32_t result =
475         Remote()->SendRequest(static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::OFF), data, reply, option);
476     if (result != ERR_NONE) {
477         HILOG_ERROR("WallpaperServiceProxy::Off fail, result = %{public}d!", result);
478         return E_DEAL_FAILED;
479     }
480 
481     return ConvertIntToErrorCode(reply.ReadInt32());
482 }
483 
RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback)484 bool WallpaperServiceProxy::RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback)
485 {
486     HILOG_DEBUG("WallpaperServiceProxy::RegisterWallpaperCallback in.");
487     MessageParcel data;
488     MessageParcel reply;
489     MessageOption option;
490     if (!data.WriteInterfaceToken(GetDescriptor())) {
491         HILOG_ERROR("Failed to write parcelable!");
492         return false;
493     }
494     if (callback == nullptr) {
495         HILOG_ERROR("callback is nullptr.");
496         return false;
497     }
498     if (!data.WriteRemoteObject(callback->AsObject())) {
499         HILOG_ERROR("write subscribe type or parcel failed!");
500         return false;
501     }
502     int32_t result = Remote()->SendRequest(
503         static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::REGISTER_CALLBACK), data, reply, option);
504     if (result != ERR_NONE) {
505         HILOG_ERROR("WallpaperServiceProxy::REGISTER_CALLBACK fail, result = %{public}d!", result);
506         return false;
507     }
508     int32_t status = reply.ReadInt32();
509     bool ret = status == static_cast<int32_t>(E_OK);
510     return ret;
511 }
512 
PixelMapToVector(std::shared_ptr<OHOS::Media::PixelMap> pixelMap)513 std::vector<std::uint8_t> WallpaperServiceProxy::PixelMapToVector(std::shared_ptr<OHOS::Media::PixelMap> pixelMap)
514 {
515     HILOG_DEBUG("PixelMapToVector start.");
516     if (pixelMap == nullptr) {
517         return {};
518     }
519     std::vector<std::uint8_t> value;
520     if (!pixelMap->EncodeTlv(value)) {
521         HILOG_ERROR("pixelMap encode failed!");
522         return {};
523     }
524     return value;
525 }
526 
SetAllWallpapers(std::vector<WallpaperPictureInfo> allWallpaperInfos,int32_t wallpaperType)527 ErrorCode WallpaperServiceProxy::SetAllWallpapers(
528     std::vector<WallpaperPictureInfo> allWallpaperInfos, int32_t wallpaperType)
529 {
530     return SetAllWallpapersInner(
531         allWallpaperInfos, wallpaperType, WallpaperServiceIpcInterfaceCode::SET_ALL_WALLPAPERS);
532 }
533 
SetAllWallpapersInner(std::vector<WallpaperPictureInfo> allWallpaperInfos,int32_t wallpaperType,WallpaperServiceIpcInterfaceCode code)534 ErrorCode WallpaperServiceProxy::SetAllWallpapersInner(
535     std::vector<WallpaperPictureInfo> allWallpaperInfos, int32_t wallpaperType, WallpaperServiceIpcInterfaceCode code)
536 {
537     HILOG_DEBUG("WallpaperServiceProxy::SetAllWallpapersInner --> start.");
538     MessageParcel data;
539     MessageParcel reply;
540     MessageOption option;
541 
542     if (!data.WriteInterfaceToken(GetDescriptor())) {
543         HILOG_ERROR("Failed to write parcelable!");
544         return E_WRITE_PARCEL_ERROR;
545     }
546     if (!data.WriteInt32(allWallpaperInfos.size())) {
547         HILOG_ERROR("Failed to WriteInt32!");
548         return E_WRITE_PARCEL_ERROR;
549     }
550     for (const auto &wallpaperInfo : allWallpaperInfos) {
551         if (!data.WriteInt32(wallpaperInfo.foldState)) {
552             HILOG_ERROR("Failed to WriteInt32!");
553             return E_WRITE_PARCEL_ERROR;
554         }
555         if (!data.WriteInt32(wallpaperInfo.rotateState)) {
556             HILOG_ERROR("Failed to WriteInt32!");
557             return E_WRITE_PARCEL_ERROR;
558         }
559         if (!data.WriteFileDescriptor(wallpaperInfo.fd)) {
560             HILOG_ERROR("Failed to WriteInt32!");
561             return E_WRITE_PARCEL_ERROR;
562         }
563         if (!data.WriteInt32(wallpaperInfo.length)) {
564             HILOG_ERROR("Failed to WriteInt32!");
565             return E_WRITE_PARCEL_ERROR;
566         }
567     }
568     if (!data.WriteInt32(wallpaperType)) {
569         HILOG_ERROR("Failed to WriteInt32!");
570         return E_WRITE_PARCEL_ERROR;
571     }
572 
573     int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
574     if (result != ERR_NONE) {
575         HILOG_ERROR("WallpaperCallbackProxy::SetAllWallpapersInner fail, result = %{public}d.", result);
576         return E_DEAL_FAILED;
577     }
578 
579     return ConvertIntToErrorCode(reply.ReadInt32());
580 }
581 
GetCorrespondWallpaper(int32_t wallpaperType,int32_t foldState,int32_t rotateState,IWallpaperService::FdInfo & fdInfo)582 ErrorCode WallpaperServiceProxy::GetCorrespondWallpaper(
583     int32_t wallpaperType, int32_t foldState, int32_t rotateState, IWallpaperService::FdInfo &fdInfo)
584 {
585     return GetCorrespondWallpaperInner(
586         wallpaperType, foldState, rotateState, WallpaperServiceIpcInterfaceCode::GET_CORRESPOND_WALLPAPER, fdInfo);
587 }
588 
GetCorrespondWallpaperInner(int32_t wallpaperType,int32_t foldState,int32_t rotateState,WallpaperServiceIpcInterfaceCode code,IWallpaperService::FdInfo & fdInfo)589 ErrorCode WallpaperServiceProxy::GetCorrespondWallpaperInner(int32_t wallpaperType, int32_t foldState,
590     int32_t rotateState, WallpaperServiceIpcInterfaceCode code, IWallpaperService::FdInfo &fdInfo)
591 {
592     HILOG_DEBUG("WallpaperServiceProxy::getPixelMap --> start.");
593     MessageParcel data;
594     MessageParcel reply;
595     MessageOption option;
596 
597     if (!data.WriteInterfaceToken(GetDescriptor())) {
598         HILOG_ERROR("Failed to write parcelable!");
599         return E_WRITE_PARCEL_ERROR;
600     }
601 
602     if (!data.WriteInt32(wallpaperType)) {
603         HILOG_ERROR("Failed to WriteInt32!");
604         return E_WRITE_PARCEL_ERROR;
605     }
606 
607     if (!data.WriteInt32(foldState)) {
608         HILOG_ERROR("Failed to WriteInt32!");
609         return E_WRITE_PARCEL_ERROR;
610     }
611     if (!data.WriteInt32(rotateState)) {
612         HILOG_ERROR("Failed to WriteInt32!");
613         return E_WRITE_PARCEL_ERROR;
614     }
615     int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
616     if (result != ERR_NONE) {
617         HILOG_ERROR("WallpaperServiceProxy::GetPixelMapInner fail, result = %{public}d!", result);
618         return E_DEAL_FAILED;
619     }
620     ErrorCode wallpaperErrorCode = ConvertIntToErrorCode(reply.ReadInt32());
621     if (wallpaperErrorCode == E_OK) {
622         fdInfo.size = reply.ReadInt32();
623         fdInfo.fd = reply.ReadFileDescriptor();
624     }
625     return wallpaperErrorCode;
626 }
627 
ConvertIntToErrorCode(int32_t errorCode)628 ErrorCode WallpaperServiceProxy::ConvertIntToErrorCode(int32_t errorCode)
629 {
630     ErrorCode wallpaperErrorCode = E_UNKNOWN;
631     switch (errorCode) {
632         case static_cast<int32_t>(E_OK):
633         case static_cast<int32_t>(E_SA_DIED):
634         case static_cast<int32_t>(E_READ_PARCEL_ERROR):
635         case static_cast<int32_t>(E_WRITE_PARCEL_ERROR):
636         case static_cast<int32_t>(E_PUBLISH_FAIL):
637         case static_cast<int32_t>(E_TRANSACT_ERROR):
638         case static_cast<int32_t>(E_DEAL_FAILED):
639         case static_cast<int32_t>(E_PARAMETERS_INVALID):
640         case static_cast<int32_t>(E_SET_RTC_FAILED):
641         case static_cast<int32_t>(E_NOT_FOUND):
642         case static_cast<int32_t>(E_NO_PERMISSION):
643         case static_cast<int32_t>(E_FILE_ERROR):
644         case static_cast<int32_t>(E_IMAGE_ERRCODE):
645         case static_cast<int32_t>(E_NO_MEMORY):
646         case static_cast<int32_t>(E_NOT_SYSTEM_APP):
647         case static_cast<int32_t>(E_USER_IDENTITY_ERROR):
648             wallpaperErrorCode = static_cast<ErrorCode>(errorCode);
649             break;
650         default:
651             break;
652     }
653     return wallpaperErrorCode;
654 }
655 } // namespace WallpaperMgrService
656 } // namespace OHOS
657