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 <mutex> 20 #include <list> 21 #include <map> 22 #include "concurrent_map.h" 23 #include "system_ability.h" 24 #include "wallpaper_service_stub.h" 25 #include "wallpaper_common.h" 26 #include "event_handler.h" 27 #include "pixel_map.h" 28 #include "wallpaper_manager_common_info.h" 29 #include "wallpaper_color_change_listener.h" 30 #include "wallpaper_data.h" 31 #include "i_wallpaper_manager_callback.h" 32 #include "component_name.h" 33 #include "file_deal.h" 34 #include "fault_reporter.h" 35 #include "statistic_reporter.h" 36 #include "wm_common.h" 37 #include "window_option.h" 38 #include "surface_buffer.h" 39 #include "display_manager.h" 40 #include "ipc_skeleton.h" 41 #include "accesstoken_kit.h" 42 #include "image_source.h" 43 #include "want.h" 44 #include "wallpaper_extension_ability_connection.h" 45 #include "ability_manager_errors.h" 46 #include "os_account_manager.h" 47 #include "ability_connect_callback_interface.h" 48 #include "bundle_mgr_interface.h" 49 50 namespace OHOS { 51 namespace ColorManager { 52 class Color; 53 } 54 namespace WallpaperMgrService { 55 enum class ServiceRunningState { 56 STATE_NOT_START, 57 STATE_RUNNING 58 }; 59 60 class WallpaperService : public SystemAbility, public WallpaperServiceStub { 61 DECLARE_SYSTEM_ABILITY(WallpaperService); 62 63 public: 64 DISALLOW_COPY_AND_MOVE(WallpaperService); 65 66 WallpaperService(int32_t systemAbilityId, bool runOnCreate); 67 WallpaperService(); 68 ~WallpaperService(); 69 70 static sptr<WallpaperService> GetInstance(); 71 int32_t SetWallpaperByFD(int fd, int wallpaperType, int length) override; 72 int32_t SetWallpaperByMap(int fd, int wallpaperType, int length) override; 73 int32_t GetPixelMap(int wallpaperType, FdInfo &fdInfo) override; 74 std::vector<uint64_t> GetColors(int wallpaperType) override; 75 int32_t GetFile(int32_t wallpaperType, int32_t &wallpaperFd) override; 76 int GetWallpaperId(int wallpaperType) override; 77 int GetWallpaperMinHeight() override; 78 int GetWallpaperMinWidth() override; 79 bool IsChangePermitted() override; 80 bool IsOperationAllowed() override; 81 int32_t ResetWallpaper(int wallpaperType) override; 82 bool On(sptr<IWallpaperColorChangeListener> listener) override; 83 bool Off(sptr<IWallpaperColorChangeListener> listener) override; 84 bool RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback) override; 85 int Dump(int fd, const std::vector<std::u16string> &args) override; 86 public: 87 bool SetLockWallpaperCallback(IWallpaperManagerCallback* cb); 88 static void OnBootPhase(); 89 void ReporterFault(MiscServices::FaultType faultType, MiscServices::FaultCode faultCode); 90 void ReporterUsageTimeStatisic(); 91 void RegisterSubscriber(int times); 92 void StartWallpaperExtension(); 93 void AddWallpaperExtensionDeathRecipient(const sptr<IRemoteObject> &remoteObject); 94 95 protected: 96 void OnStart() override; 97 void OnStop() override; 98 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 99 100 private: 101 /** 102 * Get current user id. 103 * @param none 104 * @return userid 105 */ 106 int GetUserId(); 107 108 /** 109 * Get current Display Id. 110 * @param none 111 * @return displayid 112 */ 113 int GetDisplayId(); 114 115 /** 116 * initData ,such as dir,filename,and so on. 117 * @param 118 * @return none 119 */ 120 void InitData(); 121 int64_t WritePixelMapToFile(const std::string &filePath, std::unique_ptr< OHOS::Media::PixelMap> pixelMap); 122 bool CompareColor(const uint64_t &localColor, const ColorManager::Color &color); 123 bool SaveColor(int wallpaperType); 124 void LoadSettingsLocked(int userId, bool keepDimensionHints); 125 void MigrateFromOld(); 126 std::string GetWallpaperDir(); 127 bool GetWallpaperSafeLocked(int userId, int wpType, WallpaperData paperdata); 128 void ClearWallpaperLocked(int userId, int wpType); 129 int32_t SetDefaultDateForWallpaper(int userId, int wpType); 130 int MakeWallpaperIdLocked(); 131 bool WPCheckCallingPermission(const std::string &promissionName); 132 bool WPGetBundleNameByUid(std::int32_t uid, std::string &bname); 133 bool MakeCropWallpaper(int wallpaperType); 134 int32_t SetWallpaperBackupData(std::string uriOrPixelMap, int wallpaperType); 135 int32_t ConnectExtensionAbility(const OHOS::AAFwk::Want& want); 136 int32_t GetFilePath(int wallpaperType, std::string &filePath); 137 bool IsSystemApp(); 138 OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr(); 139 private: 140 141 int32_t Init(); 142 ServiceRunningState state_; 143 void InitServiceHandler(); 144 bool CopySystemWallpaper(); 145 bool CopyScreenLockWallpaper(); 146 147 static std::mutex instanceLock_; 148 static sptr<WallpaperService> instance_; 149 static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 150 std::string wallpaperLockScreenFilePath_; 151 std::string wallpaperSystemFilePath_; 152 std::string wallpaperLockScreenFileFullPath_; 153 std::string wallpaperSystemFileFullPath_; 154 std::string wallpaperLockScreenCropFileFullPath_; 155 std::string wallpaperSystemCropFileFullPath_; 156 std::string wallpaperTmpFullPath_; 157 std::string wallpaperCropPath; 158 typedef std::map<int, WallpaperColorChangeListener*> DISPLAYIDCOLORSLISTENERMAP; 159 typedef std::map<int, DISPLAYIDCOLORSLISTENERMAP> COLORSLISTENERMAP; 160 typedef std::list<WallpaperColorChangeListener*> LISTENERLIST; 161 LISTENERLIST colorListeners_; 162 COLORSLISTENERMAP colorsChangedListeners_; 163 ConcurrentMap<int, WallpaperData> wallpaperMap_; 164 ConcurrentMap<int, WallpaperData> lockWallpaperMap_; 165 atomic<int32_t> wallpaperId_; 166 int userId_; 167 static const std::string WALLPAPER; 168 static const std::string WALLPAPER_CROP; 169 static const std::string WALLPAPER_LOCK_ORIG; 170 static const std::string WALLPAPER_LOCK_CROP; 171 static const std::string WALLPAPER_BUNDLE_NAME; 172 sptr<IWallpaperCallback> callbackProxy = nullptr; 173 sptr<IRemoteObject::DeathRecipient> recipient_; 174 175 std::string name_; 176 std::mutex mtx; 177 uint64_t lockWallpaperColor_; 178 uint64_t systemWallpaperColor_; 179 std::map<int, sptr<IWallpaperColorChangeListener>> colorChangeListenerMap_; 180 std::mutex listenerMapMutex_; 181 }; 182 } 183 } // OHOS 184 #endif // SERVICES_INCLUDE_WALLPAPER_SERVICES_H