• 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;
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(const std::string & uri,int32_t wallpaperType)184 ErrorCode WallpaperServiceProxy::SetCustomWallpaper(const std::string &uri, int32_t wallpaperType)
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.WriteString(uri)) {
195         HILOG_ERROR("Failed to WriteString");
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     int32_t result = Remote()->SendRequest(
203         static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SET_CUSTOM), data, reply, option);
204     if (result != ERR_NONE) {
205         HILOG_ERROR("Set video failed, result: %{public}d", result);
206         return E_DEAL_FAILED;
207     }
208     return ConvertIntToErrorCode(reply.ReadInt32());
209 }
210 
GetPixelMapInner(int32_t wallpaperType,WallpaperServiceIpcInterfaceCode code,IWallpaperService::FdInfo & fdInfo)211 ErrorCode WallpaperServiceProxy::GetPixelMapInner(
212     int32_t wallpaperType, WallpaperServiceIpcInterfaceCode code, IWallpaperService::FdInfo &fdInfo)
213 {
214     HILOG_INFO(" WallpaperServiceProxy::getPixelMap --> start ");
215     MessageParcel data;
216     MessageParcel reply;
217     MessageOption option;
218 
219     if (!data.WriteInterfaceToken(GetDescriptor())) {
220         HILOG_ERROR(" Failed to write parcelable ");
221         return E_WRITE_PARCEL_ERROR;
222     }
223 
224     if (!data.WriteInt32(wallpaperType)) {
225         HILOG_ERROR(" Failed to WriteInt32 ");
226         return E_DEAL_FAILED;
227     }
228     int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
229     if (result != ERR_NONE) {
230         HILOG_ERROR(" WallpaperServiceProxy::GetPixelMap fail, result = %{public}d ", result);
231         return E_DEAL_FAILED;
232     }
233     ErrorCode wallpaperErrorCode = ConvertIntToErrorCode(reply.ReadInt32());
234     if (wallpaperErrorCode == E_OK) {
235         fdInfo.size = reply.ReadInt32();
236         fdInfo.fd = reply.ReadFileDescriptor();
237     }
238     return wallpaperErrorCode;
239 }
240 
GetWallpaperId(int32_t wallpaperType)241 int32_t WallpaperServiceProxy::GetWallpaperId(int32_t wallpaperType)
242 {
243     int32_t iWallpaperId = 1;
244     MessageParcel data;
245     MessageParcel reply;
246     MessageOption option;
247 
248     if (!data.WriteInterfaceToken(GetDescriptor())) {
249         HILOG_ERROR(" Failed to write parcelable ");
250         return false;
251     }
252 
253     data.WriteInt32(wallpaperType);
254     int32_t result = Remote()->SendRequest(
255         static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::GET_WALLPAPER_ID), data, reply, option);
256     if (result != ERR_NONE) {
257         HILOG_ERROR(" WallpaperServiceProxy::GetWallpaperId fail, result = %{public}d ", result);
258         return -1;
259     }
260 
261     iWallpaperId = reply.ReadInt32();
262     HILOG_INFO(" End => iWallpaperId[%{public}d]", iWallpaperId);
263     return iWallpaperId;
264 }
265 
IsChangePermitted()266 bool WallpaperServiceProxy::IsChangePermitted()
267 {
268     bool bFlag = false;
269     MessageParcel data;
270     MessageParcel reply;
271     MessageOption option;
272 
273     if (!data.WriteInterfaceToken(GetDescriptor())) {
274         HILOG_ERROR(" Failed to write parcelable ");
275         return false;
276     }
277 
278     int32_t result = Remote()->SendRequest(
279         static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::IS_CHANGE_PERMITTED), data, reply, option);
280     if (result != ERR_NONE) {
281         HILOG_ERROR(" WallpaperServiceProxy::IsChangePermitted fail, result = %{public}d ", result);
282         return false;
283     }
284 
285     bFlag = reply.ReadBool();
286     return bFlag;
287 }
288 
IsOperationAllowed()289 bool WallpaperServiceProxy::IsOperationAllowed()
290 {
291     bool bFlag = false;
292     MessageParcel data;
293     MessageParcel reply;
294     MessageOption option;
295 
296     if (!data.WriteInterfaceToken(GetDescriptor())) {
297         HILOG_ERROR(" Failed to write parcelable ");
298         return false;
299     }
300 
301     int32_t result = Remote()->SendRequest(
302         static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::IS_OPERATION_ALLOWED), data, reply, option);
303     if (result != ERR_NONE) {
304         HILOG_ERROR(" WallpaperServiceProxyIsOperationAllowed fail, result = %{public}d ", result);
305         return false;
306     }
307 
308     bFlag = reply.ReadBool();
309     return bFlag;
310 }
311 
ResetWallpaper(int32_t wallpaperType)312 ErrorCode WallpaperServiceProxy::ResetWallpaper(int32_t wallpaperType)
313 {
314     return ResetWallpaperInner(wallpaperType, WallpaperServiceIpcInterfaceCode::RESET_WALLPAPER);
315 }
316 
ResetWallpaperV9(int32_t wallpaperType)317 ErrorCode WallpaperServiceProxy::ResetWallpaperV9(int32_t wallpaperType)
318 {
319     return ResetWallpaperInner(wallpaperType, WallpaperServiceIpcInterfaceCode::RESET_WALLPAPER_V9);
320 }
321 
ResetWallpaperInner(int32_t wallpaperType,WallpaperServiceIpcInterfaceCode code)322 ErrorCode WallpaperServiceProxy::ResetWallpaperInner(int32_t wallpaperType, WallpaperServiceIpcInterfaceCode code)
323 {
324     MessageParcel data;
325     MessageParcel reply;
326     MessageOption option;
327 
328     if (!data.WriteInterfaceToken(GetDescriptor())) {
329         HILOG_ERROR(" Failed to write parcelable ");
330         return E_WRITE_PARCEL_ERROR;
331     }
332 
333     data.WriteInt32(wallpaperType);
334     int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
335     if (result != ERR_NONE) {
336         HILOG_ERROR(" WallpaperServiceProxy::ResetWallpaper fail, result = %{public}d ", result);
337         return E_DEAL_FAILED;
338     }
339     return ConvertIntToErrorCode(reply.ReadInt32());
340 }
341 
SendEvent(const std::string & eventType)342 ErrorCode WallpaperServiceProxy::SendEvent(const std::string &eventType)
343 {
344     MessageParcel data;
345     MessageParcel reply;
346     MessageOption option;
347 
348     if (!data.WriteInterfaceToken(GetDescriptor())) {
349         HILOG_ERROR("Failed to write parcelable");
350         return E_WRITE_PARCEL_ERROR;
351     }
352 
353     if (!data.WriteString(eventType)) {
354         HILOG_ERROR("write eventType failed.");
355         return E_WRITE_PARCEL_ERROR;
356     }
357 
358     int32_t result = Remote()->SendRequest(
359         static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SEND_EVENT), data, reply, option);
360     if (result != ERR_NONE) {
361         HILOG_ERROR("Failed to send event, result: %{public}d", result);
362         return E_DEAL_FAILED;
363     }
364     return ConvertIntToErrorCode(reply.ReadInt32());
365 }
366 
On(const std::string & type,sptr<IWallpaperEventListener> listener)367 ErrorCode WallpaperServiceProxy::On(const std::string &type, sptr<IWallpaperEventListener> listener)
368 {
369     HILOG_DEBUG("WallpaperServiceProxy::On in");
370     MessageParcel data;
371     MessageParcel reply;
372     MessageOption option;
373     if (!data.WriteInterfaceToken(GetDescriptor())) {
374         HILOG_ERROR(" Failed to write parcelable ");
375         return E_WRITE_PARCEL_ERROR;
376     }
377     if (listener == nullptr) {
378         HILOG_ERROR("listener is nullptr");
379         return E_DEAL_FAILED;
380     }
381 
382     if (!data.WriteString(type)) {
383         HILOG_ERROR("write type failed.");
384         return E_WRITE_PARCEL_ERROR;
385     }
386     if (!data.WriteRemoteObject(listener->AsObject())) {
387         HILOG_ERROR("write subscribe type or parcel failed.");
388         return E_WRITE_PARCEL_ERROR;
389     }
390     int32_t result =
391         Remote()->SendRequest(static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::ON), data, reply, option);
392     if (result != ERR_NONE) {
393         HILOG_ERROR(" WallpaperServiceProxy::On fail, result = %{public}d ", result);
394         return E_DEAL_FAILED;
395     }
396 
397     HILOG_DEBUG("WallpaperServiceProxy::On out");
398     return ConvertIntToErrorCode(reply.ReadInt32());
399 }
400 
Off(const std::string & type,sptr<IWallpaperEventListener> listener)401 ErrorCode WallpaperServiceProxy::Off(const std::string &type, sptr<IWallpaperEventListener> listener)
402 {
403     HILOG_DEBUG("WallpaperServiceProxy::Off in");
404     MessageParcel data;
405     MessageParcel reply;
406     MessageOption option;
407     if (!data.WriteInterfaceToken(GetDescriptor())) {
408         HILOG_ERROR(" Failed to write parcelable ");
409         return E_WRITE_PARCEL_ERROR;
410     }
411     if (!data.WriteString(type)) {
412         HILOG_ERROR("write type failed.");
413         return E_WRITE_PARCEL_ERROR;
414     }
415     if (listener != nullptr) {
416         if (!data.WriteRemoteObject(listener->AsObject())) {
417             HILOG_ERROR("write subscribe type or parcel failed.");
418             return E_WRITE_PARCEL_ERROR;
419         }
420     }
421 
422     int32_t result =
423         Remote()->SendRequest(static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::OFF), data, reply, option);
424     if (result != ERR_NONE) {
425         HILOG_ERROR(" WallpaperServiceProxy::Off fail, result = %{public}d ", result);
426         return E_DEAL_FAILED;
427     }
428 
429     HILOG_DEBUG("WallpaperServiceProxy::Off out");
430     return ConvertIntToErrorCode(reply.ReadInt32());
431 }
432 
RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback)433 bool WallpaperServiceProxy::RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback)
434 {
435     HILOG_DEBUG("WallpaperServiceProxy::RegisterWallpaperCallback in");
436     MessageParcel data;
437     MessageParcel reply;
438     MessageOption option;
439     if (!data.WriteInterfaceToken(GetDescriptor())) {
440         HILOG_ERROR(" Failed to write parcelable ");
441         return false;
442     }
443     if (callback == nullptr) {
444         HILOG_ERROR("callback is nullptr");
445         return false;
446     }
447     if (!data.WriteRemoteObject(callback->AsObject())) {
448         HILOG_ERROR("write subscribe type or parcel failed.");
449         return false;
450     }
451     HILOG_INFO("  Remote()->SendRequest");
452     int32_t result = Remote()->SendRequest(
453         static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::REGISTER_CALLBACK), data, reply, option);
454     if (result != ERR_NONE) {
455         HILOG_ERROR(" WallpaperServiceProxy::REGISTER_CALLBACK fail, result = %{public}d ", result);
456         return false;
457     }
458     int32_t status = reply.ReadInt32();
459     bool ret = status == static_cast<int32_t>(E_OK);
460     HILOG_DEBUG("WallpaperServiceProxy::REGISTER_CALLBACK out");
461     return ret;
462 }
463 
ConvertIntToErrorCode(int32_t errorCode)464 ErrorCode WallpaperServiceProxy::ConvertIntToErrorCode(int32_t errorCode)
465 {
466     ErrorCode wallpaperErrorCode = E_UNKNOWN;
467     switch (errorCode) {
468         case static_cast<int32_t>(E_OK):
469         case static_cast<int32_t>(E_SA_DIED):
470         case static_cast<int32_t>(E_READ_PARCEL_ERROR):
471         case static_cast<int32_t>(E_WRITE_PARCEL_ERROR):
472         case static_cast<int32_t>(E_PUBLISH_FAIL):
473         case static_cast<int32_t>(E_TRANSACT_ERROR):
474         case static_cast<int32_t>(E_DEAL_FAILED):
475         case static_cast<int32_t>(E_PARAMETERS_INVALID):
476         case static_cast<int32_t>(E_SET_RTC_FAILED):
477         case static_cast<int32_t>(E_NOT_FOUND):
478         case static_cast<int32_t>(E_NO_PERMISSION):
479         case static_cast<int32_t>(E_FILE_ERROR):
480         case static_cast<int32_t>(E_IMAGE_ERRCODE):
481         case static_cast<int32_t>(E_NO_MEMORY):
482         case static_cast<int32_t>(E_NOT_SYSTEM_APP):
483         case static_cast<int32_t>(E_USER_IDENTITY_ERROR):
484             wallpaperErrorCode = static_cast<ErrorCode>(errorCode);
485             break;
486         default:
487             break;
488     }
489     return wallpaperErrorCode;
490 }
491 } // namespace WallpaperMgrService
492 } // namespace OHOS
493