i18n监听事件失效问题修复

Signed-off-by: LY <liuyong235@huawei.com>
This commit is contained in:
LY 2024-08-22 16:29:20 +08:00
parent cf04385e70
commit b5d6dbf110
3 changed files with 45 additions and 96 deletions

View File

@ -51,7 +51,7 @@ void MultiUsers::InitMultiUser()
AddUser(localId);
preferences->PutBool(INIT_KEY, true);
preferences->Flush();
HILOG_ERROR_I18N("InitMultiUser: init multi user data success");
HILOG_INFO_I18N("InitMultiUser: init multi user data success");
}
}
@ -70,7 +70,7 @@ void MultiUsers::SwitchUser(const std::string& curLocalId)
void MultiUsers::AddUser(const std::string& localId)
{
if (!IsValidLocalId(localId)) {
HILOG_ERROR_I18N("AddUser: %{public}s is invalid local id", localId.c_str());
HILOG_ERROR_I18N("AddUser: localId is invalid");
return;
}
I18nErrorCode errCode = SaveGlobalParam(localId);
@ -82,7 +82,7 @@ void MultiUsers::AddUser(const std::string& localId)
void MultiUsers::RemoveUser(const std::string& localId)
{
if (!IsValidLocalId(localId)) {
HILOG_ERROR_I18N("RemoveUser: %{public}s is invalid local id", localId.c_str());
HILOG_ERROR_I18N("RemoveUser: localId is invalid");
return;
}
I18nErrorCode errCode = RemoveGlobalParam(localId);
@ -118,7 +118,7 @@ I18nErrorCode MultiUsers::SaveLanguage(const std::string& localId, const std::st
errCode =
WriteMultiUsersParameter(MULTI_USERS_LANGUAGE_KEY, language, foregroundLocalId, false);
if (errCode != I18nErrorCode::SUCCESS) {
HILOG_ERROR_I18N("SaveLanguage: save language of user %{public}s failed", foregroundLocalId.c_str());
HILOG_ERROR_I18N("SaveLanguage: save language failed");
return I18nErrorCode::FAILED;
}
return I18nErrorCode::SUCCESS;
@ -139,7 +139,7 @@ I18nErrorCode MultiUsers::SaveLocale(const std::string& localId, const std::stri
errCode =
WriteMultiUsersParameter(MULTI_USERS_LOCALE_KEY, locale, foregroundLocalId, false);
if (errCode != I18nErrorCode::SUCCESS) {
HILOG_ERROR_I18N("SaveLocale: save locale of user %{public}s failed", foregroundLocalId.c_str());
HILOG_ERROR_I18N("SaveLocale: save locale failed");
return I18nErrorCode::FAILED;
}
return I18nErrorCode::SUCCESS;
@ -147,23 +147,19 @@ I18nErrorCode MultiUsers::SaveLocale(const std::string& localId, const std::stri
I18nErrorCode MultiUsers::SaveIs24Hour(const std::string& localId, const std::string& is24Hour)
{
HILOG_ERROR_I18N("TestLog: SaveIs24Hour start");
std::string foregroundLocalId = localId;
I18nErrorCode errCode = I18nErrorCode::SUCCESS;
if (localId.empty()) {
HILOG_ERROR_I18N("TestLog: localId");
errCode = MultiUsers::GetForegroundLocalId(foregroundLocalId);
}
if (errCode != I18nErrorCode::SUCCESS) {
HILOG_ERROR_I18N("TestLog: get foreground locale Id failed");
HILOG_ERROR_I18N("SaveLanguage: get foreground locale Id failed");
return I18nErrorCode::FAILED;
}
HILOG_ERROR_I18N("TestLog: foregroundLocalId is %{public}s", foregroundLocalId.c_str());
errCode =
WriteMultiUsersParameter(MULTI_USERS_HOUR_KEY, is24Hour, foregroundLocalId, false);
if (errCode != I18nErrorCode::SUCCESS) {
HILOG_ERROR_I18N("SaveIs24Hour: save is24Hour of user %{public}s failed", foregroundLocalId.c_str());
HILOG_ERROR_I18N("SaveIs24Hour: save is24Hour failed");
return I18nErrorCode::FAILED;
}
return I18nErrorCode::SUCCESS;
@ -176,17 +172,17 @@ I18nErrorCode MultiUsers::SaveGlobalParam(const std::string& localId)
std::string is24Hour = ReadSystemParameter(LocaleConfig::GetHourKey().data(), CONFIG_LEN);
I18nErrorCode errCode = SaveLanguage(localId, language);
if (errCode != I18nErrorCode::SUCCESS) {
HILOG_ERROR_I18N("SaveGlobalParam: save language of user %{public}s failed", localId.c_str());
HILOG_ERROR_I18N("SaveGlobalParam: save language failed");
return I18nErrorCode::FAILED;
}
errCode = SaveLocale(localId, locale);
if (errCode != I18nErrorCode::SUCCESS) {
HILOG_ERROR_I18N("SaveGlobalParam: save locale of user %{public}s failed", localId.c_str());
HILOG_ERROR_I18N("SaveGlobalParam: save locale failed");
return I18nErrorCode::FAILED;
}
errCode = SaveIs24Hour(localId, is24Hour);
if (errCode != I18nErrorCode::SUCCESS) {
HILOG_ERROR_I18N("SaveGlobalParam: save is24Hour of user %{public}s failed", localId.c_str());
HILOG_ERROR_I18N("SaveGlobalParam: save is24Hour failed");
return I18nErrorCode::FAILED;
}
return I18nErrorCode::SUCCESS;
@ -196,19 +192,19 @@ I18nErrorCode MultiUsers::LoadGlobalParam(const std::string& localId)
{
std::string newLocale = ReadMultiUsersParameter(MULTI_USERS_LOCALE_KEY, localId);
if (!newLocale.empty() && SetParameter(LocaleConfig::GetLocaleKey().data(), newLocale.data()) != 0) {
HILOG_ERROR_I18N("LoadGlobalParam: set locale %{public}s failed", newLocale.c_str());
HILOG_ERROR_I18N("LoadGlobalParam: set locale failed");
return I18nErrorCode::FAILED;
}
std::string newLanguage = ReadMultiUsersParameter(MULTI_USERS_LANGUAGE_KEY, localId);
if (!newLanguage.empty() && LocaleConfig::SetSystemLanguage(newLanguage) != 0) {
HILOG_ERROR_I18N("LoadGlobalParam: set language %{public}s failed", newLanguage.c_str());
HILOG_ERROR_I18N("LoadGlobalParam: set language failed");
return I18nErrorCode::FAILED;
}
std::string newIs24Hour = ReadMultiUsersParameter(MULTI_USERS_HOUR_KEY, localId);
if (!newIs24Hour.empty() && LocaleConfig::Set24HourClock(newIs24Hour) != 0) {
HILOG_ERROR_I18N("LoadGlobalParam: set is24Hour %{public}s failed", newIs24Hour.c_str());
HILOG_ERROR_I18N("LoadGlobalParam: set is24Hour failed");
return I18nErrorCode::FAILED;
}
return I18nErrorCode::SUCCESS;
@ -218,19 +214,19 @@ I18nErrorCode MultiUsers::RemoveGlobalParam(const std::string& localId)
{
I18nErrorCode errCode = WriteMultiUsersParameter(MULTI_USERS_LANGUAGE_KEY, "", localId, true);
if (errCode != I18nErrorCode::SUCCESS) {
HILOG_ERROR_I18N("RemoveGlobalParam: remove language of user %{public}s failed", localId.c_str());
HILOG_ERROR_I18N("RemoveGlobalParam: remove language failed");
return I18nErrorCode::FAILED;
}
errCode = WriteMultiUsersParameter(MULTI_USERS_LOCALE_KEY, "", localId, true);
if (errCode != I18nErrorCode::SUCCESS) {
HILOG_ERROR_I18N("RemoveGlobalParam: remove locale of user %{public}s failed", localId.c_str());
HILOG_ERROR_I18N("RemoveGlobalParam: remove locale failed");
return I18nErrorCode::FAILED;
}
errCode = WriteMultiUsersParameter(MULTI_USERS_HOUR_KEY, "", localId, true);
if (errCode != I18nErrorCode::SUCCESS) {
HILOG_ERROR_I18N("RemoveGlobalParam: remove is24Hour of user %{public}s failed", localId.c_str());
HILOG_ERROR_I18N("RemoveGlobalParam: remove is24Hour failed");
return I18nErrorCode::FAILED;
}
return I18nErrorCode::SUCCESS;
@ -299,7 +295,7 @@ I18nErrorCode MultiUsers::WriteMultiUsersParameter(const std::string& paramKey,
bool MultiUsers::IsValidLocalId(const std::string& localId)
{
if (std::atoi(localId.c_str()) < DEFAULT_LOCAL_ID) {
HILOG_ERROR_I18N("IsValidLocalId: %{public}s is an invalid local ID", localId.c_str());
HILOG_ERROR_I18N("IsValidLocalId: invalid local ID");
return false;
}
return true;
@ -308,7 +304,7 @@ bool MultiUsers::IsValidLocalId(const std::string& localId)
void MultiUsers::InitPreferences()
{
if (preferences == nullptr) {
HILOG_ERROR_I18N("InitPreferences: preferences Init");
HILOG_INFO_I18N("InitPreferences: preferences Init");
OHOS::NativePreferences::Options opt(PREFERENCE_PATH);
int status;
preferences = NativePreferences::PreferencesHelper::GetPreferences(opt, status);

View File

@ -35,6 +35,7 @@ public:
void CheckStartReason(const SystemAbilityOnDemandReason& startReason);
void SubscriberEvent();
void OnReceiveEvent(const EventFwk::CommonEventData& data);
private:
class I18nServiceSubscriber : public EventFwk::CommonEventSubscriber {
@ -54,21 +55,9 @@ private:
I18nServiceEvent& registry_;
};
void UnSubscriberEvent();
void OnReceiveEvent(const EventFwk::CommonEventData& data);
void HandleSwitchUser(const EventFwk::CommonEventData& data) const;
void HandleAddUser(const EventFwk::CommonEventData& data) const;
void HandleRemoveUser(const EventFwk::CommonEventData& data) const;
void HandleMultiUser(const std::string& action, const std::string& localId);
using EventHandle = std::function<void(const EventFwk::CommonEventData&)>;
typedef void (I18nServiceEvent::*HandleEventFunc)(const EventFwk::CommonEventData&) const;
std::unordered_map<std::string, EventHandle> eventHandles;
std::unordered_map<std::string, HandleEventFunc> handleEventFunc;
std::shared_ptr<I18nServiceSubscriber> subscriber = nullptr;
std::string userSwitchKey;
std::string userAdded;
std::string userRemoved;
static const int RETRY;
};
} // namespace I18n

View File

@ -29,22 +29,18 @@ const int I18nServiceEvent::RETRY = 3;
I18nServiceEvent::I18nServiceEvent()
{
userSwitchKey = "usual.event.USER_SWITCHED";
userAdded = "usual.event.USER_ADDED";
userRemoved = "usual.event.USER_REMOVED";
handleEventFunc.emplace(userSwitchKey, &I18nServiceEvent::HandleSwitchUser);
handleEventFunc.emplace(userAdded, &I18nServiceEvent::HandleAddUser);
handleEventFunc.emplace(userRemoved, &I18nServiceEvent::HandleRemoveUser);
for (auto& it : I18nServiceEvent::handleEventFunc) {
eventHandles.emplace(it.first, std::bind(it.second, this, std::placeholders::_1));
}
HILOG_INFO_I18N("I18nServiceEvent: start.");
}
I18nServiceEvent::~I18nServiceEvent()
{
eventHandles.clear();
handleEventFunc.clear();
UnSubscriberEvent();
if (subscriber) {
bool status = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber);
if (!status) {
HILOG_ERROR_I18N("UnSubscriberEvent: UnSubscriberEvent failed");
}
subscriber = nullptr;
}
}
void I18nServiceEvent::SubscriberEvent()
@ -53,83 +49,51 @@ void I18nServiceEvent::SubscriberEvent()
return;
}
EventFwk::MatchingSkills matchingSkills;
for (auto& event : handleEventFunc) {
matchingSkills.AddEvent(event.first);
}
matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED);
matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
subscriber = std::make_shared<I18nServiceSubscriber>(subscribeInfo, *this);
int32_t retry = 3;
int32_t retry = RETRY;
do {
bool status = EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
if (status) {
HILOG_INFO_I18N("SubscriberEvent: SubscriberEvent success.");
return;
}
HILOG_INFO_I18N("SubscriberEvent: SubscriberEvent failed %{public}d.", retry);
HILOG_ERROR_I18N("SubscriberEvent: SubscriberEvent failed %{public}d.", retry);
retry--;
sleep(1);
} while (retry);
HILOG_INFO_I18N("SubscriberEvent: SubscriberEvent failed");
}
void I18nServiceEvent::UnSubscriberEvent()
{
if (subscriber) {
bool status = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber);
if (status != 0) {
HILOG_INFO_I18N("UnSubscriberEvent: UnSubscriberEvent failed");
}
subscriber = nullptr;
}
}
void I18nServiceEvent::CheckStartReason(const SystemAbilityOnDemandReason& startReason)
{
std::string action = startReason.GetName();
std::string localId = std::to_string(startReason.GetExtraData().GetCode());
HILOG_INFO_I18N("CheckStartReason: start reason is %{public}s", action.c_str());
if (action == userSwitchKey) {
std::string localId = std::to_string(startReason.GetExtraData().GetCode());
MultiUsers::SwitchUser(localId);
} else if (action == userAdded) {
std::string localId = std::to_string(startReason.GetExtraData().GetCode());
MultiUsers::AddUser(localId);
} else if (action == userRemoved) {
std::string localId = std::to_string(startReason.GetExtraData().GetCode());
MultiUsers::RemoveUser(localId);
}
HandleMultiUser(action, localId);
}
void I18nServiceEvent::OnReceiveEvent(const EventFwk::CommonEventData& data)
{
AAFwk::Want want = data.GetWant();
std::string action = want.GetAction();
auto it = eventHandles.find(action);
if (it == eventHandles.end()) {
HILOG_INFO_I18N("OnReceiveEvent: ignore event %{public}s", action.c_str());
return;
}
std::string action = data.GetWant().GetAction();
std::string localId = std::to_string(data.GetCode());
HILOG_INFO_I18N("OnReceiveEvent: handle event %{public}s", action.c_str());
it->second(data);
HandleMultiUser(action, localId);
}
void I18nServiceEvent::HandleSwitchUser(const EventFwk::CommonEventData& data) const
void I18nServiceEvent::HandleMultiUser(const std::string& action, const std::string& localId)
{
AAFwk::Want want = data.GetWant();
std::string localId = std::to_string(data.GetCode());
MultiUsers::SwitchUser(localId);
}
void I18nServiceEvent::HandleAddUser(const EventFwk::CommonEventData& data) const
{
std::string localId = std::to_string(data.GetCode());
MultiUsers::AddUser(localId);
}
void I18nServiceEvent::HandleRemoveUser(const EventFwk::CommonEventData& data) const
{
std::string localId = std::to_string(data.GetCode());
MultiUsers::RemoveUser(localId);
if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
MultiUsers::SwitchUser(localId);
} else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED) {
MultiUsers::AddUser(localId);
} else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
MultiUsers::RemoveUser(localId);
}
}
} // namespace I18n
} // namespace Global