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