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 <string> 19 #include "iservice_registry.h" 20 #include "system_ability_definition.h" 21 #include "ui_appearance_ability_proxy.h" 22 #include "ui_appearance_log.h" 23 #include "xcollie/xcollie.h" 24 #include "xcollie/xcollie_define.h" 25 26 namespace OHOS { 27 namespace ArkUi::UiAppearance { GetInstance()28sptr<UiAppearanceAbilityClient> UiAppearanceAbilityClient::GetInstance() 29 { 30 if (!instance_) { 31 std::lock_guard<std::mutex> autoLock(instanceLock_); 32 if (!instance_) { 33 uiAppearanceServiceProxy_ = CreateUiAppearanceServiceProxy(); 34 instance_ = new UiAppearanceAbilityClient; 35 } 36 } 37 return instance_; 38 } 39 GetUiAppearanceServiceProxy()40sptr<UiAppearanceAbilityInterface> UiAppearanceAbilityClient::GetUiAppearanceServiceProxy() 41 { 42 if (uiAppearanceServiceProxy_ == nullptr) { 43 LOGE("Redo CreateUiAppearanceServiceProxy"); 44 uiAppearanceServiceProxy_ = CreateUiAppearanceServiceProxy(); 45 } 46 return uiAppearanceServiceProxy_; 47 } 48 SetDarkMode(UiAppearanceAbilityInterface::DarkMode mode)49int32_t UiAppearanceAbilityClient::SetDarkMode(UiAppearanceAbilityInterface::DarkMode mode) 50 { 51 if (!GetUiAppearanceServiceProxy()) { 52 LOGE("SetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed."); 53 return UiAppearanceAbilityInterface::ErrCode::SYS_ERR; 54 } 55 return uiAppearanceServiceProxy_->SetDarkMode(mode); 56 } 57 GetDarkMode()58int32_t UiAppearanceAbilityClient::GetDarkMode() 59 { 60 if (!GetUiAppearanceServiceProxy()) { 61 LOGE("GetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed."); 62 return UiAppearanceAbilityInterface::ErrCode::SYS_ERR; 63 } 64 return uiAppearanceServiceProxy_->GetDarkMode(); 65 } 66 SetFontScale(std::string & fontScale)67int32_t UiAppearanceAbilityClient::SetFontScale(std::string &fontScale) 68 { 69 if (!GetUiAppearanceServiceProxy()) { 70 LOGE("SetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed."); 71 return UiAppearanceAbilityInterface::ErrCode::SYS_ERR; 72 } 73 return uiAppearanceServiceProxy_->SetFontScale(fontScale); 74 } 75 GetFontScale(std::string & fontScale)76int32_t UiAppearanceAbilityClient::GetFontScale(std::string &fontScale) 77 { 78 if (!GetUiAppearanceServiceProxy()) { 79 LOGE("GetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed."); 80 return UiAppearanceAbilityInterface::ErrCode::SYS_ERR; 81 } 82 int id = HiviewDFX::XCollie::GetInstance().SetTimer( 83 "GetFontScale", 10, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG); 84 auto res = uiAppearanceServiceProxy_->GetFontScale(fontScale); 85 HiviewDFX::XCollie::GetInstance().CancelTimer(id); 86 return res; 87 } 88 SetFontWeightScale(std::string & fontWeightScale)89int32_t UiAppearanceAbilityClient::SetFontWeightScale(std::string &fontWeightScale) 90 { 91 if (!GetUiAppearanceServiceProxy()) { 92 LOGE("SetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed."); 93 return UiAppearanceAbilityInterface::ErrCode::SYS_ERR; 94 } 95 return uiAppearanceServiceProxy_->SetFontWeightScale(fontWeightScale); 96 } 97 GetFontWeightScale(std::string & fontWeightScale)98int32_t UiAppearanceAbilityClient::GetFontWeightScale(std::string &fontWeightScale) 99 { 100 if (!GetUiAppearanceServiceProxy()) { 101 LOGE("GetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed."); 102 return UiAppearanceAbilityInterface::ErrCode::SYS_ERR; 103 } 104 return uiAppearanceServiceProxy_->GetFontWeightScale(fontWeightScale); 105 } 106 CreateUiAppearanceServiceProxy()107sptr<UiAppearanceAbilityInterface> UiAppearanceAbilityClient::CreateUiAppearanceServiceProxy() 108 { 109 sptr<ISystemAbilityManager> systemAbilityManager = 110 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 111 if (systemAbilityManager == nullptr) { 112 LOGE("Get SystemAbilityManager failed."); 113 return nullptr; 114 } 115 116 sptr<IRemoteObject> systemAbility = systemAbilityManager->GetSystemAbility(ARKUI_UI_APPEARANCE_SERVICE_ID); 117 if (systemAbility == nullptr) { 118 LOGE("Get SystemAbility failed."); 119 return nullptr; 120 } 121 122 deathRecipient_ = new UiAppearanceDeathRecipient; 123 systemAbility->AddDeathRecipient(deathRecipient_); 124 sptr<UiAppearanceAbilityInterface> uiAppearanceServiceProxy = 125 iface_cast<UiAppearanceAbilityInterface>(systemAbility); 126 if (uiAppearanceServiceProxy == nullptr) { 127 LOGE("Get uiAppearanceServiceProxy from SA failed."); 128 return nullptr; 129 } 130 LOGI("Get uiAppearanceServiceProxy successful."); 131 return uiAppearanceServiceProxy; 132 } 133 OnRemoteSaDied(const wptr<IRemoteObject> & remote)134void UiAppearanceAbilityClient::OnRemoteSaDied(const wptr<IRemoteObject>& remote) 135 { 136 // Used for new connections after the service may be disconnected. 137 uiAppearanceServiceProxy_ = CreateUiAppearanceServiceProxy(); 138 } 139 OnRemoteDied(const wptr<IRemoteObject> & object)140void UiAppearanceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object) 141 { 142 LOGI("UiAppearanceDeathRecipient on remote systemAbility died."); 143 UiAppearanceAbilityClient::GetInstance()->OnRemoteSaDied(object); 144 } 145 } // namespace ArkUi::UiAppearance 146 } // namespace OHOS 147