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