• 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 <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     int32_t GetColors(int32_t wallpaperType, std::vector<uint64_t> &colors) override;
75     int32_t GetFile(int32_t wallpaperType, int32_t &wallpaperFd) override;
76     int32_t GetWallpaperId(int32_t wallpaperType) override;
77     int32_t GetWallpaperMinHeight(int32_t &minHeight) override;
78     int32_t GetWallpaperMinWidth(int32_t &minWidth) 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 
86     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
87 
88     int32_t SetWallpaperByFDV9(int32_t fd, int32_t wallpaperType, int32_t length) override;
89     int32_t SetWallpaperByMapV9(int32_t fd, int32_t wallpaperType, int32_t length) override;
90     int32_t GetPixelMapV9(int32_t wallpaperType, FdInfo &fdInfo) override;
91     int32_t GetColorsV9(int32_t wallpaperType, std::vector<uint64_t> &colors) override;
92     int32_t GetWallpaperMinHeightV9(int32_t &minHeight) override;
93     int32_t GetWallpaperMinWidthV9(int32_t &minWidth) override;
94     int32_t ResetWallpaperV9(int32_t wallpaperType) override;
95 public:
96     bool SetLockWallpaperCallback(IWallpaperManagerCallback* cb);
97     static void OnBootPhase();
98     void ReporterFault(MiscServices::FaultType faultType, MiscServices::FaultCode faultCode);
99     void ReporterUsageTimeStatisic();
100     void RegisterSubscriber(int times);
101     void StartWallpaperExtension();
102     void AddWallpaperExtensionDeathRecipient(const sptr<IRemoteObject> &remoteObject);
103 
104 protected:
105     void OnStart() override;
106     void OnStop() override;
107     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
108 
109 private:
110     /**
111      * Get current user id.
112      * @param none
113      * @return  userid
114      */
115     int GetUserId();
116 
117     /**
118      * Get current Display Id.
119      * @param none
120      * @return  displayid
121      */
122     int GetDisplayId();
123 
124      /**
125     *  initData ,such as dir,filename,and so on.
126     * @param
127     * @return none
128     */
129     void InitData();
130     int64_t WritePixelMapToFile(const std::string &filePath, std::unique_ptr< OHOS::Media::PixelMap> pixelMap);
131     bool CompareColor(const uint64_t &localColor, const ColorManager::Color &color);
132     bool SaveColor(int wallpaperType);
133     void LoadSettingsLocked(int userId, bool keepDimensionHints);
134     void MigrateFromOld();
135     std::string GetWallpaperDir();
136     bool GetWallpaperSafeLocked(int userId, int wpType, WallpaperData paperdata);
137     void ClearWallpaperLocked(int userId, int wpType);
138     int32_t SetDefaultDateForWallpaper(int userId, int wpType);
139     int MakeWallpaperIdLocked();
140     bool WPCheckCallingPermission(const std::string &promissionName);
141     bool WPGetBundleNameByUid(std::int32_t uid, std::string &bname);
142     bool MakeCropWallpaper(int wallpaperType);
143     int32_t SetWallpaperBackupData(std::string uriOrPixelMap, int wallpaperType);
144     int32_t ConnectExtensionAbility(const OHOS::AAFwk::Want& want);
145     int32_t GetFilePath(int wallpaperType, std::string &filePath);
146     void SetPixelMapCropParameters(std::unique_ptr<Media::PixelMap> wallpaperPixelMap,
147         Media::DecodeOptions &decodeOpts);
148     bool IsSystemApp();
149     OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr();
150     int32_t GetImageFd(int32_t wallpaperType, int32_t &fd);
151     int32_t GetImageSize(int32_t wallpaperType, int32_t &size);
152 
153 private:
154 
155     int32_t Init();
156     ServiceRunningState state_;
157     void InitServiceHandler();
158     bool CopySystemWallpaper();
159     bool CopyScreenLockWallpaper();
160 
161     static std::mutex instanceLock_;
162     static sptr<WallpaperService> instance_;
163     static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
164     std::string wallpaperLockScreenFilePath_;
165     std::string wallpaperSystemFilePath_;
166     std::string wallpaperLockScreenFileFullPath_;
167     std::string wallpaperSystemFileFullPath_;
168     std::string wallpaperLockScreenCropFileFullPath_;
169     std::string wallpaperSystemCropFileFullPath_;
170     std::string wallpaperTmpFullPath_;
171     std::string wallpaperCropPath;
172     typedef std::map<int, WallpaperColorChangeListener*> DISPLAYIDCOLORSLISTENERMAP;
173     typedef std::map<int, DISPLAYIDCOLORSLISTENERMAP> COLORSLISTENERMAP;
174     typedef std::list<WallpaperColorChangeListener*> LISTENERLIST;
175     LISTENERLIST colorListeners_;
176     COLORSLISTENERMAP colorsChangedListeners_;
177     ConcurrentMap<int, WallpaperData> wallpaperMap_;
178     ConcurrentMap<int, WallpaperData> lockWallpaperMap_;
179     atomic<int32_t> wallpaperId_;
180     int userId_;
181     static const std::string WALLPAPER;
182     static const std::string WALLPAPER_CROP;
183     static const std::string WALLPAPER_LOCK_ORIG;
184     static const std::string WALLPAPER_LOCK_CROP;
185     static const std::string WALLPAPER_BUNDLE_NAME;
186     sptr<IWallpaperCallback> callbackProxy = nullptr;
187     sptr<IRemoteObject::DeathRecipient> recipient_;
188 
189     std::string name_;
190     std::mutex mtx;
191     uint64_t lockWallpaperColor_;
192     uint64_t systemWallpaperColor_;
193     std::map<int, sptr<IWallpaperColorChangeListener>> colorChangeListenerMap_;
194     std::mutex listenerMapMutex_;
195 };
196 }
197 } // OHOS
198 #endif // SERVICES_INCLUDE_WALLPAPER_SERVICES_H