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