• 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 GetPixelMap(int32_t wallpaperType, FdInfo &fdInfo) override;
68     ErrorCode GetColors(int32_t wallpaperType, std::vector<uint64_t> &colors) override;
69     ErrorCode GetFile(int32_t wallpaperType, int32_t &wallpaperFd) override;
70     int32_t GetWallpaperId(int32_t wallpaperType) override;
71     bool IsChangePermitted() override;
72     bool IsOperationAllowed() override;
73     ErrorCode ResetWallpaper(int32_t wallpaperType) override;
74     ErrorCode On(const std::string &type, sptr<IWallpaperEventListener> listener) override;
75     ErrorCode Off(const std::string &type, sptr<IWallpaperEventListener> listener) override;
76     bool RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback) override;
77     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
78 
79     ErrorCode SetWallpaperV9(int32_t fd, int32_t wallpaperType, int32_t length) override;
80     ErrorCode GetPixelMapV9(int32_t wallpaperType, FdInfo &fdInfo) override;
81     ErrorCode GetColorsV9(int32_t wallpaperType, std::vector<uint64_t> &colors) override;
82     ErrorCode ResetWallpaperV9(int32_t wallpaperType) override;
83     ErrorCode SetVideo(int32_t fd, int32_t wallpaperType, int32_t length) override;
84     ErrorCode SetCustomWallpaper(int32_t fd, int32_t wallpaperType, int32_t length) override;
85     ErrorCode SendEvent(const std::string &eventType) override;
86 
87 public:
88     static void OnBootPhase();
89     void OnInitUser(int32_t newUserId);
90     void OnRemovedUser(int32_t userId);
91     void OnSwitchedUser(int32_t userId);
92     void ReporterFault(MiscServices::FaultType faultType, MiscServices::FaultCode faultCode);
93     void RegisterSubscriber(int32_t times);
94 #ifndef THEME_SERVICE
95     void AddWallpaperExtensionDeathRecipient(const sptr<IRemoteObject> &remoteObject);
96     void StartExtensionAbility(int32_t times);
97 #endif
98 protected:
99     void OnStart() override;
100     void OnStop() override;
101     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
102 
103 private:
104     void InitData();
105     void InitResources(int32_t userId, WallpaperType wallpaperType);
106     void InitQueryUserId(int32_t times);
107     bool InitUsersOnBoot();
108     bool CompareColor(const uint64_t &localColor, const ColorManager::Color &color);
109     bool SaveColor(int32_t userId, WallpaperType wallpaperType);
110     void LoadSettingsLocked(int32_t userId);
111     std::string GetWallpaperDir(int32_t userId, WallpaperType wallpaperType);
112     bool GetFileNameFromMap(int32_t userId, WallpaperType wallpaperType, std::string &fileName);
113     bool GetPictureFileName(int32_t userId, WallpaperType wallpaperType, std::string &fileName);
114     bool GetWallpaperSafeLocked(int32_t userId, WallpaperType wallpaperType, WallpaperData &wallpaperData);
115     void ClearWallpaperLocked(int32_t userId, WallpaperType wallpaperType);
116     ErrorCode SetDefaultDataForWallpaper(int32_t userId, WallpaperType wallpaperType);
117     int32_t MakeWallpaperIdLocked();
118     bool CheckCallingPermission(const std::string &permissionName);
119     bool GetBundleNameByUid(std::int32_t uid, std::string &bname);
120     ErrorCode SetWallpaperBackupData(int32_t userId, WallpaperResourceType resourceType,
121         const std::string &uriOrPixelMap, WallpaperType wallpaperType);
122 #ifndef THEME_SERVICE
123     bool ConnectExtensionAbility();
124 #endif
125     bool IsSystemApp();
126     ErrorCode GetImageFd(int32_t userId, WallpaperType wallpaperType, int32_t &fd);
127     ErrorCode GetImageSize(int32_t userId, WallpaperType wallpaperType, int32_t &size);
128     bool RestoreUserResources(const WallpaperData &wallpaperData, WallpaperType wallpaperType);
129     bool InitUserDir(int32_t userId);
130     bool BlockRetry(int64_t interval, uint32_t maxRetryTimes, std::function<bool()> function);
131     int32_t QueryActiveUserId();
132     bool CheckUserPermissionById(int32_t userId);
133 
134     bool SendWallpaperChangeEvent(int32_t userId, WallpaperType wallpaperType);
135     ErrorCode SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType);
136     void OnColorsChange(WallpaperType wallpaperType, const ColorManager::Color &color);
137     ErrorCode CheckValid(int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType);
138     bool WallpaperChanged(WallpaperType wallpaperType, WallpaperResourceType resType, const std::string &uri);
139     void NotifyColorChange(const std::vector<uint64_t> &colors, const WallpaperType &wallpaperType);
140     bool SaveWallpaperState(int32_t userId, WallpaperType wallpaperType, WallpaperResourceType resourceType);
141     void LoadWallpaperState();
142     WallpaperResourceType GetResType(int32_t userId, WallpaperType wallpaperType);
143     void InitBundleNameParameter();
144     void RemoveExtensionDeathRecipient();
145     static void GetWallpaperFile(WallpaperResourceType resourceType, const WallpaperData &wallpaperData,
146         std::string &wallpaperFile);
147 #ifdef THEME_SERVICE
148     void InitThemeResource();
149 #endif
150 
151 private:
152     int32_t Init();
153     ServiceRunningState state_;
154     void InitServiceHandler();
155     static std::mutex instanceLock_;
156     static sptr<WallpaperService> instance_;
157     static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
158     std::string wallpaperLockScreenFilePath_;
159     std::string wallpaperSystemFilePath_;
160     std::string wallpaperLockScreenFileFullPath_;
161     std::string wallpaperSystemFileFullPath_;
162     std::string wallpaperLockScreenCropFileFullPath_;
163     std::string wallpaperSystemCropFileFullPath_;
164     std::string wallpaperTmpFullPath_;
165     std::string wallpaperCropPath_;
166     typedef std::map<int32_t, WallpaperEventListener *> DISPLAYIDCOLORSLISTENERMAP;
167     typedef std::map<int32_t, DISPLAYIDCOLORSLISTENERMAP> COLORSLISTENERMAP;
168     typedef std::list<WallpaperEventListener *> LISTENERLIST;
169     LISTENERLIST colorListeners_;
170     COLORSLISTENERMAP colorsChangedListeners_;
171     ConcurrentMap<int32_t, WallpaperData> systemWallpaperMap_;
172     ConcurrentMap<int32_t, WallpaperData> lockWallpaperMap_;
173     atomic<int32_t> wallpaperId_;
174     sptr<IWallpaperCallback> callbackProxy_ = nullptr;
175     std::shared_ptr<WallpaperCommonEventSubscriber> subscriber_;
176 #ifndef THEME_SERVICE
177     sptr<WallpaperExtensionAbilityConnection> connection_;
178 #endif
179     sptr<IRemoteObject::DeathRecipient> recipient_;
180     sptr<IRemoteObject> extensionRemoteObject_;
181     std::mutex remoteObjectMutex_;
182 
183     std::string name_;
184     std::mutex mtx_;
185     uint64_t lockWallpaperColor_;
186     uint64_t systemWallpaperColor_;
187     std::map<std::string, WallpaperListenerMap> wallpaperEventMap_;
188     std::mutex listenerMapMutex_;
189     int32_t pictureWidth_ = 0;
190     int32_t pictureHeight_ = 0;
191     std::string appBundleName_;
192 };
193 } // namespace WallpaperMgrService
194 } // namespace OHOS
195 #endif // SERVICES_INCLUDE_WALLPAPER_SERVICES_H