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 "multi_sim_monitor.h"
17
18 #include "string_ex.h"
19
20 #include "radio_event.h"
21
22 namespace OHOS {
23 namespace Telephony {
24 static const int32_t RETRY_COUNT = 30;
25
MultiSimMonitor(const std::shared_ptr<AppExecFwk::EventRunner> & runner,const std::shared_ptr<MultiSimController> & controller,std::vector<std::shared_ptr<Telephony::SimStateManager>> simStateManager,std::vector<std::shared_ptr<Telephony::SimFileManager>> simFileManager)26 MultiSimMonitor::MultiSimMonitor(const std::shared_ptr<AppExecFwk::EventRunner> &runner,
27 const std::shared_ptr<MultiSimController> &controller,
28 std::vector<std::shared_ptr<Telephony::SimStateManager>> simStateManager,
29 std::vector<std::shared_ptr<Telephony::SimFileManager>> simFileManager)
30 : AppExecFwk::EventHandler(runner), controller_(controller),
31 simStateManager_(simStateManager), simFileManager_(simFileManager)
32 {
33 if (runner != nullptr) {
34 runner->Run();
35 }
36 if (observerHandler_ == nullptr) {
37 observerHandler_ = std::make_unique<ObserverHandler>();
38 }
39 }
40
Init()41 void MultiSimMonitor::Init()
42 {
43 SendEvent(MSG_SIM_FORGET_ALLDATA);
44 }
45
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)46 void MultiSimMonitor::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
47 {
48 if (event == nullptr) {
49 TELEPHONY_LOGE("start ProcessEvent but event is null!");
50 return;
51 }
52 auto eventCode = event->GetInnerEventId();
53 switch (eventCode) {
54 case RadioEvent::RADIO_SIM_RECORDS_LOADED: {
55 auto slotId = event->GetParam();
56 InitData(slotId);
57 break;
58 }
59 case RadioEvent::RADIO_SIM_STATE_CHANGE: {
60 auto slotId = event->GetParam();
61 RefreshData(slotId);
62 break;
63 }
64 case MSG_SIM_FORGET_ALLDATA:
65 TELEPHONY_LOGI("MultiSimMonitor::forget all data");
66 ready_ = controller_->ForgetAllData();
67 break;
68 default:
69 break;
70 }
71 }
72
InitData(int32_t slotId)73 void MultiSimMonitor::InitData(int32_t slotId)
74 {
75 TELEPHONY_LOGI("MultiSimMonitor::InitData slotId = %{public}d", slotId);
76 if (!IsValidSlotId(slotId)) {
77 TELEPHONY_LOGE("MultiSimMonitor::InitData slotId is invalid");
78 return;
79 }
80 if (controller_ == nullptr) {
81 TELEPHONY_LOGE("MultiSimMonitor::InitData controller_ is nullptr");
82 return;
83 }
84 for (int i = 0; i < RETRY_COUNT; i++) {
85 if (ready_) {
86 TELEPHONY_LOGI("MultiSimMonitor::InitData dataAbility is ready");
87 break;
88 }
89 }
90 if (!controller_->InitData(slotId)) {
91 TELEPHONY_LOGE("MultiSimMonitor::InitData failed");
92 return;
93 }
94 if (observerHandler_ == nullptr) {
95 TELEPHONY_LOGE("MultiSimMonitor::InitData observerHandler_ is nullptr");
96 return;
97 }
98 NotifySimAccountChanged();
99 observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_ACCOUNT_LOADED, slotId);
100 }
101
RefreshData(int32_t slotId)102 void MultiSimMonitor::RefreshData(int32_t slotId)
103 {
104 if (!IsValidSlotId(slotId)) {
105 TELEPHONY_LOGE("MultiSimMonitor::RefreshData slotId is invalid");
106 return;
107 }
108 if (controller_ == nullptr || simStateManager_[slotId] == nullptr || simFileManager_[slotId] == nullptr) {
109 TELEPHONY_LOGE("MultiSimMonitor::RefreshData controller_ or simStateManager_ is nullptr");
110 return;
111 }
112 if (simStateManager_[slotId]->GetSimState() == SimState::SIM_STATE_NOT_PRESENT) {
113 TELEPHONY_LOGI("MultiSimMonitor::RefreshData clear data when sim is absent");
114 controller_->ForgetAllData(slotId);
115 controller_->GetListFromDataBase();
116 simFileManager_[slotId]->ClearData();
117 }
118 NotifySimAccountChanged();
119 }
120
RegisterForIccLoaded(int32_t slotId)121 bool MultiSimMonitor::RegisterForIccLoaded(int32_t slotId)
122 {
123 TELEPHONY_LOGI("MultiSimMonitor::RegisterForIccLoaded");
124 if (simFileManager_[slotId] == nullptr) {
125 TELEPHONY_LOGE("MultiSimMonitor::RegisterForIccLoaded simFileManager_ is nullptr");
126 return false;
127 }
128 simFileManager_[slotId]->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_RECORDS_LOADED);
129 return true;
130 }
131
UnRegisterForIccLoaded(int32_t slotId)132 bool MultiSimMonitor::UnRegisterForIccLoaded(int32_t slotId)
133 {
134 TELEPHONY_LOGI("MultiSimMonitor::UnRegisterForIccLoaded");
135 if (simFileManager_[slotId] == nullptr) {
136 TELEPHONY_LOGE("MultiSimMonitor::UnRegisterForIccLoaded simFileManager_ is nullptr");
137 return false;
138 }
139 simFileManager_[slotId]->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_RECORDS_LOADED);
140 return true;
141 }
142
RegisterForSimStateChanged(int32_t slotId)143 bool MultiSimMonitor::RegisterForSimStateChanged(int32_t slotId)
144 {
145 TELEPHONY_LOGI("MultiSimMonitor::RegisterForSimStateChanged");
146 if (simFileManager_[slotId] == nullptr) {
147 TELEPHONY_LOGE("MultiSimMonitor::RegisterForSimStateChanged simFileManager_ is nullptr");
148 return false;
149 }
150 simFileManager_[slotId]->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_CHANGE);
151 return true;
152 }
153
UnRegisterForSimStateChanged(int32_t slotId)154 bool MultiSimMonitor::UnRegisterForSimStateChanged(int32_t slotId)
155 {
156 TELEPHONY_LOGI("MultiSimMonitor::UnRegisterForSimStateChanged");
157 if (simFileManager_[slotId] == nullptr) {
158 TELEPHONY_LOGE("MultiSimMonitor::UnRegisterForSimStateChanged simFileManager_ is nullptr");
159 return false;
160 }
161 simFileManager_[slotId]->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_CHANGE);
162 return true;
163 }
164
RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> & handler,int what)165 void MultiSimMonitor::RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what)
166 {
167 if (observerHandler_ == nullptr) {
168 TELEPHONY_LOGE("MultiSimMonitor::RegisterCoreNotify observerHandler_ is nullptr");
169 return;
170 }
171 observerHandler_->RegObserver(what, handler);
172 }
173
IsValidSlotId(int32_t slotId)174 bool MultiSimMonitor::IsValidSlotId(int32_t slotId)
175 {
176 return (slotId >= DEFAULT_SIM_SLOT_ID) && (slotId < SIM_SLOT_COUNT);
177 }
178
RegisterSimAccountCallback(const std::string & bundleName,const sptr<SimAccountCallback> & callback)179 int32_t MultiSimMonitor::RegisterSimAccountCallback(
180 const std::string &bundleName, const sptr<SimAccountCallback> &callback)
181 {
182 if (callback == nullptr) {
183 TELEPHONY_LOGE(" callback is nullptr");
184 return TELEPHONY_ERR_ARGUMENT_NULL;
185 }
186 bool isExisted = false;
187 std::lock_guard<std::mutex> lock(mutexInner_);
188 for (auto iter : listSimAccountCallbackRecord_) {
189 if ((iter.bundleName == bundleName)) {
190 iter.simAccountCallback = callback;
191 isExisted = true;
192 break;
193 }
194 }
195 if (isExisted) {
196 TELEPHONY_LOGI("Ignore register action, since callback is existent");
197 return TELEPHONY_SUCCESS;
198 }
199
200 SimAccountCallbackRecord simAccountRecord;
201 simAccountRecord.bundleName = bundleName;
202 simAccountRecord.simAccountCallback = callback;
203 listSimAccountCallbackRecord_.push_back(simAccountRecord);
204 TELEPHONY_LOGI("Register successfully, callback list size is %{public}zu", listSimAccountCallbackRecord_.size());
205 return TELEPHONY_SUCCESS;
206 }
207
UnregisterSimAccountCallback(const std::string & bundleName)208 int32_t MultiSimMonitor::UnregisterSimAccountCallback(const std::string &bundleName)
209 {
210 bool isSuccess = false;
211 std::lock_guard<std::mutex> lock(mutexInner_);
212 auto iter = listSimAccountCallbackRecord_.begin();
213 for (; iter != listSimAccountCallbackRecord_.end();) {
214 if ((iter->bundleName == bundleName)) {
215 iter = listSimAccountCallbackRecord_.erase(iter);
216 isSuccess = true;
217 break;
218 }
219 iter++;
220 }
221 if (!isSuccess) {
222 TELEPHONY_LOGE("Ignore unregister action, since callback is nonexistent");
223 return TELEPHONY_ERROR;
224 }
225 TELEPHONY_LOGI("Unregister successfully, callback list size is %{public}zu", listSimAccountCallbackRecord_.size());
226 return TELEPHONY_SUCCESS;
227 }
228
NotifySimAccountChanged()229 void MultiSimMonitor::NotifySimAccountChanged()
230 {
231 TELEPHONY_LOGI("NotifySimAccountChanged");
232 bool isExisted = false;
233 std::lock_guard<std::mutex> lock(mutexInner_);
234 for (auto iter : listSimAccountCallbackRecord_) {
235 if (iter.simAccountCallback != nullptr) {
236 isExisted = true;
237 iter.simAccountCallback->OnSimAccountChanged();
238 }
239 }
240 if (!isExisted) {
241 TELEPHONY_LOGI("SimAccountCallback has not been registered");
242 }
243 }
244 } // namespace Telephony
245 } // namespace OHOS
246