1 /* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "ui_appearance_ability_client.h" 17 18 #include "iservice_registry.h" 19 #include "system_ability_definition.h" 20 #include "ui_appearance_ability_proxy.h" 21 #include "ui_appearance_log.h" 22 23 namespace OHOS { 24 namespace ArkUi::UiAppearance { GetInstance()25sptr<UiAppearanceAbilityClient> UiAppearanceAbilityClient::GetInstance() 26 { 27 if (!instance_) { 28 std::lock_guard<std::mutex> autoLock(instanceLock_); 29 if (!instance_) { 30 instance_ = new UiAppearanceAbilityClient; 31 uiAppearanceServiceProxy_ = CreateUiAppearanceServiceProxy(); 32 } 33 } 34 return instance_; 35 } 36 GetUiAppearanceServiceProxy()37sptr<UiAppearanceAbilityInterface> UiAppearanceAbilityClient::GetUiAppearanceServiceProxy() 38 { 39 if (uiAppearanceServiceProxy_ == nullptr) { 40 HILOG_ERROR("Redo CreateUiAppearanceServiceProxy"); 41 uiAppearanceServiceProxy_ = CreateUiAppearanceServiceProxy(); 42 } 43 return uiAppearanceServiceProxy_; 44 } 45 SetDarkMode(UiAppearanceAbilityInterface::DarkMode mode)46int32_t UiAppearanceAbilityClient::SetDarkMode(UiAppearanceAbilityInterface::DarkMode mode) 47 { 48 if (!GetUiAppearanceServiceProxy()) { 49 HILOG_ERROR("SetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed."); 50 return UiAppearanceAbilityInterface::ErrCode::SYS_ERR; 51 } 52 return uiAppearanceServiceProxy_->SetDarkMode(mode); 53 } 54 GetDarkMode()55int32_t UiAppearanceAbilityClient::GetDarkMode() 56 { 57 if (!GetUiAppearanceServiceProxy()) { 58 HILOG_ERROR("GetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed."); 59 return UiAppearanceAbilityInterface::ErrCode::SYS_ERR; 60 } 61 return uiAppearanceServiceProxy_->GetDarkMode(); 62 } 63 CreateUiAppearanceServiceProxy()64sptr<UiAppearanceAbilityInterface> UiAppearanceAbilityClient::CreateUiAppearanceServiceProxy() 65 { 66 sptr<ISystemAbilityManager> systemAbilityManager = 67 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 68 if (systemAbilityManager == nullptr) { 69 HILOG_ERROR("Get SystemAbilityManager failed."); 70 return nullptr; 71 } 72 73 sptr<IRemoteObject> systemAbility = systemAbilityManager->GetSystemAbility(ARKUI_UI_APPEARANCE_SERVICE_ID); 74 if (systemAbility == nullptr) { 75 HILOG_ERROR("Get SystemAbility failed."); 76 return nullptr; 77 } 78 79 deathRecipient_ = new UiAppearanceDeathRecipient; 80 systemAbility->AddDeathRecipient(deathRecipient_); 81 sptr<UiAppearanceAbilityInterface> uiAppearanceServiceProxy = 82 iface_cast<UiAppearanceAbilityInterface>(systemAbility); 83 if (uiAppearanceServiceProxy == nullptr) { 84 HILOG_ERROR("Get uiAppearanceServiceProxy from SA failed."); 85 return nullptr; 86 } 87 HILOG_INFO("Get uiAppearanceServiceProxy successful."); 88 return uiAppearanceServiceProxy; 89 } 90 OnRemoteSaDied(const wptr<IRemoteObject> & remote)91void UiAppearanceAbilityClient::OnRemoteSaDied(const wptr<IRemoteObject>& remote) 92 { 93 // Used for new connections after the service may be disconnected. 94 uiAppearanceServiceProxy_ = CreateUiAppearanceServiceProxy(); 95 } 96 OnRemoteDied(const wptr<IRemoteObject> & object)97void UiAppearanceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object) 98 { 99 HILOG_INFO("UiAppearanceDeathRecipient on remote systemAbility died."); 100 UiAppearanceAbilityClient::GetInstance()->OnRemoteSaDied(object); 101 } 102 } // namespace ArkUi::UiAppearance 103 } // namespace OHOS 104