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 "telephony_ext_wrapper.h"
24 #include "thread"
25
26 namespace OHOS {
27 namespace Telephony {
28 constexpr int32_t OPKEY_VMSG_LENTH = 3;
29 const int32_t ACTIVE_USER_ID = 100;
SimStateTracker(std::weak_ptr<SimFileManager> simFileManager,std::shared_ptr<OperatorConfigCache> operatorConfigCache,int32_t slotId)30 SimStateTracker::SimStateTracker(std::weak_ptr<SimFileManager> simFileManager,
31 std::shared_ptr<OperatorConfigCache> operatorConfigCache, int32_t slotId)
32 : TelEventHandler("SimStateTracker"), simFileManager_(simFileManager), operatorConfigCache_(operatorConfigCache),
33 slotId_(slotId)
34 {
35 if (simFileManager.lock() == nullptr) {
36 TELEPHONY_LOGE("can not make OperatorConfigLoader");
37 }
38 operatorConfigLoader_ = std::make_shared<OperatorConfigLoader>(simFileManager, operatorConfigCache);
39 InitListener();
40 }
41
~SimStateTracker()42 SimStateTracker::~SimStateTracker()
43 {
44 if (statusChangeListener_ != nullptr) {
45 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
46 if (samgrProxy != nullptr) {
47 samgrProxy->UnSubscribeSystemAbility(OHOS::SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, statusChangeListener_);
48 samgrProxy->UnSubscribeSystemAbility(OHOS::COMMON_EVENT_SERVICE_ID, statusChangeListener_);
49 statusChangeListener_ = nullptr;
50 }
51 }
52 }
53
InitListener()54 void SimStateTracker::InitListener()
55 {
56 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
57 statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(slotId_, operatorConfigLoader_);
58 if (samgrProxy == nullptr || statusChangeListener_ == nullptr) {
59 TELEPHONY_LOGE("samgrProxy or statusChangeListener_ is nullptr");
60 return;
61 }
62 int32_t ret = samgrProxy->SubscribeSystemAbility(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, statusChangeListener_);
63 TELEPHONY_LOGI("SubscribeSystemAbility SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN result:%{public}d", ret);
64 ret = samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
65 TELEPHONY_LOGI("SubscribeSystemAbility COMMON_EVENT_SERVICE_ID result:%{public}d", ret);
66 }
67
ProcessSimRecordLoad(const AppExecFwk::InnerEvent::Pointer & event)68 void SimStateTracker::ProcessSimRecordLoad(const AppExecFwk::InnerEvent::Pointer &event)
69 {
70 TELEPHONY_LOGI("SimStateTracker::Refresh config");
71 auto slotId = event->GetParam();
72 if (slotId != slotId_) {
73 TELEPHONY_LOGE("is not current slotId");
74 return;
75 }
76 bool hasSimCard = false;
77 CoreManagerInner::GetInstance().HasSimCard(slotId_, hasSimCard);
78 if (!hasSimCard) {
79 TELEPHONY_LOGE("sim is not exist");
80 return;
81 }
82 TelFFRTUtils::Submit([&]() { operatorConfigLoader_->LoadOperatorConfig(slotId_); });
83 }
84
ProcessSimOpkeyLoad(const AppExecFwk::InnerEvent::Pointer & event)85 void SimStateTracker::ProcessSimOpkeyLoad(const AppExecFwk::InnerEvent::Pointer &event)
86 {
87 std::shared_ptr<std::vector<std::string>> msgObj = event->GetSharedObject<std::vector<std::string>>();
88 if ((msgObj == nullptr) || ((*msgObj).size() != OPKEY_VMSG_LENTH)) {
89 TELEPHONY_LOGI("argument count error");
90 return;
91 }
92 int slotId;
93 if (!StrToInt((*msgObj)[0], slotId)) {
94 return;
95 }
96 if (slotId != slotId_) {
97 TELEPHONY_LOGE("is not current slotId");
98 return;
99 }
100 std::string opkey = (*msgObj)[1];
101 std::string opName = (*msgObj)[2];
102 TELEPHONY_LOGI("OnOpkeyLoad slotId, %{public}d opkey: %{public}s opName: %{public}s",
103 slotId, opkey.data(), opName.data());
104 if (!opkey.empty()) {
105 auto simFileManager = simFileManager_.lock();
106 if (simFileManager != nullptr) {
107 simFileManager->SetOpKey(opkey);
108 simFileManager->SetOpName(opName);
109 }
110 TelFFRTUtils::Submit([&]() {
111 OperatorConfig opc;
112 operatorConfigCache_->LoadOperatorConfig(slotId_, opc);
113 });
114 } else {
115 bool hasSimCard = false;
116 CoreManagerInner::GetInstance().HasSimCard(slotId_, hasSimCard);
117 if (!hasSimCard) {
118 TELEPHONY_LOGE("sim is not exist");
119 return;
120 }
121 TelFFRTUtils::Submit([&]() { operatorConfigLoader_->LoadOperatorConfig(slotId_); });
122 }
123 }
124
ProcessOperatorCacheDel(const AppExecFwk::InnerEvent::Pointer & event)125 void SimStateTracker::ProcessOperatorCacheDel(const AppExecFwk::InnerEvent::Pointer &event)
126 {
127 TELEPHONY_LOGI("SimStateTracker::ProcessOperatorCacheDel");
128 auto slotId = event->GetParam();
129 if (slotId != slotId_) {
130 TELEPHONY_LOGE("is not current slotId");
131 return;
132 }
133 if (operatorConfigCache_ == nullptr) {
134 TELEPHONY_LOGE("operatorConfigCache is nullptr");
135 return;
136 }
137 operatorConfigCache_->ClearOperatorValue(slotId);
138 operatorConfigCache_->ClearMemoryCache(slotId);
139 }
140
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)141 void SimStateTracker::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
142 {
143 if (event == nullptr) {
144 TELEPHONY_LOGE("start ProcessEvent but event is null!");
145 return;
146 }
147 if (operatorConfigLoader_ == nullptr) {
148 TELEPHONY_LOGE("operatorConfigLoader_ is null!");
149 return;
150 }
151 if (event->GetInnerEventId() == RadioEvent::RADIO_SIM_RECORDS_LOADED) {
152 ProcessSimRecordLoad(event);
153 }
154
155 if (event->GetInnerEventId() == RadioEvent::RADIO_SIM_OPKEY_LOADED) {
156 ProcessSimOpkeyLoad(event);
157 }
158
159 if (event->GetInnerEventId() == RadioEvent::RADIO_OPERATOR_CACHE_DELETE) {
160 ProcessOperatorCacheDel(event);
161 }
162 }
163
RegisterForIccLoaded()164 bool SimStateTracker::RegisterForIccLoaded()
165 {
166 TELEPHONY_LOGI("SimStateTracker::RegisterForIccLoaded");
167 auto simFileManager = simFileManager_.lock();
168 if (simFileManager == nullptr) {
169 TELEPHONY_LOGE("SimStateTracker::can not get SimFileManager");
170 return false;
171 }
172 simFileManager->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_RECORDS_LOADED);
173 return true;
174 }
175
RegisterOpkeyLoaded()176 bool SimStateTracker::RegisterOpkeyLoaded()
177 {
178 TELEPHONY_LOGI("SimStateTracker::RegisterOpkeyLoaded");
179 auto simFileManager = simFileManager_.lock();
180 if (simFileManager == nullptr) {
181 TELEPHONY_LOGE("simFileManager::can not get simFileManager");
182 return false;
183 }
184 simFileManager->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_OPKEY_LOADED);
185 return true;
186 }
187
RegisterOperatorCacheDel()188 bool SimStateTracker::RegisterOperatorCacheDel()
189 {
190 TELEPHONY_LOGI("SimStateTracker::RegisterOperatorCacheDel");
191 auto simFileManager = simFileManager_.lock();
192 if (simFileManager == nullptr) {
193 TELEPHONY_LOGE("simFileManager::can not get simFileManager");
194 return false;
195 }
196 simFileManager->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_OPERATOR_CACHE_DELETE);
197 return true;
198 }
199
UnRegisterForIccLoaded()200 bool SimStateTracker::UnRegisterForIccLoaded()
201 {
202 TELEPHONY_LOGI("SimStateTracker::UnRegisterForIccLoaded");
203 auto simFileManager = simFileManager_.lock();
204 if (simFileManager == nullptr) {
205 TELEPHONY_LOGE("SimStateTracker::can not get SimFileManager");
206 return false;
207 }
208 simFileManager->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_RECORDS_LOADED);
209 return true;
210 }
211
UnRegisterOpkeyLoaded()212 bool SimStateTracker::UnRegisterOpkeyLoaded()
213 {
214 TELEPHONY_LOGI("SimStateTracker::UnRegisterOpkeyLoaded");
215 auto simFileManager = simFileManager_.lock();
216 if (simFileManager == nullptr) {
217 TELEPHONY_LOGE("simFileManager::can not get simFileManager");
218 return false;
219 }
220 simFileManager->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_OPKEY_LOADED);
221 return true;
222 }
223
UnregisterOperatorCacheDel()224 bool SimStateTracker::UnregisterOperatorCacheDel()
225 {
226 TELEPHONY_LOGI("SimStateTracker::UnregisterOperatorCacheDel");
227 auto simFileManager = simFileManager_.lock();
228 if (simFileManager == nullptr) {
229 TELEPHONY_LOGE("simFileManager::can not get simFileManager");
230 return false;
231 }
232 simFileManager->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_OPERATOR_CACHE_DELETE);
233 return true;
234 }
235
SystemAbilityStatusChangeListener(int32_t slotId,std::shared_ptr<OperatorConfigLoader> configLoader)236 SimStateTracker::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
237 int32_t slotId, std::shared_ptr<OperatorConfigLoader> configLoader)
238 : slotId_(slotId), configLoader_(configLoader)
239 {}
240
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)241 void SimStateTracker::SystemAbilityStatusChangeListener::OnAddSystemAbility(
242 int32_t systemAbilityId, const std::string &deviceId)
243 {
244 if (configLoader_ == nullptr) {
245 TELEPHONY_LOGE("configLoader_ is nullptr.");
246 return;
247 }
248 switch (systemAbilityId) {
249 case SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN: {
250 TELEPHONY_LOGI("SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN running");
251 std::vector<int32_t> activeList = { 0 };
252 DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance()->QueryActiveOsAccountIds(activeList);
253 TELEPHONY_LOGI("current active user id is :%{public}d", activeList[0]);
254 if (activeList[0] == ACTIVE_USER_ID) {
255 configLoader_->LoadOperatorConfig(slotId_);
256 }
257 break;
258 }
259 case COMMON_EVENT_SERVICE_ID: {
260 TELEPHONY_LOGI("COMMON_EVENT_SERVICE_ID running, isUserSwitchSubscribered is :%{public}d",
261 isUserSwitchSubscribered);
262 if (isUserSwitchSubscribered) {
263 return;
264 }
265 MatchingSkills matchingSkills;
266 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
267 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
268 subscriberInfo.SetThreadMode(CommonEventSubscribeInfo::COMMON);
269 userSwitchSubscriber_ = std::make_shared<UserSwitchEventSubscriber>(subscriberInfo, slotId_, configLoader_);
270 if (CommonEventManager::SubscribeCommonEvent(userSwitchSubscriber_)) {
271 isUserSwitchSubscribered = true;
272 TELEPHONY_LOGI("Subscribe user switched success");
273 }
274 break;
275 }
276 default:
277 TELEPHONY_LOGE("systemAbilityId is invalid");
278 break;
279 }
280 }
281
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)282 void SimStateTracker::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
283 int32_t systemAbilityId, const std::string &deviceId)
284 {
285 switch (systemAbilityId) {
286 case SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN: {
287 TELEPHONY_LOGE("SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN stopped");
288 break;
289 }
290 case COMMON_EVENT_SERVICE_ID: {
291 TELEPHONY_LOGI("COMMON_EVENT_SERVICE_ID stopped, isUserSwitchSubscribered is :%{public}d",
292 isUserSwitchSubscribered);
293 if (!isUserSwitchSubscribered) {
294 return;
295 }
296 if (userSwitchSubscriber_ != nullptr && CommonEventManager::UnSubscribeCommonEvent(userSwitchSubscriber_)) {
297 userSwitchSubscriber_ = nullptr;
298 isUserSwitchSubscribered = false;
299 TELEPHONY_LOGI("Unsubscribe user switched success");
300 }
301 break;
302 }
303 default:
304 TELEPHONY_LOGE("systemAbilityId is invalid");
305 break;
306 }
307 }
308
OnReceiveEvent(const CommonEventData & data)309 void SimStateTracker::UserSwitchEventSubscriber::OnReceiveEvent(const CommonEventData &data)
310 {
311 OHOS::EventFwk::Want want = data.GetWant();
312 std::string action = data.GetWant().GetAction();
313 TELEPHONY_LOGI("action = %{public}s", action.c_str());
314 if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
315 int32_t userId = data.GetCode();
316 TELEPHONY_LOGI("current user id is :%{public}d", userId);
317 if (userId == ACTIVE_USER_ID) {
318 configLoader_->LoadOperatorConfig(slotId_);
319 }
320 }
321 }
322 } // namespace Telephony
323 } // namespace OHOS
324