• 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 "cellular_data_rdb_helper.h"
17 
18 #include "cellular_data_constant.h"
19 #include "core_manager_inner.h"
20 #include "telephony_log_wrapper.h"
21 
22 namespace OHOS {
23 namespace Telephony {
CellularDataRdbHelper()24 CellularDataRdbHelper::CellularDataRdbHelper() : cellularDataUri_(CELLULAR_DATA_RDB_SELECTION) {}
25 
26 CellularDataRdbHelper::~CellularDataRdbHelper() = default;
27 
CreateDataAbilityHelper()28 std::shared_ptr<DataShare::DataShareHelper> CellularDataRdbHelper::CreateDataAbilityHelper()
29 {
30     TELEPHONY_LOGI("Create data ability helper");
31     sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
32     if (saManager == nullptr) {
33         TELEPHONY_LOGE("GetSystemAbilityManager failed.");
34         return nullptr;
35     }
36     sptr<IRemoteObject> remoteObj = saManager->GetSystemAbility(TELEPHONY_CELLULAR_DATA_SYS_ABILITY_ID);
37     if (remoteObj == nullptr) {
38         TELEPHONY_LOGE("GetSystemAbility Service Failed.");
39         return nullptr;
40     }
41     return DataShare::DataShareHelper::Creator(remoteObj, CELLULAR_DATA_RDB_URI);
42 }
43 
Update(const DataShare::DataShareValuesBucket & value,const DataShare::DataSharePredicates & predicates)44 int CellularDataRdbHelper::Update(
45     const DataShare::DataShareValuesBucket &value, const DataShare::DataSharePredicates &predicates)
46 {
47     std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataAbilityHelper();
48     if (dataShareHelper == nullptr) {
49         TELEPHONY_LOGE("dataShareHelper is null");
50         return NULL_POINTER_EXCEPTION;
51     }
52     TELEPHONY_LOGI("Cellular data RDB helper update");
53     int32_t result = dataShareHelper->Update(cellularDataUri_, predicates, value);
54     dataShareHelper->Release();
55     return result;
56 }
57 
Insert(const DataShare::DataShareValuesBucket & values)58 int CellularDataRdbHelper::Insert(const DataShare::DataShareValuesBucket &values)
59 {
60     std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataAbilityHelper();
61     if (dataShareHelper == nullptr) {
62         TELEPHONY_LOGE("dataShareHelper is null");
63         return NULL_POINTER_EXCEPTION;
64     }
65     TELEPHONY_LOGI("Cellular data RDB helper insert");
66     int32_t result = dataShareHelper->Insert(cellularDataUri_, values);
67     dataShareHelper->Release();
68     return result;
69 }
70 
ResetApns()71 bool CellularDataRdbHelper::ResetApns()
72 {
73     std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataAbilityHelper();
74     if (dataShareHelper == nullptr) {
75         TELEPHONY_LOGE("dataShareHelper is null");
76         return false;
77     }
78     TELEPHONY_LOGI("Reset apns");
79     Uri resetApnUri(CELLULAR_DATA_RDB_RESET);
80     DataShare::DataSharePredicates predicates;
81     DataShare::DataShareValuesBucket values;
82     int32_t result = dataShareHelper->Update(resetApnUri, predicates, values);
83     dataShareHelper->Release();
84     return result >= 0;
85 }
86 
QueryApns(const std::string & mcc,const std::string & mnc,std::vector<PdpProfile> & apnVec)87 bool CellularDataRdbHelper::QueryApns(const std::string &mcc, const std::string &mnc, std::vector<PdpProfile> &apnVec)
88 {
89     std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataAbilityHelper();
90     if (dataShareHelper == nullptr) {
91         TELEPHONY_LOGE("dataShareHelper is null");
92         return false;
93     }
94     std::vector<std::string> columns;
95     DataShare::DataSharePredicates predicates;
96     predicates.EqualTo(PdpProfileData::MCC, mcc)->EqualTo(PdpProfileData::MNC, mnc);
97     std::shared_ptr<DataShare::DataShareResultSet> result =
98         dataShareHelper->Query(cellularDataUri_, predicates, columns);
99     if (result == nullptr) {
100         TELEPHONY_LOGE("query apns error");
101         dataShareHelper->Release();
102         return false;
103     }
104     ReadApnResult(result, apnVec);
105     result->Close();
106     dataShareHelper->Release();
107     return true;
108 }
109 
QueryMvnoApnsByType(const std::string & mcc,const std::string & mnc,const std::string & mvnoType,const std::string & mvnoDataFromSim,std::vector<PdpProfile> & mvnoApnVec)110 bool CellularDataRdbHelper::QueryMvnoApnsByType(const std::string &mcc, const std::string &mnc,
111     const std::string &mvnoType, const std::string &mvnoDataFromSim, std::vector<PdpProfile> &mvnoApnVec)
112 {
113     if (mvnoDataFromSim.empty()) {
114         TELEPHONY_LOGE("mvnoDataFromSim is empty!");
115         return true;
116     }
117     std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataAbilityHelper();
118     if (dataShareHelper == nullptr) {
119         TELEPHONY_LOGE("dataShareHelper is null");
120         return false;
121     }
122     std::vector<std::string> columns;
123     DataShare::DataSharePredicates predicates;
124     predicates.EqualTo(PdpProfileData::MVNO_TYPE, mvnoType)
125         ->EqualTo(PdpProfileData::MCC, mcc)
126         ->EqualTo(PdpProfileData::MNC, mnc);
127     std::shared_ptr<DataShare::DataShareResultSet> result =
128         dataShareHelper->Query(cellularDataUri_, predicates, columns);
129     if (result == nullptr) {
130         TELEPHONY_LOGE("Query apns error");
131         dataShareHelper->Release();
132         return false;
133     }
134     ReadMvnoApnResult(result, mvnoDataFromSim, mvnoApnVec);
135     result->Close();
136     dataShareHelper->Release();
137     return true;
138 }
139 
QueryPreferApn(int32_t slotId,std::vector<PdpProfile> & apnVec)140 bool CellularDataRdbHelper::QueryPreferApn(int32_t slotId, std::vector<PdpProfile> &apnVec)
141 {
142     std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataAbilityHelper();
143     if (dataShareHelper == nullptr) {
144         TELEPHONY_LOGE("dataShareHelper is null");
145         return false;
146     }
147     std::vector<std::string> columns;
148     DataShare::DataSharePredicates predicates;
149     int32_t simId = CoreManagerInner::GetInstance().GetSimId(slotId);
150     Uri preferApnUri(std::string(CELLULAR_DATA_RDB_PREFER) + "?Proxy=true&simId=" + std::to_string(simId));
151     std::shared_ptr<DataShare::DataShareResultSet> result = dataShareHelper->Query(preferApnUri, predicates, columns);
152     if (result == nullptr) {
153         TELEPHONY_LOGE("query prefer apns error");
154         dataShareHelper->Release();
155         return false;
156     }
157     ReadApnResult(result, apnVec);
158     result->Close();
159     dataShareHelper->Release();
160     if (apnVec.size() > 0) {
161         return true;
162     }
163     return false;
164 }
165 
ReadApnResult(const std::shared_ptr<DataShare::DataShareResultSet> & result,std::vector<PdpProfile> & apnVec)166 void CellularDataRdbHelper::ReadApnResult(
167     const std::shared_ptr<DataShare::DataShareResultSet> &result, std::vector<PdpProfile> &apnVec)
168 {
169     if (result == nullptr) {
170         TELEPHONY_LOGI("result is nullptr");
171         return;
172     }
173     int rowCnt = 0;
174     result->GetRowCount(rowCnt);
175     TELEPHONY_LOGI("query apns rowCnt = %{public}d", rowCnt);
176     for (int i = 0; i < rowCnt; ++i) {
177         PdpProfile apnBean;
178         MakePdpProfile(result, i, apnBean);
179         if (apnBean.mvnoType.empty()) {
180             apnVec.push_back(apnBean);
181         }
182     }
183 }
184 
ReadMvnoApnResult(const std::shared_ptr<DataShare::DataShareResultSet> & result,const std::string & mvnoDataFromSim,std::vector<PdpProfile> & apnVec)185 void CellularDataRdbHelper::ReadMvnoApnResult(const std::shared_ptr<DataShare::DataShareResultSet> &result,
186     const std::string &mvnoDataFromSim, std::vector<PdpProfile> &apnVec)
187 {
188     if (result == nullptr) {
189         TELEPHONY_LOGI("result is nullptr");
190         return;
191     }
192     int rowCnt = 0;
193     result->GetRowCount(rowCnt);
194     TELEPHONY_LOGI("query mvno apns rowCnt = %{public}d", rowCnt);
195     for (int i = 0; i < rowCnt; ++i) {
196         PdpProfile apnBean;
197         MakePdpProfile(result, i, apnBean);
198         if (IsMvnoDataMatched(mvnoDataFromSim, apnBean)) {
199             apnVec.push_back(apnBean);
200         }
201     }
202 }
203 
MakePdpProfile(const std::shared_ptr<DataShare::DataShareResultSet> & result,int i,PdpProfile & apnBean)204 void CellularDataRdbHelper::MakePdpProfile(
205     const std::shared_ptr<DataShare::DataShareResultSet> &result, int i, PdpProfile &apnBean)
206 {
207     int index = 0;
208     result->GoToRow(i);
209     result->GetColumnIndex(PdpProfileData::PROFILE_ID, index);
210     result->GetInt(index, apnBean.profileId);
211     result->GetColumnIndex(PdpProfileData::PROFILE_NAME, index);
212     result->GetString(index, apnBean.profileName);
213     result->GetColumnIndex(PdpProfileData::MCC, index);
214     result->GetString(index, apnBean.mcc);
215     result->GetColumnIndex(PdpProfileData::MNC, index);
216     result->GetString(index, apnBean.mnc);
217     result->GetColumnIndex(PdpProfileData::APN, index);
218     result->GetString(index, apnBean.apn);
219     result->GetColumnIndex(PdpProfileData::AUTH_TYPE, index);
220     result->GetInt(index, apnBean.authType);
221     result->GetColumnIndex(PdpProfileData::AUTH_USER, index);
222     result->GetString(index, apnBean.authUser);
223     result->GetColumnIndex(PdpProfileData::AUTH_PWD, index);
224     result->GetString(index, apnBean.authPwd);
225     result->GetColumnIndex(PdpProfileData::APN_TYPES, index);
226     result->GetString(index, apnBean.apnTypes);
227     result->GetColumnIndex(PdpProfileData::APN_PROTOCOL, index);
228     result->GetString(index, apnBean.pdpProtocol);
229     result->GetColumnIndex(PdpProfileData::APN_ROAM_PROTOCOL, index);
230     result->GetString(index, apnBean.roamPdpProtocol);
231     result->GetColumnIndex(PdpProfileData::MVNO_TYPE, index);
232     result->GetString(index, apnBean.mvnoType);
233     result->GetColumnIndex(PdpProfileData::MVNO_MATCH_DATA, index);
234     result->GetString(index, apnBean.mvnoMatchData);
235     result->GetColumnIndex(PdpProfileData::EDITED_STATUS, index);
236     result->GetInt(index, apnBean.edited);
237     result->GetColumnIndex(PdpProfileData::PROXY_IP_ADDRESS, index);
238     result->GetString(index, apnBean.proxyIpAddress);
239     if (apnBean.pdpProtocol.empty()) {
240         apnBean.pdpProtocol = "IP";
241     }
242     if (apnBean.roamPdpProtocol.empty()) {
243         apnBean.roamPdpProtocol = "IP";
244     }
245 }
246 
IsMvnoDataMatched(const std::string & mvnoDataFromSim,const PdpProfile & apnBean)247 bool CellularDataRdbHelper::IsMvnoDataMatched(const std::string &mvnoDataFromSim, const PdpProfile &apnBean)
248 {
249     if (mvnoDataFromSim.empty()) {
250         return false;
251     }
252     if (apnBean.mvnoType.compare(MvnoType::ICCID) == 0) {
253         return std::regex_match(mvnoDataFromSim, std::regex(apnBean.mvnoMatchData));
254     }
255     if (apnBean.mvnoType.compare(MvnoType::SPN) == 0) {
256         return std::regex_match(mvnoDataFromSim, std::regex(apnBean.mvnoMatchData));
257     }
258     if (apnBean.mvnoType.compare(MvnoType::IMSI) == 0) {
259         return std::regex_match(mvnoDataFromSim, std::regex(apnBean.mvnoMatchData));
260     }
261     if (apnBean.mvnoType.compare(MvnoType::GID1) == 0) {
262         return mvnoDataFromSim.compare(0, apnBean.mvnoMatchData.size(), apnBean.mvnoMatchData) == 0;
263     }
264     return false;
265 }
266 
RegisterObserver(const sptr<AAFwk::IDataAbilityObserver> & dataObserver)267 void CellularDataRdbHelper::RegisterObserver(const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
268 {
269     std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataAbilityHelper();
270     if (dataShareHelper == nullptr) {
271         TELEPHONY_LOGE("dataShareHelper is null");
272         return;
273     }
274     Uri preferApnUri(CELLULAR_DATA_RDB_PREFER);
275     Uri resetApnUri(CELLULAR_DATA_RDB_RESET);
276     dataShareHelper->RegisterObserver(resetApnUri, dataObserver);
277     dataShareHelper->RegisterObserver(preferApnUri, dataObserver);
278     dataShareHelper->RegisterObserver(cellularDataUri_, dataObserver);
279     dataShareHelper->Release();
280     TELEPHONY_LOGI("RegisterObserver Success");
281 }
282 
UnRegisterObserver(const sptr<AAFwk::IDataAbilityObserver> & dataObserver)283 void CellularDataRdbHelper::UnRegisterObserver(const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
284 {
285     std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataAbilityHelper();
286     if (dataShareHelper == nullptr) {
287         TELEPHONY_LOGE("dataShareHelper is null");
288         return;
289     }
290     Uri preferApnUri(CELLULAR_DATA_RDB_PREFER);
291     Uri resetApnUri(CELLULAR_DATA_RDB_RESET);
292     dataShareHelper->UnregisterObserver(resetApnUri, dataObserver);
293     dataShareHelper->UnregisterObserver(preferApnUri, dataObserver);
294     dataShareHelper->UnregisterObserver(cellularDataUri_, dataObserver);
295     dataShareHelper->Release();
296     TELEPHONY_LOGI("UnRegisterObserver Success");
297 }
298 } // namespace Telephony
299 } // namespace OHOS
300