uiAppearance支持多用户配置

Signed-off-by: wlh2624 <1968860844@qq.com>
Change-Id: I17b6e96f230c671613767e433613b0cec31da84a
This commit is contained in:
wlh2624
2024-08-22 11:29:50 +08:00
parent 2f40909ac5
commit be10154573
7 changed files with 431 additions and 192 deletions
+4 -2
View File
@@ -33,7 +33,9 @@
"ipc",
"napi",
"safwk",
"samgr"
"samgr",
"os_account",
"common_event_service"
],
"third_party": []
},
@@ -48,4 +50,4 @@
]
}
}
}
}
+2 -1
View File
@@ -13,4 +13,5 @@
persist.ace.darkmode=light
persist.sys.font_scale_for_user0=1.0
persist.sys.font_wght_scale_for_user0=1.0
persist.sys.font_wght_scale_for_user0=1.0
persist.sys.first_initialization=1
+5 -1
View File
@@ -13,4 +13,8 @@
persist.ace.darkmode = uiserver:servicectrl:0750
persist.sys.font_scale_for_user0 = uiserver:servicectrl:0755
persist.sys.font_wght_scale_for_user0 = uiserver:servicectrl:0755
persist.sys.font_wght_scale_for_user0 = uiserver:servicectrl:0755
persist.ace.darkmode. = uiserver:servicectrl:0700
persist.sys.font_scale_for_user. = uiserver:servicectrl:0700
persist.sys.font_wght_scale_for_user. = uiserver:servicectrl:0700
persist.sys.first_initialization = uiserver:servicectrl:0700
+3
View File
@@ -38,9 +38,12 @@ ohos_shared_library("ui_appearance_service") {
"ability_runtime:app_manager",
"access_token:libaccesstoken_sdk",
"c_utils:utils",
"common_event_service:cesfwk_core",
"common_event_service:cesfwk_innerkits",
"hilog:libhilog",
"init:libbegetutil",
"ipc:ipc_single",
"os_account:os_account_innerkits",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
]
+55 -15
View File
@@ -18,25 +18,44 @@
#include <cstdint>
#include <string>
#include "system_ability.h"
#include "appmgr/app_mgr_proxy.h"
#include "common_event_manager.h"
#include "common_event_support.h"
#include "system_ability.h"
#include "ui_appearance_ability_stub.h"
namespace OHOS {
namespace ArkUi::UiAppearance {
class UserSwitchEventSubscriber : public EventFwk::CommonEventSubscriber {
public:
explicit UserSwitchEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscriberInfo,
const std::function<void(const int32_t)>& userSwitchCallback);
~UserSwitchEventSubscriber() override = default;
void OnReceiveEvent(const EventFwk::CommonEventData& data) override;
private:
std::function<void(const int32_t)> userSwitchCallback_;
};
class UiAppearanceAbility : public SystemAbility, public UiAppearanceAbilityStub {
DECLARE_SYSTEM_ABILITY(UiAppearanceAbility);
public:
struct UiAppearanceParam {
DarkMode darkMode = DarkMode::ALWAYS_LIGHT;
std::string fontScale = "1";
std::string fontWeightScale = "1";
};
UiAppearanceAbility(int32_t saId, bool runOnCreate);
~UiAppearanceAbility() = default;
int32_t SetDarkMode(DarkMode mode) override;
int32_t GetDarkMode() override;
int32_t GetFontScale(std::string &fontScale) override;
int32_t SetFontScale(std::string &fontScale) override;
int32_t GetFontWeightScale(std::string &fontWeightScale) override;
int32_t SetFontWeightScale(std::string &fontWeightScale) override;
int32_t GetFontScale(std::string& fontScale) override;
int32_t SetFontScale(std::string& fontScale) override;
int32_t GetFontWeightScale(std::string& fontWeightScale) override;
int32_t SetFontWeightScale(std::string& fontWeightScale) override;
protected:
void OnStart() override;
@@ -48,17 +67,38 @@ protected:
private:
sptr<AppExecFwk::IAppMgr> GetAppManagerInstance();
bool VerifyAccessToken(const std::string& permissionName);
int32_t OnSetDarkMode(DarkMode mode);
int32_t OnGetDarkMode();
int32_t OnGetFontScale(std::string &fontScale);
int32_t OnSetFontScale(std::string &fontScale);
int32_t OnGetFontWeightScale(std::string &fontWeightScale);
int32_t OnSetFontWeightScale(std::string &fontWeightScale);
void Init();
void SubscribeUserSwitchEvent();
bool IsUserExist(const int32_t userId);
bool UpdateConfiguration(const AppExecFwk::Configuration& configuration, const int32_t userId);
void DoCompatibleProcess();
int32_t GetCallingUserId();
std::vector<int32_t> GetUserIds();
void UserSwitchFunc(const int32_t userId);
void DoInitProcess();
DarkMode darkMode_ = DarkMode::ALWAYS_LIGHT;
std::string fontScale_ = "1";
std::string fontWeightScale_ = "1";
bool GetParameterWrap(const std::string& paramName, std::string& value, const std::string& defaultValue);
bool GetParameterWrap(const std::string& paramName, std::string& value);
bool SetParameterWrap(const std::string& paramName, const std::string& value);
void UpdateCurrentUserConfiguration(const int32_t userId);
int32_t OnSetDarkMode(const int32_t userId, DarkMode mode);
UiAppearanceAbility::DarkMode InitGetDarkMode(const int32_t userId);
int32_t OnSetFontScale(const int32_t userId, std::string& fontScale);
int32_t OnSetFontWeightScale(const int32_t userId, std::string& fontWeightScale);
std::string DarkNodeConfigurationAssignUser(const int32_t userId);
std::string FontScaleConfigurationAssignUser(const int32_t userId);
std::string FontWeightScaleConfigurationAssignUser(const int32_t userId);
std::string DarkModeParamAssignUser(const int32_t userId);
std::string FontScaleParamAssignUser(const int32_t userId);
std::string FontWeightScaleParamAssignUser(const int32_t userId);
std::shared_ptr<UserSwitchEventSubscriber> userSwitchSubscriber_;
std::recursive_mutex usersParamMutex_;
std::map<int32_t, UiAppearanceParam> usersParam_;
std::atomic<bool> isNeedDoCompatibleProcess_ = false;
std::atomic<bool> isInitializationFinished_ = false;
};
} // namespace ArkUi::UiAppearance
} // namespace OHOS
#endif // UI_APPEARANCE_ABILITY_H
#endif // UI_APPEARANCE_ABILITY_H
+360 -171
View File
@@ -18,11 +18,15 @@
#include <string>
#include "accesstoken_kit.h"
#include "common_event_manager.h"
#include "common_event_support.h"
#include "global_configuration_key.h"
#include "ipc_skeleton.h"
#include "iservice_registry.h"
#include "matching_skills.h"
#include "os_account_manager.h"
#include "syspara/parameter.h"
#include "system_ability_definition.h"
#include "global_configuration_key.h"
#include "ui_appearance_log.h"
namespace {
@@ -34,10 +38,35 @@ static const std::string PERMISSION_UPDATE_CONFIGURATION = "ohos.permission.UPDA
// current default accountId = 0, will change when have more user.
static const std::string FONT_SCAL_FOR_USER0 = "persist.sys.font_scale_for_user0";
static const std::string FONT_Weight_SCAL_FOR_USER0 = "persist.sys.font_wght_scale_for_user0";
static const std::string PERSIST_DARKMODE_KEY_FOR_NONE = "persist.ace.darkmode.";
static const std::string FONT_SCAL_FOR_NONE = "persist.sys.font_scale_for_user.";
static const std::string FONT_WEIGHT_SCAL_FOR_NONE = "persist.sys.font_wght_scale_for_user.";
static const std::string FIRST_INITIALIZATION = "persist.sys.first_initialization";
} // namespace
namespace OHOS {
namespace ArkUi::UiAppearance {
UserSwitchEventSubscriber::UserSwitchEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscriberInfo,
const std::function<void(const int32_t)>& userSwitchCallback)
: EventFwk::CommonEventSubscriber(subscriberInfo), userSwitchCallback_(userSwitchCallback)
{}
void UserSwitchEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data)
{
const AAFwk::Want& want = data.GetWant();
std::string action = want.GetAction();
LOGI("action:%{public}s", action.c_str());
if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
if (userSwitchCallback_ != nullptr) {
userSwitchCallback_(data.GetCode());
}
}
}
REGISTER_SYSTEM_ABILITY_BY_ID(UiAppearanceAbility, ARKUI_UI_APPEARANCE_SERVICE_ID, true);
UiAppearanceAbility::UiAppearanceAbility(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate) {}
@@ -76,7 +105,256 @@ bool UiAppearanceAbility::VerifyAccessToken(const std::string& permissionName)
return false;
}
int32_t UiAppearanceAbility::OnSetDarkMode(DarkMode mode)
void UiAppearanceAbility::OnStart()
{
bool res = Publish(this); // SA registers with SAMGR
if (!res) {
LOGE("publish failed.");
return;
}
LOGI("AddSystemAbilityListener start.");
AddSystemAbilityListener(APP_MGR_SERVICE_ID);
return;
}
void UiAppearanceAbility::OnStop()
{
LOGI("UiAppearanceAbility SA stop.");
}
std::vector<int32_t> UiAppearanceAbility::GetUserIds()
{
std::vector<AccountSA::OsAccountInfo> infos;
AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(infos);
std::vector<int32_t> ids;
for (const auto& info : infos) {
ids.push_back(info.GetLocalId());
}
return ids;
}
void UiAppearanceAbility::DoCompatibleProcess()
{
auto getOldParam = [this](const std::string& paramName, std::string& result) {
return GetParameterWrap(paramName, result);
};
const std::vector<int32_t> userIds = GetUserIds();
std::string darkMode = LIGHT;
if (getOldParam(PERSIST_DARKMODE_KEY, darkMode)) {
for (auto id : userIds) {
SetParameterWrap(DarkModeParamAssignUser(id), darkMode);
}
LOGD(" set darkMode %{public}s", darkMode.c_str());
}
std::string fontSize = BASE_SCALE;
if (getOldParam(FONT_SCAL_FOR_USER0, fontSize)) {
for (auto id : userIds) {
SetParameterWrap(FontScaleParamAssignUser(id), fontSize);
}
LOGD(" set fontSize %{public}s", fontSize.c_str());
}
std::string fontWeightSize = BASE_SCALE;
if (getOldParam(FONT_Weight_SCAL_FOR_USER0, fontWeightSize)) {
for (auto id : userIds) {
SetParameterWrap(FontWeightScaleParamAssignUser(id), fontWeightSize);
}
LOGD(" set fontWeightSize %{public}s", fontWeightSize.c_str());
}
SetParameterWrap(FIRST_INITIALIZATION, "0");
isNeedDoCompatibleProcess_ = false;
}
void UiAppearanceAbility::DoInitProcess()
{
const std::vector<int32_t> userIds = GetUserIds();
std::unique_lock<std::recursive_mutex> guard(usersParamMutex_);
for (auto userId : userIds) {
std::string darkValue = LIGHT;
GetParameterWrap(DarkModeParamAssignUser(userId), darkValue);
std::string fontSize = BASE_SCALE;
GetParameterWrap(FontScaleParamAssignUser(userId), fontSize);
std::string fontWeight = BASE_SCALE;
GetParameterWrap(FontWeightScaleParamAssignUser(userId), fontWeight);
UiAppearanceParam tmpParam;
tmpParam.darkMode = darkValue == DARK ? DarkMode::ALWAYS_DARK : DarkMode::ALWAYS_LIGHT;
tmpParam.fontScale = fontSize;
tmpParam.fontWeightScale = fontWeight;
usersParam_[userId] = tmpParam;
}
isInitializationFinished_ = true;
}
void UiAppearanceAbility::UpdateCurrentUserConfiguration(const int32_t userId)
{
UiAppearanceParam tmpParam = usersParam_[userId];
AppExecFwk::Configuration config;
config.AddItem(
AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, tmpParam.darkMode == DarkMode::ALWAYS_DARK ? DARK : LIGHT);
config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE, tmpParam.fontScale);
config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_WEIGHT_SCALE, tmpParam.fontWeightScale);
auto appManagerInstance = GetAppManagerInstance();
if (appManagerInstance != nullptr) {
appManagerInstance->UpdateConfiguration(config, 0);
SetParameterWrap(PERSIST_DARKMODE_KEY, tmpParam.darkMode == DarkMode::ALWAYS_DARK ? DARK : LIGHT);
SetParameterWrap(FONT_SCAL_FOR_USER0, tmpParam.fontScale);
SetParameterWrap(FONT_Weight_SCAL_FOR_USER0, tmpParam.fontWeightScale);
LOGD("user switch update config %{public}s", config.GetName().c_str());
}
}
void UiAppearanceAbility::UserSwitchFunc(const int32_t userId)
{
if (isNeedDoCompatibleProcess_) {
DoCompatibleProcess();
}
if (!isInitializationFinished_) {
DoInitProcess();
}
UpdateCurrentUserConfiguration(userId);
}
void UiAppearanceAbility::SubscribeUserSwitchEvent()
{
EventFwk::MatchingSkills matchingSkills;
matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
subscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
userSwitchSubscriber_ = std::make_shared<UserSwitchEventSubscriber>(
subscribeInfo, [this](const int32_t userId) { UserSwitchFunc(userId); });
bool subResult = EventFwk::CommonEventManager::SubscribeCommonEvent(userSwitchSubscriber_);
if (!subResult) {
LOGW("subscribe user switch event error");
}
}
void UiAppearanceAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
{
if (systemAbilityId != APP_MGR_SERVICE_ID) {
return;
}
auto checkIfFirstUpgrade = [this]() {
std::string initFlag = "0";
GetParameterWrap(FIRST_INITIALIZATION, initFlag);
if (initFlag == "1") {
return true;
}
return false;
};
isNeedDoCompatibleProcess_ = checkIfFirstUpgrade();
SubscribeUserSwitchEvent();
if (isNeedDoCompatibleProcess_ && !GetUserIds().empty()) {
DoCompatibleProcess();
}
if (!isInitializationFinished_ && !GetUserIds().empty()) {
DoInitProcess();
AccountSA::OsAccountInfo info;
AccountSA::OsAccountManager::QueryCurrentOsAccount(info);
UpdateCurrentUserConfiguration(info.GetLocalId());
}
}
void UiAppearanceAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
{
LOGI("systemAbilityId = %{public}d removed.", systemAbilityId);
}
int32_t UiAppearanceAbility::GetCallingUserId()
{
const static int32_t UID_TRANSFORM_DIVISOR = 200000;
return OHOS::IPCSkeleton::GetCallingUid() / UID_TRANSFORM_DIVISOR;
}
std::string UiAppearanceAbility::DarkModeParamAssignUser(const int32_t userId)
{
return PERSIST_DARKMODE_KEY_FOR_NONE + std::to_string(userId);
}
std::string UiAppearanceAbility::FontScaleParamAssignUser(const int32_t userId)
{
return FONT_SCAL_FOR_NONE + std::to_string(userId);
}
std::string UiAppearanceAbility::FontWeightScaleParamAssignUser(const int32_t userId)
{
return FONT_WEIGHT_SCAL_FOR_NONE + std::to_string(userId);
}
bool UiAppearanceAbility::IsUserExist(const int32_t userId)
{
std::unique_lock<std::recursive_mutex> guard(usersParamMutex_);
return usersParam_.find(userId) != usersParam_.end();
}
bool UiAppearanceAbility::GetParameterWrap(
const std::string& paramName, std::string& value, const std::string& defaultValue)
{
char buf[256] = { 0 };
auto res = GetParameter(paramName.c_str(), defaultValue.c_str(), buf, sizeof(buf));
if (res <= 0) {
LOGE("get parameter %{public}s failed", paramName.c_str());
return false;
}
LOGD("get parameter %{public}s:%{public}s", paramName.c_str(), value.c_str());
value = buf;
return true;
}
bool UiAppearanceAbility::GetParameterWrap(const std::string& paramName, std::string& value)
{
const auto defaultValue = value;
return GetParameterWrap(paramName, value, defaultValue);
}
bool UiAppearanceAbility::SetParameterWrap(const std::string& paramName, const std::string& value)
{
auto res = SetParameter(paramName.c_str(), value.c_str());
if (res != 0) {
LOGE("set parameter %{public}s failed", paramName.c_str());
return false;
}
LOGD("set parameter %{public}s:%{public}s", paramName.c_str(), value.c_str());
return true;
}
bool UiAppearanceAbility::UpdateConfiguration(const AppExecFwk::Configuration& configuration, const int32_t userId)
{
auto appManagerInstance = GetAppManagerInstance();
if (appManagerInstance == nullptr) {
LOGE("Get app manager proxy failed.");
return false;
}
LOGI("update Configuration start,userId:%{public}d config = %{public}s.", userId, configuration.GetName().c_str());
auto errcode = appManagerInstance->UpdateConfiguration(configuration, userId);
if (errcode != 0) {
AppExecFwk::Configuration config;
auto retVal = appManagerInstance->GetConfiguration(config);
if (retVal != 0) {
LOGE("get configuration failed, update error, error is %{public}d.", retVal);
return false;
}
std::vector<std::string> diffVe;
config.CompareDifferent(diffVe, configuration);
if (!diffVe.empty()) {
LOGE("update configuration failed, errcode = %{public}d.", errcode);
return false;
} else {
LOGW("uiappearance is different against configuration. Forced to use the configuration, error is "
"%{public}d.",
errcode);
}
}
return true;
}
int32_t UiAppearanceAbility::OnSetDarkMode(const int32_t userId, DarkMode mode)
{
bool ret = false;
std::string paramValue;
@@ -102,34 +380,24 @@ int32_t UiAppearanceAbility::OnSetDarkMode(DarkMode mode)
return INVALID_ARG;
}
auto appManagerInstance = GetAppManagerInstance();
if (appManagerInstance == nullptr) {
LOGE("Get app manager proxy failed.");
if (!UpdateConfiguration(config, userId)) {
return SYS_ERR;
}
LOGI("update Configuration start, mode = %{public}d.", mode);
auto errcode = appManagerInstance->UpdateConfiguration(config);
if (errcode != 0) {
auto retVal = appManagerInstance->GetConfiguration(config);
if (retVal != 0) {
LOGE("get configuration failed, update error, error is %{public}d.", retVal);
return SYS_ERR;
}
auto colorMode = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
if (colorMode != paramValue) {
LOGE("update configuration failed, errcode = %{public}d.", errcode);
return SYS_ERR;
{
std::unique_lock<std::recursive_mutex> guard(usersParamMutex_);
if (IsUserExist(userId)) {
usersParam_[userId].darkMode = mode;
} else {
LOGW("uiappearance is different against configuration. Forced to use the configuration, error is "
"%{public}d.", errcode);
UiAppearanceParam tmpParam;
tmpParam.darkMode = mode;
usersParam_[userId] = tmpParam;
}
}
darkMode_ = mode;
SetParameterWrap(PERSIST_DARKMODE_KEY, paramValue);
// persist to file: etc/para/ui_appearance.para
auto isSetPara = SetParameter(PERSIST_DARKMODE_KEY.c_str(), paramValue.c_str());
if (isSetPara < 0) {
auto isSetPara = SetParameterWrap(DarkModeParamAssignUser(userId), paramValue);
if (!isSetPara) {
LOGE("set parameter failed");
return SYS_ERR;
}
@@ -144,33 +412,38 @@ int32_t UiAppearanceAbility::SetDarkMode(DarkMode mode)
LOGE("permission verification failed");
return PERMISSION_ERR;
}
if (mode != darkMode_) {
return OnSetDarkMode(mode);
std::unique_lock<std::recursive_mutex> guard(usersParamMutex_);
auto it = usersParam_.find(GetCallingUserId());
if (it != usersParam_.end()) {
if (mode != it->second.darkMode) {
return OnSetDarkMode(GetCallingUserId(), mode);
} else {
LOGW("current color mode is %{public}d, no need to change", mode);
}
} else {
LOGW("current color mode is %{public}d, no need to change!", darkMode_);
return OnSetDarkMode(GetCallingUserId(), mode);
}
return SYS_ERR;
}
int32_t UiAppearanceAbility::OnGetDarkMode()
UiAppearanceAbility::DarkMode UiAppearanceAbility::InitGetDarkMode(const int32_t userId)
{
constexpr int buffSize = 64; // buff len: 64
char valueGet[buffSize] = { 0 };
std::string valueGet = LIGHT;
// LIGHT is the default.
auto res = GetParameter(PERSIST_DARKMODE_KEY.c_str(), LIGHT.c_str(), valueGet, buffSize);
if (res <= 0) {
LOGE("get parameter failed.");
return SYS_ERR;
auto res = GetParameterWrap(DarkModeParamAssignUser(userId), valueGet);
if (!res) {
return ALWAYS_LIGHT;
}
if (strcmp(valueGet, DARK.c_str()) == 0) {
if (valueGet == DARK) {
LOGI("current color mode is dark.");
return ALWAYS_DARK;
} else if (strcmp(valueGet, LIGHT.c_str()) == 0) {
} else if (valueGet == LIGHT) {
LOGI("current color mode is light.");
return ALWAYS_LIGHT;
}
return SYS_ERR;
return ALWAYS_LIGHT;
}
int32_t UiAppearanceAbility::GetDarkMode()
@@ -180,10 +453,15 @@ int32_t UiAppearanceAbility::GetDarkMode()
LOGE("permission verification failed");
return PERMISSION_ERR;
}
return darkMode_;
std::unique_lock<std::recursive_mutex> guard(usersParamMutex_);
auto it = usersParam_.find(GetCallingUserId());
if (it != usersParam_.end()) {
return it->second.darkMode;
}
return DarkMode::ALWAYS_LIGHT;
}
int32_t UiAppearanceAbility::OnSetFontScale(std::string &fontScale)
int32_t UiAppearanceAbility::OnSetFontScale(const int32_t userId, std::string& fontScale)
{
bool ret = false;
AppExecFwk::Configuration config;
@@ -192,56 +470,30 @@ int32_t UiAppearanceAbility::OnSetFontScale(std::string &fontScale)
LOGE("AddItem failed, fontScale = %{public}s", fontScale.c_str());
return INVALID_ARG;
}
auto appManagerInstance = GetAppManagerInstance();
if (appManagerInstance == nullptr) {
LOGE("Get app manager proxy failed.");
if (!UpdateConfiguration(config, userId)) {
return SYS_ERR;
}
LOGI("update Configuration start, fontScale = %{public}s.", fontScale.c_str());
auto errcode = appManagerInstance->UpdateConfiguration(config);
if (errcode != 0) {
auto retVal = appManagerInstance->GetConfiguration(config);
if (retVal != 0) {
LOGE("get configuration failed, update error, error is %{public}d.", retVal);
return SYS_ERR;
}
auto currentFontScale = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE);
if (currentFontScale != fontScale) {
LOGE("update configuration failed, errcode = %{public}d.", errcode);
return SYS_ERR;
{
std::unique_lock<std::recursive_mutex> guard(usersParamMutex_);
if (IsUserExist(userId)) {
usersParam_[userId].fontScale = fontScale;
} else {
LOGW("uiappearance is different against configuration. Forced to use the configuration, error is "
"%{public}d.", errcode);
UiAppearanceParam tmpParam;
tmpParam.fontScale = fontScale;
usersParam_[userId] = tmpParam;
}
}
fontScale_ = fontScale;
SetParameterWrap(FONT_SCAL_FOR_USER0, fontScale);
// persist to file: etc/para/ui_appearance.para
auto isSetPara = SetParameter(FONT_SCAL_FOR_USER0.c_str(), fontScale.c_str());
if (isSetPara < 0) {
auto isSetPara = SetParameterWrap(FontScaleParamAssignUser(userId), fontScale);
if (!isSetPara) {
LOGE("set parameter failed");
return SYS_ERR;
}
return SUCCEEDED;
}
int32_t UiAppearanceAbility::OnGetFontScale(std::string &fontScale)
{
constexpr int buffSize = 64; // buff len: 64
char valueGet[buffSize] = { 0 };
auto res = GetParameter(FONT_SCAL_FOR_USER0.c_str(), BASE_SCALE.c_str(), valueGet, buffSize);
if (res <= 0) {
LOGE("get parameter failed.");
return SYS_ERR;
}
fontScale = valueGet;
return SUCCEEDED;
}
int32_t UiAppearanceAbility::SetFontScale(std::string& fontScale)
{
// Verify permissions
@@ -251,25 +503,32 @@ int32_t UiAppearanceAbility::SetFontScale(std::string& fontScale)
return PERMISSION_ERR;
}
if (!fontScale.empty()) {
return OnSetFontScale(fontScale);
return OnSetFontScale(GetCallingUserId(), fontScale);
} else {
LOGE("current fontScale is empty!");
}
return SYS_ERR;
}
int32_t UiAppearanceAbility::GetFontScale(std::string &fontScale)
int32_t UiAppearanceAbility::GetFontScale(std::string& fontScale)
{
auto isCallingPerm = VerifyAccessToken(PERMISSION_UPDATE_CONFIGURATION);
if (!isCallingPerm) {
LOGE("permission verification failed");
return PERMISSION_ERR;
}
fontScale = fontScale_;
std::unique_lock<std::recursive_mutex> guard(usersParamMutex_);
auto it = usersParam_.find(GetCallingUserId());
if (it != usersParam_.end()) {
fontScale = it->second.fontScale;
} else {
fontScale = BASE_SCALE;
}
LOGD("get font scale :%{public}s", fontScale.c_str());
return SUCCEEDED;
}
int32_t UiAppearanceAbility::OnSetFontWeightScale(std::string &fontWeightScale)
int32_t UiAppearanceAbility::OnSetFontWeightScale(const int32_t userId, std::string& fontWeightScale)
{
bool ret = false;
AppExecFwk::Configuration config;
@@ -279,55 +538,31 @@ int32_t UiAppearanceAbility::OnSetFontWeightScale(std::string &fontWeightScale)
return INVALID_ARG;
}
auto appManagerInstance = GetAppManagerInstance();
if (appManagerInstance == nullptr) {
LOGE("Get app manager proxy failed.");
if (!UpdateConfiguration(config, userId)) {
return SYS_ERR;
}
LOGI("update Configuration start, fontWeightScale = %{public}s.", fontWeightScale.c_str());
auto errcode = appManagerInstance->UpdateConfiguration(config);
if (errcode != 0) {
auto retVal = appManagerInstance->GetConfiguration(config);
if (retVal != 0) {
LOGE("get configuration failed, update error, error is %{public}d.", retVal);
return SYS_ERR;
}
auto currentFontScale = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_WEIGHT_SCALE);
if (currentFontScale != fontWeightScale) {
LOGE("update configuration failed, errcode = %{public}d.", errcode);
return SYS_ERR;
{
std::unique_lock<std::recursive_mutex> guard(usersParamMutex_);
if (IsUserExist(userId)) {
usersParam_[userId].fontWeightScale = fontWeightScale;
} else {
LOGW("uiappearance is different against configuration. Forced to use the configuration, error is "
"%{public}d.", errcode);
UiAppearanceParam tmpParam;
tmpParam.fontWeightScale = fontWeightScale;
usersParam_[userId] = tmpParam;
}
}
fontWeightScale_ = fontWeightScale;
SetParameterWrap(FONT_Weight_SCAL_FOR_USER0, fontWeightScale);
// persist to file: etc/para/ui_appearance.para
auto isSetPara = SetParameter(FONT_Weight_SCAL_FOR_USER0.c_str(), fontWeightScale.c_str());
if (isSetPara < 0) {
auto isSetPara = SetParameterWrap(FontWeightScaleParamAssignUser(userId), fontWeightScale);
if (!isSetPara) {
LOGE("set parameter failed");
return SYS_ERR;
}
return SUCCEEDED;
}
int32_t UiAppearanceAbility::OnGetFontWeightScale(std::string &fontWeightScale)
{
constexpr int buffSize = 64; // buff len: 64
char valueGet[buffSize] = { 0 };
auto res = GetParameter(FONT_Weight_SCAL_FOR_USER0.c_str(), BASE_SCALE.c_str(), valueGet, buffSize);
if (res <= 0) {
LOGE("get parameter failed.");
return SYS_ERR;
}
fontWeightScale = valueGet;
return SUCCEEDED;
}
int32_t UiAppearanceAbility::SetFontWeightScale(std::string& fontWeightScale)
{
// Verify permissions
@@ -337,76 +572,30 @@ int32_t UiAppearanceAbility::SetFontWeightScale(std::string& fontWeightScale)
return PERMISSION_ERR;
}
if (!fontWeightScale.empty()) {
return OnSetFontWeightScale(fontWeightScale);
return OnSetFontWeightScale(GetCallingUserId(), fontWeightScale);
} else {
LOGE("current fontWeightScale is empty!");
}
return SYS_ERR;
}
int32_t UiAppearanceAbility::GetFontWeightScale(std::string &fontWeightScale)
int32_t UiAppearanceAbility::GetFontWeightScale(std::string& fontWeightScale)
{
auto isCallingPerm = VerifyAccessToken(PERMISSION_UPDATE_CONFIGURATION);
if (!isCallingPerm) {
LOGE("permission verification failed");
return PERMISSION_ERR;
}
fontWeightScale = fontWeightScale_;
std::unique_lock<std::recursive_mutex> guard(usersParamMutex_);
auto it = usersParam_.find(GetCallingUserId());
if (it != usersParam_.end()) {
fontWeightScale = it->second.fontWeightScale;
} else {
fontWeightScale = BASE_SCALE;
}
LOGD("get font weight scale :%{public}s", fontWeightScale.c_str());
return SUCCEEDED;
}
void UiAppearanceAbility::OnStart()
{
bool res = Publish(this); // SA registers with SAMGR
if (!res) {
LOGE("publish failed.");
return;
}
LOGI("AddSystemAbilityListener start.");
AddSystemAbilityListener(APP_MGR_SERVICE_ID);
return;
}
void UiAppearanceAbility::OnStop()
{
LOGI("UiAppearanceAbility SA stop.");
}
void UiAppearanceAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
{
LOGI("systemAbilityId = %{public}d added.", systemAbilityId);
if (systemAbilityId == APP_MGR_SERVICE_ID) {
auto res = OnSetDarkMode(static_cast<UiAppearanceAbilityInterface::DarkMode>(OnGetDarkMode()));
if (res < 0) {
LOGE("set darkmode init error.");
}
std::string fontScale;
res = OnGetFontScale(fontScale);
if (res < 0) {
LOGE("get font init error.");
fontScale = BASE_SCALE;
}
res = OnSetFontScale(fontScale);
if (res < 0) {
LOGE("set Font init error.");
}
std::string fontWeightScale;
res = OnGetFontWeightScale(fontWeightScale);
if (res < 0) {
LOGE("get font init error.");
fontScale = BASE_SCALE;
}
res = OnSetFontWeightScale(fontWeightScale);
if (res < 0) {
LOGE("set Font init error.");
}
}
}
void UiAppearanceAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
{
LOGI("systemAbilityId = %{public}d removed.", systemAbilityId);
}
} // namespace ArkUi::UiAppearance
} // namespace OHOS
+2 -2
View File
@@ -17,9 +17,9 @@
namespace OHOS {
namespace AppExecFwk {
int32_t AppMgrProxy::UpdateConfiguration(const Configuration& config)
int32_t AppMgrProxy::UpdateConfiguration(const Configuration& config, const int32_t userId)
{
return 0;
}
} // namespace AppExecFwk
} // namespace OHOS
} // namespace OHOS