• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     HILOG_INFO("OnSetDarkMode start.");
77     bool ret = false;
78     std::string paramValue;
79     AppExecFwk::Configuration config;
80     switch (mode) {
81         case ALWAYS_LIGHT: {
82             ret = config.AddItem(
83                 AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, AppExecFwk::ConfigurationInner::COLOR_MODE_LIGHT);
84             paramValue.assign(LIGHT);
85             break;
86         }
87         case ALWAYS_DARK: {
88             ret = config.AddItem(
89                 AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, AppExecFwk::ConfigurationInner::COLOR_MODE_DARK);
90             paramValue.assign(DARK);
91             break;
92         }
93         default:
94             break;
95     }
96     if (!ret) {
97         HILOG_ERROR("AddItem failed, mode = %{public}d", mode);
98         return INVALID_ARG;
99     }
100 
101     auto appManagerInstance = GetAppManagerInstance();
102     if (appManagerInstance == nullptr) {
103         HILOG_ERROR("Get app manager proxy failed.");
104         return SYS_ERR;
105     }
106     auto errcode = appManagerInstance->UpdateConfiguration(config);
107     if (errcode != 0) {
108         HILOG_ERROR("update configuration failed.");
109         return SYS_ERR;
110     }
111     darkMode_ = mode;
112 
113     HILOG_INFO("set parameter begin, persist param = %{public}s.", paramValue.c_str());
114     // persist to file: etc/para/ui_appearance.para
115     auto isSetPara = SetParameter(PERSIST_DARKMODE_KEY.c_str(), paramValue.c_str());
116     if (isSetPara < 0) {
117         HILOG_ERROR("set parameter failed");
118         return SYS_ERR;
119     }
120     return SUCCEEDED;
121 }
122 
SetDarkMode(DarkMode mode)123 int32_t UiAppearanceAbility::SetDarkMode(DarkMode mode)
124 {
125     // Verify permissions
126     auto isCallingPerm = VerifyAccessToken(PERMISSION_UPDATE_CONFIGURATION);
127     if (!isCallingPerm) {
128         HILOG_ERROR("permission verification failed");
129         return PERMISSION_ERR;
130     }
131     if (mode != darkMode_) {
132         return OnSetDarkMode(mode);
133     }
134     return SUCCEEDED;
135 }
136 
OnGetDarkMode()137 int32_t UiAppearanceAbility::OnGetDarkMode()
138 {
139     HILOG_INFO("OnGetDarkMode start.");
140     constexpr int buffSize = 64; // buff len: 64
141     char valueGet[buffSize] = { 0 };
142 
143     // LIGHT is the default.
144     auto res = GetParameter(PERSIST_DARKMODE_KEY.c_str(), LIGHT.c_str(), valueGet, buffSize);
145     if (res <= 0) {
146         HILOG_ERROR("get parameter failed.");
147         return SYS_ERR;
148     }
149     if (strcmp(valueGet, DARK.c_str()) == 0) {
150         return ALWAYS_DARK;
151     } else if (strcmp(valueGet, LIGHT.c_str()) == 0) {
152         return ALWAYS_LIGHT;
153     }
154     return SYS_ERR;
155 }
156 
GetDarkMode()157 int32_t UiAppearanceAbility::GetDarkMode()
158 {
159     auto isCallingPerm = VerifyAccessToken(PERMISSION_UPDATE_CONFIGURATION);
160     if (!isCallingPerm) {
161         HILOG_ERROR("permission verification failed");
162         return PERMISSION_ERR;
163     }
164     return darkMode_;
165 }
166 
OnStart()167 void UiAppearanceAbility::OnStart()
168 {
169     HILOG_INFO("publish start.");
170 
171     bool res = Publish(this); // SA registers with SAMGR
172     if (!res) {
173         HILOG_ERROR("publish failed.");
174         return;
175     }
176 
177     HILOG_INFO("AddSystemAbilityListener start.");
178     AddSystemAbilityListener(APP_MGR_SERVICE_ID);
179     return;
180 }
181 
OnStop()182 void UiAppearanceAbility::OnStop()
183 {
184     HILOG_INFO("UiAppearanceAbility SA stop.");
185 }
186 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)187 void UiAppearanceAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
188 {
189     HILOG_INFO("systemAbilityId = %{public}d added.", systemAbilityId);
190     if (systemAbilityId == APP_MGR_SERVICE_ID) {
191         auto res = OnSetDarkMode(static_cast<UiAppearanceAbilityInterface::DarkMode>(OnGetDarkMode()));
192         if (res < 0) {
193             HILOG_ERROR("set darkmode init error.");
194         }
195     }
196 }
197 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)198 void UiAppearanceAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
199 {
200     HILOG_INFO("systemAbilityId = %{public}d removed.", systemAbilityId);
201 }
202 } // namespace ArkUi::UiAppearance
203 } // namespace OHOS