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