1ce968135Sopenharmony_ci/*
2ce968135Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3ce968135Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4ce968135Sopenharmony_ci * you may not use this file except in compliance with the License.
5ce968135Sopenharmony_ci * You may obtain a copy of the License at
6ce968135Sopenharmony_ci *
7ce968135Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8ce968135Sopenharmony_ci *
9ce968135Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10ce968135Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11ce968135Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12ce968135Sopenharmony_ci * See the License for the specific language governing permissions and
13ce968135Sopenharmony_ci * limitations under the License.
14ce968135Sopenharmony_ci */
15ce968135Sopenharmony_ci
16ce968135Sopenharmony_ci#ifndef SERVICES_INCLUDE_WALLPAPER_SERVICES_H
17ce968135Sopenharmony_ci#define SERVICES_INCLUDE_WALLPAPER_SERVICES_H
18ce968135Sopenharmony_ci
19ce968135Sopenharmony_ci#include <list>
20ce968135Sopenharmony_ci#include <map>
21ce968135Sopenharmony_ci#include <mutex>
22ce968135Sopenharmony_ci
23ce968135Sopenharmony_ci#include "accesstoken_kit.h"
24ce968135Sopenharmony_ci#include "component_name.h"
25ce968135Sopenharmony_ci#include "concurrent_map.h"
26ce968135Sopenharmony_ci#include "event_handler.h"
27ce968135Sopenharmony_ci#include "fault_reporter.h"
28ce968135Sopenharmony_ci#include "file_deal.h"
29ce968135Sopenharmony_ci#include "i_wallpaper_manager_callback.h"
30ce968135Sopenharmony_ci#include "image_source.h"
31ce968135Sopenharmony_ci#include "ipc_skeleton.h"
32ce968135Sopenharmony_ci#include "os_account_manager.h"
33ce968135Sopenharmony_ci#include "pixel_map.h"
34ce968135Sopenharmony_ci#include "system_ability.h"
35ce968135Sopenharmony_ci#include "wallpaper_common.h"
36ce968135Sopenharmony_ci#include "wallpaper_common_event_subscriber.h"
37ce968135Sopenharmony_ci#include "wallpaper_data.h"
38ce968135Sopenharmony_ci#include "wallpaper_event_listener.h"
39ce968135Sopenharmony_ci#include "wallpaper_manager_common_info.h"
40ce968135Sopenharmony_ci#include "wallpaper_service_stub.h"
41ce968135Sopenharmony_ci#include "want.h"
42ce968135Sopenharmony_ci
43ce968135Sopenharmony_ci#ifndef THEME_SERVICE
44ce968135Sopenharmony_ci#include "ability_connect_callback_interface.h"
45ce968135Sopenharmony_ci#include "ability_manager_errors.h"
46ce968135Sopenharmony_ci#include "wallpaper_extension_ability_connection.h"
47ce968135Sopenharmony_ci#endif
48ce968135Sopenharmony_cinamespace OHOS {
49ce968135Sopenharmony_cinamespace ColorManager {
50ce968135Sopenharmony_ciclass Color;
51ce968135Sopenharmony_ci}
52ce968135Sopenharmony_cinamespace WallpaperMgrService {
53ce968135Sopenharmony_ciclass WallpaperService : public SystemAbility, public WallpaperServiceStub {
54ce968135Sopenharmony_ci    DECLARE_SYSTEM_ABILITY(WallpaperService);
55ce968135Sopenharmony_ci    enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
56ce968135Sopenharmony_ci    enum class FileType : uint8_t { WALLPAPER_FILE, CROP_FILE };
57ce968135Sopenharmony_ci    using WallpaperListenerMap = std::map<int32_t, sptr<IWallpaperEventListener>>;
58ce968135Sopenharmony_ci
59ce968135Sopenharmony_cipublic:
60ce968135Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(WallpaperService);
61ce968135Sopenharmony_ci
62ce968135Sopenharmony_ci    WallpaperService(int32_t systemAbilityId, bool runOnCreate);
63ce968135Sopenharmony_ci    WallpaperService();
64ce968135Sopenharmony_ci    ~WallpaperService();
65ce968135Sopenharmony_ci
66ce968135Sopenharmony_ci    ErrorCode SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length) override;
67ce968135Sopenharmony_ci    ErrorCode SetWallpaperByPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType) override;
68ce968135Sopenharmony_ci    ErrorCode GetPixelMap(int32_t wallpaperType, FdInfo &fdInfo) override;
69ce968135Sopenharmony_ci    ErrorCode GetColors(int32_t wallpaperType, std::vector<uint64_t> &colors) override;
70ce968135Sopenharmony_ci    ErrorCode GetFile(int32_t wallpaperType, int32_t &wallpaperFd) override;
71ce968135Sopenharmony_ci    int32_t GetWallpaperId(int32_t wallpaperType) override;
72ce968135Sopenharmony_ci    bool IsChangePermitted() override;
73ce968135Sopenharmony_ci    bool IsOperationAllowed() override;
74ce968135Sopenharmony_ci    ErrorCode ResetWallpaper(int32_t wallpaperType) override;
75ce968135Sopenharmony_ci    ErrorCode On(const std::string &type, sptr<IWallpaperEventListener> listener) override;
76ce968135Sopenharmony_ci    ErrorCode Off(const std::string &type, sptr<IWallpaperEventListener> listener) override;
77ce968135Sopenharmony_ci    bool RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback) override;
78ce968135Sopenharmony_ci    int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
79ce968135Sopenharmony_ci
80ce968135Sopenharmony_ci    ErrorCode SetWallpaperV9(int32_t fd, int32_t wallpaperType, int32_t length) override;
81ce968135Sopenharmony_ci    ErrorCode SetWallpaperV9ByPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType) override;
82ce968135Sopenharmony_ci    ErrorCode GetPixelMapV9(int32_t wallpaperType, FdInfo &fdInfo) override;
83ce968135Sopenharmony_ci    ErrorCode GetColorsV9(int32_t wallpaperType, std::vector<uint64_t> &colors) override;
84ce968135Sopenharmony_ci    ErrorCode ResetWallpaperV9(int32_t wallpaperType) override;
85ce968135Sopenharmony_ci    ErrorCode SetVideo(int32_t fd, int32_t wallpaperType, int32_t length) override;
86ce968135Sopenharmony_ci    ErrorCode SetCustomWallpaper(int32_t fd, int32_t wallpaperType, int32_t length) override;
87ce968135Sopenharmony_ci    ErrorCode SendEvent(const std::string &eventType) override;
88ce968135Sopenharmony_ci
89ce968135Sopenharmony_cipublic:
90ce968135Sopenharmony_ci    void OnInitUser(int32_t newUserId);
91ce968135Sopenharmony_ci    void OnRemovedUser(int32_t userId);
92ce968135Sopenharmony_ci    void OnSwitchedUser(int32_t userId);
93ce968135Sopenharmony_ci    void ReporterFault(MiscServices::FaultType faultType, MiscServices::FaultCode faultCode);
94ce968135Sopenharmony_ci    void RegisterSubscriber(int32_t times);
95ce968135Sopenharmony_ci#ifndef THEME_SERVICE
96ce968135Sopenharmony_ci    void AddWallpaperExtensionDeathRecipient(const sptr<IRemoteObject> &remoteObject);
97ce968135Sopenharmony_ci    void StartExtensionAbility(int32_t times);
98ce968135Sopenharmony_ci#endif
99ce968135Sopenharmony_ciprotected:
100ce968135Sopenharmony_ci    void OnStart() override;
101ce968135Sopenharmony_ci    void OnStop() override;
102ce968135Sopenharmony_ci    void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
103ce968135Sopenharmony_ci
104ce968135Sopenharmony_ciprivate:
105ce968135Sopenharmony_ci    void InitData();
106ce968135Sopenharmony_ci    void InitQueryUserId(int32_t times);
107ce968135Sopenharmony_ci    bool InitUsersOnBoot();
108ce968135Sopenharmony_ci    bool CompareColor(const uint64_t &localColor, const ColorManager::Color &color);
109ce968135Sopenharmony_ci    bool SaveColor(int32_t userId, WallpaperType wallpaperType);
110ce968135Sopenharmony_ci    void UpdataWallpaperMap(int32_t userId, WallpaperType wallpaperType);
111ce968135Sopenharmony_ci    std::string GetWallpaperDir(int32_t userId, WallpaperType wallpaperType);
112ce968135Sopenharmony_ci    bool GetFileNameFromMap(int32_t userId, WallpaperType wallpaperType, std::string &fileName);
113ce968135Sopenharmony_ci    bool GetPictureFileName(int32_t userId, WallpaperType wallpaperType, std::string &fileName);
114ce968135Sopenharmony_ci    bool GetWallpaperSafeLocked(int32_t userId, WallpaperType wallpaperType, WallpaperData &wallpaperData);
115ce968135Sopenharmony_ci    void ClearWallpaperLocked(int32_t userId, WallpaperType wallpaperType);
116ce968135Sopenharmony_ci    ErrorCode SetDefaultDataForWallpaper(int32_t userId, WallpaperType wallpaperType);
117ce968135Sopenharmony_ci    int32_t MakeWallpaperIdLocked();
118ce968135Sopenharmony_ci    bool CheckCallingPermission(const std::string &permissionName);
119ce968135Sopenharmony_ci    ErrorCode SetWallpaperBackupData(int32_t userId, WallpaperResourceType resourceType,
120ce968135Sopenharmony_ci        const std::string &uriOrPixelMap, WallpaperType wallpaperType);
121ce968135Sopenharmony_ci    ErrorCode WritePixelMapToFile(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, std::string wallpaperTmpFullPath,
122ce968135Sopenharmony_ci        int32_t wallpaperType, WallpaperResourceType resourceType);
123ce968135Sopenharmony_ci    int64_t WritePixelMapToStream(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, std::ostream &outputStream);
124ce968135Sopenharmony_ci#ifndef THEME_SERVICE
125ce968135Sopenharmony_ci    bool ConnectExtensionAbility();
126ce968135Sopenharmony_ci#endif
127ce968135Sopenharmony_ci    bool IsSystemApp();
128ce968135Sopenharmony_ci    ErrorCode GetImageFd(int32_t userId, WallpaperType wallpaperType, int32_t &fd);
129ce968135Sopenharmony_ci    ErrorCode GetImageSize(int32_t userId, WallpaperType wallpaperType, int32_t &size);
130ce968135Sopenharmony_ci    bool RestoreUserResources(int32_t userId, WallpaperData &wallpaperData, WallpaperType wallpaperType);
131ce968135Sopenharmony_ci    bool InitUserDir(int32_t userId);
132ce968135Sopenharmony_ci    int32_t QueryActiveUserId();
133ce968135Sopenharmony_ci    bool CheckUserPermissionById(int32_t userId);
134ce968135Sopenharmony_ci
135ce968135Sopenharmony_ci    bool SendWallpaperChangeEvent(int32_t userId, WallpaperType wallpaperType);
136ce968135Sopenharmony_ci    ErrorCode SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType);
137ce968135Sopenharmony_ci    ErrorCode SetWallpaperByPixelMap(
138ce968135Sopenharmony_ci        std::shared_ptr<OHOS::Media::PixelMap> pixelMap, int32_t wallpaperType, WallpaperResourceType resourceType);
139ce968135Sopenharmony_ci    void OnColorsChange(WallpaperType wallpaperType, const ColorManager::Color &color);
140ce968135Sopenharmony_ci    ErrorCode CheckValid(int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType);
141ce968135Sopenharmony_ci    bool WallpaperChanged(WallpaperType wallpaperType, WallpaperResourceType resType, const std::string &uri);
142ce968135Sopenharmony_ci    void NotifyColorChange(const std::vector<uint64_t> &colors, const WallpaperType &wallpaperType);
143ce968135Sopenharmony_ci    bool SaveWallpaperState(int32_t userId, WallpaperType wallpaperType, WallpaperResourceType resourceType);
144ce968135Sopenharmony_ci    void LoadWallpaperState();
145ce968135Sopenharmony_ci    WallpaperResourceType GetResType(int32_t userId, WallpaperType wallpaperType);
146ce968135Sopenharmony_ci    void RemoveExtensionDeathRecipient();
147ce968135Sopenharmony_ci    static void GetWallpaperFile(
148ce968135Sopenharmony_ci        WallpaperResourceType resourceType, const WallpaperData &wallpaperData, std::string &wallpaperFile);
149ce968135Sopenharmony_ci    std::string GetDefaultResDir();
150ce968135Sopenharmony_ci    std::string GetWallpaperDefaultPath(WallpaperType wallpaperType);
151ce968135Sopenharmony_ci    std::string GetWallpaperPathInJson(const std::string filePath);
152ce968135Sopenharmony_ci    void ClearRedundantFile(int32_t userId, WallpaperType wallpaperType, std::string fileName);
153ce968135Sopenharmony_ci
154ce968135Sopenharmony_ciprivate:
155ce968135Sopenharmony_ci    int32_t Init();
156ce968135Sopenharmony_ci    ServiceRunningState state_;
157ce968135Sopenharmony_ci    void InitServiceHandler();
158ce968135Sopenharmony_ci    static std::mutex instanceLock_;
159ce968135Sopenharmony_ci    static sptr<WallpaperService> instance_;
160ce968135Sopenharmony_ci    static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
161ce968135Sopenharmony_ci    std::string wallpaperLockScreenFilePath_;
162ce968135Sopenharmony_ci    std::string wallpaperSystemFilePath_;
163ce968135Sopenharmony_ci    std::string wallpaperLockScreenFileFullPath_;
164ce968135Sopenharmony_ci    std::string wallpaperSystemFileFullPath_;
165ce968135Sopenharmony_ci    std::string wallpaperLockScreenCropFileFullPath_;
166ce968135Sopenharmony_ci    std::string wallpaperSystemCropFileFullPath_;
167ce968135Sopenharmony_ci    std::string wallpaperTmpFullPath_;
168ce968135Sopenharmony_ci    std::string wallpaperCropPath_;
169ce968135Sopenharmony_ci    typedef std::map<int32_t, WallpaperEventListener *> DISPLAYIDCOLORSLISTENERMAP;
170ce968135Sopenharmony_ci    typedef std::map<int32_t, DISPLAYIDCOLORSLISTENERMAP> COLORSLISTENERMAP;
171ce968135Sopenharmony_ci    typedef std::list<WallpaperEventListener *> LISTENERLIST;
172ce968135Sopenharmony_ci    LISTENERLIST colorListeners_;
173ce968135Sopenharmony_ci    COLORSLISTENERMAP colorsChangedListeners_;
174ce968135Sopenharmony_ci    ConcurrentMap<int32_t, WallpaperData> systemWallpaperMap_;
175ce968135Sopenharmony_ci    ConcurrentMap<int32_t, WallpaperData> lockWallpaperMap_;
176ce968135Sopenharmony_ci    atomic<int32_t> wallpaperId_;
177ce968135Sopenharmony_ci    sptr<IWallpaperCallback> callbackProxy_ = nullptr;
178ce968135Sopenharmony_ci    std::shared_ptr<WallpaperCommonEventSubscriber> subscriber_;
179ce968135Sopenharmony_ci#ifndef THEME_SERVICE
180ce968135Sopenharmony_ci    sptr<WallpaperExtensionAbilityConnection> connection_;
181ce968135Sopenharmony_ci#endif
182ce968135Sopenharmony_ci    sptr<IRemoteObject::DeathRecipient> recipient_;
183ce968135Sopenharmony_ci    sptr<IRemoteObject> extensionRemoteObject_;
184ce968135Sopenharmony_ci    std::mutex remoteObjectMutex_;
185ce968135Sopenharmony_ci
186ce968135Sopenharmony_ci    std::mutex mtx_;
187ce968135Sopenharmony_ci    uint64_t lockWallpaperColor_;
188ce968135Sopenharmony_ci    uint64_t systemWallpaperColor_;
189ce968135Sopenharmony_ci    std::map<std::string, WallpaperListenerMap> wallpaperEventMap_;
190ce968135Sopenharmony_ci    std::mutex listenerMapMutex_;
191ce968135Sopenharmony_ci    std::int32_t currentUserId_;
192ce968135Sopenharmony_ci    std::string appBundleName_;
193ce968135Sopenharmony_ci    std::mutex wallpaperColorMtx_;
194ce968135Sopenharmony_ci};
195ce968135Sopenharmony_ci} // namespace WallpaperMgrService
196ce968135Sopenharmony_ci} // namespace OHOS
197ce968135Sopenharmony_ci#endif // SERVICES_INCLUDE_WALLPAPER_SERVICES_H