1 /*
2 * Copyright (C) 2021-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 "multi_sim_monitor.h"
17
18 #include <atomic>
19
20 #include "common_event_manager.h"
21 #include "common_event_support.h"
22 #include "os_account_manager_wrapper.h"
23 #include "operator_file_parser.h"
24 #include "radio_event.h"
25 #include "string_ex.h"
26 #include "telephony_errors.h"
27 #include "telephony_ext_wrapper.h"
28 #include "sim_account_callback_death_recipient.h"
29
30 namespace OHOS {
31 namespace Telephony {
32 const int64_t DELAY_TIME = 1000;
33 const int64_t RETRY_TIME = 3 * 60 * 1000;
34 const int32_t ACTIVE_USER_ID = 100;
35 const int INIT_TIMES = 15;
36 const int INIT_DATA_TIMES = 5;
37 constexpr const char *IS_BLOCK_LOAD_OPERATORCONFIG = "telephony.is_block_load_operatorconfig";
MultiSimMonitor(const std::shared_ptr<MultiSimController> & controller,std::vector<std::shared_ptr<Telephony::SimStateManager>> simStateManager,std::vector<std::weak_ptr<Telephony::SimFileManager>> simFileManager)38 MultiSimMonitor::MultiSimMonitor(const std::shared_ptr<MultiSimController> &controller,
39 std::vector<std::shared_ptr<Telephony::SimStateManager>> simStateManager,
40 std::vector<std::weak_ptr<Telephony::SimFileManager>> simFileManager)
41 : TelEventHandler("MultiSimMonitor"), controller_(controller), simStateManager_(simStateManager),
42 simFileManager_(simFileManager)
43 {
44 if (observerHandler_ == nullptr) {
45 observerHandler_ = std::make_unique<ObserverHandler>();
46 }
47 }
48
~MultiSimMonitor()49 MultiSimMonitor::~MultiSimMonitor()
50 {
51 TELEPHONY_LOGD("destory");
52 UnSubscribeListeners();
53 }
54
Init()55 void MultiSimMonitor::Init()
56 {
57 TELEPHONY_LOGD("init");
58 isSimAccountLoaded_.resize(SIM_SLOT_COUNT, 0);
59 initDataRemainCount_.resize(SIM_SLOT_COUNT, INIT_DATA_TIMES);
60 SendEvent(MultiSimMonitor::REGISTER_SIM_NOTIFY_EVENT);
61 InitListener();
62 }
63
AddExtraManagers(std::shared_ptr<Telephony::SimStateManager> simStateManager,std::shared_ptr<Telephony::SimFileManager> simFileManager)64 void MultiSimMonitor::AddExtraManagers(std::shared_ptr<Telephony::SimStateManager> simStateManager,
65 std::shared_ptr<Telephony::SimFileManager> simFileManager)
66 {
67 if (static_cast<int32_t>(simStateManager_.size()) == SIM_SLOT_COUNT) {
68 simStateManager_.push_back(simStateManager);
69 simFileManager_.push_back(simFileManager);
70 isSimAccountLoaded_.push_back(0);
71 initDataRemainCount_.push_back(INIT_DATA_TIMES);
72 RegisterSimNotify(SIM_SLOT_2);
73 }
74 }
75
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)76 void MultiSimMonitor::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
77 {
78 if (event == nullptr) {
79 TELEPHONY_LOGE("start ProcessEvent but event is null!");
80 return;
81 }
82 auto eventCode = event->GetInnerEventId();
83 TELEPHONY_LOGI("eventCode is %{public}d", eventCode);
84 switch (eventCode) {
85 case MultiSimMonitor::INIT_DATA_RETRY_EVENT:{
86 auto slotId = event->GetParam();
87 InitData(slotId);
88 break;
89 }
90 case RadioEvent::RADIO_QUERY_ICCID_DONE:
91 case RadioEvent::RADIO_SIM_STATE_LOCKED:
92 case RadioEvent::RADIO_SIM_STATE_READY: {
93 RemoveEvent(MultiSimMonitor::INIT_DATA_RETRY_EVENT);
94 auto slotId = event->GetParam();
95 InitData(slotId);
96 break;
97 }
98 case RadioEvent::RADIO_SIM_STATE_CHANGE: {
99 auto slotId = event->GetParam();
100 RefreshData(slotId);
101 break;
102 }
103 case MultiSimMonitor::REGISTER_SIM_NOTIFY_EVENT: {
104 RemoveEvent(MultiSimMonitor::REGISTER_SIM_NOTIFY_RETRY_EVENT);
105 RegisterSimNotify();
106 break;
107 }
108 case MultiSimMonitor::REGISTER_SIM_NOTIFY_RETRY_EVENT: {
109 RegisterSimNotify();
110 break;
111 }
112 case MultiSimMonitor::RESET_OPKEY_CONFIG: {
113 RemoveEvent(MultiSimMonitor::RETRY_RESET_OPKEY_CONFIG);
114 UpdateAllOpkeyConfigs();
115 break;
116 }
117 case MultiSimMonitor::RETRY_RESET_OPKEY_CONFIG: {
118 RemoveEvent(MultiSimMonitor::RETRY_RESET_OPKEY_CONFIG);
119 CheckDataShareError();
120 CheckSimNotifyRegister();
121 break;
122 }
123 default:
124 break;
125 }
126 }
127
CheckOpcNeedUpdata(const bool isDataShareError)128 void MultiSimMonitor::CheckOpcNeedUpdata(const bool isDataShareError)
129 {
130 TelFFRTUtils::Submit([=]() {
131 bool isOpcVersionUpdated = CheckUpdateOpcVersion() == TELEPHONY_SUCCESS;
132 if (isOpcVersionUpdated) {
133 OperatorFileParser::ClearFilesCache();
134 }
135 TELEPHONY_LOGI("CheckOpcNeedUpdata isDataShareError: %{public}d, isOpcVersionUpdated: %{public}d",
136 isDataShareError, isOpcVersionUpdated);
137 if (isOpcVersionUpdated || isDataShareError) {
138 SendEvent(MultiSimMonitor::RESET_OPKEY_CONFIG);
139 }
140 });
141 }
142
CheckUpdateOpcVersion()143 int32_t MultiSimMonitor::CheckUpdateOpcVersion()
144 {
145 if (TELEPHONY_EXT_WRAPPER.checkOpcVersionIsUpdate_ != nullptr &&
146 TELEPHONY_EXT_WRAPPER.updateOpcVersion_ != nullptr) {
147 std::lock_guard<std::mutex> lock(mutexForData_);
148 if (TELEPHONY_EXT_WRAPPER.checkOpcVersionIsUpdate_()) {
149 TELEPHONY_LOGI("need update config");
150 SetParameter(IS_BLOCK_LOAD_OPERATORCONFIG, "true");
151 if (controller_->UpdateOpKeyInfo() != TELEPHONY_SUCCESS) {
152 TELEPHONY_LOGW("UpdateOpKeyInfo error");
153 return TELEPHONY_ERROR;
154 }
155 TELEPHONY_EXT_WRAPPER.updateOpcVersion_();
156 TELEPHONY_LOGI("Version updated succ");
157 return TELEPHONY_SUCCESS;
158 }
159 }
160 return TELEPHONY_ERROR;
161 }
162
UpdateAllOpkeyConfigs()163 void MultiSimMonitor::UpdateAllOpkeyConfigs()
164 {
165 for (size_t slotId = 0; slotId < simFileManager_.size(); slotId++) {
166 auto simFileManager = simFileManager_[slotId].lock();
167 if (simFileManager == nullptr) {
168 TELEPHONY_LOGE("simFileManager is nullptr, slotId : %{public}zu", slotId);
169 continue;
170 }
171 simFileManager->UpdateOpkeyConfig();
172 }
173 }
174
InitData(int32_t slotId)175 void MultiSimMonitor::InitData(int32_t slotId)
176 {
177 TELEPHONY_LOGI("MultiSimMonitor::InitData slotId = %{public}d", slotId);
178 if (!IsValidSlotId(slotId)) {
179 TELEPHONY_LOGE("MultiSimMonitor::InitData slotId is invalid");
180 return;
181 }
182 if (isSimAccountLoaded_[slotId]) {
183 TELEPHONY_LOGI("MultiSimMonitor::InitData simAccountInfo is already loaded");
184 return;
185 }
186 if (controller_ == nullptr) {
187 TELEPHONY_LOGE("MultiSimMonitor::InitData controller_ is nullptr");
188 return;
189 }
190 if (!controller_->InitData(slotId)) {
191 TELEPHONY_LOGE("MultiSimMonitor::InitData failed");
192 if (initDataRemainCount_[slotId] > 0) {
193 SendEvent(MultiSimMonitor::INIT_DATA_RETRY_EVENT, slotId, DELAY_TIME);
194 TELEPHONY_LOGI("retry remain %{public}d", initDataRemainCount_[slotId]);
195 initDataRemainCount_[slotId]--;
196 }
197 return;
198 }
199 isSimAccountLoaded_[slotId] = 1;
200 if (observerHandler_ == nullptr) {
201 TELEPHONY_LOGE("MultiSimMonitor::InitData observerHandler_ is nullptr");
202 return;
203 }
204 NotifySimAccountChanged();
205 observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_ACCOUNT_LOADED, slotId);
206 }
207
RefreshData(int32_t slotId)208 void MultiSimMonitor::RefreshData(int32_t slotId)
209 {
210 if (!IsValidSlotId(slotId)) {
211 TELEPHONY_LOGE("MultiSimMonitor::RefreshData slotId is invalid");
212 return;
213 }
214 auto simFileManager = simFileManager_[slotId].lock();
215 if (controller_ == nullptr || simStateManager_[slotId] == nullptr || simFileManager == nullptr) {
216 TELEPHONY_LOGE("MultiSimMonitor::RefreshData controller_ or simStateManager_ is nullptr");
217 return;
218 }
219 if (simStateManager_[slotId]->GetSimState() == SimState::SIM_STATE_NOT_PRESENT) {
220 TELEPHONY_LOGI("MultiSimMonitor::RefreshData clear data when slotId %{public}d is absent", slotId);
221 controller_->ForgetAllData(slotId);
222 controller_->GetListFromDataBase();
223 controller_->ResetSetPrimarySlotRemain(slotId);
224 isSimAccountLoaded_[slotId] = 0;
225 initDataRemainCount_[slotId] = INIT_DATA_TIMES;
226 simFileManager->ClearData();
227 } else if (simStateManager_[slotId]->GetSimState() == SimState::SIM_STATE_UNKNOWN) {
228 TELEPHONY_LOGI("MultiSimMonitor::RefreshData clear data when sim is unknown");
229 simFileManager->ClearData();
230 }
231 if (controller_->unInitModemSlotId_ == slotId) {
232 TELEPHONY_LOGI("need to recheck primary");
233 controller_->ReCheckPrimary();
234 }
235 NotifySimAccountChanged();
236 }
237
RegisterCoreNotify(int32_t slotId,const std::shared_ptr<AppExecFwk::EventHandler> & handler,int what)238 void MultiSimMonitor::RegisterCoreNotify(
239 int32_t slotId, const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what)
240 {
241 if (observerHandler_ == nullptr || handler == nullptr) {
242 TELEPHONY_LOGE("observerHandler_ or handler is nullptr");
243 return;
244 }
245 observerHandler_->RegObserver(what, handler);
246 if (!IsValidSlotId(slotId)) {
247 TELEPHONY_LOGE("MultiSimMonitor::RegisterCoreNotify slotId is invalid");
248 return;
249 }
250 if (isSimAccountLoaded_[slotId] || IsVSimSlotId(slotId)) {
251 TELEPHONY_LOGI("notify slotId:%{public}d sim account loaded", slotId);
252 TelEventHandler::SendTelEvent(handler, RadioEvent::RADIO_SIM_ACCOUNT_LOADED, slotId, 0);
253 }
254 }
255
IsValidSlotId(int32_t slotId)256 bool MultiSimMonitor::IsValidSlotId(int32_t slotId)
257 {
258 return (slotId >= DEFAULT_SIM_SLOT_ID) && slotId < static_cast<int32_t>(simStateManager_.size());
259 }
260
IsVSimSlotId(int32_t slotId)261 bool MultiSimMonitor::IsVSimSlotId(int32_t slotId)
262 {
263 if (TELEPHONY_EXT_WRAPPER.getVSimSlotId_) {
264 int vSimSlotId = DEFAULT_SIM_SLOT_ID_REMOVE;
265 TELEPHONY_EXT_WRAPPER.getVSimSlotId_(vSimSlotId);
266 return vSimSlotId == slotId;
267 }
268 return false;
269 }
270
UnSubscribeListeners()271 void MultiSimMonitor::UnSubscribeListeners()
272 {
273 if (dataShareSubscriber_ != nullptr && CommonEventManager::UnSubscribeCommonEvent(dataShareSubscriber_)) {
274 dataShareSubscriber_ = nullptr;
275 TELEPHONY_LOGI("Unsubscribe datashare ready success");
276 }
277 if (userSwitchSubscriber_ != nullptr && CommonEventManager::UnSubscribeCommonEvent(userSwitchSubscriber_)) {
278 userSwitchSubscriber_ = nullptr;
279 TELEPHONY_LOGI("Unsubscribe UserSwitch success");
280 }
281 if (statusChangeListener_ != nullptr) {
282 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
283 if (samgrProxy != nullptr) {
284 samgrProxy->UnSubscribeSystemAbility(OHOS::COMMON_EVENT_SERVICE_ID, statusChangeListener_);
285 statusChangeListener_ = nullptr;
286 TELEPHONY_LOGI("Unsubscribe COMMON_EVENT_SERVICE_ID success");
287 }
288 }
289 }
290
InitListener()291 void MultiSimMonitor::InitListener()
292 {
293 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
294 statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(*this);
295 if (samgrProxy == nullptr || statusChangeListener_ == nullptr) {
296 TELEPHONY_LOGE("samgrProxy or statusChangeListener_ is nullptr");
297 return;
298 }
299 auto ret = samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
300 TELEPHONY_LOGI("SubscribeSystemAbility COMMON_EVENT_SERVICE_ID result is %{public}d", ret);
301 CheckOpcNeedUpdata(false);
302 }
303
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)304 void MultiSimMonitor::SystemAbilityStatusChangeListener::OnAddSystemAbility(int32_t systemAbilityId,
305 const std::string &deviceId)
306 {
307 switch (systemAbilityId) {
308 case COMMON_EVENT_SERVICE_ID: {
309 TELEPHONY_LOGI("COMMON_EVENT_SERVICE_ID is running");
310 handler_.SubscribeDataShareReady();
311 handler_.SubscribeUserSwitch();
312 break;
313 }
314 default:
315 TELEPHONY_LOGE("systemAbilityId is invalid");
316 break;
317 }
318 }
319
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)320 void MultiSimMonitor::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(int32_t systemAbilityId,
321 const std::string &deviceId)
322 {
323 switch (systemAbilityId) {
324 case COMMON_EVENT_SERVICE_ID: {
325 TELEPHONY_LOGI("COMMON_EVENT_SERVICE_ID stopped");
326 handler_.UnSubscribeListeners();
327 break;
328 }
329 default:
330 TELEPHONY_LOGE("systemAbilityId is invalid");
331 break;
332 }
333 }
334
SubscribeDataShareReady()335 void MultiSimMonitor::SubscribeDataShareReady()
336 {
337 if (dataShareSubscriber_ != nullptr) {
338 TELEPHONY_LOGW("datashare ready has Subscribed");
339 return;
340 }
341 MatchingSkills matchingSkills;
342 matchingSkills.AddEvent(DATASHARE_READY_EVENT);
343 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
344 subscriberInfo.SetThreadMode(CommonEventSubscribeInfo::COMMON);
345 dataShareSubscriber_ = std::make_shared<DataShareEventSubscriber>(subscriberInfo, *this);
346 if (CommonEventManager::SubscribeCommonEvent(dataShareSubscriber_)) {
347 TELEPHONY_LOGI("Subscribe datashare ready success");
348 } else {
349 dataShareSubscriber_ = nullptr;
350 TELEPHONY_LOGE("Subscribe datashare ready fail");
351 }
352 SendEvent(MultiSimMonitor::RETRY_RESET_OPKEY_CONFIG, 0, RETRY_TIME);
353 }
354
SubscribeUserSwitch()355 void MultiSimMonitor::SubscribeUserSwitch()
356 {
357 if (userSwitchSubscriber_ != nullptr) {
358 TELEPHONY_LOGW("UserSwitch has Subscribed");
359 return;
360 }
361 MatchingSkills matchingSkills;
362 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
363 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
364 subscriberInfo.SetThreadMode(CommonEventSubscribeInfo::COMMON);
365 userSwitchSubscriber_ = std::make_shared<UserSwitchEventSubscriber>(subscriberInfo, *this);
366 if (CommonEventManager::SubscribeCommonEvent(userSwitchSubscriber_)) {
367 TELEPHONY_LOGI("Subscribe UserSwitch success");
368 } else {
369 userSwitchSubscriber_ = nullptr;
370 TELEPHONY_LOGE("Subscribe UserSwitch fail");
371 }
372 }
373
OnReceiveEvent(const CommonEventData & data)374 void MultiSimMonitor::DataShareEventSubscriber::OnReceiveEvent(const CommonEventData &data)
375 {
376 OHOS::EventFwk::Want want = data.GetWant();
377 std::string action = want.GetAction();
378 TELEPHONY_LOGI("action = %{public}s", action.c_str());
379 std::vector<int32_t> activeList = { 0 };
380 DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance()->QueryActiveOsAccountIds(activeList);
381 if (action == DATASHARE_READY_EVENT) {
382 handler_.isDataShareReady_ = true;
383 if (activeList[0] == ACTIVE_USER_ID) {
384 handler_.CheckDataShareError();
385 handler_.CheckSimNotifyRegister();
386 }
387 }
388 }
389
OnReceiveEvent(const CommonEventData & data)390 void MultiSimMonitor::UserSwitchEventSubscriber::OnReceiveEvent(const CommonEventData &data)
391 {
392 OHOS::EventFwk::Want want = data.GetWant();
393 std::string action = want.GetAction();
394 TELEPHONY_LOGI("action = %{public}s", action.c_str());
395 if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
396 int32_t userId = data.GetCode();
397 TELEPHONY_LOGI("current user id is :%{public}d", userId);
398 if (userId == ACTIVE_USER_ID && handler_.isDataShareReady_) {
399 handler_.CheckDataShareError();
400 handler_.CheckSimNotifyRegister();
401 }
402 }
403 }
404
CheckSimNotifyRegister()405 void MultiSimMonitor::CheckSimNotifyRegister()
406 {
407 RemoveEvent(MultiSimMonitor::REGISTER_SIM_NOTIFY_RETRY_EVENT);
408 setRemainCount(INIT_TIMES);
409 RegisterSimNotify();
410 }
411
CheckDataShareError()412 void MultiSimMonitor::CheckDataShareError()
413 {
414 if (controller_->IsDataShareError()) {
415 TELEPHONY_LOGI("CheckDataShareError, need Reset Opkey");
416 CheckOpcNeedUpdata(true);
417 }
418 }
419
setRemainCount(int remainCount)420 void MultiSimMonitor::setRemainCount(int remainCount)
421 {
422 remainCount_ = remainCount;
423 }
424
RegisterSimAccountCallback(const int32_t tokenId,const sptr<SimAccountCallback> & callback)425 int32_t MultiSimMonitor::RegisterSimAccountCallback(
426 const int32_t tokenId, const sptr<SimAccountCallback> &callback)
427 {
428 if (callback == nullptr) {
429 TELEPHONY_LOGE("callback is nullptr");
430 return TELEPHONY_ERR_ARGUMENT_NULL;
431 }
432 std::lock_guard<std::mutex> lock(mutexInner_);
433 bool isExisted = false;
434 for (auto &iter : listSimAccountCallbackRecord_) {
435 if (iter.simAccountCallback == nullptr) {
436 continue;
437 }
438 if (iter.simAccountCallback->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) {
439 isExisted = true;
440 break;
441 }
442 }
443 if (isExisted) {
444 TELEPHONY_LOGI("Ignore register action, since callback is existent");
445 return TELEPHONY_SUCCESS;
446 }
447
448 SimAccountCallbackRecord simAccountRecord;
449 simAccountRecord.tokenId = tokenId;
450 simAccountRecord.simAccountCallback = callback;
451 deathRecipient_ = new (std::nothrow) SimAccountCallbackDeathRecipient(*this);
452 if (deathRecipient_ == nullptr) {
453 TELEPHONY_LOGE("deathRecipient is null");
454 return TELEPHONY_ERR_STRCPY_FAIL;
455 }
456 if (!callback->AsObject()->AddDeathRecipient(deathRecipient_)) {
457 TELEPHONY_LOGE("simAccountCallback remote server add death recipient failed");
458 }
459 listSimAccountCallbackRecord_.push_back(simAccountRecord);
460 TELEPHONY_LOGI("Register successfully, callback list size is %{public}zu", listSimAccountCallbackRecord_.size());
461 return TELEPHONY_SUCCESS;
462 }
463
UnregisterSimAccountCallback(const sptr<SimAccountCallback> & callback)464 int32_t MultiSimMonitor::UnregisterSimAccountCallback(const sptr<SimAccountCallback> &callback)
465 {
466 if (callback == nullptr) {
467 TELEPHONY_LOGE("callback is nullptr");
468 return TELEPHONY_ERR_ARGUMENT_NULL;
469 }
470 std::lock_guard<std::mutex> lock(mutexInner_);
471 bool isSuccess = false;
472 auto iter = listSimAccountCallbackRecord_.begin();
473 for (; iter != listSimAccountCallbackRecord_.end();) {
474 if (iter->simAccountCallback == nullptr) {
475 iter++;
476 continue;
477 }
478 if (iter->simAccountCallback->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) {
479 iter = listSimAccountCallbackRecord_.erase(iter);
480 isSuccess = true;
481 break;
482 }
483 iter++;
484 }
485 if (!isSuccess) {
486 TELEPHONY_LOGE("Ignore unregister action, since callback is nonexistent");
487 return TELEPHONY_ERROR;
488 }
489 TELEPHONY_LOGI("Unregister successfully, callback list size is %{public}zu", listSimAccountCallbackRecord_.size());
490 return TELEPHONY_SUCCESS;
491 }
492
GetSimAccountCallbackRecords()493 std::list<MultiSimMonitor::SimAccountCallbackRecord> MultiSimMonitor::GetSimAccountCallbackRecords()
494 {
495 std::lock_guard<std::mutex> lock(mutexInner_);
496 return listSimAccountCallbackRecord_;
497 }
498
NotifySimAccountChanged()499 void MultiSimMonitor::NotifySimAccountChanged()
500 {
501 std::list<SimAccountCallbackRecord> CallbackRecord = GetSimAccountCallbackRecords();
502 TELEPHONY_LOGD("CallbackRecord size is %{public}zu", CallbackRecord.size());
503 for (auto iter : CallbackRecord) {
504 if (iter.simAccountCallback != nullptr) {
505 iter.simAccountCallback->OnSimAccountChanged();
506 }
507 }
508 DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateIccAccount();
509 }
510
RegisterSimNotify()511 void MultiSimMonitor::RegisterSimNotify()
512 {
513 if (isForgetAllDataDone_) {
514 TELEPHONY_LOGI("RegisterSimNotify has done");
515 return;
516 }
517 if (controller_ == nullptr) {
518 TELEPHONY_LOGE("MultiSimController is null");
519 return;
520 }
521 if (!controller_->ForgetAllData()) {
522 if (remainCount_ > 0) {
523 SendEvent(MultiSimMonitor::REGISTER_SIM_NOTIFY_RETRY_EVENT, 0, DELAY_TIME);
524 TELEPHONY_LOGI("retry remain %{public}d", static_cast<int32_t>(remainCount_));
525 remainCount_--;
526 }
527 return;
528 }
529 isForgetAllDataDone_ = true;
530 TELEPHONY_LOGI("Register with time left %{public}d", static_cast<int32_t>(remainCount_));
531 for (size_t slotId = 0; slotId < simFileManager_.size(); slotId++) {
532 RegisterSimNotify(static_cast<int32_t>(slotId));
533 }
534 }
535
ResetSimLoadAccount(int32_t slotId)536 int32_t MultiSimMonitor::ResetSimLoadAccount(int32_t slotId)
537 {
538 if (!IsValidSlotId(slotId)) {
539 TELEPHONY_LOGE("ResetSimLoadAccount slotId is invalid");
540 return TELEPHONY_ERR_SLOTID_INVALID;
541 }
542 isSimAccountLoaded_[slotId] = 0;
543 initDataRemainCount_[slotId] = INIT_DATA_TIMES;
544 return TELEPHONY_SUCCESS;
545 }
546
RegisterSimNotify(int32_t slotId)547 void MultiSimMonitor::RegisterSimNotify(int32_t slotId)
548 {
549 if (!IsValidSlotId(slotId)) {
550 TELEPHONY_LOGE("RegisterSimNotify slotId is invalid");
551 return;
552 }
553 auto simFileManager = simFileManager_[slotId].lock();
554 if (simFileManager == nullptr) {
555 TELEPHONY_LOGE("simFileManager is null slotId : %{public}d", slotId);
556 return;
557 }
558 simFileManager->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_QUERY_ICCID_DONE);
559 if (simStateManager_[slotId] == nullptr) {
560 TELEPHONY_LOGE("simStateManager is null slotId : %{public}d", slotId);
561 return;
562 }
563 simStateManager_[slotId]->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_CHANGE);
564 simStateManager_[slotId]->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_LOCKED);
565 simStateManager_[slotId]->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_READY);
566 TELEPHONY_LOGI("RegisterSimNotify %{public}d", slotId);
567 }
568
UnRegisterSimNotify()569 void MultiSimMonitor::UnRegisterSimNotify()
570 {
571 for (size_t slotId = 0; slotId < simFileManager_.size(); slotId++) {
572 auto simFileManager = simFileManager_[slotId].lock();
573 if (simFileManager == nullptr) {
574 TELEPHONY_LOGE("simFileManager is null slotId : %{public}zu", slotId);
575 continue;
576 }
577 simFileManager->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_QUERY_ICCID_DONE);
578 if (simStateManager_[slotId] == nullptr) {
579 TELEPHONY_LOGE("simStateManager is null slotId : %{public}zu", slotId);
580 continue;
581 }
582 simStateManager_[slotId]->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_CHANGE);
583 simStateManager_[slotId]->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_LOCKED);
584 simStateManager_[slotId]->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_READY);
585 }
586 }
587 } // namespace Telephony
588 } // namespace OHOS
589