• 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(int wallpaperType)28 std::vector<uint64_t> WallpaperServiceProxy::GetColors(int wallpaperType)
29 {
30     std::vector<uint64_t> colors;
31     MessageParcel data;
32     MessageParcel reply;
33     MessageOption option;
34 
35     if (!data.WriteInterfaceToken(GetDescriptor())) {
36         HILOG_ERROR(" Failed to write parcelable ");
37         return colors;
38     }
39     if (!data.WriteInt32(wallpaperType)) {
40         HILOG_ERROR(" Failed to WriteInt32 ");
41         return colors;
42     }
43 
44     int32_t result = Remote()->SendRequest(GET_COLORS, data, reply, option);
45     if (result != ERR_NONE) {
46         HILOG_ERROR(" get colors result = %{public}d ", result);
47         return colors;
48     }
49 
50     if (!reply.ReadUInt64Vector(&colors)) {
51         HILOG_ERROR(" Failed to ReadUInt64Vector ");
52     }
53     return colors;
54 }
55 
GetFile(int32_t wallpaperType,int32_t & wallpaperFd)56 int32_t WallpaperServiceProxy::GetFile(int32_t wallpaperType, int32_t &wallpaperFd)
57 {
58     MessageParcel data;
59     MessageParcel reply;
60     MessageOption option;
61 
62     if (!data.WriteInterfaceToken(GetDescriptor())) {
63         HILOG_ERROR(" Failed to write parcelable ");
64         return INVALID_FD;
65     }
66     if (!data.WriteInt32(wallpaperType)) {
67         HILOG_ERROR(" Failed to WriteInt32 ");
68         return INVALID_FD;
69     }
70 
71     int32_t result = Remote()->SendRequest(GET_FILE, data, reply, option);
72     if (result != ERR_NONE) {
73         HILOG_ERROR(" get file result = %{public}d ", result);
74         return INVALID_FD;
75     }
76     int32_t wallpaperErrorCode = reply.ReadInt32();
77     if (wallpaperErrorCode == static_cast<int32_t>(E_OK)) {
78         wallpaperFd = reply.ReadFileDescriptor();
79     }
80     return wallpaperErrorCode;
81 }
82 
getUrl()83 std::string WallpaperServiceProxy::getUrl()
84 {
85     HILOG_INFO("return FWReadUrl= %{public}s ", FWReadUrl.c_str());
86     return FWReadUrl;
87 }
SetWallpaperByMap(int fd,int wallpaperType,int length)88 int32_t WallpaperServiceProxy::SetWallpaperByMap(int fd, int wallpaperType, int length)
89 {
90     HILOG_INFO(" SetWallpaperByMap ");
91     MessageParcel data;
92     MessageParcel reply;
93     MessageOption option;
94 
95     if (!data.WriteInterfaceToken(GetDescriptor())) {
96         HILOG_ERROR(" Failed to write parcelable ");
97         return static_cast<int32_t>(E_WRITE_PARCEL_ERROR);
98     }
99     if (!data.WriteFileDescriptor(fd)) {
100         HILOG_ERROR(" Failed to WriteFileDescriptor ");
101         return static_cast<int32_t>(E_WRITE_PARCEL_ERROR);
102     }
103 
104     if (!data.WriteInt32(wallpaperType)) {
105         HILOG_ERROR(" Failed to WriteInt32 ");
106         return static_cast<int32_t>(E_WRITE_PARCEL_ERROR);
107     }
108     if (!data.WriteInt32(length)) {
109         HILOG_ERROR(" Failed to WriteInt32 ");
110         return static_cast<int32_t>(E_WRITE_PARCEL_ERROR);
111     }
112 
113     int32_t result = Remote()->SendRequest(SET_WALLPAPER_MAP, data, reply, option);
114     if (result != ERR_NONE) {
115         HILOG_ERROR(" WallpaperCalbackProxy::SetWallpaperfail, result = %{public}d ", result);
116         return static_cast<int32_t>(E_DEAL_FAILED);
117     }
118 
119     return reply.ReadInt32();
120 }
SetWallpaperByFD(int fd,int wallpaperType,int length)121 int32_t WallpaperServiceProxy::SetWallpaperByFD(int fd, int wallpaperType, int length)
122 {
123     HILOG_INFO(" SetWallpaperByFD ");
124     MessageParcel data;
125     MessageParcel reply;
126     MessageOption option;
127 
128     if (!data.WriteInterfaceToken(GetDescriptor())) {
129         HILOG_ERROR(" Failed to write parcelable ");
130         return static_cast<int32_t>(E_WRITE_PARCEL_ERROR);
131     }
132     if (!data.WriteFileDescriptor(fd)) {
133         HILOG_ERROR(" Failed to WriteFileDescriptor ");
134         return static_cast<int32_t>(E_WRITE_PARCEL_ERROR);
135     }
136     if (!data.WriteInt32(wallpaperType)) {
137         HILOG_ERROR(" Failed to WriteInt32 ");
138         return static_cast<int32_t>(E_WRITE_PARCEL_ERROR);
139     }
140     if (!data.WriteInt32(length)) {
141         HILOG_ERROR(" Failed to WriteInt32 ");
142         return static_cast<int32_t>(E_WRITE_PARCEL_ERROR);
143     }
144 
145     int32_t result = Remote()->SendRequest(SET_WALLPAPER_URI_FD, data, reply, option);
146     if (result != ERR_NONE) {
147         HILOG_ERROR(" WallpaperCalbackProxy::SetWallpaperfail, result = %{public}d ", result);
148         return static_cast<int32_t>(E_DEAL_FAILED);
149     }
150     return reply.ReadInt32();
151 }
152 
GetPixelMap(int wallpaperType,IWallpaperService::FdInfo & fdInfo)153 int32_t WallpaperServiceProxy::GetPixelMap(int wallpaperType, IWallpaperService::FdInfo &fdInfo)
154 {
155     HILOG_INFO(" WallpaperServiceProxy::getPixelMap --> start ");
156     MessageParcel data;
157     MessageParcel reply;
158     MessageOption option;
159 
160     if (!data.WriteInterfaceToken(GetDescriptor())) {
161         HILOG_ERROR(" Failed to write parcelable ");
162         return static_cast<int32_t>(E_WRITE_PARCEL_ERROR);
163     }
164 
165     if (!data.WriteInt32(wallpaperType)) {
166         HILOG_ERROR(" Failed to WriteInt32 ");
167         return static_cast<int32_t>(E_DEAL_FAILED);
168     }
169     int32_t result = Remote()->SendRequest(GET_PIXELMAP, data, reply, option);
170     if (result != ERR_NONE) {
171         HILOG_ERROR(" WallpaperServiceProxy::GetPixelMap fail, result = %{public}d ", result);
172         return static_cast<int32_t>(E_DEAL_FAILED);
173     }
174     int32_t wallpaperErrorCode = reply.ReadInt32();
175     if (wallpaperErrorCode == static_cast<int32_t>(E_OK)) {
176         fdInfo.size = reply.ReadInt32();
177         fdInfo.fd = reply.ReadFileDescriptor();
178     }
179     return wallpaperErrorCode;
180 }
181 
GetWallpaperId(int wallpaperType)182 int WallpaperServiceProxy::GetWallpaperId(int wallpaperType)
183 {
184     int iWallpaperId = 1;
185     MessageParcel data;
186     MessageParcel reply;
187     MessageOption option;
188 
189     if (!data.WriteInterfaceToken(GetDescriptor())) {
190         HILOG_ERROR(" Failed to write parcelable ");
191         return false;
192     }
193 
194     data.WriteInt32(wallpaperType);
195     int32_t result = Remote()->SendRequest(GET_WALLPAPER_ID, data, reply, option);
196     if (result != ERR_NONE) {
197         HILOG_ERROR(" WallpaperServiceProxy::GetWallpaperId fail, result = %{public}d ", result);
198         return -1;
199     }
200 
201     iWallpaperId = reply.ReadInt32();
202     HILOG_INFO(" End => iWallpaperId[%{public}d]", iWallpaperId);
203     return iWallpaperId;
204 }
GetWallpaperMinHeight()205 int WallpaperServiceProxy::GetWallpaperMinHeight()
206 {
207     int iWallpaperMinHeight = 0;
208     MessageParcel data;
209     MessageParcel reply;
210     MessageOption option;
211 
212     if (!data.WriteInterfaceToken(GetDescriptor())) {
213         HILOG_ERROR(" Failed to write parcelable ");
214         return -1;
215     }
216 
217     int32_t result = Remote()->SendRequest(GET_WALLPAPER_MIN_HEIGHT, data, reply, option);
218     if (result != ERR_NONE) {
219         HILOG_ERROR(" WallpaperServiceProxy::GetWallpaperMinHeight fail, result = %{public}d ", result);
220         return -1;
221     }
222 
223     iWallpaperMinHeight = reply.ReadInt32();
224     HILOG_INFO(" End => iWallpaperMinHeight[%{public}d]", iWallpaperMinHeight);
225     return iWallpaperMinHeight;
226 }
227 
GetWallpaperMinWidth()228 int WallpaperServiceProxy::GetWallpaperMinWidth()
229 {
230     int iWallpaperMinWidth = 0;
231     MessageParcel data;
232     MessageParcel reply;
233     MessageOption option;
234 
235     if (!data.WriteInterfaceToken(GetDescriptor())) {
236         HILOG_ERROR(" Failed to write parcelable ");
237         return -1;
238     }
239 
240     int32_t result = Remote()->SendRequest(GET_WALLPAPER_MIN_WIDTH, data, reply, option);
241     if (result != ERR_NONE) {
242         HILOG_ERROR(" WallpaperServiceProxy::GetWallpaperMinWidth fail, result = %{public}d ", result);
243         return -1;
244     }
245 
246     iWallpaperMinWidth = reply.ReadInt32();
247     HILOG_INFO(" End => iWallpaperMinWidth[%{public}d]", iWallpaperMinWidth);
248     return iWallpaperMinWidth;
249 }
250 
IsChangePermitted()251 bool WallpaperServiceProxy::IsChangePermitted()
252 {
253     bool bFlag = false;
254     MessageParcel data;
255     MessageParcel reply;
256     MessageOption option;
257 
258     if (!data.WriteInterfaceToken(GetDescriptor())) {
259         HILOG_ERROR(" Failed to write parcelable ");
260         return false;
261     }
262 
263     int32_t result = Remote()->SendRequest(IS_CHANGE_PERMITTED, data, reply, option);
264     if (result != ERR_NONE) {
265         HILOG_ERROR(" WallpaperServiceProxy::IsChangePermitted fail, result = %{public}d ", result);
266         return false;
267     }
268 
269     bFlag = reply.ReadBool();
270     return bFlag;
271 }
272 
IsOperationAllowed()273 bool WallpaperServiceProxy::IsOperationAllowed()
274 {
275     bool bFlag = false;
276     MessageParcel data;
277     MessageParcel reply;
278     MessageOption option;
279 
280     if (!data.WriteInterfaceToken(GetDescriptor())) {
281         HILOG_ERROR(" Failed to write parcelable ");
282         return false;
283     }
284 
285     int32_t result = Remote()->SendRequest(IS_OPERATION_ALLOWED, data, reply, option);
286     if (result != ERR_NONE) {
287         HILOG_ERROR(" WallpaperServiceProxyIsOperationAllowed fail, result = %{public}d ", result);
288         return false;
289     }
290 
291     bFlag = reply.ReadBool();
292     return bFlag;
293 }
294 
ResetWallpaper(int wallpaperType)295 int32_t WallpaperServiceProxy::ResetWallpaper(int wallpaperType)
296 {
297     MessageParcel data;
298     MessageParcel reply;
299     MessageOption option;
300 
301     if (!data.WriteInterfaceToken(GetDescriptor())) {
302         HILOG_ERROR(" Failed to write parcelable ");
303         return static_cast<int32_t>(E_WRITE_PARCEL_ERROR);
304     }
305 
306     data.WriteInt32(wallpaperType);
307     int32_t result = Remote()->SendRequest(RESET_WALLPAPER, data, reply, option);
308     if (result != ERR_NONE) {
309         HILOG_ERROR(" WallpaperServiceProxy::ResetWallpaper fail, result = %{public}d ", result);
310         return static_cast<int32_t>(E_DEAL_FAILED);
311     }
312     return reply.ReadInt32();
313 }
314 
On(sptr<IWallpaperColorChangeListener> listener)315 bool WallpaperServiceProxy::On(sptr<IWallpaperColorChangeListener> listener)
316 {
317     HILOG_DEBUG("WallpaperServiceProxy::On in");
318     MessageParcel data;
319     MessageParcel reply;
320     MessageOption option;
321     if (!data.WriteInterfaceToken(GetDescriptor())) {
322         HILOG_ERROR(" Failed to write parcelable ");
323         return false;
324     }
325     if (listener == nullptr) {
326         HILOG_ERROR("listener is nullptr");
327         return false;
328     }
329     if (!data.WriteRemoteObject(listener->AsObject())) {
330         HILOG_ERROR("write subscribe type or parcel failed.");
331         return false;
332     }
333     int32_t result = Remote()->SendRequest(ON, data, reply, option);
334     if (result != ERR_NONE) {
335         HILOG_ERROR(" WallpaperServiceProxy::On fail, result = %{public}d ", result);
336         return false;
337     }
338 
339     int32_t status = reply.ReadInt32();
340     bool ret = (status == static_cast<int32_t>(E_OK)) ? true : false;
341     HILOG_DEBUG("WallpaperServiceProxy::On out");
342     return ret;
343 }
344 
Off(sptr<IWallpaperColorChangeListener> listener)345 bool WallpaperServiceProxy::Off(sptr<IWallpaperColorChangeListener> listener)
346 {
347     HILOG_DEBUG("WallpaperServiceProxy::Off in");
348     MessageParcel data;
349     MessageParcel reply;
350     MessageOption option;
351     if (!data.WriteInterfaceToken(GetDescriptor())) {
352         HILOG_ERROR(" Failed to write parcelable ");
353         return false;
354     }
355     if (listener != nullptr) {
356         if (!data.WriteRemoteObject(listener->AsObject())) {
357             HILOG_ERROR("write subscribe type or parcel failed.");
358             return false;
359         }
360     }
361 
362     int32_t result = Remote()->SendRequest(OFF, data, reply, option);
363     if (result != ERR_NONE) {
364         HILOG_ERROR(" WallpaperServiceProxy::Off fail, result = %{public}d ", result);
365         return false;
366     }
367 
368     int32_t status = reply.ReadInt32();
369     bool ret = (status == static_cast<int32_t>(E_OK)) ? true : false;
370     HILOG_DEBUG("WallpaperServiceProxy::Off out");
371     return ret;
372 }
373 
RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback)374 bool WallpaperServiceProxy::RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback)
375 {
376     HILOG_DEBUG("WallpaperServiceProxy::RegisterWallpaperCallback in");
377     MessageParcel data;
378     MessageParcel reply;
379     MessageOption option;
380     if (!data.WriteInterfaceToken(GetDescriptor())) {
381         HILOG_ERROR(" Failed to write parcelable ");
382         return false;
383     }
384     if (callback == nullptr) {
385         HILOG_ERROR("callback is nullptr");
386         return false;
387     }
388     if (!data.WriteRemoteObject(callback->AsObject())) {
389         HILOG_ERROR("write subscribe type or parcel failed.");
390         return false;
391     }
392     HILOG_INFO("  Remote()->SendRequest");
393     int32_t result = Remote()->SendRequest(REGISTER_CALLBACK, data, reply, option);
394     if (result != ERR_NONE) {
395         HILOG_ERROR(" WallpaperServiceProxy::REGISTER_CALLBACK fail, result = %{public}d ", result);
396         return false;
397     }
398 
399     int32_t status = reply.ReadInt32();
400     bool ret = (status == static_cast<int32_t>(E_OK)) ? true : false;
401     HILOG_DEBUG("WallpaperServiceProxy::REGISTER_CALLBACK out");
402     return ret;
403 }
404 } // namespace WallpaperMgrService
405 } // namespace OHOS
406