• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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_account_manager.h"
17 
18 #include "string_ex.h"
19 
20 namespace OHOS {
21 namespace Telephony {
SimAccountManager(std::shared_ptr<Telephony::ITelRilManager> telRilManager,std::shared_ptr<SimStateManager> simStateManager,std::shared_ptr<SimFileManager> simFileManager)22 SimAccountManager::SimAccountManager(std::shared_ptr<Telephony::ITelRilManager> telRilManager,
23     std::shared_ptr<SimStateManager> simStateManager, std::shared_ptr<SimFileManager> simFileManager)
24     : telRilManager_(telRilManager), simStateManager_(simStateManager), simFileManager_(simFileManager)
25 {
26     TELEPHONY_LOGI("SimAccountManager construct");
27 }
28 
~SimAccountManager()29 SimAccountManager::~SimAccountManager()
30 {
31     if (simStateTracker_ != nullptr) {
32         simStateTracker_->UnRegisterForIccLoaded();
33     }
34     if (operatorConfigCache_ != nullptr) {
35         operatorConfigCache_->UnRegisterForIccChange();
36     }
37 }
38 
Init(int32_t slotId)39 void SimAccountManager::Init(int32_t slotId)
40 {
41     TELEPHONY_LOGI("SimAccountManager::Init = %{public}d", slotId);
42     if ((telRilManager_ == nullptr) || (simFileManager_ == nullptr) || (simStateManager_ == nullptr)) {
43         TELEPHONY_LOGE("can not init simAccountManager");
44         return;
45     }
46     if (!IsValidSlotId(slotId)) {
47         TELEPHONY_LOGE("SimAccountManager::init SimAccountManager invalid slotId = %{public}d", slotId);
48         return;
49     }
50     operatorConfigCacheRunner_ = AppExecFwk::EventRunner::Create("OperatorConfigCache");
51     if (operatorConfigCacheRunner_.get() == nullptr) {
52         TELEPHONY_LOGE("SimAccountManager::Init operatorConfigCacheRunner_ failed");
53         return;
54     }
55     operatorConfigCache_ = std::make_shared<OperatorConfigCache>(operatorConfigCacheRunner_, simFileManager_, slotId);
56     if (operatorConfigCache_ == nullptr) {
57         TELEPHONY_LOGE("SimAccountManager::operatorConfigCache_ is null");
58         return;
59     }
60     operatorConfigCache_->RegisterForIccChange();
61     simStateTrackerRunner_ = AppExecFwk::EventRunner::Create("SimStateTracker");
62     if (simStateTrackerRunner_.get() == nullptr) {
63         TELEPHONY_LOGE("SimAccountManager::Init simStateTrackerRunner_ failed");
64         return;
65     }
66     simStateTracker_ =
67         std::make_shared<SimStateTracker>(simStateTrackerRunner_, simFileManager_, operatorConfigCache_, slotId);
68     if (simStateTracker_ == nullptr) {
69         TELEPHONY_LOGE("SimAccountManager::simStateTracker_ is null");
70         return;
71     }
72     simStateTracker_->RegisterForIccLoaded();
73 }
74 
GetOperatorConfigs(int32_t slotId,OHOS::Telephony::OperatorConfig & poc)75 int32_t SimAccountManager::GetOperatorConfigs(int32_t slotId, OHOS::Telephony::OperatorConfig &poc)
76 {
77     TELEPHONY_LOGI("SimAccountManager::GetOperatorConfigs");
78     if (operatorConfigCache_ == nullptr) {
79         TELEPHONY_LOGE("SimAccountManager::GetOperatorConfigs operatorConfigCache_ is null");
80         return TELEPHONY_ERR_LOCAL_PTR_NULL;
81     }
82     return operatorConfigCache_->GetOperatorConfigs(static_cast<int32_t>(slotId), poc);
83 }
84 
IsValidSlotId(int32_t slotId)85 bool SimAccountManager::IsValidSlotId(int32_t slotId)
86 {
87     int32_t count = SIM_SLOT_COUNT;
88     if ((slotId >= DEFAULT_SIM_SLOT_ID) && (slotId < count)) {
89         return true;
90     } else {
91         TELEPHONY_LOGE("SimAccountManager slotId is InValid = %{public}d", slotId);
92         return false;
93     }
94 }
95 
IsValidSlotIdForDefault(int32_t slotId)96 bool SimAccountManager::IsValidSlotIdForDefault(int32_t slotId)
97 {
98     int32_t count = SIM_SLOT_COUNT;
99     if ((slotId >= DEFAULT_SIM_SLOT_ID_REMOVE) && (slotId < count)) {
100         return true;
101     } else {
102         return false;
103     }
104 }
105 
HasOperatorPrivileges(const int32_t slotId,bool & hasOperatorPrivileges)106 int32_t SimAccountManager::HasOperatorPrivileges(const int32_t slotId, bool &hasOperatorPrivileges)
107 {
108     TELEPHONY_LOGI("SimAccountManager::HasOperatorPrivileges begin");
109     if (privilegeController_ != nullptr) {
110         return privilegeController_->HasOperatorPrivileges(hasOperatorPrivileges);
111     }
112     if (privilegesRunner_.get() == nullptr) {
113         TELEPHONY_LOGE("make privilegesRunner_");
114         privilegesRunner_ = AppExecFwk::EventRunner::Create("PrivilegeController");
115     }
116     if ((privilegesRunner_ == nullptr) || (telRilManager_ == nullptr) || (simStateManager_ == nullptr)) {
117         TELEPHONY_LOGE("has nullptr at privilegesRunner_ or telRilManager_ or simStateManager_");
118         return TELEPHONY_ERR_LOCAL_PTR_NULL;
119     }
120     auto controller =
121         std::make_shared<IccOperatorPrivilegeController>(privilegesRunner_, telRilManager_, simStateManager_);
122     if (controller == nullptr) {
123         TELEPHONY_LOGE("Make IccOperatorPrivilegeController fail!!");
124         return TELEPHONY_ERR_LOCAL_PTR_NULL;
125     }
126     controller->Init(slotId);
127     privilegeController_ = controller;
128     return controller->HasOperatorPrivileges(hasOperatorPrivileges);
129 }
130 } // namespace Telephony
131 } // namespace OHOS
132