• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core_manager_inner.h"
19 #include "radio_event.h"
20 
21 namespace OHOS {
22 namespace Telephony {
SimStateTracker(const std::shared_ptr<AppExecFwk::EventRunner> & runner,std::shared_ptr<SimFileManager> simFileManager,std::shared_ptr<OperatorConfigCache> operatorConfigCache,int32_t slotId)23 SimStateTracker::SimStateTracker(const std::shared_ptr<AppExecFwk::EventRunner> &runner,
24     std::shared_ptr<SimFileManager> simFileManager, std::shared_ptr<OperatorConfigCache> operatorConfigCache,
25     int32_t slotId)
26     : AppExecFwk::EventHandler(runner), simFileManager_(simFileManager), operatorConfigCache_(operatorConfigCache),
27       slotId_(slotId)
28 {
29     if (runner != nullptr) {
30         runner->Run();
31     }
32     if (simFileManager == nullptr) {
33         TELEPHONY_LOGE("can not make OperatorConfigLoader");
34     }
35     operatorConfigLoader_ = std::make_shared<OperatorConfigLoader>(simFileManager, operatorConfigCache);
36     InitListener();
37 }
38 
InitListener()39 void SimStateTracker::InitListener()
40 {
41     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42     statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(slotId_, operatorConfigLoader_);
43     if (samgrProxy == nullptr || statusChangeListener_ == nullptr) {
44         TELEPHONY_LOGE("samgrProxy or statusChangeListener_ is nullptr");
45         return;
46     }
47     int32_t ret = samgrProxy->SubscribeSystemAbility(ACCESSIBILITY_MANAGER_SERVICE_ID, statusChangeListener_);
48     TELEPHONY_LOGI("SubscribeSystemAbility ACCESSIBILITY_MANAGER_SERVICE_ID result:%{public}d", ret);
49 }
50 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)51 void SimStateTracker::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
52 {
53     if (event == nullptr) {
54         TELEPHONY_LOGE("start ProcessEvent but event is null!");
55         return;
56     }
57     if (operatorConfigLoader_ == nullptr) {
58         TELEPHONY_LOGE("operatorConfigLoader_ is null!");
59         return;
60     }
61     if (event->GetInnerEventId() == RadioEvent::RADIO_SIM_RECORDS_LOADED) {
62         TELEPHONY_LOGI("SimStateTracker::Refresh config");
63         auto slotId = event->GetParam();
64         if (slotId != slotId_) {
65             TELEPHONY_LOGE("is not current slotId");
66             return;
67         }
68         bool hasSimCard = false;
69         CoreManagerInner::GetInstance().HasSimCard(slotId_, hasSimCard);
70         if (!hasSimCard) {
71             TELEPHONY_LOGE("sim is not exist");
72             return;
73         }
74         operatorConfigLoader_->LoadOperatorConfig(slotId_);
75     }
76 }
77 
RegisterForIccLoaded()78 bool SimStateTracker::RegisterForIccLoaded()
79 {
80     TELEPHONY_LOGI("SimStateTracker::RegisterForIccLoaded");
81     if (simFileManager_ == nullptr) {
82         TELEPHONY_LOGE("SimStateTracker::can not get SimFileManager");
83         return false;
84     }
85     simFileManager_->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_RECORDS_LOADED);
86     return true;
87 }
88 
UnRegisterForIccLoaded()89 bool SimStateTracker::UnRegisterForIccLoaded()
90 {
91     TELEPHONY_LOGI("SimStateTracker::UnRegisterForIccLoaded");
92     if (simFileManager_ == nullptr) {
93         TELEPHONY_LOGE("SimStateTracker::can not get SimFileManager");
94         return false;
95     }
96     simFileManager_->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_RECORDS_LOADED);
97     return true;
98 }
99 
SystemAbilityStatusChangeListener(int32_t slotId,std::shared_ptr<OperatorConfigLoader> configLoader)100 SimStateTracker::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
101     int32_t slotId, std::shared_ptr<OperatorConfigLoader> configLoader)
102     : slotId_(slotId), configLoader_(configLoader)
103 {}
104 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)105 void SimStateTracker::SystemAbilityStatusChangeListener::OnAddSystemAbility(
106     int32_t systemAbilityId, const std::string &deviceId)
107 {
108     if (systemAbilityId == ACCESSIBILITY_MANAGER_SERVICE_ID && configLoader_ != nullptr) {
109         TELEPHONY_LOGI("SystemAbilityStatusChangeListener::LoadOperatorConfig");
110         configLoader_->LoadOperatorConfig(slotId_);
111     }
112 }
113 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)114 void SimStateTracker::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
115     int32_t systemAbilityId, const std::string &deviceId)
116 {
117     if (systemAbilityId == ACCESSIBILITY_MANAGER_SERVICE_ID) {
118         TELEPHONY_LOGE("ACCESSIBILITY_MANAGER_SERVICE_ID stopped");
119     }
120 }
121 } // namespace Telephony
122 } // namespace OHOS
123