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 #ifndef INNERKITS_WALLPAPER_MANAGER_H
17 #define INNERKITS_WALLPAPER_MANAGER_H
18 
19 #include <list>
20 #include <map>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 
25 #include "avmetadatahelper.h"
26 #include "fault_reporter.h"
27 #include "i_wallpaper_service.h"
28 #include "ipc_skeleton.h"
29 #include "singleton.h"
30 #include "wallpaper_common.h"
31 
32 using JScallback = bool (*)(int32_t);
33 namespace OHOS {
34 using namespace MiscServices;
35 namespace WallpaperMgrService {
36 class WallpaperManager {
37     WallpaperManager();
38     ~WallpaperManager();
39 
40 public:
41     DISALLOW_COPY_AND_MOVE(WallpaperManager);
42     static WallpaperManager &GetInstance();
43 
44     /**
45     * Wallpaper set.
46     * @param  uriOrPixelMap Wallpaper picture; wallpaperType Wallpaper type,
47     * values for WALLPAPER_SYSTEM or WALLPAPER_LOCKSCREEN
48     * @return  ErrorCode
49     */
50     ErrorCode SetWallpaper(std::string uri, int32_t wallpaperType, const ApiInfo &apiInfo);
51 
52     /**
53     * Wallpaper set.
54     * @param  pixelMap:picture pixelMap struct; wallpaperType Wallpaper type,
55     * values for WALLPAPER_SYSTEM or WALLPAPER_LOCKSCREEN
56     * @return  ErrorCode
57     */
58     ErrorCode SetWallpaper(
59         std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType, const ApiInfo &apiInfo);
60 
61     /**
62         *Obtains the default pixel map of a wallpaper of the specified type.
63         * @param wallpaperType Wallpaper type, values for WALLPAPER_SYSTEM or WALLPAPER_LOCKSCREEN;
64         * Obtains image.PixelMap png type The bitmap file of wallpaper
65         * @return ErrorCode
66         * @permission ohos.permission.GET_WALLPAPER
67         * @systemapi Hide this for inner system use.
68     */
69     ErrorCode GetPixelMap(
70         int32_t wallpaperType, const ApiInfo &apiInfo, std::shared_ptr<OHOS::Media::PixelMap> &PixelMap);
71 
72     /**
73      * Obtains the WallpaperColorsCollection instance for the wallpaper of the specified type.
74      * @param wallpaperType Wallpaper type, values for WALLPAPER_SYSTEM or WALLPAPER_LOCKSCREEN
75      * @return number type of array callback function
76      */
77     ErrorCode GetColors(int32_t wallpaperType, const ApiInfo &apiInfo, std::vector<uint64_t> &colors);
78 
79     /**
80      * Obtains the ID of the wallpaper of the specified type.
81      * @param wallpaperType Wallpaper type, values for WALLPAPER_SYSTEM or WALLPAPER_LOCKSCREEN
82      * @return number type of callback function
83      */
84     int32_t GetWallpaperId(int32_t wallpaperType);
85 
86     ErrorCode GetFile(int32_t wallpaperType, int32_t &wallpaperFd);
87 
88     /**
89      * Obtains the minimum height of the wallpaper.
90      * @return number type of callback function
91      */
92     ErrorCode GetWallpaperMinHeight(const ApiInfo &apiInfo, int32_t &minHeight);
93 
94     /**
95      * Obtains the minimum width of the wallpaper.
96      * @return number type of callback function
97      */
98     ErrorCode GetWallpaperMinWidth(const ApiInfo &apiInfo, int32_t &minWidth);
99 
100     /**
101      * Checks whether to allow the application to change the wallpaper for the current user.
102      * @return boolean type of callback function
103      */
104     bool IsChangePermitted();
105 
106     /**
107      * Checks whether a user is allowed to set wallpapers.
108      * @return boolean type of callback function
109      */
110     bool IsOperationAllowed();
111 
112     /**
113      * Removes a wallpaper of the specified type and restores the default one.
114      * @param wallpaperType  Wallpaper type, values for WALLPAPER_SYSTEM or WALLPAPER_LOCKSCREEN
115      * @permission ohos.permission.SET_WALLPAPER
116      */
117     ErrorCode ResetWallpaper(std::int32_t wallpaperType, const ApiInfo &apiInfo);
118 
119     /**
120      * Registers a listener for wallpaper event to receive notifications about the changes.
121      * @param type event type
122      * @param listener event listener
123      * @return error code
124      */
125     ErrorCode On(const std::string &type, std::shared_ptr<WallpaperEventListener> listener);
126 
127     /**
128      * Unregisters a listener for wallpaper event to receive notifications about the changes.
129      * @param type event type
130      * @param listener event listener
131      * @return error code
132      */
133     ErrorCode Off(const std::string &type, std::shared_ptr<WallpaperEventListener> listener);
134 
135     /**
136      * Sets live wallpaper of the specified type based on the uri path of the MP4 file.
137      * @param uri Indicates the uri path of the MP4 file.
138      * @param wallpaperType Wallpaper type, values for WALLPAPER_SYSTEM or WALLPAPER_LOCKSCREEN
139      * @return ErrorCode
140      * @permission ohos.permission.SET_WALLPAPER
141      */
142     ErrorCode SetVideo(const std::string &uri, const int32_t wallpaperType);
143 
144     /**
145      * Sets custom wallpaper of the specified type based on the uri path.
146      * @param uri Indicates the uri path.
147      * @param wallpaperType Wallpaper type, values for WALLPAPER_SYSTEM or WALLPAPER_LOCKSCREEN
148      * @return ErrorCode
149      * @permission ohos.permission.SET_WALLPAPER
150      */
151     ErrorCode SetCustomWallpaper(const std::string &uri, int32_t wallpaperType);
152 
153     /**
154      * The application sends the event to the wallpaper service.
155      * @param eventType Event type, values for SHOW_SYSTEMSCREEN or SHOW_LOCKSCREEN
156      * @return ErrorCode
157      * @permission ohos.permission.SET_WALLPAPER
158      */
159     ErrorCode SendEvent(const std::string &eventType);
160     ErrorCode SetAllWallpapers(std::vector<WallpaperInfo> wallpaperInfo, int32_t wallpaperType);
161     bool RegisterWallpaperCallback(JScallback callback);
162     ErrorCode GetCorrespondWallpaper(int32_t wallpaperType, int32_t foldState, int32_t rotateState,
163         std::shared_ptr<OHOS::Media::PixelMap> &pixelMap);
164 
165     JScallback GetCallback();
166 
167     void SetCallback(JScallback cb);
168 
169     void ReporterFault(FaultType faultType, FaultCode faultCode);
170 
171     void CloseWallpaperFd(int32_t wallpaperType);
172 
173     bool RegisterWallpaperListener();
174 
175 private:
176     class DeathRecipient final : public IRemoteObject::DeathRecipient {
177     public:
178         explicit DeathRecipient();
179         ~DeathRecipient() override;
180         DISALLOW_COPY_AND_MOVE(DeathRecipient);
181 
182         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
183     };
184 
185     template<typename F, typename... Args> ErrCode CallService(F func, Args &&...args);
186     bool CheckVideoFormat(const std::string &fileName);
187     void ResetService(const wptr<IRemoteObject> &remote);
188     sptr<IWallpaperService> GetService();
189     int64_t WritePixelMapToStream(std::ostream &outputStream, std::shared_ptr<OHOS::Media::PixelMap> pixelMap);
190     bool OpenFile(const std::string &fileName, int32_t &fd, int64_t &fileSize);
191     ErrorCode CheckWallpaperFormat(const std::string &realPath, bool isLive, long &length);
192     ErrorCode CreatePixelMapByFd(int32_t fd, int32_t size, std::shared_ptr<OHOS::Media::PixelMap> &pixelMap);
193     ErrorCode GetFdByPath(
194         const WallpaperInfo &wallpaperInfo, WallpaperPictureInfo &wallpaperPictureInfo, std::string fileRealPath);
195     void CloseWallpaperInfoFd(std::vector<WallpaperPictureInfo> wallpaperPictureInfos);
196     sptr<IWallpaperService> wallpaperProxy_{};
197     sptr<IRemoteObject::DeathRecipient> deathRecipient_{};
198     std::mutex wallpaperFdLock_;
199     std::map<int32_t, int32_t> wallpaperFdMap_;
200     std::mutex wallpaperProxyLock_;
201     std::mutex listenerMapLock_;
202     std::map<std::string, sptr<WallpaperEventListenerClient>> listenerMap_;
203     bool (*callback)(int32_t);
204 };
205 } // namespace WallpaperMgrService
206 } // namespace OHOS
207 #endif
208