1 /*
2 * Copyright (c) 2024 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 <common_event_data.h>
17 #include <common_event_manager.h>
18 #include <common_event_support.h>
19 #include "common_event_subscriber.h"
20 #include "i18n_hilog.h"
21 #include "multi_users.h"
22
23 #include "i18n_service_event.h"
24
25 namespace OHOS {
26 namespace Global {
27 namespace I18n {
28 const int I18nServiceEvent::RETRY = 3;
29 const int32_t I18nServiceEvent::OS_ACCOUNT_UID = 3058;
30 const std::string I18nServiceEvent::OS_ACCOUNT_PERMISSION = "ohos.permission.MANAGE_LOCAL_ACCOUNTS";
31
I18nServiceEvent()32 I18nServiceEvent::I18nServiceEvent()
33 {
34 HILOG_INFO_I18N("I18nServiceEvent: start.");
35 }
36
~I18nServiceEvent()37 I18nServiceEvent::~I18nServiceEvent()
38 {
39 if (subscriber) {
40 bool status = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber);
41 if (!status) {
42 HILOG_ERROR_I18N("UnSubscriberEvent: UnSubscriberEvent failed");
43 }
44 subscriber = nullptr;
45 }
46 }
47
SubscriberEvent()48 void I18nServiceEvent::SubscriberEvent()
49 {
50 if (subscriber) {
51 return;
52 }
53 EventFwk::MatchingSkills matchingSkills;
54 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
55 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED);
56 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
57 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
58 subscribeInfo.SetPublisherUid(OS_ACCOUNT_UID);
59 subscribeInfo.SetPermission(OS_ACCOUNT_PERMISSION);
60 subscriber = std::make_shared<I18nServiceSubscriber>(subscribeInfo, *this);
61
62 int32_t retry = RETRY;
63 do {
64 bool status = EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
65 if (status) {
66 HILOG_INFO_I18N("SubscriberEvent: SubscriberEvent success.");
67 return;
68 }
69 HILOG_ERROR_I18N("SubscriberEvent: SubscriberEvent failed %{public}d.", retry);
70 retry--;
71 sleep(1);
72 } while (retry);
73 HILOG_INFO_I18N("SubscriberEvent: SubscriberEvent failed");
74 }
75
CheckStartReason(const SystemAbilityOnDemandReason & startReason)76 void I18nServiceEvent::CheckStartReason(const SystemAbilityOnDemandReason& startReason)
77 {
78 std::string action = startReason.GetName();
79 std::string localId = std::to_string(startReason.GetExtraData().GetCode());
80 HILOG_INFO_I18N("CheckStartReason: start reason is %{public}s", action.c_str());
81 HandleMultiUser(action, localId);
82 }
83
OnReceiveEvent(const EventFwk::CommonEventData & data)84 void I18nServiceEvent::OnReceiveEvent(const EventFwk::CommonEventData& data)
85 {
86 std::string action = data.GetWant().GetAction();
87 std::string localId = std::to_string(data.GetCode());
88 HILOG_INFO_I18N("OnReceiveEvent: handle event %{public}s", action.c_str());
89 HandleMultiUser(action, localId);
90 }
91
HandleMultiUser(const std::string & action,const std::string & localId)92 void I18nServiceEvent::HandleMultiUser(const std::string& action, const std::string& localId)
93 {
94 #ifdef SUPPORT_MULTI_USER
95 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
96 MultiUsers::SwitchUser(localId);
97 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED) {
98 MultiUsers::AddUser(localId);
99 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
100 MultiUsers::RemoveUser(localId);
101 }
102 #endif
103 }
104 } // namespace I18n
105 } // namespace Global
106 } // namespace OHOS