• 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 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     bool IsDefaultWallpaperResource(int32_t userId, int32_t wallpaperType) override;
92 
93 public:
94     void OnInitUser(int32_t newUserId);
95     void OnRemovedUser(int32_t userId);
96     void OnSwitchedUser(int32_t userId);
97     void ReporterFault(MiscServices::FaultType faultType, MiscServices::FaultCode faultCode);
98     void RegisterSubscriber(int32_t times);
99 #ifndef THEME_SERVICE
100     void AddWallpaperExtensionDeathRecipient(const sptr<IRemoteObject> &remoteObject);
101     void StartExtensionAbility(int32_t times);
102 #endif
103 protected:
104     void OnStart() override;
105     void OnStop() override;
106     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
107 
108 private:
109     void InitData();
110     void InitQueryUserId(int32_t times);
111     bool InitUsersOnBoot();
112     bool CompareColor(const uint64_t &localColor, const ColorManager::Color &color);
113     bool SaveColor(int32_t userId, WallpaperType wallpaperType);
114     void UpdataWallpaperMap(int32_t userId, WallpaperType wallpaperType);
115     std::string GetWallpaperDir(int32_t userId, WallpaperType wallpaperType);
116     bool GetFileNameFromMap(int32_t userId, WallpaperType wallpaperType, std::string &fileName);
117     bool GetPictureFileName(int32_t userId, WallpaperType wallpaperType, std::string &fileName);
118     bool GetWallpaperSafeLocked(int32_t userId, WallpaperType wallpaperType, WallpaperData &wallpaperData);
119     void ClearWallpaperLocked(int32_t userId, WallpaperType wallpaperType);
120     ErrorCode SetDefaultDataForWallpaper(int32_t userId, WallpaperType wallpaperType);
121     int32_t MakeWallpaperIdLocked();
122     bool CheckCallingPermission(const std::string &permissionName);
123     ErrorCode SetWallpaperBackupData(int32_t userId, WallpaperResourceType resourceType,
124         const std::string &uriOrPixelMap, WallpaperType wallpaperType);
125     ErrorCode WritePixelMapToFile(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, std::string wallpaperTmpFullPath,
126         int32_t wallpaperType, WallpaperResourceType resourceType);
127     int64_t WritePixelMapToStream(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, std::ostream &outputStream);
128 #ifndef THEME_SERVICE
129     bool ConnectExtensionAbility();
130 #endif
131     bool IsSystemApp();
132     bool IsNativeSa();
133     ErrorCode GetImageFd(int32_t userId, WallpaperType wallpaperType, int32_t &fd);
134     ErrorCode GetImageSize(int32_t userId, WallpaperType wallpaperType, int32_t &size);
135     bool RestoreUserResources(int32_t userId, WallpaperData &wallpaperData, WallpaperType wallpaperType);
136     bool InitUserDir(int32_t userId);
137     int32_t QueryActiveUserId();
138     bool CheckUserPermissionById(int32_t userId);
139 
140     bool SendWallpaperChangeEvent(int32_t userId, WallpaperType wallpaperType);
141     ErrorCode SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType);
142     ErrorCode SetWallpaperByPixelMap(
143         std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType, WallpaperResourceType resourceType);
144     void OnColorsChange(WallpaperType wallpaperType, const ColorManager::Color &color);
145     ErrorCode CheckValid(int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType);
146     bool WallpaperChanged(WallpaperType wallpaperType, WallpaperResourceType resType, const std::string &uri);
147     void NotifyColorChange(const std::vector<uint64_t> &colors, const WallpaperType &wallpaperType);
148     bool SaveWallpaperState(int32_t userId, WallpaperType wallpaperType, WallpaperResourceType resourceType);
149     void LoadWallpaperState();
150     WallpaperResourceType GetResType(int32_t userId, WallpaperType wallpaperType);
151     void RemoveExtensionDeathRecipient();
152     static void GetWallpaperFile(
153         WallpaperResourceType resourceType, const WallpaperData &wallpaperData, std::string &wallpaperFile);
154     std::string GetDefaultResDir();
155     WallpaperData GetWallpaperDefaultPath(WallpaperType wallpaperType);
156     std::string GetWallpaperPathInJson(const std::string manifestName, const std::string filePath);
157     void ClearRedundantFile(int32_t userId, WallpaperType wallpaperType, std::string fileName);
158     std::string GetExistFilePath(const std::string &filePath);
159     ErrorCode SetAllWallpapers(
160         std::vector<WallpaperPictureInfo> allWallpaperInfo, int32_t wallpaperType, WallpaperResourceType resourceType);
161     ErrorCode WriteFdToFile(WallpaperPictureInfo &wallpaperPictureInfo, std::string &path);
162     ErrorCode SetAllWallpaperBackupData(std::vector<WallpaperPictureInfo> allWallpaperInfos, int32_t userId,
163         WallpaperType wallpaperType, WallpaperData &wallpaperData);
164     std::string GetWallpaperDataFile(
165         WallpaperPictureInfo &wallpaperPictureInfo, int32_t userId, WallpaperType wallpaperType);
166     ErrorCode GetImageSize(
167         int32_t userId, WallpaperType wallpaperType, int32_t &size, int32_t foldState, int32_t rotateState);
168     bool GetWallpaperDataPath(int32_t userId, WallpaperType wallpaperType, std::string &filePathName,
169         int32_t foldState, int32_t rotateState);
170     ErrorCode GetImageFd(
171         int32_t userId, WallpaperType wallpaperType, int32_t &fd, int32_t foldState, int32_t rotateState);
172     void DeleteTempResource(std::vector<WallpaperPictureInfo> &tempResourceFiles);
173     void UpdateWallpaperDataFile(WallpaperPictureInfo &wallpaperPictureInfo, int32_t userId,
174         WallpaperType wallpaperType, WallpaperData &wallpaperData);
175     ErrorCode UpdateWallpaperData(
176         std::vector<WallpaperPictureInfo> allWallpaperInfos, int32_t userId, WallpaperType wallpaperType);
177     std::string GetWallpaperPath(WallpaperData wallpaperData);
178     void ClearnWallpaperDataFile(WallpaperData &wallpaperData);
179     std::string GetFoldStateName(FoldState foldState);
180     std::string GetRotateStateName(RotateState rotateState);
181     std::string GetWallpaperPath(int32_t foldState, int32_t rotateState, WallpaperData &wallpaperData);
182 
183 private:
184     int32_t Init();
185     ServiceRunningState state_;
186     void InitServiceHandler();
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