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 SERVICES_INCLUDE_WALLPAPER_SERVICES_H 17 #define SERVICES_INCLUDE_WALLPAPER_SERVICES_H 18 19 #include <list> 20 #include <map> 21 #include <mutex> 22 23 #include "accesstoken_kit.h" 24 #include "component_name.h" 25 #include "concurrent_map.h" 26 #include "event_handler.h" 27 #include "fault_reporter.h" 28 #include "file_deal.h" 29 #include "i_wallpaper_manager_callback.h" 30 #include "image_source.h" 31 #include "ipc_skeleton.h" 32 #include "os_account_manager.h" 33 #include "pixel_map.h" 34 #include "system_ability.h" 35 #include "wallpaper_common.h" 36 #include "wallpaper_common_event_subscriber.h" 37 #include "wallpaper_data.h" 38 #include "wallpaper_event_listener.h" 39 #include "wallpaper_manager_common_info.h" 40 #include "wallpaper_service_stub.h" 41 #include "want.h" 42 43 #ifndef THEME_SERVICE 44 #include "ability_connect_callback_interface.h" 45 #include "ability_manager_errors.h" 46 #include "wallpaper_extension_ability_connection.h" 47 #endif 48 namespace OHOS { 49 namespace ColorManager { 50 class Color; 51 } 52 namespace WallpaperMgrService { 53 class WallpaperService : public SystemAbility, public WallpaperServiceStub { 54 DECLARE_SYSTEM_ABILITY(WallpaperService); 55 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 56 enum class FileType : uint8_t { WALLPAPER_FILE, CROP_FILE }; 57 using WallpaperListenerMap = std::map<int32_t, sptr<IWallpaperEventListener>>; 58 59 public: 60 DISALLOW_COPY_AND_MOVE(WallpaperService); 61 62 WallpaperService(int32_t systemAbilityId, bool runOnCreate); 63 WallpaperService(); 64 ~WallpaperService(); 65 66 ErrorCode SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length) override; 67 ErrorCode SetWallpaperByPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType) override; 68 ErrorCode GetPixelMap(int32_t wallpaperType, FdInfo &fdInfo) override; 69 ErrorCode GetColors(int32_t wallpaperType, std::vector<uint64_t> &colors) override; 70 ErrorCode GetFile(int32_t wallpaperType, int32_t &wallpaperFd) override; 71 int32_t GetWallpaperId(int32_t wallpaperType) override; 72 bool IsChangePermitted() override; 73 bool IsOperationAllowed() override; 74 ErrorCode ResetWallpaper(int32_t wallpaperType) override; 75 ErrorCode On(const std::string &type, sptr<IWallpaperEventListener> listener) override; 76 ErrorCode Off(const std::string &type, sptr<IWallpaperEventListener> listener) override; 77 bool RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback) override; 78 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 79 80 ErrorCode SetWallpaperV9(int32_t fd, int32_t wallpaperType, int32_t length) override; 81 ErrorCode SetWallpaperV9ByPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType) override; 82 ErrorCode GetPixelMapV9(int32_t wallpaperType, FdInfo &fdInfo) override; 83 ErrorCode GetColorsV9(int32_t wallpaperType, std::vector<uint64_t> &colors) override; 84 ErrorCode ResetWallpaperV9(int32_t wallpaperType) override; 85 ErrorCode SetVideo(int32_t fd, int32_t wallpaperType, int32_t length) override; 86 ErrorCode SetCustomWallpaper(int32_t fd, int32_t wallpaperType, int32_t length) override; 87 ErrorCode SendEvent(const std::string &eventType) override; 88 ErrorCode SetAllWallpapers(std::vector<WallpaperPictureInfo> allWallpaperInfo, int32_t wallpaperType) override; 89 ErrorCode GetCorrespondWallpaper( 90 int32_t wallpaperType, int32_t foldState, int32_t rotateState, IWallpaperService::FdInfo &fdInfo) override; 91 92 public: 93 void OnInitUser(int32_t newUserId); 94 void OnRemovedUser(int32_t userId); 95 void OnSwitchedUser(int32_t userId); 96 void ReporterFault(MiscServices::FaultType faultType, MiscServices::FaultCode faultCode); 97 void RegisterSubscriber(int32_t times); 98 #ifndef THEME_SERVICE 99 void AddWallpaperExtensionDeathRecipient(const sptr<IRemoteObject> &remoteObject); 100 void StartExtensionAbility(int32_t times); 101 #endif 102 protected: 103 void OnStart() override; 104 void OnStop() override; 105 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 106 107 private: 108 void InitData(); 109 void InitQueryUserId(int32_t times); 110 bool InitUsersOnBoot(); 111 bool CompareColor(const uint64_t &localColor, const ColorManager::Color &color); 112 bool SaveColor(int32_t userId, WallpaperType wallpaperType); 113 void UpdataWallpaperMap(int32_t userId, WallpaperType wallpaperType); 114 std::string GetWallpaperDir(int32_t userId, WallpaperType wallpaperType); 115 bool GetFileNameFromMap(int32_t userId, WallpaperType wallpaperType, std::string &fileName); 116 bool GetPictureFileName(int32_t userId, WallpaperType wallpaperType, std::string &fileName); 117 bool GetWallpaperSafeLocked(int32_t userId, WallpaperType wallpaperType, WallpaperData &wallpaperData); 118 void ClearWallpaperLocked(int32_t userId, WallpaperType wallpaperType); 119 ErrorCode SetDefaultDataForWallpaper(int32_t userId, WallpaperType wallpaperType); 120 int32_t MakeWallpaperIdLocked(); 121 bool CheckCallingPermission(const std::string &permissionName); 122 ErrorCode SetWallpaperBackupData(int32_t userId, WallpaperResourceType resourceType, 123 const std::string &uriOrPixelMap, WallpaperType wallpaperType); 124 ErrorCode WritePixelMapToFile(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, std::string wallpaperTmpFullPath, 125 int32_t wallpaperType, WallpaperResourceType resourceType); 126 int64_t WritePixelMapToStream(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, std::ostream &outputStream); 127 #ifndef THEME_SERVICE 128 bool ConnectExtensionAbility(); 129 #endif 130 bool IsSystemApp(); 131 ErrorCode GetImageFd(int32_t userId, WallpaperType wallpaperType, int32_t &fd); 132 ErrorCode GetImageSize(int32_t userId, WallpaperType wallpaperType, int32_t &size); 133 bool RestoreUserResources(int32_t userId, WallpaperData &wallpaperData, WallpaperType wallpaperType); 134 bool InitUserDir(int32_t userId); 135 int32_t QueryActiveUserId(); 136 bool CheckUserPermissionById(int32_t userId); 137 138 bool SendWallpaperChangeEvent(int32_t userId, WallpaperType wallpaperType); 139 ErrorCode SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType); 140 ErrorCode SetWallpaperByPixelMap( 141 std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType, WallpaperResourceType resourceType); 142 void OnColorsChange(WallpaperType wallpaperType, const ColorManager::Color &color); 143 ErrorCode CheckValid(int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType); 144 bool WallpaperChanged(WallpaperType wallpaperType, WallpaperResourceType resType, const std::string &uri); 145 void NotifyColorChange(const std::vector<uint64_t> &colors, const WallpaperType &wallpaperType); 146 bool SaveWallpaperState(int32_t userId, WallpaperType wallpaperType, WallpaperResourceType resourceType); 147 void LoadWallpaperState(); 148 WallpaperResourceType GetResType(int32_t userId, WallpaperType wallpaperType); 149 void RemoveExtensionDeathRecipient(); 150 static void GetWallpaperFile( 151 WallpaperResourceType resourceType, const WallpaperData &wallpaperData, std::string &wallpaperFile); 152 std::string GetDefaultResDir(); 153 WallpaperData GetWallpaperDefaultPath(WallpaperType wallpaperType); 154 std::string GetWallpaperPathInJson(const std::string manifestName, const std::string filePath); 155 void ClearRedundantFile(int32_t userId, WallpaperType wallpaperType, std::string fileName); 156 std::string GetExistFilePath(const std::string &filePath); 157 ErrorCode SetAllWallpapers( 158 std::vector<WallpaperPictureInfo> allWallpaperInfo, int32_t wallpaperType, WallpaperResourceType resourceType); 159 ErrorCode WriteFdToFile(WallpaperPictureInfo &wallpaperPictureInfo, std::string &path); 160 ErrorCode SetAllWallpaperBackupData(std::vector<WallpaperPictureInfo> allWallpaperInfos, int32_t userId, 161 WallpaperType wallpaperType, WallpaperData &wallpaperData); 162 std::string GetWallpaperDataFile( 163 WallpaperPictureInfo &wallpaperPictureInfo, int32_t userId, WallpaperType wallpaperType); 164 ErrorCode GetImageSize( 165 int32_t userId, WallpaperType wallpaperType, int32_t &size, int32_t foldState, int32_t rotateState); 166 bool GetWallpaperDataPath(int32_t userId, WallpaperType wallpaperType, std::string &filePathName, 167 int32_t foldState, int32_t rotateState); 168 ErrorCode GetImageFd( 169 int32_t userId, WallpaperType wallpaperType, int32_t &fd, int32_t foldState, int32_t rotateState); 170 void DeleteTempResource(std::vector<WallpaperPictureInfo> &tempResourceFiles); 171 void UpdateWallpaperDataFile(WallpaperPictureInfo &wallpaperPictureInfo, int32_t userId, 172 WallpaperType wallpaperType, WallpaperData &wallpaperData); 173 ErrorCode UpdateWallpaperData( 174 std::vector<WallpaperPictureInfo> allWallpaperInfos, int32_t userId, WallpaperType wallpaperType); 175 std::string GetWallpaperPath(WallpaperData wallpaperData); 176 void ClearnWallpaperDataFile(WallpaperData &wallpaperData); 177 std::string GetFoldStateName(FoldState foldState); 178 std::string GetRotateStateName(RotateState rotateState); 179 std::string GetWallpaperPath(int32_t foldState, int32_t rotateState, WallpaperData &wallpaperData); 180 181 private: 182 int32_t Init(); 183 ServiceRunningState state_; 184 void InitServiceHandler(); 185 static std::mutex instanceLock_; 186 static sptr<WallpaperService> instance_; 187 static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 188 std::string wallpaperLockScreenFilePath_; 189 std::string wallpaperSystemFilePath_; 190 std::string wallpaperLockScreenFileFullPath_; 191 std::string wallpaperSystemFileFullPath_; 192 std::string wallpaperLockScreenCropFileFullPath_; 193 std::string wallpaperSystemCropFileFullPath_; 194 std::string wallpaperTmpFullPath_; 195 std::string wallpaperCropPath_; 196 typedef std::map<int32_t, WallpaperEventListener *> DISPLAYIDCOLORSLISTENERMAP; 197 typedef std::map<int32_t, DISPLAYIDCOLORSLISTENERMAP> COLORSLISTENERMAP; 198 typedef std::list<WallpaperEventListener *> LISTENERLIST; 199 LISTENERLIST colorListeners_; 200 COLORSLISTENERMAP colorsChangedListeners_; 201 ConcurrentMap<int32_t, WallpaperData> systemWallpaperMap_; 202 ConcurrentMap<int32_t, WallpaperData> lockWallpaperMap_; 203 atomic<int32_t> wallpaperId_; 204 sptr<IWallpaperCallback> callbackProxy_ = nullptr; 205 std::shared_ptr<WallpaperCommonEventSubscriber> subscriber_; 206 #ifndef THEME_SERVICE 207 sptr<WallpaperExtensionAbilityConnection> connection_; 208 #endif 209 sptr<IRemoteObject::DeathRecipient> recipient_; 210 sptr<IRemoteObject> extensionRemoteObject_; 211 std::mutex remoteObjectMutex_; 212 213 std::mutex mtx_; 214 uint64_t lockWallpaperColor_; 215 uint64_t systemWallpaperColor_; 216 std::map<std::string, WallpaperListenerMap> wallpaperEventMap_; 217 std::mutex listenerMapMutex_; 218 std::int32_t currentUserId_; 219 std::string appBundleName_; 220 std::mutex wallpaperColorMtx_; 221 }; 222 } // namespace WallpaperMgrService 223 } // namespace OHOS 224 #endif // SERVICES_INCLUDE_WALLPAPER_SERVICES_H