1 /*
2 * Copyright (C) 2021 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 "sim_state_tracker.h"
17
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "core_manager_inner.h"
21 #include "os_account_manager_wrapper.h"
22 #include "radio_event.h"
23 #include "thread"
24
25 namespace OHOS {
26 namespace Telephony {
27 const int32_t ACTIVE_USER_ID = 100;
SimStateTracker(std::weak_ptr<SimFileManager> simFileManager,std::shared_ptr<OperatorConfigCache> operatorConfigCache,int32_t slotId)28 SimStateTracker::SimStateTracker(std::weak_ptr<SimFileManager> simFileManager,
29 std::shared_ptr<OperatorConfigCache> operatorConfigCache, int32_t slotId)
30 : TelEventHandler("SimStateTracker"), simFileManager_(simFileManager), operatorConfigCache_(operatorConfigCache),
31 slotId_(slotId)
32 {
33 if (simFileManager.lock() == nullptr) {
34 TELEPHONY_LOGE("can not make OperatorConfigLoader");
35 }
36 operatorConfigLoader_ = std::make_shared<OperatorConfigLoader>(simFileManager, operatorConfigCache);
37 InitListener();
38 }
39
~SimStateTracker()40 SimStateTracker::~SimStateTracker()
41 {
42 if (statusChangeListener_ != nullptr) {
43 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
44 if (samgrProxy != nullptr) {
45 samgrProxy->UnSubscribeSystemAbility(OHOS::SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, statusChangeListener_);
46 samgrProxy->UnSubscribeSystemAbility(OHOS::COMMON_EVENT_SERVICE_ID, statusChangeListener_);
47 statusChangeListener_ = nullptr;
48 }
49 }
50 }
51
InitListener()52 void SimStateTracker::InitListener()
53 {
54 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
55 statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(slotId_, operatorConfigLoader_);
56 if (samgrProxy == nullptr || statusChangeListener_ == nullptr) {
57 TELEPHONY_LOGE("samgrProxy or statusChangeListener_ is nullptr");
58 return;
59 }
60 int32_t ret = samgrProxy->SubscribeSystemAbility(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, statusChangeListener_);
61 TELEPHONY_LOGI("SubscribeSystemAbility SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN result:%{public}d", ret);
62 ret = samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
63 TELEPHONY_LOGI("SubscribeSystemAbility COMMON_EVENT_SERVICE_ID result:%{public}d", ret);
64 }
65
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)66 void SimStateTracker::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
67 {
68 if (event == nullptr) {
69 TELEPHONY_LOGE("start ProcessEvent but event is null!");
70 return;
71 }
72 if (operatorConfigLoader_ == nullptr) {
73 TELEPHONY_LOGE("operatorConfigLoader_ is null!");
74 return;
75 }
76 if (event->GetInnerEventId() == RadioEvent::RADIO_SIM_RECORDS_LOADED) {
77 TELEPHONY_LOGI("SimStateTracker::Refresh config");
78 auto slotId = event->GetParam();
79 if (slotId != slotId_) {
80 TELEPHONY_LOGE("is not current slotId");
81 return;
82 }
83 bool hasSimCard = false;
84 CoreManagerInner::GetInstance().HasSimCard(slotId_, hasSimCard);
85 if (!hasSimCard) {
86 TELEPHONY_LOGE("sim is not exist");
87 return;
88 }
89 TelFFRTUtils::Submit([&]() { operatorConfigLoader_->LoadOperatorConfig(slotId_); });
90 }
91 }
92
RegisterForIccLoaded()93 bool SimStateTracker::RegisterForIccLoaded()
94 {
95 TELEPHONY_LOGI("SimStateTracker::RegisterForIccLoaded");
96 auto simFileManager = simFileManager_.lock();
97 if (simFileManager == nullptr) {
98 TELEPHONY_LOGE("SimStateTracker::can not get SimFileManager");
99 return false;
100 }
101 simFileManager->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_RECORDS_LOADED);
102 return true;
103 }
104
UnRegisterForIccLoaded()105 bool SimStateTracker::UnRegisterForIccLoaded()
106 {
107 TELEPHONY_LOGI("SimStateTracker::UnRegisterForIccLoaded");
108 auto simFileManager = simFileManager_.lock();
109 if (simFileManager == nullptr) {
110 TELEPHONY_LOGE("SimStateTracker::can not get SimFileManager");
111 return false;
112 }
113 simFileManager->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_RECORDS_LOADED);
114 return true;
115 }
116
SystemAbilityStatusChangeListener(int32_t slotId,std::shared_ptr<OperatorConfigLoader> configLoader)117 SimStateTracker::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
118 int32_t slotId, std::shared_ptr<OperatorConfigLoader> configLoader)
119 : slotId_(slotId), configLoader_(configLoader)
120 {}
121
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)122 void SimStateTracker::SystemAbilityStatusChangeListener::OnAddSystemAbility(
123 int32_t systemAbilityId, const std::string &deviceId)
124 {
125 if (configLoader_ == nullptr) {
126 TELEPHONY_LOGE("configLoader_ is nullptr.");
127 return;
128 }
129 switch (systemAbilityId) {
130 case SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN: {
131 TELEPHONY_LOGI("SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN running");
132 std::vector<int32_t> activeList = { 0 };
133 DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance()->QueryActiveOsAccountIds(activeList);
134 TELEPHONY_LOGI("current active user id is :%{public}d", activeList[0]);
135 if (activeList[0] == ACTIVE_USER_ID) {
136 configLoader_->LoadOperatorConfig(slotId_);
137 }
138 break;
139 }
140 case COMMON_EVENT_SERVICE_ID: {
141 TELEPHONY_LOGI("COMMON_EVENT_SERVICE_ID running");
142 MatchingSkills matchingSkills;
143 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
144 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
145 subscriberInfo.SetThreadMode(CommonEventSubscribeInfo::COMMON);
146 userSwitchSubscriber_ = std::make_shared<UserSwitchEventSubscriber>(subscriberInfo, slotId_, configLoader_);
147 bool subRet = CommonEventManager::SubscribeCommonEvent(userSwitchSubscriber_);
148 TELEPHONY_LOGI("Subscribe user switched subRet is :%{public}d", subRet);
149 break;
150 }
151 default:
152 TELEPHONY_LOGE("systemAbilityId is invalid");
153 break;
154 }
155 }
156
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)157 void SimStateTracker::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
158 int32_t systemAbilityId, const std::string &deviceId)
159 {
160 switch (systemAbilityId) {
161 case SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN: {
162 TELEPHONY_LOGE("SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN stopped");
163 break;
164 }
165 case COMMON_EVENT_SERVICE_ID: {
166 TELEPHONY_LOGE("COMMON_EVENT_SERVICE_ID stopped");
167 if (userSwitchSubscriber_ != nullptr) {
168 bool subRet = CommonEventManager::UnSubscribeCommonEvent(userSwitchSubscriber_);
169 TELEPHONY_LOGI("Unsubscribe user switched subRet is :%{public}d", subRet);
170 userSwitchSubscriber_ = nullptr;
171 }
172 break;
173 }
174 default:
175 TELEPHONY_LOGE("systemAbilityId is invalid");
176 break;
177 }
178 }
179
OnReceiveEvent(const CommonEventData & data)180 void SimStateTracker::UserSwitchEventSubscriber::OnReceiveEvent(const CommonEventData &data)
181 {
182 OHOS::EventFwk::Want want = data.GetWant();
183 std::string action = data.GetWant().GetAction();
184 TELEPHONY_LOGI("action = %{public}s", action.c_str());
185 if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
186 int32_t userId = data.GetCode();
187 TELEPHONY_LOGI("current user id is :%{public}d", userId);
188 if (userId == ACTIVE_USER_ID) {
189 configLoader_->LoadOperatorConfig(slotId_);
190 }
191 }
192 }
193 } // namespace Telephony
194 } // namespace OHOS
195