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.h"
17
18 #include <string>
19
20 #include "accesstoken_kit.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "syspara/parameter.h"
24 #include "system_ability_definition.h"
25 #include "ui_appearance_log.h"
26
27 namespace {
28 static const std::string LIGHT = "light";
29 static const std::string DARK = "dark";
30 static const std::string PERSIST_DARKMODE_KEY = "persist.ace.darkmode";
31 static const std::string PERMISSION_UPDATE_CONFIGURATION = "ohos.permission.UPDATE_CONFIGURATION";
32 } // namespace
33
34 namespace OHOS {
35 namespace ArkUi::UiAppearance {
36 REGISTER_SYSTEM_ABILITY_BY_ID(UiAppearanceAbility, ARKUI_UI_APPEARANCE_SERVICE_ID, true);
37
UiAppearanceAbility(int32_t saId,bool runOnCreate)38 UiAppearanceAbility::UiAppearanceAbility(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate) {}
39
GetAppManagerInstance()40 sptr<AppExecFwk::IAppMgr> UiAppearanceAbility::GetAppManagerInstance()
41 {
42 sptr<ISystemAbilityManager> systemAbilityManager =
43 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
44 if (systemAbilityManager == nullptr) {
45 HILOG_ERROR("Getting systemAbilityManager failed.");
46 return nullptr;
47 }
48
49 sptr<IRemoteObject> appObject = systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID);
50 if (appObject == nullptr) {
51 HILOG_ERROR("Get systemAbility failed.");
52 return nullptr;
53 }
54
55 sptr<AppExecFwk::IAppMgr> systemAbility = iface_cast<AppExecFwk::IAppMgr>(appObject);
56 if (systemAbility == nullptr) {
57 HILOG_ERROR("Get AppMgrProxy from SA failed.");
58 return nullptr;
59 }
60 return systemAbility;
61 }
62
VerifyAccessToken(const std::string & permissionName)63 bool UiAppearanceAbility::VerifyAccessToken(const std::string& permissionName)
64 {
65 auto callerToken = IPCSkeleton::GetCallingTokenID();
66 int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permissionName);
67 if (ret == Security::AccessToken::PermissionState::PERMISSION_DENIED) {
68 HILOG_ERROR("permission %{private}s: PERMISSION_DENIED", permissionName.c_str());
69 return false;
70 }
71 return true;
72 }
73
OnSetDarkMode(DarkMode mode)74 int32_t UiAppearanceAbility::OnSetDarkMode(DarkMode mode)
75 {
76 bool ret = false;
77 std::string paramValue;
78 AppExecFwk::Configuration config;
79 switch (mode) {
80 case ALWAYS_LIGHT: {
81 ret = config.AddItem(
82 AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, AppExecFwk::ConfigurationInner::COLOR_MODE_LIGHT);
83 paramValue.assign(LIGHT);
84 break;
85 }
86 case ALWAYS_DARK: {
87 ret = config.AddItem(
88 AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, AppExecFwk::ConfigurationInner::COLOR_MODE_DARK);
89 paramValue.assign(DARK);
90 break;
91 }
92 default:
93 break;
94 }
95 if (!ret) {
96 HILOG_ERROR("AddItem failed, mode = %{public}d", mode);
97 return INVALID_ARG;
98 }
99
100 auto appManagerInstance = GetAppManagerInstance();
101 if (appManagerInstance == nullptr) {
102 HILOG_ERROR("Get app manager proxy failed.");
103 return SYS_ERR;
104 }
105
106 HILOG_INFO("update Configuration start, mode = %{public}d.", mode);
107 auto errcode = appManagerInstance->UpdateConfiguration(config);
108 if (errcode != 0) {
109 auto retVal = appManagerInstance->GetConfiguration(config);
110 if (retVal != 0) {
111 HILOG_ERROR("get configuration failed, update error, error is %{public}d.", retVal);
112 return SYS_ERR;
113 }
114 auto colorMode = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
115 if (colorMode != paramValue) {
116 HILOG_ERROR("update configuration failed, errcode = %{public}d.", errcode);
117 return SYS_ERR;
118 } else {
119 HILOG_WARN("uiappearance is different against configuration. Forced to use the configuration, error is "
120 "%{public}d.", errcode);
121 }
122 }
123 darkMode_ = mode;
124
125 // persist to file: etc/para/ui_appearance.para
126 auto isSetPara = SetParameter(PERSIST_DARKMODE_KEY.c_str(), paramValue.c_str());
127 if (isSetPara < 0) {
128 HILOG_ERROR("set parameter failed");
129 return SYS_ERR;
130 }
131 return SUCCEEDED;
132 }
133
SetDarkMode(DarkMode mode)134 int32_t UiAppearanceAbility::SetDarkMode(DarkMode mode)
135 {
136 // Verify permissions
137 auto isCallingPerm = VerifyAccessToken(PERMISSION_UPDATE_CONFIGURATION);
138 if (!isCallingPerm) {
139 HILOG_ERROR("permission verification failed");
140 return PERMISSION_ERR;
141 }
142 if (mode != darkMode_) {
143 return OnSetDarkMode(mode);
144 } else {
145 HILOG_WARN("current color mode is %{public}d, no need to change!", darkMode_);
146 }
147 return SYS_ERR;
148 }
149
OnGetDarkMode()150 int32_t UiAppearanceAbility::OnGetDarkMode()
151 {
152 constexpr int buffSize = 64; // buff len: 64
153 char valueGet[buffSize] = { 0 };
154
155 // LIGHT is the default.
156 auto res = GetParameter(PERSIST_DARKMODE_KEY.c_str(), LIGHT.c_str(), valueGet, buffSize);
157 if (res <= 0) {
158 HILOG_ERROR("get parameter failed.");
159 return SYS_ERR;
160 }
161 if (strcmp(valueGet, DARK.c_str()) == 0) {
162 HILOG_INFO("current color mode is dark.");
163 return ALWAYS_DARK;
164 } else if (strcmp(valueGet, LIGHT.c_str()) == 0) {
165 HILOG_INFO("current color mode is light.");
166 return ALWAYS_LIGHT;
167 }
168 return SYS_ERR;
169 }
170
GetDarkMode()171 int32_t UiAppearanceAbility::GetDarkMode()
172 {
173 auto isCallingPerm = VerifyAccessToken(PERMISSION_UPDATE_CONFIGURATION);
174 if (!isCallingPerm) {
175 HILOG_ERROR("permission verification failed");
176 return PERMISSION_ERR;
177 }
178 return darkMode_;
179 }
180
OnStart()181 void UiAppearanceAbility::OnStart()
182 {
183 bool res = Publish(this); // SA registers with SAMGR
184 if (!res) {
185 HILOG_ERROR("publish failed.");
186 return;
187 }
188
189 HILOG_INFO("AddSystemAbilityListener start.");
190 AddSystemAbilityListener(APP_MGR_SERVICE_ID);
191 return;
192 }
193
OnStop()194 void UiAppearanceAbility::OnStop()
195 {
196 HILOG_INFO("UiAppearanceAbility SA stop.");
197 }
198
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)199 void UiAppearanceAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
200 {
201 HILOG_INFO("systemAbilityId = %{public}d added.", systemAbilityId);
202 if (systemAbilityId == APP_MGR_SERVICE_ID) {
203 auto res = OnSetDarkMode(static_cast<UiAppearanceAbilityInterface::DarkMode>(OnGetDarkMode()));
204 if (res < 0) {
205 HILOG_ERROR("set darkmode init error.");
206 }
207 }
208 }
209
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)210 void UiAppearanceAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
211 {
212 HILOG_INFO("systemAbilityId = %{public}d removed.", systemAbilityId);
213 }
214 } // namespace ArkUi::UiAppearance
215 } // namespace OHOS