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