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