• 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 #include "wallpaper_service.h"
16 
17 #include <cerrno>
18 #include <cstdio>
19 #include <cstdlib>
20 #include <cstring>
21 #include <fcntl.h>
22 #include <iostream>
23 #include <sys/prctl.h>
24 #include <sys/sendfile.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <thread>
28 #include <unistd.h>
29 
30 #include "bundle_mgr_interface.h"
31 #include "bundle_mgr_proxy.h"
32 #include "color_picker.h"
33 #include "command.h"
34 #include "directory_ex.h"
35 #include "dump_helper.h"
36 #include "effect_errors.h"
37 #include "export/color.h"
38 #include "file_deal.h"
39 #include "file_ex.h"
40 #include "hilog_wrapper.h"
41 #include "hitrace_meter.h"
42 #include "image_packer.h"
43 #include "image_source.h"
44 #include "image_type.h"
45 #include "image_utils.h"
46 #include "iservice_registry.h"
47 #include "memory_guard.h"
48 #include "nlohmann/json.hpp"
49 #include "parameter.h"
50 #include "pixel_map.h"
51 #include "scene_board_judgement.h"
52 #include "system_ability_definition.h"
53 #include "tokenid_kit.h"
54 #include "uri.h"
55 #include "wallpaper_common.h"
56 #include "wallpaper_common_event_manager.h"
57 #include "wallpaper_manager_common_info.h"
58 #include "wallpaper_service_cb_proxy.h"
59 #include "want.h"
60 #include "window.h"
61 
62 #ifdef THEME_SERVICE
63 #include "theme_manager_client.h"
64 #else
65 #include "ability_manager_client.h"
66 #include "wallpaper_extension_ability_connection.h"
67 #include "wallpaper_extension_ability_death_recipient.h"
68 #endif
69 
70 namespace OHOS {
71 namespace WallpaperMgrService {
72 REGISTER_SYSTEM_ABILITY_BY_ID(WallpaperService, WALLPAPER_MANAGER_SERVICE_ID, true);
73 
74 using namespace OHOS::Media;
75 using namespace OHOS::MiscServices;
76 using namespace OHOS::Security::AccessToken;
77 using namespace OHOS::AccountSA;
78 
79 constexpr const char *WALLPAPER_SYSTEM_ORIG = "wallpaper_system_orig";
80 constexpr const char *WALLPAPER_LOCK_ORIG = "wallpaper_lock_orig";
81 constexpr const char *LIVE_WALLPAPER_SYSTEM_ORIG = "live_wallpaper_system_orig";
82 constexpr const char *LIVE_WALLPAPER_LOCK_ORIG = "live_wallpaper_lock_orig";
83 constexpr const char *CUSTOM_WALLPAPER_LOCK = "custom_lock.zip";
84 constexpr const char *CUSTOM_WALLPAPER_SYSTEM = "custom_system.zip";
85 constexpr const char *OHOS_WALLPAPER_BUNDLE_NAME = "com.ohos.launcher";
86 constexpr const char *SHOW_SYSTEM_SCREEN = "SHOW_SYSTEMSCREEN";
87 constexpr const char *SHOW_LOCK_SCREEN = "SHOW_LOCKSCREEN";
88 constexpr const char *SYSTEM_RES_TYPE = "SystemResType";
89 constexpr const char *LOCKSCREEN_RES_TYPE = "LockScreenResType";
90 constexpr const char *WALLPAPER_CHANGE = "wallpaperChange";
91 constexpr const char *COLOR_CHANGE = "colorChange";
92 constexpr const char *BUNDLE_NAME_KEY = "persist.wallpaper_mgr.bundleName";
93 constexpr const char *SCENEBOARD_BUNDLE_NAME = "com.ohos.sceneboard";
94 
95 constexpr const char *WALLPAPER_USERID_PATH = "/data/service/el1/public/wallpaper/";
96 constexpr const char *WALLPAPER_SYSTEM_DIRNAME = "system";
97 constexpr const char *WALLPAPER_TMP_DIRNAME = "fwsettmp";
98 constexpr const char *WALLPAPER_LOCKSCREEN_DIRNAME = "lockscreen";
99 constexpr const char *WALLPAPER_DEFAULT_FILEFULLPATH = "/system/etc/wallpaperdefault.jpeg";
100 constexpr const char *WALLPAPER_DEFAULT_LOCK_FILEFULLPATH = "/system/etc/wallpaperlockdefault.jpeg";
101 constexpr const char *WALLPAPER_PREFABRICATE_FILEFULLPATH = "/sys_prod/media/themes/wallpaperdefault.jpeg";
102 constexpr const char *WALLPAPER_PREFABRICATE_LOCK_FILEFULLPATH = "/sys_prod/media/themes/wallpaperlockdefault.jpeg";
103 constexpr const char *WALLPAPER_CROP_PICTURE = "crop_file";
104 
105 constexpr int64_t INIT_INTERVAL = 10000L;
106 constexpr int64_t DELAY_TIME = 1000L;
107 constexpr int64_t QUERY_USER_ID_INTERVAL = 300L;
108 constexpr int32_t FOO_MAX_LEN = 52428800;
109 constexpr int32_t MAX_RETRY_TIMES = 20;
110 constexpr int32_t QUERY_USER_MAX_RETRY_TIMES = 100;
111 constexpr int32_t DEFAULT_WALLPAPER_ID = -1;
112 constexpr int32_t DEFAULT_USER_ID = 0;
113 constexpr int32_t MAX_VIDEO_SIZE = 104857600;
114 const int CONFIG_LEN = 30;
115 
116 #ifdef THEME_SERVICE
117 constexpr int64_t INIT_THEME_INTERVAL = 300L;
118 constexpr int32_t USER_ID = 100;
119 #else
120 constexpr int32_t CONNECT_EXTENSION_INTERVAL = 100;
121 constexpr int32_t DEFAULT_VALUE = -1;
122 constexpr int32_t CONNECT_EXTENSION_MAX_RETRY_TIMES = 50;
123 #endif
124 
125 std::mutex WallpaperService::instanceLock_;
126 
127 sptr<WallpaperService> WallpaperService::instance_;
128 
129 std::shared_ptr<AppExecFwk::EventHandler> WallpaperService::serviceHandler_;
130 
WallpaperService(int32_t systemAbilityId,bool runOnCreate)131 WallpaperService::WallpaperService(int32_t systemAbilityId, bool runOnCreate)
132     : SystemAbility(systemAbilityId, runOnCreate), state_(ServiceRunningState::STATE_NOT_START)
133 {
134 }
135 
WallpaperService()136 WallpaperService::WallpaperService() : state_(ServiceRunningState::STATE_NOT_START)
137 {
138 }
139 
~WallpaperService()140 WallpaperService::~WallpaperService()
141 {
142 }
143 
Init()144 int32_t WallpaperService::Init()
145 {
146     InitQueryUserId(QUERY_USER_MAX_RETRY_TIMES);
147     bool ret = Publish(this);
148     if (!ret) {
149         HILOG_ERROR("Publish failed.");
150         ReporterFault(FaultType::SERVICE_FAULT, FaultCode::SF_SERVICE_UNAVAILABLE);
151         return -1;
152     }
153     HILOG_INFO("Publish success.");
154     state_ = ServiceRunningState::STATE_RUNNING;
155 #ifndef THEME_SERVICE
156     StartExtensionAbility(CONNECT_EXTENSION_MAX_RETRY_TIMES);
157 #endif
158     return E_OK;
159 }
160 
OnStart()161 void WallpaperService::OnStart()
162 {
163     HILOG_INFO("Enter OnStart.");
164     MemoryGuard cacheGuard;
165     if (state_ == ServiceRunningState::STATE_RUNNING) {
166         HILOG_ERROR("WallpaperService is already running.");
167         return;
168     }
169     InitData();
170     InitServiceHandler();
171     AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
172     auto cmd = std::make_shared<Command>(std::vector<std::string>({ "-all" }), "Show all",
173         [this](const std::vector<std::string> &input, std::string &output) -> bool {
174             output.append(
175                 "WallpaperExtensionAbility\t: ExtensionInfo{" + std::string(OHOS_WALLPAPER_BUNDLE_NAME) + "}\n");
176             return true;
177         });
178     DumpHelper::GetInstance().RegisterCommand(cmd);
179     if (Init() != E_OK) {
180         auto callback = [=]() { Init(); };
181         serviceHandler_->PostTask(callback, INIT_INTERVAL);
182         HILOG_ERROR("Init failed. Try again 10s later");
183     }
184 #ifdef THEME_SERVICE
185     InitThemeResource();
186 #endif
187     return;
188 }
189 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)190 void WallpaperService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
191 {
192     HILOG_INFO("OnAddSystemAbility systemAbilityId:%{public}d added!", systemAbilityId);
193     if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
194         int32_t times = 0;
195         RegisterSubscriber(times);
196     }
197 }
198 
RegisterSubscriber(int32_t times)199 void WallpaperService::RegisterSubscriber(int32_t times)
200 {
201     MemoryGuard cacheGuard;
202     times++;
203     subscriber_ = std::make_shared<WallpaperCommonEventSubscriber>(*this);
204     bool subRes = OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
205     if (!subRes && times <= MAX_RETRY_TIMES) {
206         HILOG_INFO("RegisterSubscriber failed");
207         auto callback = [this, times]() { RegisterSubscriber(times); };
208         serviceHandler_->PostTask(callback, DELAY_TIME);
209     }
210 }
211 
InitServiceHandler()212 void WallpaperService::InitServiceHandler()
213 {
214     HILOG_INFO("InitServiceHandler started.");
215     if (serviceHandler_ != nullptr) {
216         HILOG_ERROR("InitServiceHandler already init.");
217         return;
218     }
219     std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("WallpaperService");
220     serviceHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
221 
222     HILOG_INFO("InitServiceHandler succeeded.");
223 }
224 
OnStop()225 void WallpaperService::OnStop()
226 {
227     HILOG_INFO("OnStop started.");
228     if (state_ != ServiceRunningState::STATE_RUNNING) {
229         return;
230     }
231     serviceHandler_ = nullptr;
232 #ifndef THEME_SERVICE
233     connection_ = nullptr;
234 #endif
235     recipient_ = nullptr;
236     extensionRemoteObject_ = nullptr;
237     if (subscriber_ != nullptr) {
238         bool unSubscribeResult = OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
239         subscriber_ = nullptr;
240         HILOG_INFO("UnregisterSubscriber end, unSubscribeResult = %{public}d", unSubscribeResult);
241     }
242     state_ = ServiceRunningState::STATE_NOT_START;
243     HILOG_INFO("OnStop end.");
244 }
245 
InitData()246 void WallpaperService::InitData()
247 {
248     HILOG_INFO("WallpaperService::initData --> start ");
249     wallpaperId_ = DEFAULT_WALLPAPER_ID;
250     int32_t userId = DEFAULT_USER_ID;
251     systemWallpaperMap_.Clear();
252     lockWallpaperMap_.Clear();
253     wallpaperTmpFullPath_ = std::string(WALLPAPER_USERID_PATH) + std::string(WALLPAPER_TMP_DIRNAME);
254     wallpaperCropPath_ = std::string(WALLPAPER_USERID_PATH) + std::string(WALLPAPER_CROP_PICTURE);
255     systemWallpaperColor_ = 0;
256     lockWallpaperColor_ = 0;
257     wallpaperEventMap_.clear();
258     appBundleName_ = SCENEBOARD_BUNDLE_NAME;
259     InitBundleNameParameter();
260     InitUserDir(userId);
261     LoadSettingsLocked(userId);
262     InitResources(userId, WALLPAPER_SYSTEM);
263     InitResources(userId, WALLPAPER_LOCKSCREEN);
264     LoadWallpaperState();
265     HILOG_INFO("WallpaperService::initData --> end ");
266 }
267 
InitBundleNameParameter()268 void WallpaperService::InitBundleNameParameter()
269 {
270     char value[CONFIG_LEN] = "";
271     if (GetParameter(BUNDLE_NAME_KEY, "", value, CONFIG_LEN) < 0 || strlen(value) == 0) {
272         HILOG_ERROR("No found bundle name from system parameter.");
273         return;
274     }
275     appBundleName_ = value;
276     HILOG_INFO("get appBundleName_ :%{public}s", appBundleName_.c_str());
277 }
278 
279 #ifndef THEME_SERVICE
AddWallpaperExtensionDeathRecipient(const sptr<IRemoteObject> & remoteObject)280 void WallpaperService::AddWallpaperExtensionDeathRecipient(const sptr<IRemoteObject> &remoteObject)
281 {
282     if (remoteObject != nullptr) {
283         std::lock_guard<std::mutex> lock(remoteObjectMutex_);
284         IPCObjectProxy *proxy = reinterpret_cast<IPCObjectProxy *>(remoteObject.GetRefPtr());
285         if (recipient_ == nullptr) {
286             recipient_ = sptr<IRemoteObject::DeathRecipient>(new WallpaperExtensionAbilityDeathRecipient(*this));
287         }
288         if (proxy != nullptr && !proxy->IsObjectDead()) {
289             HILOG_INFO("get remoteObject succeed");
290             proxy->AddDeathRecipient(recipient_);
291             extensionRemoteObject_ = remoteObject;
292         }
293     }
294 }
295 #endif
296 
RemoveExtensionDeathRecipient()297 void WallpaperService::RemoveExtensionDeathRecipient()
298 {
299     if (extensionRemoteObject_ != nullptr && recipient_ != nullptr) {
300         HILOG_INFO("Remove Extension DeathRecipient");
301         std::lock_guard<std::mutex> lock(remoteObjectMutex_);
302         extensionRemoteObject_->RemoveDeathRecipient(recipient_);
303         recipient_ = nullptr;
304         extensionRemoteObject_ = nullptr;
305     }
306 }
307 
InitQueryUserId(int32_t times)308 void WallpaperService::InitQueryUserId(int32_t times)
309 {
310     times--;
311     bool ret = InitUsersOnBoot();
312     if (!ret && times > 0) {
313         HILOG_INFO("InitQueryUserId failed");
314         auto callback = [this, times]() { InitQueryUserId(times); };
315         serviceHandler_->PostTask(callback, QUERY_USER_ID_INTERVAL);
316     }
317 }
318 
319 #ifndef THEME_SERVICE
StartExtensionAbility(int32_t times)320 void WallpaperService::StartExtensionAbility(int32_t times)
321 {
322     times--;
323     bool ret = ConnectExtensionAbility();
324     if (!ret && times > 0 && serviceHandler_ != nullptr) {
325         HILOG_ERROR("StartExtensionAbilty failed, remainder of the times: %{public}d", times);
326         auto callback = [this, times]() { StartExtensionAbility(times); };
327         serviceHandler_->PostTask(callback, CONNECT_EXTENSION_INTERVAL);
328     }
329 }
330 #endif
331 
InitUsersOnBoot()332 bool WallpaperService::InitUsersOnBoot()
333 {
334     std::vector<int32_t> userIds;
335     if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(userIds) != ERR_OK || userIds.empty()) {
336         HILOG_INFO("WallpaperService: failed to get current userIds");
337         return false;
338     }
339     HILOG_INFO("WallpaperService::get current userIds success, Current userId: %{public}d", userIds[0]);
340     for (auto userId : userIds) {
341         InitUserDir(userId);
342         LoadSettingsLocked(userId);
343         InitResources(userId, WALLPAPER_SYSTEM);
344         InitResources(userId, WALLPAPER_LOCKSCREEN);
345     }
346     return true;
347 }
348 
OnInitUser(int32_t userId)349 void WallpaperService::OnInitUser(int32_t userId)
350 {
351     if (userId < 0) {
352         HILOG_ERROR("userId error, userId = %{public}d", userId);
353         return;
354     }
355     std::string userDir = WALLPAPER_USERID_PATH + std::to_string(userId);
356     if (FileDeal::IsFileExist(userDir)) {
357         std::lock_guard<std::mutex> lock(mtx_);
358         if (!OHOS::ForceRemoveDirectory(userDir)) {
359             HILOG_ERROR("Force remove user directory path failed, errno %{public}d, userId:%{public}d", errno, userId);
360             return;
361         }
362     }
363     if (!InitUserDir(userId)) {
364         return;
365     }
366     LoadSettingsLocked(userId);
367     InitResources(userId, WALLPAPER_SYSTEM);
368     InitResources(userId, WALLPAPER_LOCKSCREEN);
369     HILOG_INFO("OnInitUser success, userId = %{public}d", userId);
370 }
371 
InitResources(int32_t userId,WallpaperType wallpaperType)372 void WallpaperService::InitResources(int32_t userId, WallpaperType wallpaperType)
373 {
374     std::string pathName;
375     if (!GetFileNameFromMap(userId, wallpaperType, pathName)) {
376         HILOG_ERROR("Get user file name from map failed, userId = %{public}d", userId);
377         return;
378     }
379     if (!FileDeal::IsFileExist(pathName)) {
380         WallpaperData wallpaperData;
381         if (!GetWallpaperSafeLocked(userId, wallpaperType, wallpaperData)) {
382             HILOG_ERROR("Get wallpaper data failed, userId = %{public}d", userId);
383             return;
384         }
385         if (!RestoreUserResources(wallpaperData, wallpaperType)) {
386             HILOG_ERROR("RestoreUserResources error");
387             return;
388         }
389     }
390 }
391 
InitUserDir(int32_t userId)392 bool WallpaperService::InitUserDir(int32_t userId)
393 {
394     std::string userDir = WALLPAPER_USERID_PATH + std::to_string(userId);
395     if (!FileDeal::Mkdir(userDir)) {
396         HILOG_ERROR("Failed to create destination path, userId:%{public}d ", userId);
397         return false;
398     }
399     std::string wallpaperSystemFilePath = userDir + "/" + WALLPAPER_SYSTEM_DIRNAME;
400     if (!FileDeal::Mkdir(wallpaperSystemFilePath)) {
401         HILOG_ERROR("Failed to create destination wallpaper system path, userId:%{public}d, type:%{public}s", userId,
402             WALLPAPER_SYSTEM_DIRNAME);
403         return false;
404     }
405     std::string wallpaperLockScreenFilePath = userDir + "/" + WALLPAPER_LOCKSCREEN_DIRNAME;
406     if (!FileDeal::Mkdir(wallpaperLockScreenFilePath)) {
407         HILOG_ERROR("Failed to create destination wallpaper lockscreen path, userId:%{public}d, type:%{public}s",
408             userId, WALLPAPER_LOCKSCREEN_DIRNAME);
409         return false;
410     }
411     return true;
412 }
413 
RestoreUserResources(const WallpaperData & wallpaperData,WallpaperType wallpaperType)414 bool WallpaperService::RestoreUserResources(const WallpaperData &wallpaperData, WallpaperType wallpaperType)
415 {
416     std::string wallpaperDefaultPath = wallpaperType == WallpaperType::WALLPAPER_SYSTEM
417                                            ? WALLPAPER_DEFAULT_FILEFULLPATH
418                                            : WALLPAPER_DEFAULT_LOCK_FILEFULLPATH;
419     if (wallpaperType == WallpaperType::WALLPAPER_SYSTEM) {
420         if (FileDeal::IsFileExist(WALLPAPER_PREFABRICATE_FILEFULLPATH)) {
421             wallpaperDefaultPath = WALLPAPER_PREFABRICATE_FILEFULLPATH;
422         }
423     } else {
424         if (FileDeal::IsFileExist(WALLPAPER_PREFABRICATE_LOCK_FILEFULLPATH)) {
425             wallpaperDefaultPath = WALLPAPER_PREFABRICATE_LOCK_FILEFULLPATH;
426         }
427     }
428     if (!FileDeal::IsFileExist(wallpaperDefaultPath) ||
429         !FileDeal::IsFileExist(GetWallpaperDir(wallpaperData.userId, wallpaperType))) {
430         HILOG_INFO("Copy file path is not exist");
431         return false;
432     }
433     std::lock_guard<std::mutex> lock(mtx_);
434     if (!FileDeal::CopyFile(wallpaperDefaultPath, wallpaperData.wallpaperFile)) {
435         HILOG_INFO("CopyFile WALLPAPER_DEFAULT_FILEFULLPATH failed");
436         return false;
437     }
438     HILOG_INFO("Restore user resources end ");
439     return true;
440 }
441 
OnRemovedUser(int32_t userId)442 void WallpaperService::OnRemovedUser(int32_t userId)
443 {
444     if (userId < 0) {
445         HILOG_ERROR("userId error, userId = %{public}d", userId);
446         return;
447     }
448     ClearWallpaperLocked(userId, WALLPAPER_SYSTEM);
449     ClearWallpaperLocked(userId, WALLPAPER_LOCKSCREEN);
450     std::string userDir = WALLPAPER_USERID_PATH + std::to_string(userId);
451     std::lock_guard<std::mutex> lock(mtx_);
452     if (!OHOS::ForceRemoveDirectory(userDir)) {
453         HILOG_ERROR("Force remove user directory path failed, errno %{public}d.", errno);
454     }
455     HILOG_INFO("OnRemovedUser end, userId = %{public}d", userId);
456 }
457 
OnSwitchedUser(int32_t userId)458 void WallpaperService::OnSwitchedUser(int32_t userId)
459 {
460     if (userId < 0) {
461         HILOG_ERROR("userId error, userId = %{public}d", userId);
462         return;
463     }
464     RemoveExtensionDeathRecipient();
465 #ifndef THEME_SERVICE
466     ConnectExtensionAbility();
467 #endif
468     std::string userDir = WALLPAPER_USERID_PATH + std::to_string(userId);
469     LoadSettingsLocked(userId);
470     if (!FileDeal::IsFileExist(userDir)) {
471         HILOG_INFO("User file is not exist, userId = %{public}d", userId);
472         InitUserDir(userId);
473         InitResources(userId, WALLPAPER_SYSTEM);
474         InitResources(userId, WALLPAPER_LOCKSCREEN);
475     }
476     LoadWallpaperState();
477     SendWallpaperChangeEvent(userId, WALLPAPER_SYSTEM);
478     SendWallpaperChangeEvent(userId, WALLPAPER_LOCKSCREEN);
479     SaveColor(userId, WALLPAPER_SYSTEM);
480     SaveColor(userId, WALLPAPER_LOCKSCREEN);
481     HILOG_INFO("OnSwitchedUser end, newUserId = %{public}d", userId);
482 }
483 
OnBootPhase()484 void WallpaperService::OnBootPhase()
485 {
486     HILOG_INFO("WallpaperService OnBootPhase");
487 }
488 
GetWallpaperDir(int32_t userId,WallpaperType wallpaperType)489 std::string WallpaperService::GetWallpaperDir(int32_t userId, WallpaperType wallpaperType)
490 {
491     std::string userIdPath = WALLPAPER_USERID_PATH + std::to_string(userId);
492     std::string wallpaperFilePath;
493     if (wallpaperType == WALLPAPER_SYSTEM) {
494         wallpaperFilePath = userIdPath + "/" + WALLPAPER_SYSTEM_DIRNAME;
495     } else if (wallpaperType == WALLPAPER_LOCKSCREEN) {
496         wallpaperFilePath = userIdPath + "/" + WALLPAPER_LOCKSCREEN_DIRNAME;
497     }
498     return wallpaperFilePath;
499 }
500 
GetFileNameFromMap(int32_t userId,WallpaperType wallpaperType,std::string & filePathName)501 bool WallpaperService::GetFileNameFromMap(int32_t userId, WallpaperType wallpaperType, std::string &filePathName)
502 {
503     auto iterator = wallpaperType == WALLPAPER_SYSTEM ? systemWallpaperMap_.Find(userId)
504                                                       : lockWallpaperMap_.Find(userId);
505     if (!iterator.first) {
506         HILOG_ERROR("system wallpaper already cleared");
507         return false;
508     }
509     HILOG_INFO("GetFileNameFromMap resourceType : %{public}d", static_cast<int32_t>(iterator.second.resourceType));
510     switch (iterator.second.resourceType) {
511         case PICTURE:
512             filePathName = iterator.second.wallpaperFile;
513             break;
514         case VIDEO:
515             filePathName = iterator.second.liveWallpaperFile;
516             break;
517         case DEFAULT:
518             filePathName = iterator.second.wallpaperFile;
519             break;
520         case PACKAGE:
521             filePathName = iterator.second.customPackageUri;
522             break;
523         default:
524             filePathName = "";
525             break;
526     }
527     return filePathName != "";
528 }
529 
GetPictureFileName(int32_t userId,WallpaperType wallpaperType,std::string & filePathName)530 bool WallpaperService::GetPictureFileName(int32_t userId, WallpaperType wallpaperType, std::string &filePathName)
531 {
532     auto iterator = wallpaperType == WALLPAPER_SYSTEM ? systemWallpaperMap_.Find(userId)
533                                                       : lockWallpaperMap_.Find(userId);
534     if (!iterator.first) {
535         HILOG_INFO("WallpaperType:%{public}d ,WallpaperMap not found userId: %{public}d", wallpaperType, userId);
536         OnInitUser(userId);
537         iterator = wallpaperType == WALLPAPER_SYSTEM ? systemWallpaperMap_.Find(userId)
538                                                      : lockWallpaperMap_.Find(userId);
539     }
540     filePathName = iterator.second.wallpaperFile;
541     HILOG_INFO("GetPictureFileName filePathName : %{public}s", filePathName.c_str());
542     return filePathName != "";
543 }
544 
MakeWallpaperIdLocked()545 int32_t WallpaperService::MakeWallpaperIdLocked()
546 {
547     HILOG_INFO("MakeWallpaperIdLocked start");
548     if (wallpaperId_ == INT32_MAX) {
549         wallpaperId_ = DEFAULT_WALLPAPER_ID;
550     }
551     return ++wallpaperId_;
552 }
553 
LoadSettingsLocked(int32_t userId)554 void WallpaperService::LoadSettingsLocked(int32_t userId)
555 {
556     HILOG_INFO("load Setting locked start.");
557     if (!systemWallpaperMap_.Contains(userId)) {
558         HILOG_INFO("systemWallpaperMap_ does not contains userId");
559         std::string wallpaperSystemFilePath = GetWallpaperDir(userId, WALLPAPER_SYSTEM);
560         WallpaperData wallpaperSystem(userId, wallpaperSystemFilePath + "/" + WALLPAPER_SYSTEM_ORIG);
561         wallpaperSystem.liveWallpaperFile = wallpaperSystemFilePath + "/" + LIVE_WALLPAPER_SYSTEM_ORIG;
562         wallpaperSystem.customPackageUri = wallpaperSystemFilePath + "/" + CUSTOM_WALLPAPER_SYSTEM;
563         wallpaperSystem.allowBackup = true;
564         wallpaperSystem.resourceType = PICTURE;
565         wallpaperSystem.wallpaperId = DEFAULT_WALLPAPER_ID;
566         systemWallpaperMap_.InsertOrAssign(userId, wallpaperSystem);
567     }
568     if (!lockWallpaperMap_.Contains(userId)) {
569         HILOG_INFO("lockWallpaperMap_ does not Contains userId");
570         std::string wallpaperLockScreenFilePath = GetWallpaperDir(userId, WALLPAPER_LOCKSCREEN);
571         WallpaperData wallpaperLock(userId, wallpaperLockScreenFilePath + "/" + WALLPAPER_LOCK_ORIG);
572         wallpaperLock.liveWallpaperFile = wallpaperLockScreenFilePath + "/" + LIVE_WALLPAPER_LOCK_ORIG;
573         wallpaperLock.customPackageUri = wallpaperLockScreenFilePath + "/" + CUSTOM_WALLPAPER_LOCK;
574         wallpaperLock.allowBackup = true;
575         wallpaperLock.resourceType = PICTURE;
576         wallpaperLock.wallpaperId = DEFAULT_WALLPAPER_ID;
577         lockWallpaperMap_.InsertOrAssign(userId, wallpaperLock);
578     }
579     HILOG_INFO("load Setting locked end.");
580 }
581 
GetColors(int32_t wallpaperType,std::vector<uint64_t> & colors)582 ErrorCode WallpaperService::GetColors(int32_t wallpaperType, std::vector<uint64_t> &colors)
583 {
584     if (wallpaperType == WALLPAPER_SYSTEM) {
585         colors.emplace_back(systemWallpaperColor_);
586     } else if (wallpaperType == WALLPAPER_LOCKSCREEN) {
587         colors.emplace_back(lockWallpaperColor_);
588     }
589     HILOG_INFO(" Service End!");
590     return E_OK;
591 }
592 
GetColorsV9(int32_t wallpaperType,std::vector<uint64_t> & colors)593 ErrorCode WallpaperService::GetColorsV9(int32_t wallpaperType, std::vector<uint64_t> &colors)
594 {
595     if (!IsSystemApp()) {
596         HILOG_INFO("CallingApp is not SystemApp");
597         return E_NOT_SYSTEM_APP;
598     }
599     return GetColors(wallpaperType, colors);
600 }
601 
GetFile(int32_t wallpaperType,int32_t & wallpaperFd)602 ErrorCode WallpaperService::GetFile(int32_t wallpaperType, int32_t &wallpaperFd)
603 {
604     if (!CheckCallingPermission(WALLPAPER_PERMISSION_NAME_GET_WALLPAPER)) {
605         HILOG_ERROR("GetPixelMap no get permission!");
606         return E_NO_PERMISSION;
607     }
608     if (wallpaperType != static_cast<int32_t>(WALLPAPER_LOCKSCREEN) &&
609         wallpaperType != static_cast<int32_t>(WALLPAPER_SYSTEM)) {
610         return E_PARAMETERS_INVALID;
611     }
612     auto type = static_cast<WallpaperType>(wallpaperType);
613     int32_t userId = QueryActiveUserId();
614     HILOG_INFO("QueryCurrentOsAccount userId: %{public}d", userId);
615     ErrorCode ret = GetImageFd(userId, type, wallpaperFd);
616     HILOG_INFO("GetImageFd fd:%{public}d, ret:%{public}d", wallpaperFd, ret);
617     return ret;
618 }
619 
CompareColor(const uint64_t & localColor,const ColorManager::Color & color)620 bool WallpaperService::CompareColor(const uint64_t &localColor, const ColorManager::Color &color)
621 {
622     return localColor == color.PackValue();
623 }
624 
SaveColor(int32_t userId,WallpaperType wallpaperType)625 bool WallpaperService::SaveColor(int32_t userId, WallpaperType wallpaperType)
626 {
627     uint32_t errorCode = 0;
628     OHOS::Media::SourceOptions opts;
629     opts.formatHint = "image/jpeg";
630     std::string pathName;
631     if (!GetPictureFileName(userId, wallpaperType, pathName)) {
632         return false;
633     }
634     std::unique_ptr<OHOS::Media::ImageSource> imageSource =
635         OHOS::Media::ImageSource::CreateImageSource(pathName, opts, errorCode);
636     if (errorCode != 0) {
637         HILOG_ERROR("CreateImageSource failed");
638         return false;
639     }
640     OHOS::Media::DecodeOptions decodeOpts;
641     std::unique_ptr<PixelMap> wallpaperPixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
642     if (errorCode != 0) {
643         HILOG_ERROR("CreatePixelMap failed");
644         return false;
645     }
646 
647     pictureHeight_ = wallpaperPixelMap->GetHeight();
648     pictureWidth_ = wallpaperPixelMap->GetWidth();
649 
650     auto colorPicker = Rosen::ColorPicker::CreateColorPicker(std::move(wallpaperPixelMap), errorCode);
651     if (errorCode != 0) {
652         HILOG_ERROR("CreateColorPicker failed");
653         return false;
654     }
655     auto color = ColorManager::Color();
656     uint32_t ret = colorPicker->GetMainColor(color);
657     if (ret != Rosen::SUCCESS) {
658         HILOG_ERROR("GetMainColor failed ret is : %{public}d", ret);
659         return false;
660     }
661     OnColorsChange(wallpaperType, color);
662     return true;
663 }
664 
SetWallpaper(int32_t fd,int32_t wallpaperType,int32_t length)665 ErrorCode WallpaperService::SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length)
666 {
667     StartAsyncTrace(HITRACE_TAG_MISC, "SetWallpaper", static_cast<int32_t>(TraceTaskId::SET_WALLPAPER));
668     ErrorCode wallpaperErrorCode = SetWallpaper(fd, wallpaperType, length, PICTURE);
669     FinishAsyncTrace(HITRACE_TAG_MISC, "SetWallpaper", static_cast<int32_t>(TraceTaskId::SET_WALLPAPER));
670     return wallpaperErrorCode;
671 }
672 
SetWallpaperV9(int32_t fd,int32_t wallpaperType,int32_t length)673 ErrorCode WallpaperService::SetWallpaperV9(int32_t fd, int32_t wallpaperType, int32_t length)
674 {
675     if (!IsSystemApp()) {
676         HILOG_INFO("CallingApp is not SystemApp");
677         return E_NOT_SYSTEM_APP;
678     }
679     return SetWallpaper(fd, wallpaperType, length);
680 }
681 
SetWallpaperBackupData(int32_t userId,WallpaperResourceType resourceType,const std::string & uriOrPixelMap,WallpaperType wallpaperType)682 ErrorCode WallpaperService::SetWallpaperBackupData(int32_t userId, WallpaperResourceType resourceType,
683     const std::string &uriOrPixelMap, WallpaperType wallpaperType)
684 {
685     HILOG_INFO("set wallpaper and backup data Start.");
686     if (!OHOS::FileExists(uriOrPixelMap)) {
687         return E_DEAL_FAILED;
688     }
689     WallpaperData wallpaperData;
690     bool ret = GetWallpaperSafeLocked(userId, wallpaperType, wallpaperData);
691     if (!ret) {
692         HILOG_ERROR("GetWallpaperSafeLocked failed !");
693         return E_DEAL_FAILED;
694     }
695     wallpaperData.resourceType = resourceType;
696     wallpaperData.wallpaperId = MakeWallpaperIdLocked();
697     std::string wallpaperFile;
698     WallpaperService::GetWallpaperFile(resourceType, wallpaperData, wallpaperFile);
699     if (!FileDeal::CopyFile(uriOrPixelMap, wallpaperFile)) {
700         HILOG_ERROR("CopyFile failed !");
701         return E_DEAL_FAILED;
702     }
703     if (!FileDeal::DeleteFile(uriOrPixelMap)) {
704         return E_DEAL_FAILED;
705     }
706     if (!SaveWallpaperState(userId, wallpaperType, resourceType)) {
707         HILOG_ERROR("Save wallpaper state failed!");
708         return E_DEAL_FAILED;
709     }
710     if (wallpaperType == WALLPAPER_SYSTEM) {
711         systemWallpaperMap_.InsertOrAssign(userId, wallpaperData);
712     } else if (wallpaperType == WALLPAPER_LOCKSCREEN) {
713         lockWallpaperMap_.InsertOrAssign(userId, wallpaperData);
714     }
715     if (!SendWallpaperChangeEvent(userId, wallpaperType)) {
716         HILOG_ERROR("Send wallpaper state failed!");
717         return E_DEAL_FAILED;
718     }
719     return E_OK;
720 }
721 
GetWallpaperFile(WallpaperResourceType resourceType,const WallpaperData & wallpaperData,std::string & wallpaperFile)722 void WallpaperService::GetWallpaperFile(WallpaperResourceType resourceType, const WallpaperData &wallpaperData,
723     std::string &wallpaperFile)
724 {
725     switch (resourceType) {
726         case PICTURE:
727             wallpaperFile = wallpaperData.wallpaperFile;
728             break;
729         case DEFAULT:
730             wallpaperFile = wallpaperData.wallpaperFile;
731             break;
732         case VIDEO:
733             wallpaperFile = wallpaperData.liveWallpaperFile;
734             break;
735         case PACKAGE:
736             wallpaperFile = wallpaperData.customPackageUri;
737             break;
738         default:
739             HILOG_DEBUG("Non-existent error type!");
740             break;
741     }
742 }
743 
GetResType(int32_t userId,WallpaperType wallpaperType)744 WallpaperResourceType WallpaperService::GetResType(int32_t userId, WallpaperType wallpaperType)
745 {
746     if (wallpaperType == WALLPAPER_LOCKSCREEN) {
747         auto iterator = lockWallpaperMap_.Find(userId);
748         if (iterator.first) {
749             return iterator.second.resourceType;
750         }
751     } else if (wallpaperType == WALLPAPER_SYSTEM) {
752         auto iterator = systemWallpaperMap_.Find(userId);
753         if (iterator.first) {
754             return iterator.second.resourceType;
755         }
756     }
757     return WallpaperResourceType::DEFAULT;
758 }
759 
SendEvent(const std::string & eventType)760 ErrorCode WallpaperService::SendEvent(const std::string &eventType)
761 {
762     HILOG_DEBUG("Send event start.");
763     if (!CheckCallingPermission(WALLPAPER_PERMISSION_NAME_SET_WALLPAPER)) {
764         HILOG_ERROR("Send event not set permission!");
765         return E_NO_PERMISSION;
766     }
767 
768     int32_t userId = QueryActiveUserId();
769     WallpaperType wallpaperType;
770     WallpaperData data;
771     if (eventType == SHOW_SYSTEM_SCREEN) {
772         wallpaperType = WALLPAPER_SYSTEM;
773     } else if (eventType == SHOW_LOCK_SCREEN) {
774         wallpaperType = WALLPAPER_LOCKSCREEN;
775     } else {
776         HILOG_ERROR("Event type error!");
777         return E_PARAMETERS_INVALID;
778     }
779 
780     if (!GetWallpaperSafeLocked(userId, wallpaperType, data)) {
781         HILOG_ERROR("Get wallpaper safe locked failed!");
782         return E_PARAMETERS_INVALID;
783     }
784     std::string uri;
785     GetFileNameFromMap(userId, WALLPAPER_SYSTEM, uri);
786     WallpaperChanged(wallpaperType, data.resourceType, uri);
787     return E_OK;
788 }
789 
SendWallpaperChangeEvent(int32_t userId,WallpaperType wallpaperType)790 bool WallpaperService::SendWallpaperChangeEvent(int32_t userId, WallpaperType wallpaperType)
791 {
792     WallpaperData wallpaperData;
793     if (!GetWallpaperSafeLocked(userId, wallpaperType, wallpaperData)) {
794         HILOG_ERROR("GetWallpaperSafeLocked failed !");
795         return false;
796     }
797     shared_ptr<WallpaperCommonEventManager> wallpaperCommonEventManager = make_shared<WallpaperCommonEventManager>();
798     if (wallpaperType == WALLPAPER_SYSTEM) {
799         HILOG_INFO("Send wallpaper system setting message");
800         wallpaperCommonEventManager->SendWallpaperSystemSettingMessage(wallpaperData.resourceType);
801     } else if (wallpaperType == WALLPAPER_LOCKSCREEN) {
802         HILOG_INFO("Send wallpaper lock setting message");
803         wallpaperCommonEventManager->SendWallpaperLockSettingMessage(wallpaperData.resourceType);
804     }
805     HILOG_INFO("SetWallpaperBackupData callbackProxy_->OnCall start");
806     if (callbackProxy_ != nullptr && (wallpaperData.resourceType == PICTURE || wallpaperData.resourceType == DEFAULT)) {
807         callbackProxy_->OnCall(wallpaperType);
808     }
809     std::string uri;
810     WallpaperChanged(wallpaperType, wallpaperData.resourceType, uri);
811     return true;
812 }
813 
SetVideo(int32_t fd,int32_t wallpaperType,int32_t length)814 ErrorCode WallpaperService::SetVideo(int32_t fd, int32_t wallpaperType, int32_t length)
815 {
816     if (!IsSystemApp()) {
817         HILOG_ERROR("current app is not SystemApp");
818         return E_NOT_SYSTEM_APP;
819     }
820     StartAsyncTrace(HITRACE_TAG_MISC, "SetVideo", static_cast<int32_t>(TraceTaskId::SET_VIDEO));
821     ErrorCode wallpaperErrorCode = SetWallpaper(fd, wallpaperType, length, VIDEO);
822     FinishAsyncTrace(HITRACE_TAG_MISC, "SetVideo", static_cast<int32_t>(TraceTaskId::SET_VIDEO));
823     return wallpaperErrorCode;
824 }
825 
SetCustomWallpaper(int32_t fd,int32_t type,int32_t length)826 ErrorCode WallpaperService::SetCustomWallpaper(int32_t fd, int32_t type, int32_t length)
827 {
828     if (!IsSystemApp()) {
829         HILOG_ERROR("current app is not SystemApp");
830         return E_NOT_SYSTEM_APP;
831     }
832     if (!CheckCallingPermission(WALLPAPER_PERMISSION_NAME_SET_WALLPAPER)) {
833         HILOG_INFO("SetWallpaper no set permission!");
834         return E_NO_PERMISSION;
835     }
836     if (type != static_cast<int32_t>(WALLPAPER_LOCKSCREEN) && type != static_cast<int32_t>(WALLPAPER_SYSTEM)) {
837         return E_PARAMETERS_INVALID;
838     }
839     StartAsyncTrace(HITRACE_TAG_MISC, "SetCustomWallpaper", static_cast<int32_t>(TraceTaskId::SET_CUSTOM_WALLPAPER));
840     int32_t userId = QueryActiveUserId();
841     WallpaperType wallpaperType = static_cast<WallpaperType>(type);
842     WallpaperData wallpaperData;
843     if (!GetWallpaperSafeLocked(userId, wallpaperType, wallpaperData)) {
844         HILOG_ERROR("GetWallpaper data failed!");
845         return E_DEAL_FAILED;
846     }
847     if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
848         HILOG_ERROR("SceneBoard is not Enabled");
849         return E_NO_PERMISSION;
850     }
851     if (!SaveWallpaperState(userId, wallpaperType, PACKAGE)) {
852         HILOG_ERROR("Save wallpaper state failed!");
853         return E_DEAL_FAILED;
854     }
855     ErrorCode wallpaperErrorCode = SetWallpaper(fd, wallpaperType, length, PACKAGE);
856     wallpaperData.resourceType = PACKAGE;
857     wallpaperData.wallpaperId = MakeWallpaperIdLocked();
858     if (wallpaperType == WALLPAPER_SYSTEM) {
859         systemWallpaperMap_.InsertOrAssign(userId, wallpaperData);
860     } else if (wallpaperType == WALLPAPER_LOCKSCREEN) {
861         lockWallpaperMap_.InsertOrAssign(userId, wallpaperData);
862     }
863     if (!SendWallpaperChangeEvent(userId, wallpaperType)) {
864         HILOG_ERROR("Send wallpaper state failed!");
865         return E_DEAL_FAILED;
866     }
867     FinishAsyncTrace(HITRACE_TAG_MISC, "SetCustomWallpaper", static_cast<int32_t>(TraceTaskId::SET_CUSTOM_WALLPAPER));
868     return wallpaperErrorCode;
869 }
870 
GetPixelMap(int32_t wallpaperType,IWallpaperService::FdInfo & fdInfo)871 ErrorCode WallpaperService::GetPixelMap(int32_t wallpaperType, IWallpaperService::FdInfo &fdInfo)
872 {
873     HILOG_INFO("WallpaperService::getPixelMap start ");
874     if (!IsSystemApp()) {
875         HILOG_ERROR("CallingApp is not SystemApp");
876         return E_NOT_SYSTEM_APP;
877     }
878     if (!CheckCallingPermission(WALLPAPER_PERMISSION_NAME_GET_WALLPAPER)) {
879         HILOG_ERROR("GetPixelMap no get permission!");
880         return E_NO_PERMISSION;
881     }
882     if (wallpaperType != static_cast<int32_t>(WALLPAPER_LOCKSCREEN) &&
883         wallpaperType != static_cast<int32_t>(WALLPAPER_SYSTEM)) {
884         return E_PARAMETERS_INVALID;
885     }
886     auto type = static_cast<WallpaperType>(wallpaperType);
887     int32_t userId = QueryActiveUserId();
888     HILOG_INFO("QueryCurrentOsAccount userId: %{public}d", userId);
889     // current user's wallpaper is live video, not image
890     WallpaperResourceType resType = GetResType(userId, type);
891     if (resType != PICTURE && resType != DEFAULT) {
892         HILOG_ERROR("Current user's wallpaper is live video, not image");
893         fdInfo.size = 0; // 0: empty file size
894         fdInfo.fd = -1;  // -1: invalid file description
895         return E_OK;
896     }
897     ErrorCode ret = GetImageSize(userId, type, fdInfo.size);
898     if (ret != E_OK) {
899         HILOG_ERROR("GetImageSize failed");
900         return ret;
901     }
902     ret = GetImageFd(userId, type, fdInfo.fd);
903     if (ret != E_OK) {
904         HILOG_ERROR("GetImageFd failed");
905         return ret;
906     }
907     return E_OK;
908 }
909 
GetPixelMapV9(int32_t wallpaperType,IWallpaperService::FdInfo & fdInfo)910 ErrorCode WallpaperService::GetPixelMapV9(int32_t wallpaperType, IWallpaperService::FdInfo &fdInfo)
911 {
912     return GetPixelMap(wallpaperType, fdInfo);
913 }
914 
GetWallpaperId(int32_t wallpaperType)915 int32_t WallpaperService::GetWallpaperId(int32_t wallpaperType)
916 {
917     HILOG_INFO("WallpaperService::GetWallpaperId --> start ");
918     int32_t iWallpaperId = -1;
919     int32_t userId = QueryActiveUserId();
920     HILOG_INFO("QueryCurrentOsAccount userId: %{public}d", userId);
921     if (wallpaperType == WALLPAPER_LOCKSCREEN) {
922         auto iterator = lockWallpaperMap_.Find(userId);
923         if (iterator.first) {
924             iWallpaperId = iterator.second.wallpaperId;
925         }
926     } else if (wallpaperType == WALLPAPER_SYSTEM) {
927         auto iterator = systemWallpaperMap_.Find(userId);
928         if (iterator.first) {
929             iWallpaperId = iterator.second.wallpaperId;
930         }
931     }
932     HILOG_INFO("WallpaperService::GetWallpaperId --> end ID[%{public}d]", iWallpaperId);
933     return iWallpaperId;
934 }
935 
IsChangePermitted()936 bool WallpaperService::IsChangePermitted()
937 {
938     HILOG_INFO("IsChangePermitted wallpaper Start!");
939     bool bFlag = CheckCallingPermission(WALLPAPER_PERMISSION_NAME_SET_WALLPAPER);
940     return bFlag;
941 }
942 
IsOperationAllowed()943 bool WallpaperService::IsOperationAllowed()
944 {
945     HILOG_INFO("IsOperationAllowed wallpaper Start!");
946     bool bFlag = CheckCallingPermission(WALLPAPER_PERMISSION_NAME_SET_WALLPAPER);
947     return bFlag;
948 }
949 
ResetWallpaper(int32_t wallpaperType)950 ErrorCode WallpaperService::ResetWallpaper(int32_t wallpaperType)
951 {
952     HILOG_INFO("reset wallpaper Start!");
953     bool permissionSet = CheckCallingPermission(WALLPAPER_PERMISSION_NAME_SET_WALLPAPER);
954     if (!permissionSet) {
955         HILOG_INFO("reset wallpaper no set permission!");
956         return E_NO_PERMISSION;
957     }
958 
959     if (wallpaperType != static_cast<int32_t>(WALLPAPER_LOCKSCREEN) &&
960         wallpaperType != static_cast<int32_t>(WALLPAPER_SYSTEM)) {
961         HILOG_INFO("wallpaperType = %{public}d type not support ", wallpaperType);
962         return E_PARAMETERS_INVALID;
963     }
964     WallpaperType type = static_cast<WallpaperType>(wallpaperType);
965     int32_t userId = QueryActiveUserId();
966     HILOG_INFO("QueryCurrentOsAccount userId: %{public}d", userId);
967     if (!CheckUserPermissionById(userId)) {
968         return E_USER_IDENTITY_ERROR;
969     }
970     ErrorCode wallpaperErrorCode = SetDefaultDataForWallpaper(userId, type);
971     HILOG_INFO(" Set default data result[%{public}d]", wallpaperErrorCode);
972     return wallpaperErrorCode;
973 }
974 
ResetWallpaperV9(int32_t wallpaperType)975 ErrorCode WallpaperService::ResetWallpaperV9(int32_t wallpaperType)
976 {
977     if (!IsSystemApp()) {
978         HILOG_INFO("CallingApp is not SystemApp");
979         return E_NOT_SYSTEM_APP;
980     }
981     return ResetWallpaper(wallpaperType);
982 }
983 
SetDefaultDataForWallpaper(int32_t userId,WallpaperType wallpaperType)984 ErrorCode WallpaperService::SetDefaultDataForWallpaper(int32_t userId, WallpaperType wallpaperType)
985 {
986     WallpaperData wallpaperData;
987     if (!GetWallpaperSafeLocked(userId, wallpaperType, wallpaperData)) {
988         return E_DEAL_FAILED;
989     }
990     if (!RestoreUserResources(wallpaperData, wallpaperType)) {
991         HILOG_ERROR("RestoreUserResources error");
992         return E_DEAL_FAILED;
993     }
994     if (!SaveWallpaperState(userId, wallpaperType, DEFAULT)) {
995         HILOG_ERROR("Save wallpaper state failed!");
996         return E_DEAL_FAILED;
997     }
998     wallpaperData.wallpaperId = DEFAULT_WALLPAPER_ID;
999     wallpaperData.resourceType = DEFAULT;
1000     wallpaperData.allowBackup = true;
1001     if (wallpaperType == WALLPAPER_LOCKSCREEN) {
1002         lockWallpaperMap_.InsertOrAssign(userId, wallpaperData);
1003     } else if (wallpaperType == WALLPAPER_SYSTEM) {
1004         systemWallpaperMap_.InsertOrAssign(userId, wallpaperData);
1005     }
1006     if (!SendWallpaperChangeEvent(userId, wallpaperType)) {
1007         HILOG_ERROR("Send wallpaper state failed!");
1008         return E_DEAL_FAILED;
1009     }
1010     SaveColor(userId, wallpaperType);
1011     return E_OK;
1012 }
1013 
On(const std::string & type,sptr<IWallpaperEventListener> listener)1014 ErrorCode WallpaperService::On(const std::string &type, sptr<IWallpaperEventListener> listener)
1015 {
1016     HILOG_DEBUG("WallpaperService::On in");
1017     if (listener == nullptr) {
1018         HILOG_ERROR("WallpaperService::On listener is null");
1019         return E_DEAL_FAILED;
1020     }
1021     if (type == WALLPAPER_CHANGE && !IsSystemApp()) {
1022         HILOG_ERROR("current app is not SystemApp");
1023         return E_NOT_SYSTEM_APP;
1024     }
1025     std::lock_guard<std::mutex> autoLock(listenerMapMutex_);
1026     wallpaperEventMap_[type].insert_or_assign(IPCSkeleton::GetCallingTokenID(), listener);
1027     HILOG_DEBUG("WallpaperService::On out");
1028     return E_OK;
1029 }
1030 
Off(const std::string & type,sptr<IWallpaperEventListener> listener)1031 ErrorCode WallpaperService::Off(const std::string &type, sptr<IWallpaperEventListener> listener)
1032 {
1033     HILOG_DEBUG("WallpaperService::Off in");
1034     (void)listener;
1035     if (type == WALLPAPER_CHANGE && !IsSystemApp()) {
1036         HILOG_ERROR("current app is not SystemApp");
1037         return E_NOT_SYSTEM_APP;
1038     }
1039     std::lock_guard<std::mutex> autoLock(listenerMapMutex_);
1040     auto iter = wallpaperEventMap_.find(type);
1041     if (iter != wallpaperEventMap_.end()) {
1042         auto it = iter->second.find(IPCSkeleton::GetCallingTokenID());
1043         if (it != iter->second.end()) {
1044             it->second = nullptr;
1045             iter->second.erase(it);
1046         }
1047     }
1048     HILOG_DEBUG("WallpaperService::Off out");
1049     return E_OK;
1050 }
1051 
RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback)1052 bool WallpaperService::RegisterWallpaperCallback(const sptr<IWallpaperCallback> callback)
1053 {
1054     HILOG_INFO("  WallpaperService::RegisterWallpaperCallback");
1055     callbackProxy_ = callback;
1056     return true;
1057 }
1058 
GetWallpaperSafeLocked(int32_t userId,WallpaperType wallpaperType,WallpaperData & wallpaperData)1059 bool WallpaperService::GetWallpaperSafeLocked(int32_t userId, WallpaperType wallpaperType, WallpaperData &wallpaperData)
1060 {
1061     HILOG_INFO("function start.");
1062     auto iterator = wallpaperType == WALLPAPER_SYSTEM ? systemWallpaperMap_.Find(userId)
1063                                                       : lockWallpaperMap_.Find(userId);
1064     if (!iterator.first) {
1065         HILOG_INFO(" No Lock wallpaper?  Not tracking for lock-only ");
1066         LoadSettingsLocked(userId);
1067         iterator = wallpaperType == WALLPAPER_SYSTEM ? systemWallpaperMap_.Find(userId)
1068                                                      : lockWallpaperMap_.Find(userId);
1069         if (!iterator.first) {
1070             HILOG_ERROR("Fail to get wallpaper data");
1071             return false;
1072         }
1073     }
1074     wallpaperData = iterator.second;
1075     return true;
1076 }
1077 
ClearWallpaperLocked(int32_t userId,WallpaperType wallpaperType)1078 void WallpaperService::ClearWallpaperLocked(int32_t userId, WallpaperType wallpaperType)
1079 {
1080     HILOG_INFO("Clear wallpaper Start!");
1081     auto iterator = wallpaperType == WALLPAPER_SYSTEM ? systemWallpaperMap_.Find(userId)
1082                                                       : lockWallpaperMap_.Find(userId);
1083     if (!iterator.first) {
1084         HILOG_INFO("Lock wallpaper already cleared");
1085         return;
1086     }
1087     if (!iterator.second.wallpaperFile.empty() || !iterator.second.liveWallpaperFile.empty()) {
1088         if (wallpaperType == WALLPAPER_LOCKSCREEN) {
1089             lockWallpaperMap_.Erase(userId);
1090         } else if (wallpaperType == WALLPAPER_SYSTEM) {
1091             systemWallpaperMap_.Erase(userId);
1092         }
1093     }
1094     HILOG_INFO("Clear wallpaper End!");
1095 }
1096 
CheckCallingPermission(const std::string & permissionName)1097 bool WallpaperService::CheckCallingPermission(const std::string &permissionName)
1098 {
1099     AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
1100     int32_t result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName);
1101     if (result != TypePermissionState::PERMISSION_GRANTED) {
1102         HILOG_ERROR("Check permission failed.");
1103         return false;
1104     }
1105     return true;
1106 }
1107 
GetBundleNameByUid(std::int32_t uid,std::string & bname)1108 bool WallpaperService::GetBundleNameByUid(std::int32_t uid, std::string &bname)
1109 {
1110     sptr<ISystemAbilityManager> systemMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1111     if (systemMgr == nullptr) {
1112         HILOG_ERROR("Fail to get system ability mgr");
1113         return false;
1114     }
1115 
1116     sptr<IRemoteObject> remoteObject = systemMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
1117     if (remoteObject == nullptr) {
1118         HILOG_ERROR("Fail to get bundle manager proxy");
1119         return false;
1120     }
1121 
1122     sptr<OHOS::AppExecFwk::IBundleMgr> bundleMgrProxy = iface_cast<OHOS::AppExecFwk::IBundleMgr>(remoteObject);
1123     if (bundleMgrProxy == nullptr) {
1124         HILOG_ERROR("Bundle mgr proxy is nullptr");
1125         return false;
1126     }
1127 
1128     ErrCode errCode = bundleMgrProxy->GetNameForUid(uid, bname);
1129     if (errCode != ERR_OK) {
1130         HILOG_ERROR("Get bundle name failed errcode:%{public}d", errCode);
1131         return false;
1132     }
1133     HILOG_INFO("Get bundle name is %{public}s", bname.c_str());
1134     return true;
1135 }
1136 
ReporterFault(FaultType faultType,FaultCode faultCode)1137 void WallpaperService::ReporterFault(FaultType faultType, FaultCode faultCode)
1138 {
1139     FaultMsg msg;
1140     msg.faultType = faultType;
1141     msg.errorCode = faultCode;
1142     ReportStatus nRet;
1143     if (faultType == FaultType::SERVICE_FAULT) {
1144         msg.moduleName = "WallpaperService";
1145         nRet = FaultReporter::ReportServiceFault(msg);
1146     } else {
1147         nRet = FaultReporter::ReportRuntimeFault(msg);
1148     }
1149 
1150     if (nRet == ReportStatus::SUCCESS) {
1151         HILOG_INFO("ReporterFault success");
1152     } else {
1153         HILOG_ERROR("ReporterFault failed");
1154     }
1155 }
1156 
Dump(int32_t fd,const std::vector<std::u16string> & args)1157 int32_t WallpaperService::Dump(int32_t fd, const std::vector<std::u16string> &args)
1158 {
1159     std::vector<std::string> argsStr;
1160     for (auto item : args) {
1161         argsStr.emplace_back(Str16ToStr8(item));
1162     }
1163 
1164     if (DumpHelper::GetInstance().Dispatch(fd, argsStr)) {
1165         HILOG_ERROR("DumpHelper Dispatch failed");
1166         return 0;
1167     }
1168     return 1;
1169 }
1170 
1171 #ifndef THEME_SERVICE
ConnectExtensionAbility()1172 bool WallpaperService::ConnectExtensionAbility()
1173 {
1174     HILOG_DEBUG("ConnectAdapter");
1175     MemoryGuard cacheGuard;
1176     AAFwk::Want want;
1177     want.SetElementName(OHOS_WALLPAPER_BUNDLE_NAME, "WallpaperExtAbility");
1178     ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
1179     if (errCode != ERR_OK) {
1180         HILOG_ERROR("connect ability server failed errCode=%{public}d", errCode);
1181         return false;
1182     }
1183     if (connection_ == nullptr) {
1184         connection_ = new WallpaperExtensionAbilityConnection(*this);
1185     }
1186     auto ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectExtensionAbility(want, connection_, DEFAULT_VALUE);
1187     HILOG_INFO("ConnectExtensionAbility errCode=%{public}d", ret);
1188     return ret;
1189 }
1190 #endif
1191 
IsSystemApp()1192 bool WallpaperService::IsSystemApp()
1193 {
1194     HILOG_INFO("IsSystemApp start");
1195     uint64_t tokenId = IPCSkeleton::GetCallingFullTokenID();
1196     return TokenIdKit::IsSystemAppByFullTokenID(tokenId);
1197 }
1198 
GetImageFd(int32_t userId,WallpaperType wallpaperType,int32_t & fd)1199 ErrorCode WallpaperService::GetImageFd(int32_t userId, WallpaperType wallpaperType, int32_t &fd)
1200 {
1201     HILOG_INFO("WallpaperService::GetImageFd start ");
1202     std::string filePathName;
1203     if (!GetFileNameFromMap(userId, wallpaperType, filePathName)) {
1204         return E_DEAL_FAILED;
1205     }
1206     if (GetResType(userId, wallpaperType) == WallpaperResourceType::PACKAGE) {
1207         HILOG_INFO("The current wallpaper is a custom wallpaper");
1208         return E_OK;
1209     }
1210     {
1211         std::lock_guard<std::mutex> lock(mtx_);
1212         fd = open(filePathName.c_str(), O_RDONLY, S_IREAD);
1213     }
1214     if (fd < 0) {
1215         HILOG_ERROR("Open file failed, errno %{public}d", errno);
1216         ReporterFault(FaultType::LOAD_WALLPAPER_FAULT, FaultCode::RF_FD_INPUT_FAILED);
1217         return E_DEAL_FAILED;
1218     }
1219     HILOG_INFO("fd = %{public}d", fd);
1220     return E_OK;
1221 }
1222 
GetImageSize(int32_t userId,WallpaperType wallpaperType,int32_t & size)1223 ErrorCode WallpaperService::GetImageSize(int32_t userId, WallpaperType wallpaperType, int32_t &size)
1224 {
1225     HILOG_INFO("WallpaperService::GetImageSize start ");
1226     std::string filePathName;
1227     HILOG_INFO("userId = %{public}d", userId);
1228     if (!GetPictureFileName(userId, wallpaperType, filePathName)) {
1229         return E_DEAL_FAILED;
1230     }
1231 
1232     if (!OHOS::FileExists(filePathName)) {
1233         HILOG_ERROR("file is not exist!");
1234         return E_NOT_FOUND;
1235     }
1236     std::lock_guard<std::mutex> lock(mtx_);
1237     FILE *fd = fopen(filePathName.c_str(), "rb");
1238     if (fd == nullptr) {
1239         HILOG_ERROR("fopen file failed, errno %{public}d", errno);
1240         return E_FILE_ERROR;
1241     }
1242     int32_t fend = fseek(fd, 0, SEEK_END);
1243     size = ftell(fd);
1244     int32_t fset = fseek(fd, 0, SEEK_SET);
1245     if (size <= 0 || fend != 0 || fset != 0) {
1246         HILOG_ERROR("ftell file failed or fseek file failed, errno %{public}d", errno);
1247         fclose(fd);
1248         return E_FILE_ERROR;
1249     }
1250     fclose(fd);
1251     return E_OK;
1252 }
1253 
BlockRetry(int64_t interval,uint32_t maxRetryTimes,std::function<bool ()> function)1254 bool WallpaperService::BlockRetry(int64_t interval, uint32_t maxRetryTimes, std::function<bool()> function)
1255 {
1256     uint32_t times = 0;
1257     bool ret = false;
1258     do {
1259         times++;
1260         ret = function();
1261         if (ret) {
1262             break;
1263         }
1264         std::this_thread::sleep_for(std::chrono::milliseconds(interval));
1265         HILOG_INFO("function() is: %d", ret);
1266     } while (times < maxRetryTimes);
1267     HILOG_INFO("retry times: %d", times);
1268     return ret;
1269 }
1270 
QueryActiveUserId()1271 int32_t WallpaperService::QueryActiveUserId()
1272 {
1273     std::vector<int32_t> ids;
1274     ErrCode errCode = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
1275     if (errCode != ERR_OK || ids.empty()) {
1276         HILOG_ERROR("Query active userid failed, errCode: %{public}d, ", errCode);
1277         return DEFAULT_USER_ID;
1278     }
1279     return ids[0];
1280 }
1281 
CheckUserPermissionById(int32_t userId)1282 bool WallpaperService::CheckUserPermissionById(int32_t userId)
1283 {
1284     OsAccountInfo osAccountInfo;
1285     ErrCode errCode = OsAccountManager::QueryOsAccountById(userId, osAccountInfo);
1286     if (errCode != ERR_OK) {
1287         HILOG_ERROR("Query os account info failed, errCode: %{public}d, ", errCode);
1288         return false;
1289     }
1290     HILOG_INFO("osAccountInfo GetType: %{public}d", static_cast<int32_t>(osAccountInfo.GetType()));
1291     if (osAccountInfo.GetType() == OsAccountType::GUEST) {
1292         HILOG_ERROR("The guest does not have permissions");
1293         return false;
1294     }
1295     return true;
1296 }
1297 
SetWallpaper(int32_t fd,int32_t wallpaperType,int32_t length,WallpaperResourceType resourceType)1298 ErrorCode WallpaperService::SetWallpaper(int32_t fd, int32_t wallpaperType, int32_t length,
1299     WallpaperResourceType resourceType)
1300 {
1301     int32_t userId = QueryActiveUserId();
1302     HILOG_INFO("QueryCurrentOsAccount userId: %{public}d", userId);
1303     if (!CheckUserPermissionById(userId)) {
1304         return E_USER_IDENTITY_ERROR;
1305     }
1306     ErrorCode errCode = CheckValid(wallpaperType, length, resourceType);
1307     if (errCode != E_OK) {
1308         return errCode;
1309     }
1310     std::string uri = wallpaperTmpFullPath_;
1311     char *paperBuf = new (std::nothrow) char[length]();
1312     if (paperBuf == nullptr) {
1313         return E_NO_MEMORY;
1314     }
1315     std::lock_guard<std::mutex> lock(mtx_);
1316     if (read(fd, paperBuf, length) <= 0) {
1317         HILOG_ERROR("read fd failed");
1318         delete[] paperBuf;
1319         return E_DEAL_FAILED;
1320     }
1321     mode_t mode = S_IRUSR | S_IWUSR;
1322     int32_t fdw = open(uri.c_str(), O_WRONLY | O_CREAT, mode);
1323     if (fdw < 0) {
1324         HILOG_ERROR("Open wallpaper tmpFullPath failed, errno %{public}d", errno);
1325         delete[] paperBuf;
1326         return E_DEAL_FAILED;
1327     }
1328     if (write(fdw, paperBuf, length) <= 0) {
1329         HILOG_ERROR("Write to fdw failed, errno %{public}d", errno);
1330         ReporterFault(FaultType::SET_WALLPAPER_FAULT, FaultCode::RF_DROP_FAILED);
1331         delete[] paperBuf;
1332         close(fdw);
1333         return E_DEAL_FAILED;
1334     }
1335     delete[] paperBuf;
1336     close(fdw);
1337     WallpaperType type = static_cast<WallpaperType>(wallpaperType);
1338     ErrorCode wallpaperErrorCode = SetWallpaperBackupData(userId, resourceType, uri, type);
1339     if (resourceType == PICTURE) {
1340         SaveColor(userId, type);
1341     }
1342     return wallpaperErrorCode;
1343 }
1344 
OnColorsChange(WallpaperType wallpaperType,const ColorManager::Color & color)1345 void WallpaperService::OnColorsChange(WallpaperType wallpaperType, const ColorManager::Color &color)
1346 {
1347     std::vector<uint64_t> colors;
1348     if (wallpaperType == WALLPAPER_SYSTEM && !CompareColor(systemWallpaperColor_, color)) {
1349         systemWallpaperColor_ = color.PackValue();
1350         colors.emplace_back(systemWallpaperColor_);
1351         NotifyColorChange(colors, WALLPAPER_SYSTEM);
1352     } else if (wallpaperType == WALLPAPER_LOCKSCREEN && !CompareColor(lockWallpaperColor_, color)) {
1353         lockWallpaperColor_ = color.PackValue();
1354         colors.emplace_back(lockWallpaperColor_);
1355         NotifyColorChange(colors, WALLPAPER_LOCKSCREEN);
1356     }
1357 }
1358 
CheckValid(int32_t wallpaperType,int32_t length,WallpaperResourceType resourceType)1359 ErrorCode WallpaperService::CheckValid(int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType)
1360 {
1361     if (!CheckCallingPermission(WALLPAPER_PERMISSION_NAME_SET_WALLPAPER)) {
1362         HILOG_INFO("SetWallpaper no set permission!");
1363         return E_NO_PERMISSION;
1364     }
1365     if (wallpaperType != static_cast<int32_t>(WALLPAPER_LOCKSCREEN) &&
1366         wallpaperType != static_cast<int32_t>(WALLPAPER_SYSTEM)) {
1367         return E_PARAMETERS_INVALID;
1368     }
1369 
1370     int maxLength = resourceType == VIDEO ? MAX_VIDEO_SIZE : FOO_MAX_LEN;
1371     if (length <= 0 || length > maxLength) {
1372         return E_PARAMETERS_INVALID;
1373     }
1374     return E_OK;
1375 }
1376 
WallpaperChanged(WallpaperType wallpaperType,WallpaperResourceType resType,const std::string & uri)1377 bool WallpaperService::WallpaperChanged(WallpaperType wallpaperType, WallpaperResourceType resType,
1378     const std::string &uri)
1379 {
1380     std::lock_guard<std::mutex> autoLock(listenerMapMutex_);
1381     auto it = wallpaperEventMap_.find(WALLPAPER_CHANGE);
1382     if (it != wallpaperEventMap_.end()) {
1383         for (auto iter = it->second.begin(); iter != it->second.end(); iter++) {
1384             if (iter->second == nullptr) {
1385                 continue;
1386             }
1387             iter->second->OnWallpaperChange(wallpaperType, resType, uri);
1388         }
1389         return true;
1390     }
1391     return false;
1392 }
1393 
NotifyColorChange(const std::vector<uint64_t> & colors,const WallpaperType & wallpaperType)1394 void WallpaperService::NotifyColorChange(const std::vector<uint64_t> &colors, const WallpaperType &wallpaperType)
1395 {
1396     std::lock_guard<std::mutex> autoLock(listenerMapMutex_);
1397     auto it = wallpaperEventMap_.find(COLOR_CHANGE);
1398     if (it != wallpaperEventMap_.end()) {
1399         for (auto iter = it->second.begin(); iter != it->second.end(); iter++) {
1400             if (iter->second == nullptr) {
1401                 continue;
1402             }
1403             iter->second->OnColorsChange(colors, wallpaperType);
1404         }
1405     }
1406 }
1407 
SaveWallpaperState(int32_t userId,WallpaperType wallpaperType,WallpaperResourceType resourceType)1408 bool WallpaperService::SaveWallpaperState(int32_t userId, WallpaperType wallpaperType,
1409     WallpaperResourceType resourceType)
1410 {
1411     WallpaperData systemData;
1412     WallpaperData lockScreenData;
1413     if (!GetWallpaperSafeLocked(userId, WALLPAPER_SYSTEM, systemData) ||
1414         !GetWallpaperSafeLocked(userId, WALLPAPER_LOCKSCREEN, lockScreenData)) {
1415         return false;
1416     }
1417     nlohmann::json root;
1418     if (wallpaperType == WALLPAPER_SYSTEM) {
1419         root[SYSTEM_RES_TYPE] = static_cast<int32_t>(resourceType);
1420         root[LOCKSCREEN_RES_TYPE] = static_cast<int32_t>(lockScreenData.resourceType);
1421     } else {
1422         root[LOCKSCREEN_RES_TYPE] = static_cast<int32_t>(resourceType);
1423         root[SYSTEM_RES_TYPE] = static_cast<int32_t>(systemData.resourceType);
1424     }
1425     std::string json = root.dump();
1426     if (json.empty()) {
1427         HILOG_ERROR("write user config file failed. because json content is empty.");
1428         return false;
1429     }
1430 
1431     std::string userPath = WALLPAPER_USERID_PATH + std::to_string(userId) + "/wallpapercfg";
1432     mode_t mode = S_IRUSR | S_IWUSR;
1433     int fd = open(userPath.c_str(), O_CREAT | O_WRONLY | O_SYNC, mode);
1434     if (fd <= 0) {
1435         HILOG_ERROR("open user config file failed.");
1436         return false;
1437     }
1438     ssize_t size = write(fd, json.c_str(), json.size());
1439     if (size <= 0) {
1440         HILOG_ERROR("write user config file failed.");
1441         close(fd);
1442         return false;
1443     }
1444     close(fd);
1445     return true;
1446 }
1447 
LoadWallpaperState()1448 void WallpaperService::LoadWallpaperState()
1449 {
1450     int32_t userId = QueryActiveUserId();
1451     std::string userPath = WALLPAPER_USERID_PATH + std::to_string(userId) + "/wallpapercfg";
1452     int fd = open(userPath.c_str(), O_RDONLY, S_IREAD);
1453     if (fd <= 0) {
1454         HILOG_ERROR("open user config file failed.");
1455         return;
1456     }
1457     const size_t len = 255;
1458     char buf[len] = { 0 };
1459     ssize_t size = read(fd, buf, len);
1460     if (size <= 0) {
1461         HILOG_ERROR("read user config file failed.");
1462         close(fd);
1463         return;
1464     }
1465     close(fd);
1466 
1467     if (buf[0] == '\0') {
1468         return;
1469     }
1470     WallpaperData systemData;
1471     WallpaperData lockScreenData;
1472     if (!GetWallpaperSafeLocked(userId, WALLPAPER_SYSTEM, systemData) ||
1473         !GetWallpaperSafeLocked(userId, WALLPAPER_LOCKSCREEN, lockScreenData)) {
1474         return;
1475     }
1476     auto root = nlohmann::json::parse(buf);
1477     if (root.contains(SYSTEM_RES_TYPE) && root[SYSTEM_RES_TYPE].is_number()) {
1478         systemData.resourceType = static_cast<WallpaperResourceType>(root[SYSTEM_RES_TYPE].get<int>());
1479     }
1480     if (root.contains(LOCKSCREEN_RES_TYPE) && root[SYSTEM_RES_TYPE].is_number()) {
1481         lockScreenData.resourceType = static_cast<WallpaperResourceType>(root[LOCKSCREEN_RES_TYPE].get<int>());
1482     }
1483 }
1484 #ifdef THEME_SERVICE
InitThemeResource()1485 void WallpaperService::InitThemeResource()
1486 {
1487     if (ThemeManager::ThemeManagerClient::GetInstance().InitResource(USER_ID) == true) {
1488         return;
1489     }
1490     auto callback = []() { ThemeManager::ThemeManagerClient::GetInstance().InitResource(USER_ID); };
1491     auto result = serviceHandler_->PostTask(callback, INIT_THEME_INTERVAL);
1492     if (!result) {
1493         HILOG_ERROR("retry failed since post task failed");
1494     }
1495 }
1496 #endif
1497 } // namespace WallpaperMgrService
1498 } // namespace OHOS
1499