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 "operator_name.h"
17
18 #include <common_event.h>
19 #include <common_event_manager.h>
20
21 #include "common_event_support.h"
22 #include "core_manager_inner.h"
23 #include "tel_ril_network_parcel.h"
24 #include "network_search_manager.h"
25 #include "operator_config_types.h"
26 #include "operator_name_utils.h"
27 #include "resource_utils.h"
28 #include "telephony_common_utils.h"
29 #include "telephony_log_wrapper.h"
30 #include "telephony_ext_wrapper.h"
31
32 using namespace OHOS::AppExecFwk;
33 using namespace OHOS::EventFwk;
34
35 namespace OHOS {
36 namespace Telephony {
37 namespace {
38 const int32_t FORMAT_IDX_SPN_CS = 0;
39 const int32_t PNN_CUST_STRING_SIZE = 2;
40 const int32_t OPL_CUST_STRING_SIZE = 4;
41 constexpr const char *CFG_DISPLAY_RULE_USE_ROAMING_FROM_NETWORK_STATE_BOOL =
42 "persist.radio.cfg.display_rule_use_roaming_from_network_state";
43 } // namespace
44
OperatorName(const EventFwk::CommonEventSubscribeInfo & sp,std::shared_ptr<NetworkSearchState> networkSearchState,std::shared_ptr<ISimManager> simManager,std::weak_ptr<NetworkSearchManager> networkSearchManager,int32_t slotId)45 OperatorName::OperatorName(const EventFwk::CommonEventSubscribeInfo &sp,
46 std::shared_ptr<NetworkSearchState> networkSearchState, std::shared_ptr<ISimManager> simManager,
47 std::weak_ptr<NetworkSearchManager> networkSearchManager, int32_t slotId)
48 : CommonEventSubscriber(sp), networkSearchState_(networkSearchState), simManager_(simManager),
49 networkSearchManager_(networkSearchManager), slotId_(slotId)
50 {
51 std::vector<std::string> vecSpnFormats;
52 ResourceUtils::Get().GetStringArrayValueByName(ResourceUtils::SPN_FORMATS, vecSpnFormats);
53 if (vecSpnFormats.size() > FORMAT_IDX_SPN_CS) {
54 csSpnFormat_ = vecSpnFormats[FORMAT_IDX_SPN_CS];
55 }
56 UpdateOperatorConfig();
57 }
58
OnReceiveEvent(const EventFwk::CommonEventData & data)59 void OperatorName::OnReceiveEvent(const EventFwk::CommonEventData &data)
60 {
61 const AAFwk::Want &want = data.GetWant();
62 std::string action = want.GetAction();
63 if (action == CommonEventSupport::COMMON_EVENT_OPERATOR_CONFIG_CHANGED) {
64 int32_t slotId = want.GetIntParam("slotId", 0);
65 if (slotId_ != slotId) {
66 return;
67 }
68 UpdateOperatorConfig();
69 sptr<NetworkState> networkState = GetNetworkStatus();
70 if (networkState != nullptr && networkState->GetRegStatus() == RegServiceState::REG_STATE_IN_SERVICE) {
71 NotifySpnChanged();
72 networkSearchState_->NotifyStateChange();
73 }
74 } else if (action == CommonEventSupport::COMMON_EVENT_LOCALE_CHANGED) {
75 TELEPHONY_LOGI("locale changed Slot%{public}d", slotId_);
76 TrySetLongOperatorNameWithTranslation();
77 auto networkSearchManager = networkSearchManager_.lock();
78 if (networkSearchManager == nullptr) {
79 TELEPHONY_LOGE("networkSearchManager is nullptr slotId:%{public}d", slotId_);
80 return;
81 }
82 if (networkSearchManager->CheckIsNeedNotify(slotId_)) {
83 networkSearchManager->ProcessNotifyStateChangeEvent(slotId_);
84 }
85 } else {
86 TELEPHONY_LOGI("OperatorName::OnReceiveEvent Slot%{public}d: action=%{public}s code=%{public}d", slotId_,
87 action.c_str(), data.GetCode());
88 }
89 }
90
HandleOperatorInfo(const std::shared_ptr<OperatorInfoResult> operatorInfoResult)91 void OperatorName::HandleOperatorInfo(const std::shared_ptr<OperatorInfoResult> operatorInfoResult)
92 {
93 auto networkSearchManager = networkSearchManager_.lock();
94 if (networkSearchManager == nullptr) {
95 TELEPHONY_LOGE("OperatorName::HandleOperatorInfo networkSearchManager is nullptr slotId:%{public}d", slotId_);
96 return;
97 }
98 if (operatorInfoResult == nullptr) {
99 TELEPHONY_LOGE("operatorInfoResult is nullptr slotId:%{public}d", slotId_);
100 return;
101 }
102 PhoneType type = networkSearchManager->GetPhoneType(slotId_);
103 if (type == PhoneType::PHONE_TYPE_IS_GSM) {
104 GsmOperatorInfo(operatorInfoResult);
105 } else if (type == PhoneType::PHONE_TYPE_IS_CDMA) {
106 CdmaOperatorInfo(operatorInfoResult);
107 } else {
108 TELEPHONY_LOGE("OperatorName::HandleOperatorInfo phone type:%{public}d invalid", type);
109 }
110 NotifySpnChanged();
111 networkSearchManager->TriggerTimezoneRefresh(slotId_);
112 }
113
GsmOperatorInfo(const std::shared_ptr<OperatorInfoResult> operatorInfoResult)114 void OperatorName::GsmOperatorInfo(const std::shared_ptr<OperatorInfoResult> operatorInfoResult)
115 {
116 std::string longName = "";
117 std::string shortName = "";
118 std::string numeric = "";
119 if (operatorInfoResult != nullptr) {
120 longName = operatorInfoResult->longName;
121 longName_ = operatorInfoResult->longName;
122 shortName = operatorInfoResult->shortName;
123 numeric = operatorInfoResult->numeric;
124 UpdateOperatorLongName(longName, numeric);
125 }
126 TELEPHONY_LOGI(
127 "OperatorName::GsmOperatorInfo longName : %{public}s, shortName : %{public}s, numeric : %{public}s "
128 "slotId:%{public}d",
129 longName.c_str(), shortName.c_str(), numeric.c_str(), slotId_);
130 if (networkSearchState_ != nullptr) {
131 networkSearchState_->SetOperatorInfo(longName, shortName, numeric, DomainType::DOMAIN_TYPE_CS);
132 networkSearchState_->SetOperatorInfo(longName, shortName, numeric, DomainType::DOMAIN_TYPE_PS);
133 }
134 }
135
CdmaOperatorInfo(const std::shared_ptr<OperatorInfoResult> operatorInfoResult)136 void OperatorName::CdmaOperatorInfo(const std::shared_ptr<OperatorInfoResult> operatorInfoResult)
137 {
138 std::string longName = "";
139 std::string shortName = "";
140 std::string numeric = "";
141 if (operatorInfoResult != nullptr) {
142 longName = operatorInfoResult->longName;
143 longName_ = operatorInfoResult->longName;
144 shortName = operatorInfoResult->shortName;
145 numeric = operatorInfoResult->numeric;
146 UpdateOperatorLongName(longName, numeric);
147 }
148 TELEPHONY_LOGI(
149 "OperatorName::CdmaOperatorInfo longName : %{public}s, shortName : %{public}s, numeric : %{public}s "
150 "slotId:%{public}d",
151 longName.c_str(), shortName.c_str(), numeric.c_str(), slotId_);
152 if (networkSearchState_ != nullptr) {
153 networkSearchState_->SetOperatorInfo(longName, shortName, numeric, DomainType::DOMAIN_TYPE_CS);
154 networkSearchState_->SetOperatorInfo(longName, shortName, numeric, DomainType::DOMAIN_TYPE_PS);
155 }
156 }
157
GetNetworkStatus()158 sptr<NetworkState> OperatorName::GetNetworkStatus()
159 {
160 sptr<NetworkState> networkStatus = nullptr;
161 if (networkSearchState_ != nullptr) {
162 std::unique_ptr<NetworkState> networkState = networkSearchState_->GetNetworkStatus();
163 if (networkState != nullptr) {
164 networkStatus = networkState.release();
165 return networkStatus;
166 }
167 }
168 TELEPHONY_LOGE("OperatorName::GetNetworkStatus networkState is nullptr slotId:%{public}d", slotId_);
169 return networkStatus;
170 }
171
172 /**
173 * 3GPP TS 51.011 V5.0.0(2001-12) 10.3.11
174 */
NotifySpnChanged(bool isForce)175 void OperatorName::NotifySpnChanged(bool isForce)
176 {
177 auto networkSearchManager = networkSearchManager_.lock();
178 if (networkSearchManager == nullptr) {
179 TELEPHONY_LOGE("OperatorName::NotifySpnChanged networkSearchManager is nullptr slotId:%{public}d", slotId_);
180 return;
181 }
182 TELEPHONY_LOGD("OperatorName::NotifySpnChanged slotId:%{public}d", slotId_);
183 std::string netPlmn = "";
184 std::string simPlmn = "";
185 std::string domesticSpn = "";
186 RegServiceState regStatus = RegServiceState::REG_STATE_UNKNOWN;
187 sptr<NetworkState> networkState = GetNetworkStatus();
188 if (networkState != nullptr) {
189 regStatus = networkState->GetRegStatus();
190 netPlmn = networkState->GetPlmnNumeric();
191 }
192 if (simManager_ != nullptr) {
193 std::u16string operatorNumeric = u"";
194 simManager_->GetSimOperatorNumeric(slotId_, operatorNumeric);
195 simPlmn = Str16ToStr8(operatorNumeric);
196 }
197 if (isDomesticRoaming(simPlmn, netPlmn)) {
198 domesticSpn = GetCustomName(simPlmn);
199 }
200
201 if (networkSearchManager->GetPhoneType(slotId_) == PhoneType::PHONE_TYPE_IS_GSM) {
202 NotifyGsmSpnChanged(regStatus, networkState, domesticSpn, isForce);
203 } else if (networkSearchManager->GetPhoneType(slotId_) == PhoneType::PHONE_TYPE_IS_CDMA) {
204 NotifyCdmaSpnChanged(regStatus, networkState, domesticSpn, isForce);
205 }
206 }
207
UpdatePlmn(RegServiceState regStatus,sptr<NetworkState> & networkState,OperatorNameParams & params)208 void OperatorName::UpdatePlmn(RegServiceState regStatus, sptr<NetworkState> &networkState, OperatorNameParams ¶ms)
209 {
210 if (networkState != nullptr) {
211 switch (regStatus) {
212 case RegServiceState::REG_STATE_IN_SERVICE:
213 params.plmn = GetPlmn(networkState, true);
214 params.showPlmn = !params.plmn.empty() &&
215 ((static_cast<uint32_t>(params.spnRule) & SpnShowType::SPN_CONDITION_DISPLAY_PLMN) ==
216 SpnShowType::SPN_CONDITION_DISPLAY_PLMN);
217 break;
218 case RegServiceState::REG_STATE_NO_SERVICE:
219 case RegServiceState::REG_STATE_EMERGENCY_ONLY:
220 case RegServiceState::REG_STATE_SEARCH:
221 if (networkState->IsEmergency()) {
222 ResourceUtils::Get().GetStringValueByName(ResourceUtils::EMERGENCY_CALLS_ONLY, params.plmn);
223 } else {
224 ResourceUtils::Get().GetStringValueByName(ResourceUtils::OUT_OF_SERIVCE, params.plmn);
225 }
226 params.showPlmn = true;
227 break;
228 case RegServiceState::REG_STATE_UNKNOWN:
229 case RegServiceState::REG_STATE_POWER_OFF:
230 default:
231 ResourceUtils::Get().GetStringValueByName(ResourceUtils::OUT_OF_SERIVCE, params.plmn);
232 params.showPlmn = true;
233 break;
234 }
235 }
236 }
237
UpdateSpn(RegServiceState regStatus,sptr<NetworkState> & networkState,OperatorNameParams & params)238 void OperatorName::UpdateSpn(RegServiceState regStatus, sptr<NetworkState> &networkState, OperatorNameParams ¶ms)
239 {
240 if (regStatus == RegServiceState::REG_STATE_IN_SERVICE) {
241 if (enableCust_ && !spnCust_.empty()) {
242 params.spn = spnCust_;
243 }
244 if (params.spn.empty()) {
245 std::u16string result = Str8ToStr16("");
246 if (simManager_ != nullptr) {
247 simManager_->GetSimSpn(slotId_, result);
248 }
249 params.spn = Str16ToStr8(result);
250 }
251 if (!csSpnFormat_.empty()) {
252 params.spn = NetworkUtils::FormatString(csSpnFormat_, params.spn.c_str());
253 }
254 params.showSpn = !params.spn.empty() &&
255 ((static_cast<uint32_t>(params.spnRule) & SpnShowType::SPN_CONDITION_DISPLAY_SPN) ==
256 SpnShowType::SPN_CONDITION_DISPLAY_SPN);
257 } else {
258 params.spn = "";
259 params.showSpn = false;
260 }
261 }
262
NotifyGsmSpnChanged(RegServiceState regStatus,sptr<NetworkState> & networkState,const std::string & domesticSpn,bool isForce)263 void OperatorName::NotifyGsmSpnChanged(
264 RegServiceState regStatus, sptr<NetworkState> &networkState, const std::string &domesticSpn, bool isForce)
265 {
266 if (networkState == nullptr) {
267 TELEPHONY_LOGE("OperatorName::NotifyGsmSpnChanged networkState is nullptr slotId:%{public}d", slotId_);
268 return;
269 }
270
271 OperatorNameParams params = {false, "", false, "", 0};
272 params.spnRule = static_cast<int32_t>(GetSpnRule(networkState));
273 if (slotId_ == static_cast<int32_t>(SimSlotType::VSIM_SLOT_ID)) {
274 UpdateVSimSpn(params);
275 }
276 UpdatePlmn(regStatus, networkState, params);
277 UpdateSpn(regStatus, networkState, params);
278
279 if (TELEPHONY_EXT_WRAPPER.updateOperatorNameParamsExt_ != nullptr) {
280 TELEPHONY_EXT_WRAPPER.updateOperatorNameParamsExt_(slotId_, networkState, params);
281 }
282
283 if (params.spn.empty() && !params.plmn.empty()) {
284 params.showPlmn = true;
285 }
286 if (params.showPlmn && params.spn == params.plmn) {
287 params.showSpn = false;
288 }
289 SetOperatorNameByParams(params);
290 if (IsShouldNotify(regStatus, params, isForce)) {
291 TELEPHONY_LOGI("OperatorName::NotifyGsmSpnChanged start send broadcast slotId:%{public}d...", slotId_);
292 bool isSatelliteOn = CoreManagerInner::GetInstance().IsSatelliteEnabled();
293 if (isSatelliteOn && !domesticSpn.empty()) {
294 params.showSpn = false;
295 params.plmn = domesticSpn;
296 std::string emptyDomesticSpn = "";
297 PublishEvent(params, regStatus, emptyDomesticSpn);
298 } else {
299 PublishEvent(params, regStatus, domesticSpn);
300 }
301 } else {
302 TELEPHONY_LOGD(
303 "OperatorName::NotifyGsmSpnChanged spn no changed, not need to update slotId:%{public}d", slotId_);
304 }
305 }
306
UpdateVSimSpn(OperatorNameParams & params)307 void OperatorName::UpdateVSimSpn(OperatorNameParams ¶ms)
308 {
309 if (TELEPHONY_EXT_WRAPPER.getVSimSlotId_ && TELEPHONY_EXT_WRAPPER.changeSpnAndRuleExt_) {
310 int vSimSlotId = static_cast<int>(SimSlotType::INVALID_SLOT_ID);
311 TELEPHONY_EXT_WRAPPER.getVSimSlotId_(vSimSlotId);
312 if (vSimSlotId == static_cast<int>(SimSlotType::VSIM_SLOT_ID)) {
313 TELEPHONY_EXT_WRAPPER.changeSpnAndRuleExt_(params.spn, params.spnRule, params.showSpn);
314 }
315 }
316 }
317
NotifyCdmaSpnChanged(RegServiceState regStatus,sptr<NetworkState> & networkState,const std::string & domesticSpn,bool isForce)318 void OperatorName::NotifyCdmaSpnChanged(
319 RegServiceState regStatus, sptr<NetworkState> &networkState, const std::string &domesticSpn, bool isForce)
320 {
321 if (networkState == nullptr) {
322 TELEPHONY_LOGE("OperatorName::NotifyCdmaSpnChanged networkState is nullptr slotId:%{public}d", slotId_);
323 return;
324 }
325
326 OperatorNameParams params = {false, "", false, "", 0};
327 std::string numeric = networkState->GetPlmnNumeric();
328 if (regStatus == RegServiceState::REG_STATE_IN_SERVICE) {
329 params.plmn = GetCustomName(numeric);
330 if (params.plmn.empty()) {
331 params.plmn = networkState->GetLongOperatorName();
332 }
333 if (!csSpnFormat_.empty()) {
334 params.plmn = NetworkUtils::FormatString(csSpnFormat_, params.plmn.c_str());
335 }
336 } else if (regStatus != RegServiceState::REG_STATE_POWER_OFF) {
337 ResourceUtils::Get().GetStringValueByName(ResourceUtils::OUT_OF_SERIVCE, params.plmn);
338 }
339 params.showPlmn = !params.plmn.empty();
340 SetOperatorNameByParams(params);
341 if (IsShouldNotify(regStatus, params, isForce)) {
342 TELEPHONY_LOGI("OperatorName::NotifyCdmaSpnChanged start send broadcast slotId:%{public}d...", slotId_);
343 PublishEvent(params, regStatus, domesticSpn);
344 } else {
345 TELEPHONY_LOGI(
346 "OperatorName::NotifyCdmaSpnChanged spn no changed, not need to update slotId:%{public}d", slotId_);
347 }
348 }
349
SetOperatorNameByParams(OperatorNameParams & params)350 void OperatorName::SetOperatorNameByParams(OperatorNameParams ¶ms)
351 {
352 std::string showName;
353 if (params.showPlmn) {
354 showName = params.plmn;
355 } else if (params.showSpn) {
356 showName = params.spn;
357 }
358 if (!showName.empty()) {
359 SetOperatorName(showName);
360 }
361 }
362
SetOperatorName(const std::string & operatorName)363 void OperatorName::SetOperatorName(const std::string &operatorName)
364 {
365 if (networkSearchState_ != nullptr) {
366 networkSearchState_->SetLongOperatorName(operatorName, DomainType::DOMAIN_TYPE_CS);
367 networkSearchState_->SetLongOperatorName(operatorName, DomainType::DOMAIN_TYPE_PS);
368 }
369 }
PublishEvent(OperatorNameParams params,const RegServiceState state,const std::string & domesticSpn)370 void OperatorName::PublishEvent(OperatorNameParams params, const RegServiceState state, const std::string &domesticSpn)
371 {
372 Want want;
373 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_SPN_INFO_CHANGED);
374 want.SetParam(CUR_SLOT_ID, slotId_);
375 want.SetParam(CUR_PLMN_SHOW, params.showPlmn);
376 want.SetParam(CUR_PLMN, params.plmn);
377 want.SetParam(CUR_SPN_SHOW, params.showSpn);
378 want.SetParam(CUR_SPN, params.spn);
379 want.SetParam(DOMESTIC_SPN, domesticSpn);
380 CommonEventData data;
381 data.SetWant(want);
382
383 CommonEventPublishInfo publishInfo;
384 publishInfo.SetSticky(true);
385 bool publishResult = CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
386 if (TELEPHONY_EXT_WRAPPER.publishSpnInfoChangedExt_ != nullptr) {
387 TELEPHONY_EXT_WRAPPER.publishSpnInfoChangedExt_(want);
388 }
389 TELEPHONY_LOGI("OperatorName::PublishEvent result : %{public}d slotId:%{public}d", publishResult, slotId_);
390 if (publishResult) {
391 std::unique_lock<ffrt::shared_mutex> lock(mutex_);
392 curRegState_ = state;
393 curParams_.spnRule = params.spnRule;
394 curParams_.spn = params.spn;
395 curParams_.showSpn = params.showSpn;
396 curParams_.plmn = params.plmn;
397 curParams_.showPlmn = params.showPlmn;
398 }
399 }
400
GetPlmn(const sptr<NetworkState> & networkState,bool longNameRequired)401 std::string OperatorName::GetPlmn(const sptr<NetworkState> &networkState, bool longNameRequired)
402 {
403 if (networkState == nullptr) {
404 TELEPHONY_LOGE("OperatorName::GetPlmn networkState is nullptr slotId:%{public}d", slotId_);
405 return "";
406 }
407 std::string plmn = "";
408 std::string numeric = networkState->GetPlmnNumeric();
409 bool roaming = networkState->IsRoaming();
410 int32_t lac = GetCurrentLac();
411 plmn = GetCustomName(numeric);
412 if (plmn.empty()) {
413 plmn = GetCustEons(numeric, lac, roaming, longNameRequired);
414 }
415 if (plmn.empty()) {
416 plmn = GetEons(numeric, lac, longNameRequired);
417 }
418 if (plmn.empty()) {
419 plmn = networkState->GetLongOperatorName();
420 }
421 std::string operatorLongName = longName_;
422 if (TELEPHONY_EXT_WRAPPER.processOperatorName_ != nullptr) {
423 netPriCust_ = TELEPHONY_EXT_WRAPPER.processOperatorName_(slotId_, operatorLongName, numeric);
424 TELEPHONY_LOGI("OperatorName::GetPlmn netPriCust_:%{public}d", netPriCust_);
425 }
426 if (netPriCust_) {
427 plmn = operatorLongName;
428 }
429 TELEPHONY_LOGD(
430 "OperatorName::GetPlmn lac:%{public}d, numeric:%{public}s, longNameRequired:%{public}d, plmn:%{public}s", lac,
431 numeric.c_str(), longNameRequired, plmn.c_str());
432 return plmn;
433 }
434
GetEons(const std::string & numeric,int32_t lac,bool longNameRequired)435 std::string OperatorName::GetEons(const std::string &numeric, int32_t lac, bool longNameRequired)
436 {
437 if (simManager_ == nullptr) {
438 TELEPHONY_LOGE("OperatorName::GetEons simManager_ is nullptr slotId:%{public}d", slotId_);
439 return "";
440 }
441 return Str16ToStr8(simManager_->GetSimEons(slotId_, numeric, lac, longNameRequired));
442 }
443
GetRoamStateBySimFile(const std::string & netPlmn)444 bool OperatorName::GetRoamStateBySimFile(const std::string &netPlmn)
445 {
446 if (netPlmn.empty() || simManager_ == nullptr) {
447 return false;
448 }
449 std::u16string operatorNumeric = u"";
450 int32_t errorCode = simManager_->GetSimOperatorNumeric(slotId_, operatorNumeric);
451 if (errorCode != 0 || operatorNumeric.empty()) {
452 return false;
453 }
454 std::string simPlmn = Str16ToStr8(operatorNumeric);
455 if (simPlmn == netPlmn) {
456 return false;
457 }
458 std::set<std::string> ehPlmns;
459 simManager_->GetEhPlmns(slotId_, ehPlmns);
460 auto it = ehPlmns.find(netPlmn);
461 if (it != ehPlmns.end()) {
462 return false;
463 }
464 std::set<std::string> spdiPlmns;
465 simManager_->GetSpdiPlmns(slotId_, spdiPlmns);
466 it = spdiPlmns.find(netPlmn);
467 if (it != spdiPlmns.end()) {
468 return false;
469 }
470 return true;
471 }
472
GetSpnRule(sptr<NetworkState> & networkState)473 unsigned int OperatorName::GetSpnRule(sptr<NetworkState> &networkState)
474 {
475 int32_t spnRule = 0;
476 bool roaming = false;
477 bool useRoamingFromNetworkState =
478 system::GetBoolParameter(CFG_DISPLAY_RULE_USE_ROAMING_FROM_NETWORK_STATE_BOOL, false);
479 if (useRoamingFromNetworkState) {
480 roaming = networkState->IsRoaming();
481 } else {
482 std::string netPlmn = networkState->GetPlmnNumeric();
483 roaming = GetRoamStateBySimFile(netPlmn);
484 }
485 if (enableCust_ && displayConditionCust_ != SPN_INVALID) {
486 spnRule = static_cast<int32_t>(GetCustSpnRule(roaming));
487 } else if (!networkState->IsRoaming() && IsChinaCard()) {
488 spnRule = static_cast<int32_t>(SPN_CONDITION_DISPLAY_PLMN);
489 } else {
490 std::string numeric = networkState->GetPlmnNumeric();
491 if (simManager_ != nullptr) {
492 spnRule = simManager_->ObtainSpnCondition(slotId_, roaming, numeric);
493 }
494 }
495 return spnRule;
496 }
497
GetCustSpnRule(bool roaming)498 unsigned int OperatorName::GetCustSpnRule(bool roaming)
499 {
500 unsigned int cond = 0;
501 if (displayConditionCust_ <= SPN_INVALID) {
502 return cond;
503 }
504 if (roaming) {
505 cond = SPN_CONDITION_DISPLAY_PLMN;
506 if ((static_cast<unsigned int>(displayConditionCust_) & static_cast<unsigned int>(SPN_COND)) == 0) {
507 cond |= static_cast<unsigned int>(SPN_CONDITION_DISPLAY_SPN);
508 }
509 } else {
510 cond = SPN_CONDITION_DISPLAY_SPN;
511 if ((static_cast<unsigned int>(displayConditionCust_) & static_cast<unsigned int>(SPN_COND_PLMN)) ==
512 SPN_COND_PLMN) {
513 cond |= static_cast<unsigned int>(SPN_CONDITION_DISPLAY_PLMN);
514 }
515 }
516 return cond;
517 }
518
GetCustEons(const std::string & numeric,int32_t lac,bool roaming,bool longNameRequired)519 std::string OperatorName::GetCustEons(const std::string &numeric, int32_t lac, bool roaming, bool longNameRequired)
520 {
521 if (!enableCust_ || numeric.empty() || pnnCust_.empty() || (oplCust_.empty() && roaming)) {
522 TELEPHONY_LOGI("OperatorName::GetCustEons is empty");
523 return "";
524 }
525 int32_t pnnIndex = 1;
526 for (std::shared_ptr<OperatorPlmnInfo> opl : oplCust_) {
527 if (opl == nullptr) {
528 continue;
529 }
530 pnnIndex = -1;
531 TELEPHONY_LOGI(
532 "OperatorName::GetCustEons numeric:%{public}s, opl->plmnNumeric:%{public}s, lac:%{public}d, "
533 "opl->lacStart:%{public}d, opl->lacEnd:%{public}d, opl->pnnRecordId:%{public}d",
534 numeric.c_str(), opl->plmnNumeric.c_str(), lac, opl->lacStart, opl->lacEnd, opl->pnnRecordId);
535 if (numeric.compare(opl->plmnNumeric) == 0 &&
536 ((opl->lacStart == 0 && opl->lacEnd == 0xfffe) || (opl->lacStart <= lac && opl->lacEnd >= lac))) {
537 pnnIndex = opl->pnnRecordId;
538 break;
539 }
540 }
541 TELEPHONY_LOGI("OperatorName::GetCustEons pnnIndex:%{public}d", pnnIndex);
542 std::string custEonsName = "";
543 if (pnnIndex >= 1 && pnnIndex <= (int32_t)pnnCust_.size()) {
544 TELEPHONY_LOGI(
545 "OperatorName::GetCustEons longNameRequired:%{public}d, longName:%{public}s, shortName:%{public}s,",
546 longNameRequired, pnnCust_.at(pnnIndex - 1)->longName.c_str(),
547 pnnCust_.at(pnnIndex - 1)->shortName.c_str());
548 if (longNameRequired) {
549 custEonsName = pnnCust_.at(pnnIndex - 1)->longName;
550 } else {
551 custEonsName = pnnCust_.at(pnnIndex - 1)->shortName;
552 }
553 }
554 return custEonsName;
555 }
556
GetCustomName(const std::string & numeric)557 std::string OperatorName::GetCustomName(const std::string &numeric)
558 {
559 return OperatorNameUtils::GetInstance().GetCustomName(numeric);
560 }
561
isDomesticRoaming(const std::string & simPlmn,const std::string & netPlmn)562 bool OperatorName::isDomesticRoaming(const std::string &simPlmn, const std::string &netPlmn)
563 {
564 if (isCMCard(simPlmn) && isCMDomestic(netPlmn)) {
565 return true;
566 }
567 if (isCUCard(simPlmn) && isCUDomestic(netPlmn)) {
568 return true;
569 }
570 if (isCTCard(simPlmn) && isCTDomestic(netPlmn)) {
571 return true;
572 }
573 if (isCBCard(simPlmn) && isCBDomestic(netPlmn)) {
574 return true;
575 }
576 TELEPHONY_LOGD("simPlmn not match netPlmn");
577 return false;
578 }
579
isCMCard(const std::string & numeric)580 bool OperatorName::isCMCard(const std::string &numeric)
581 {
582 if (numeric.empty()) {
583 return false;
584 }
585 auto obj = std::find(cmMccMnc_.begin(), cmMccMnc_.end(), numeric);
586 if (obj != cmMccMnc_.end()) {
587 TELEPHONY_LOGD("is CM card");
588 return true;
589 }
590 return false;
591 }
592
isCUCard(const std::string & numeric)593 bool OperatorName::isCUCard(const std::string &numeric)
594 {
595 if (numeric.empty()) {
596 return false;
597 }
598 auto obj = std::find(cuMccMnc_.begin(), cuMccMnc_.end(), numeric);
599 if (obj != cuMccMnc_.end()) {
600 TELEPHONY_LOGD("is CU card");
601 return true;
602 }
603 return false;
604 }
605
isCTCard(const std::string & numeric)606 bool OperatorName::isCTCard(const std::string &numeric)
607 {
608 if (numeric.empty()) {
609 return false;
610 }
611 auto obj = std::find(ctMccMnc_.begin(), ctMccMnc_.end(), numeric);
612 if (obj != ctMccMnc_.end()) {
613 TELEPHONY_LOGD("is CT card");
614 return true;
615 }
616 return false;
617 }
618
isCBCard(const std::string & numeric)619 bool OperatorName::isCBCard(const std::string &numeric)
620 {
621 if (numeric.empty()) {
622 return false;
623 }
624 auto obj = std::find(cbnMccMnc_.begin(), cbnMccMnc_.end(), numeric);
625 if (obj != cbnMccMnc_.end()) {
626 TELEPHONY_LOGD("is CB card");
627 return true;
628 }
629 return false;
630 }
631
isCMDomestic(const std::string & numeric)632 bool OperatorName::isCMDomestic(const std::string &numeric)
633 {
634 if (numeric.empty()) {
635 return false;
636 }
637 auto obj = std::find(cmDomesticMccMnc_.begin(), cmDomesticMccMnc_.end(), numeric);
638 if (obj != cmDomesticMccMnc_.end()) {
639 TELEPHONY_LOGD("is CM domestic");
640 return true;
641 }
642 return false;
643 }
644
isCUDomestic(const std::string & numeric)645 bool OperatorName::isCUDomestic(const std::string &numeric)
646 {
647 if (numeric.empty()) {
648 return false;
649 }
650 auto obj = std::find(cuDomesticMccMnc_.begin(), cuDomesticMccMnc_.end(), numeric);
651 if (obj != cuDomesticMccMnc_.end()) {
652 TELEPHONY_LOGD("is CU domestic");
653 return true;
654 }
655 return false;
656 }
657
isCTDomestic(const std::string & numeric)658 bool OperatorName::isCTDomestic(const std::string &numeric)
659 {
660 if (numeric.empty()) {
661 return false;
662 }
663 auto obj = std::find(ctDomesticMccMnc_.begin(), ctDomesticMccMnc_.end(), numeric);
664 if (obj != ctDomesticMccMnc_.end()) {
665 TELEPHONY_LOGD("is CT domestic");
666 return true;
667 }
668 return false;
669 }
670
isCBDomestic(const std::string & numeric)671 bool OperatorName::isCBDomestic(const std::string &numeric)
672 {
673 if (numeric.empty()) {
674 return false;
675 }
676 auto obj = std::find(cbDomesticnMccMnc_.begin(), cbDomesticnMccMnc_.end(), numeric);
677 if (obj != cbDomesticnMccMnc_.end()) {
678 TELEPHONY_LOGD("is CB domestic");
679 return true;
680 }
681 return false;
682 }
683
IsChinaCard()684 bool OperatorName::IsChinaCard()
685 {
686 std::string simPlmn = "";
687 if (simManager_ != nullptr) {
688 std::u16string operatorNumeric = u"";
689 simManager_->GetSimOperatorNumeric(slotId_, operatorNumeric);
690 simPlmn = Str16ToStr8(operatorNumeric);
691 }
692 return isCMCard(simPlmn) || isCTCard(simPlmn) || isCBCard(simPlmn);
693 }
694
GetCurrentLac()695 int32_t OperatorName::GetCurrentLac()
696 {
697 auto networkSearchManager = networkSearchManager_.lock();
698 if (networkSearchManager == nullptr) {
699 TELEPHONY_LOGE("OperatorName::GetCurrentLac networkSearchManager is nullptr slotId:%{public}d", slotId_);
700 return 0;
701 }
702 sptr<CellLocation> location = networkSearchManager->GetCellLocation(slotId_);
703 if (location == nullptr) {
704 TELEPHONY_LOGE("OperatorName::GetCurrentLac location is nullptr slotId:%{public}d", slotId_);
705 return 0;
706 }
707 if (location->GetCellLocationType() != CellLocation::CellType::CELL_TYPE_GSM) {
708 TELEPHONY_LOGE("OperatorName::GetCurrentLac location type isn't GSM slotId:%{public}d", slotId_);
709 return 0;
710 }
711 sptr<GsmCellLocation> gsmLocation = sptr<GsmCellLocation>(static_cast<GsmCellLocation *>(location.GetRefPtr()));
712 if (gsmLocation == nullptr) {
713 TELEPHONY_LOGE("OperatorName::GetCurrentLac gsmLocation is nullptr slotId:%{public}d", slotId_);
714 return 0;
715 }
716 return gsmLocation->GetLac();
717 }
718
UpdateOperatorConfig()719 void OperatorName::UpdateOperatorConfig()
720 {
721 OperatorConfig operatorConfig;
722 CoreManagerInner::GetInstance().GetOperatorConfigs(slotId_, operatorConfig);
723 if (operatorConfig.boolValue.find(KEY_ENABLE_OPERATOR_NAME_CUST_BOOL) != operatorConfig.boolValue.end()) {
724 enableCust_ = operatorConfig.boolValue[KEY_ENABLE_OPERATOR_NAME_CUST_BOOL];
725 }
726 if (operatorConfig.stringValue.find(KEY_OPERATOR_NAME_CUST_STRING) != operatorConfig.stringValue.end()) {
727 spnCust_ = operatorConfig.stringValue[KEY_OPERATOR_NAME_CUST_STRING];
728 }
729 if (operatorConfig.intValue.find(KEY_SPN_DISPLAY_CONDITION_CUST_INT) != operatorConfig.intValue.end()) {
730 displayConditionCust_ = operatorConfig.intValue[KEY_SPN_DISPLAY_CONDITION_CUST_INT];
731 }
732 if (operatorConfig.stringArrayValue.find(KEY_PNN_CUST_STRING_ARRAY) != operatorConfig.stringArrayValue.end()) {
733 UpdatePnnCust(operatorConfig.stringArrayValue[KEY_PNN_CUST_STRING_ARRAY]);
734 }
735 if (operatorConfig.stringArrayValue.find(KEY_OPL_CUST_STRING_ARRAY) != operatorConfig.stringArrayValue.end()) {
736 UpdateOplCust(operatorConfig.stringArrayValue[KEY_OPL_CUST_STRING_ARRAY]);
737 }
738 }
739
UpdatePnnCust(const std::vector<std::string> & pnnCust)740 void OperatorName::UpdatePnnCust(const std::vector<std::string> &pnnCust)
741 {
742 pnnCust_.clear();
743 if (pnnCust.empty()) {
744 TELEPHONY_LOGE("OperatorName::UpdatePnnCust pnnCust is empty slotId:%{public}d", slotId_);
745 return;
746 }
747 for (const auto &data : pnnCust) {
748 TELEPHONY_LOGI("OperatorName::UpdatePnnCust: %{public}s", data.c_str());
749 std::vector<std::string> pnnString = NetworkUtils::SplitString(data, ",");
750 if (pnnString.size() != PNN_CUST_STRING_SIZE) {
751 continue;
752 }
753 std::shared_ptr<PlmnNetworkName> pnn = std::make_shared<PlmnNetworkName>();
754 pnn->shortName = pnnString.back();
755 pnnString.pop_back();
756 pnn->longName = pnnString.back();
757 if (!pnn->longName.empty() || !pnn->shortName.empty()) {
758 pnnCust_.push_back(pnn);
759 }
760 }
761 }
762
UpdateOplCust(const std::vector<std::string> & oplCust)763 void OperatorName::UpdateOplCust(const std::vector<std::string> &oplCust)
764 {
765 oplCust_.clear();
766 if (oplCust.empty()) {
767 TELEPHONY_LOGE("OperatorName::UpdateOplCust oplCust is empty slotId:%{public}d", slotId_);
768 return;
769 }
770 for (const auto &data : oplCust) {
771 TELEPHONY_LOGI("OperatorName::UpdateOplCust: %{public}s", data.c_str());
772 std::vector<std::string> oplString = NetworkUtils::SplitString(data, ",");
773 if (oplString.size() != OPL_CUST_STRING_SIZE || oplString.back().empty()) {
774 continue;
775 }
776 std::shared_ptr<OperatorPlmnInfo> opl = std::make_shared<OperatorPlmnInfo>();
777 bool isSuccess = ConvertStrToInt(oplString.back(), opl->pnnRecordId, HEX_TYPE);
778 oplString.pop_back();
779 if (!isSuccess || oplString.back().empty()) {
780 continue;
781 }
782 isSuccess = ConvertStrToInt(oplString.back(), opl->lacEnd, HEX_TYPE);
783 oplString.pop_back();
784 if (!isSuccess || oplString.back().empty()) {
785 continue;
786 }
787 isSuccess = ConvertStrToInt(oplString.back(), opl->lacStart, HEX_TYPE);
788 oplString.pop_back();
789 opl->plmnNumeric = oplString.back();
790 if (isSuccess && !opl->plmnNumeric.empty()) {
791 oplCust_.push_back(opl);
792 }
793 }
794 }
795
UpdateOperatorLongName(std::string & operatorLongName,const std::string & numeric)796 void OperatorName::UpdateOperatorLongName(std::string &operatorLongName, const std::string &numeric)
797 {
798 sptr<NetworkState> networkState = GetNetworkStatus();
799 if (networkState == nullptr) {
800 return;
801 }
802
803 RegServiceState regStatus = networkState->GetRegStatus();
804 if (regStatus != RegServiceState::REG_STATE_IN_SERVICE) {
805 return;
806 }
807
808 operatorLongName = longName_;
809 if (TELEPHONY_EXT_WRAPPER.processOperatorName_ != nullptr) {
810 netPriCust_ = TELEPHONY_EXT_WRAPPER.processOperatorName_(slotId_, operatorLongName, numeric);
811 TELEPHONY_LOGI("OperatorName::UpdateOperatorLongName netPriCust_:%{public}d", netPriCust_);
812 }
813
814 std::string customizedOperatorLongName = GetCustomName(numeric);
815 if (!customizedOperatorLongName.empty() && !netPriCust_) {
816 operatorLongName = customizedOperatorLongName;
817 }
818 }
819
TrySetLongOperatorNameWithTranslation()820 void OperatorName::TrySetLongOperatorNameWithTranslation()
821 {
822 sptr<NetworkState> networkState = GetNetworkStatus();
823 if (networkState != nullptr && networkSearchState_ != nullptr) {
824 std::string longOperatorName = networkState->GetLongOperatorName();
825 std::string numeric = networkState->GetPlmnNumeric();
826 UpdateOperatorLongName(longOperatorName, numeric);
827 SetOperatorName(longOperatorName);
828 }
829 NotifySpnChanged();
830 }
831
IsShouldNotify(const RegServiceState & regStatus,const OperatorNameParams & params,bool isForce)832 bool OperatorName::IsShouldNotify(const RegServiceState ®Status, const OperatorNameParams ¶ms, bool isForce)
833 {
834 std::shared_lock<ffrt::shared_mutex> lock(mutex_);
835 bool shouldNotify = false;
836 if (networkSearchState_ != nullptr) {
837 shouldNotify = !networkSearchState_->IsProcessNetworkState() && (
838 isForce || curParams_.spnRule != params.spnRule || curRegState_ != regStatus ||
839 curParams_.showSpn != params.showSpn || curParams_.showPlmn != params.showPlmn ||
840 curParams_.spn.compare(params.spn) || curParams_.plmn.compare(params.plmn)
841 );
842 } else {
843 TELEPHONY_LOGE("OperatorName::IsShouldNotify networkSearchState_ = nullptr.");
844 shouldNotify = isForce || curParams_.spnRule != params.spnRule || curRegState_ != regStatus ||
845 curParams_.showSpn != params.showSpn || curParams_.showPlmn != params.showPlmn ||
846 curParams_.spn.compare(params.spn) || curParams_.plmn.compare(params.plmn);
847 }
848 TELEPHONY_LOGI(
849 "OperatorName::IsShouldNotify showSpn:%{public}d curSpn_:%{public}s spn:%{public}s "
850 "showPlmn:%{public}d curPlmn_:%{public}s plmn:%{public}s isForce:%{public}d shouldNotify:%{public}d "
851 "slotId:%{public}d", params.showSpn, curParams_.spn.c_str(), params.spn.c_str(), params.showPlmn,
852 curParams_.plmn.c_str(), params.plmn.c_str(), isForce, shouldNotify, slotId_);
853 return shouldNotify;
854 }
855 } // namespace Telephony
856 } // namespace OHOS
857