1 /*
2 * Copyright (C) 2022-2025 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()28 sptr<UiAppearanceAbilityClient> UiAppearanceAbilityClient::GetInstance()
29 {
30 static sptr<UiAppearanceAbilityClient> instance = new UiAppearanceAbilityClient;
31 return instance;
32 }
33
UiAppearanceAbilityClient()34 UiAppearanceAbilityClient::UiAppearanceAbilityClient()
35 {
36 GetUiAppearanceServiceProxy();
37 }
38
GetUiAppearanceServiceProxy()39 sptr<IUiAppearanceAbility> UiAppearanceAbilityClient::GetUiAppearanceServiceProxy()
40 {
41 std::lock_guard guard(serviceProxyLock_);
42 if (uiAppearanceServiceProxy_ == nullptr) {
43 LOGE("Redo CreateUiAppearanceServiceProxy");
44 uiAppearanceServiceProxy_ = CreateUiAppearanceServiceProxy();
45 }
46 return uiAppearanceServiceProxy_;
47 }
48
SetDarkMode(DarkMode mode)49 int32_t UiAppearanceAbilityClient::SetDarkMode(DarkMode mode)
50 {
51 if (!GetUiAppearanceServiceProxy()) {
52 LOGE("SetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed.");
53 return UiAppearanceAbilityErrCode::SYS_ERR;
54 }
55 int32_t funcRes = -1;
56 auto res = GetUiAppearanceServiceProxy()->SetDarkMode(mode, funcRes);
57 if (res != ERR_OK) {
58 return UiAppearanceAbilityErrCode::SYS_ERR;
59 }
60 return funcRes;
61 }
62
GetDarkMode()63 int32_t UiAppearanceAbilityClient::GetDarkMode()
64 {
65 if (!GetUiAppearanceServiceProxy()) {
66 LOGE("GetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed.");
67 return UiAppearanceAbilityErrCode::SYS_ERR;
68 }
69 int32_t funcRes = -1;
70 auto res = GetUiAppearanceServiceProxy()->GetDarkMode(funcRes);
71 if (res != ERR_OK) {
72 return UiAppearanceAbilityErrCode::SYS_ERR;
73 }
74 return funcRes;
75 }
76
SetFontScale(std::string & fontScale)77 int32_t UiAppearanceAbilityClient::SetFontScale(std::string &fontScale)
78 {
79 if (!GetUiAppearanceServiceProxy()) {
80 LOGE("SetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed.");
81 return UiAppearanceAbilityErrCode::SYS_ERR;
82 }
83 int32_t funcRes = -1;
84 auto res = GetUiAppearanceServiceProxy()->SetFontScale(fontScale, funcRes);
85 if (res != ERR_OK) {
86 return UiAppearanceAbilityErrCode::SYS_ERR;
87 }
88 return funcRes;
89 }
90
GetFontScale(std::string & fontScale)91 int32_t UiAppearanceAbilityClient::GetFontScale(std::string &fontScale)
92 {
93 if (!GetUiAppearanceServiceProxy()) {
94 LOGE("GetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed.");
95 return UiAppearanceAbilityErrCode::SYS_ERR;
96 }
97 int id = HiviewDFX::XCollie::GetInstance().SetTimer(
98 "GetFontScale", 10, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
99 int32_t funcRes = -1;
100 auto res = GetUiAppearanceServiceProxy()->GetFontScale(fontScale, funcRes);
101 HiviewDFX::XCollie::GetInstance().CancelTimer(id);
102 if (res != ERR_OK) {
103 return UiAppearanceAbilityErrCode::SYS_ERR;
104 }
105 return funcRes;
106 }
107
SetFontWeightScale(std::string & fontWeightScale)108 int32_t UiAppearanceAbilityClient::SetFontWeightScale(std::string &fontWeightScale)
109 {
110 if (!GetUiAppearanceServiceProxy()) {
111 LOGE("SetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed.");
112 return UiAppearanceAbilityErrCode::SYS_ERR;
113 }
114 int32_t funcRes = -1;
115 auto res = GetUiAppearanceServiceProxy()->SetFontWeightScale(fontWeightScale, funcRes);
116 if (res != ERR_OK) {
117 return UiAppearanceAbilityErrCode::SYS_ERR;
118 }
119 return funcRes;
120 }
121
GetFontWeightScale(std::string & fontWeightScale)122 int32_t UiAppearanceAbilityClient::GetFontWeightScale(std::string &fontWeightScale)
123 {
124 if (!GetUiAppearanceServiceProxy()) {
125 LOGE("GetDarkMode quit because redoing CreateUiAppearanceServiceProxy failed.");
126 return UiAppearanceAbilityErrCode::SYS_ERR;
127 }
128 int32_t funcRes = -1;
129 auto res = GetUiAppearanceServiceProxy()->GetFontWeightScale(fontWeightScale, funcRes);
130 if (res != ERR_OK) {
131 return UiAppearanceAbilityErrCode::SYS_ERR;
132 }
133 return funcRes;
134 }
135
CreateUiAppearanceServiceProxy()136 sptr<IUiAppearanceAbility> UiAppearanceAbilityClient::CreateUiAppearanceServiceProxy()
137 {
138 sptr<ISystemAbilityManager> systemAbilityManager =
139 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
140 if (systemAbilityManager == nullptr) {
141 LOGE("Get SystemAbilityManager failed.");
142 return nullptr;
143 }
144
145 sptr<IRemoteObject> systemAbility = systemAbilityManager->GetSystemAbility(ARKUI_UI_APPEARANCE_SERVICE_ID);
146 if (systemAbility == nullptr) {
147 LOGE("Get SystemAbility failed.");
148 return nullptr;
149 }
150
151 sptr<UiAppearanceDeathRecipient> deathRecipient_ = new UiAppearanceDeathRecipient;
152 systemAbility->AddDeathRecipient(deathRecipient_);
153 sptr<IUiAppearanceAbility> uiAppearanceServiceProxy =
154 iface_cast<IUiAppearanceAbility>(systemAbility);
155 if (uiAppearanceServiceProxy == nullptr) {
156 LOGE("Get uiAppearanceServiceProxy from SA failed.");
157 return nullptr;
158 }
159 LOGI("Get uiAppearanceServiceProxy successful.");
160 return uiAppearanceServiceProxy;
161 }
162
OnRemoteSaDied(const wptr<IRemoteObject> & remote)163 void UiAppearanceAbilityClient::OnRemoteSaDied(const wptr<IRemoteObject>& remote)
164 {
165 // Used for new connections after the service may be disconnected.
166 std::lock_guard guard(serviceProxyLock_);
167 uiAppearanceServiceProxy_ = CreateUiAppearanceServiceProxy();
168 }
169
OnRemoteDied(const wptr<IRemoteObject> & object)170 void UiAppearanceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
171 {
172 LOGI("UiAppearanceDeathRecipient on remote systemAbility died.");
173 UiAppearanceAbilityClient::GetInstance()->OnRemoteSaDied(object);
174 }
175 } // namespace ArkUi::UiAppearance
176 } // namespace OHOS
177