• 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 "sim_rdb_helper.h"
17 
18 #include "telephony_errors.h"
19 #include "telephony_types.h"
20 
21 namespace OHOS {
22 namespace Telephony {
SimRdbHelper()23 SimRdbHelper::SimRdbHelper()
24 {
25     helper_ = CreateDataAHelper();
26 }
27 
~SimRdbHelper()28 SimRdbHelper::~SimRdbHelper() {}
29 
CreateDataAHelper()30 std::shared_ptr<AppExecFwk::DataAbilityHelper> SimRdbHelper::CreateDataAHelper()
31 {
32     TELEPHONY_LOGI("SimRdbHelper::CreateDataAHelper");
33     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
34     if (saManager == nullptr) {
35         TELEPHONY_LOGE("SimRdbHelper Get system ability mgr failed.");
36         return nullptr;
37     }
38     auto remoteObj = saManager->GetSystemAbility(TELEPHONY_CORE_SERVICE_SYS_ABILITY_ID);
39     if (remoteObj == nullptr) {
40         TELEPHONY_LOGE("SimRdbHelper GetSystemAbility Service Failed.");
41         return nullptr;
42     }
43     return AppExecFwk::DataAbilityHelper::Creator(remoteObj);
44 }
45 
Insert(const NativeRdb::ValuesBucket & values)46 int SimRdbHelper::Insert(const NativeRdb::ValuesBucket &values)
47 {
48     if (helper_ == nullptr) {
49         TELEPHONY_LOGE("SimRdbHelper::Insert failed by nullptr");
50         return INVALID_VALUE;
51     }
52     Uri simUri(SimRdbInfo::SIM_RDB_URI);
53     TELEPHONY_LOGI("SimRdbHelper::Insert");
54     return helper_->Insert(simUri, values);
55 }
56 
Query(std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)57 std::shared_ptr<NativeRdb::AbsSharedResultSet> SimRdbHelper::Query(
58     std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
59 {
60     if (helper_ == nullptr) {
61         TELEPHONY_LOGE("SimRdbHelper::Query failed by nullptr");
62         return nullptr;
63     }
64     Uri simUri(SimRdbInfo::SIM_RDB_URI);
65     TELEPHONY_LOGI("SimRdbHelper::Query");
66     std::shared_ptr<NativeRdb::AbsSharedResultSet> ret = helper_->Query(simUri, columns, predicates);
67     return ret;
68 }
69 
Update(const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)70 int SimRdbHelper::Update(const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates)
71 {
72     if (helper_ == nullptr) {
73         TELEPHONY_LOGE("SimRdbHelper::Update failed by nullptr");
74         return INVALID_VALUE;
75     }
76     Uri simUri(SimRdbInfo::SIM_RDB_URI);
77     TELEPHONY_LOGI("SimRdbHelper::Update");
78     return helper_->Update(simUri, value, predicates);
79 }
80 
Delete(const NativeRdb::DataAbilityPredicates & predicates)81 int SimRdbHelper::Delete(const NativeRdb::DataAbilityPredicates &predicates)
82 {
83     if (helper_ == nullptr) {
84         TELEPHONY_LOGE("SimRdbHelper::Delete failed by nullptr");
85         return INVALID_VALUE;
86     }
87     Uri simUri(SimRdbInfo::SIM_RDB_URI);
88     TELEPHONY_LOGI("SimRdbHelper::Delete");
89     return helper_->Delete(simUri, predicates);
90 }
91 
GetDefaultMainCardSlotId()92 int32_t SimRdbHelper::GetDefaultMainCardSlotId()
93 {
94     TELEPHONY_LOGI("SimRdbHelper::GetDefaultMainCardSlotId");
95     int32_t mainCardSlotId = 0;
96     std::vector<std::string> colume;
97     NativeRdb::DataAbilityPredicates predicates;
98     predicates.EqualTo(SimRdbInfo::IS_MAIN_CARD, std::to_string(static_cast<int32_t>(MAIN_CARD)));
99     std::shared_ptr<NativeRdb::AbsSharedResultSet> result = Query(colume, predicates);
100     if (result == nullptr) {
101         TELEPHONY_LOGE("SimRdbHelper::get nothing");
102         return mainCardSlotId;
103     }
104     int resultSetNum = result->GoToFirstRow();
105     if (resultSetNum != 0) {
106         TELEPHONY_LOGI("SimRdbHelper::GetDefaultMainCardSlotId not found main card");
107         return mainCardSlotId;
108     }
109     int index = 0;
110     result->GetColumnIndex(SimRdbInfo::SLOT_INDEX, index);
111     result->GetInt(index, mainCardSlotId);
112     result->Close();
113     return mainCardSlotId;
114 }
115 
GetDefaultMessageCardSlotId()116 int32_t SimRdbHelper::GetDefaultMessageCardSlotId()
117 {
118     TELEPHONY_LOGI("SimRdbHelper::GetDefaultMessageCardSlotId");
119     int32_t messageCardSlotId = 0;
120     std::vector<std::string> colume;
121     NativeRdb::DataAbilityPredicates predicates;
122     predicates.EqualTo(SimRdbInfo::IS_MESSAGE_CARD, std::to_string(static_cast<int32_t>(MAIN_CARD)));
123     std::shared_ptr<NativeRdb::AbsSharedResultSet> result = Query(colume, predicates);
124     if (result == nullptr) {
125         TELEPHONY_LOGE("SimRdbHelper::get nothing");
126         return messageCardSlotId;
127     }
128     int resultSetNum = result->GoToFirstRow();
129     if (resultSetNum != 0) {
130         TELEPHONY_LOGI("SimRdbHelper::GetDefaultMessageCardSlotId not found default sms card");
131         return messageCardSlotId;
132     }
133     int index = 0;
134     result->GetColumnIndex(SimRdbInfo::SLOT_INDEX, index);
135     result->GetInt(index, messageCardSlotId);
136     result->Close();
137     return messageCardSlotId;
138 }
139 
GetDefaultCellularDataCardSlotId()140 int32_t SimRdbHelper::GetDefaultCellularDataCardSlotId()
141 {
142     TELEPHONY_LOGI("SimRdbHelper::GetDefaultCellularDataCardSlotId");
143     int32_t cellularDataCardSlotId = 0;
144     std::vector<std::string> colume;
145     NativeRdb::DataAbilityPredicates predicates;
146     predicates.EqualTo(SimRdbInfo::IS_CELLULAR_DATA_CARD, std::to_string(static_cast<int32_t>(MAIN_CARD)));
147     std::shared_ptr<NativeRdb::AbsSharedResultSet> result = Query(colume, predicates);
148     if (result == nullptr) {
149         TELEPHONY_LOGE("SimRdbHelper::get nothing");
150         return cellularDataCardSlotId;
151     }
152     int resultSetNum = result->GoToFirstRow();
153     if (resultSetNum != 0) {
154         TELEPHONY_LOGI("SimRdbHelper::GetDefaultCellularDataCardSlotId not found default data card");
155         return cellularDataCardSlotId;
156     }
157     int index = 0;
158     result->GetColumnIndex(SimRdbInfo::SLOT_INDEX, index);
159     result->GetInt(index, cellularDataCardSlotId);
160     result->Close();
161     return cellularDataCardSlotId;
162 }
163 
GetDefaultVoiceCardSlotId()164 int32_t SimRdbHelper::GetDefaultVoiceCardSlotId()
165 {
166     TELEPHONY_LOGI("SimRdbHelper::GetDefaultVoiceCardSlotId");
167     int32_t voiceCardSlotId = 0;
168     std::vector<std::string> colume;
169     NativeRdb::DataAbilityPredicates predicates;
170     predicates.EqualTo(SimRdbInfo::IS_VOICE_CARD, std::to_string(static_cast<int32_t>(MAIN_CARD)));
171     std::shared_ptr<NativeRdb::AbsSharedResultSet> result = Query(colume, predicates);
172     if (result == nullptr) {
173         TELEPHONY_LOGE("SimRdbHelper::get nothing");
174         return voiceCardSlotId;
175     }
176     int resultSetNum = result->GoToFirstRow();
177     if (resultSetNum != 0) {
178         TELEPHONY_LOGI("SimRdbHelper::GetDefaultVoiceCardSlotId not found default voice card");
179         return voiceCardSlotId;
180     }
181     int index = 0;
182     result->GetColumnIndex(SimRdbInfo::SLOT_INDEX, index);
183     result->GetInt(index, voiceCardSlotId);
184     result->Close();
185     return voiceCardSlotId;
186 }
187 
SetDefaultMainCard(int32_t slotId)188 int32_t SimRdbHelper::SetDefaultMainCard(int32_t slotId)
189 {
190     TELEPHONY_LOGI("SimRdbHelper::SetDefaultMainCard = %{public}d", slotId);
191     NativeRdb::DataAbilityPredicates predicates;
192     NativeRdb::ValuesBucket value;
193     value.PutInt(SimRdbInfo::SLOT_INDEX, slotId);
194     value.PutInt(SimRdbInfo::CARD_TYPE, static_cast<int>(DefaultCardType::MAIN));
195     if (helper_ == nullptr) {
196         TELEPHONY_LOGE("SimRdbHelper::SetDefaultMainCard failed by nullptr");
197         return INVALID_VALUE;
198     }
199     Uri defaultUri(SimRdbInfo::SIM_RDB_DEFAULT_SET_URI);
200     return helper_->Update(defaultUri, value, predicates);
201 }
202 
SetDefaultVoiceCard(int32_t slotId)203 int32_t SimRdbHelper::SetDefaultVoiceCard(int32_t slotId)
204 {
205     TELEPHONY_LOGI("SimRdbHelper::SetDefaultVoiceCard = %{public}d", slotId);
206     NativeRdb::DataAbilityPredicates predicates;
207     NativeRdb::ValuesBucket value;
208     value.PutInt(SimRdbInfo::SLOT_INDEX, slotId);
209     value.PutInt(SimRdbInfo::CARD_TYPE, static_cast<int>(DefaultCardType::VOICE));
210     if (helper_ == nullptr) {
211         TELEPHONY_LOGE("SimRdbHelper::SetDefaultVoiceCard failed by nullptr");
212         return INVALID_VALUE;
213     }
214     Uri defaultUri(SimRdbInfo::SIM_RDB_DEFAULT_SET_URI);
215     return helper_->Update(defaultUri, value, predicates);
216 }
217 
SetDefaultMessageCard(int32_t slotId)218 int32_t SimRdbHelper::SetDefaultMessageCard(int32_t slotId)
219 {
220     TELEPHONY_LOGI("SimRdbHelper::SetDefaultMessageCard = %{public}d", slotId);
221     NativeRdb::DataAbilityPredicates predicates;
222     NativeRdb::ValuesBucket value;
223     value.PutInt(SimRdbInfo::SLOT_INDEX, slotId);
224     value.PutInt(SimRdbInfo::CARD_TYPE, static_cast<int>(DefaultCardType::SMS));
225     if (helper_ == nullptr) {
226         TELEPHONY_LOGE("SimRdbHelper::SetDefaultMessageCard failed by nullptr");
227         return INVALID_VALUE;
228     }
229     Uri defaultUri(SimRdbInfo::SIM_RDB_DEFAULT_SET_URI);
230     return helper_->Update(defaultUri, value, predicates);
231 }
232 
SetDefaultCellularData(int32_t slotId)233 int32_t SimRdbHelper::SetDefaultCellularData(int32_t slotId)
234 {
235     TELEPHONY_LOGI("SimRdbHelper::SetDefaultCellularData = %{public}d", slotId);
236     NativeRdb::DataAbilityPredicates predicates;
237     NativeRdb::ValuesBucket value;
238     value.PutInt(SimRdbInfo::SLOT_INDEX, slotId);
239     value.PutInt(SimRdbInfo::CARD_TYPE, static_cast<int>(DefaultCardType::DATA));
240     if (helper_ == nullptr) {
241         TELEPHONY_LOGE("SimRdbHelper::SetDefaultCellularData failed by nullptr");
242         return INVALID_VALUE;
243     }
244     Uri defaultUri(SimRdbInfo::SIM_RDB_DEFAULT_SET_URI);
245     return helper_->Update(defaultUri, value, predicates);
246 }
247 
InsertData(int64_t & id,const NativeRdb::ValuesBucket & values)248 int32_t SimRdbHelper::InsertData(int64_t &id, const NativeRdb::ValuesBucket &values)
249 {
250     TELEPHONY_LOGI("SimRdbHelper::InsertData");
251     return Insert(values);
252 }
253 
SaveDataToBean(std::shared_ptr<NativeRdb::AbsSharedResultSet> result,SimRdbInfo & simBean)254 void SimRdbHelper::SaveDataToBean(std::shared_ptr<NativeRdb::AbsSharedResultSet> result, SimRdbInfo &simBean)
255 {
256     TELEPHONY_LOGI("SimRdbHelper::SaveDataToBean");
257     int index = 0;
258     result->GetColumnIndex(SimRdbInfo::SIM_ID, index);
259     result->GetInt(index, simBean.simId);
260     result->GetColumnIndex(SimRdbInfo::ICC_ID, index);
261     result->GetString(index, simBean.iccId);
262     result->GetColumnIndex(SimRdbInfo::CARD_ID, index);
263     result->GetString(index, simBean.cardId);
264     result->GetColumnIndex(SimRdbInfo::IMS_SWITCH, index);
265     result->GetInt(index, simBean.imsSwitch);
266     result->GetColumnIndex(SimRdbInfo::SLOT_INDEX, index);
267     result->GetInt(index, simBean.slotIndex);
268     result->GetColumnIndex(SimRdbInfo::CARD_TYPE, index);
269     result->GetInt(index, simBean.cardType);
270     result->GetColumnIndex(SimRdbInfo::SHOW_NAME, index);
271     result->GetString(index, simBean.showName);
272     result->GetColumnIndex(SimRdbInfo::PHONE_NUMBER, index);
273     result->GetString(index, simBean.phoneNumber);
274     result->GetColumnIndex(SimRdbInfo::COUNTRY_CODE, index);
275     result->GetString(index, simBean.countryCode);
276     result->GetColumnIndex(SimRdbInfo::LANGUAGE, index);
277     result->GetString(index, simBean.language);
278     result->GetColumnIndex(SimRdbInfo::IMSI, index);
279     result->GetString(index, simBean.imsi);
280     result->GetColumnIndex(SimRdbInfo::IS_MAIN_CARD, index);
281     result->GetInt(index, simBean.isMainCard);
282     result->GetColumnIndex(SimRdbInfo::IS_VOICE_CARD, index);
283     result->GetInt(index, simBean.isVoiceCard);
284     result->GetColumnIndex(SimRdbInfo::IS_MESSAGE_CARD, index);
285     result->GetInt(index, simBean.isMessageCard);
286     result->GetColumnIndex(SimRdbInfo::IS_CELLULAR_DATA_CARD, index);
287     result->GetInt(index, simBean.isCellularDataCard);
288     result->GetColumnIndex(SimRdbInfo::IS_ACTIVE, index);
289     result->GetInt(index, simBean.isActive);
290 }
291 
QueryDataBySlotId(int32_t slotId,SimRdbInfo & simBean)292 int32_t SimRdbHelper::QueryDataBySlotId(int32_t slotId, SimRdbInfo &simBean)
293 {
294     TELEPHONY_LOGI("SimRdbHelper::QueryDataBySlotId = %{public}d", slotId);
295     std::string slot = std::to_string(slotId);
296     std::vector<std::string> colume;
297     NativeRdb::DataAbilityPredicates predicates;
298     predicates.EqualTo(SimRdbInfo::SLOT_INDEX, slot);
299     std::shared_ptr<NativeRdb::AbsSharedResultSet> result = Query(colume, predicates);
300     if (result == nullptr) {
301         TELEPHONY_LOGE("SimRdbHelper::QueryDataBySlotId get nothing");
302         return INVALID_VALUE;
303     }
304     int resultSetNum = result->GoToFirstRow();
305     while (resultSetNum == 0) {
306         SaveDataToBean(result, simBean);
307         resultSetNum = result->GoToNextRow();
308     }
309     result->Close();
310     return TELEPHONY_SUCCESS;
311 }
312 
QueryDataByIccId(std::string iccId,SimRdbInfo & simBean)313 int32_t SimRdbHelper::QueryDataByIccId(std::string iccId, SimRdbInfo &simBean)
314 {
315     TELEPHONY_LOGI("SimRdbHelper::QueryDataByIccId");
316     std::vector<std::string> colume;
317     NativeRdb::DataAbilityPredicates predicates;
318     predicates.EqualTo(SimRdbInfo::ICC_ID, iccId);
319     std::shared_ptr<NativeRdb::AbsSharedResultSet> result = Query(colume, predicates);
320     if (result == nullptr) {
321         TELEPHONY_LOGE("SimRdbHelper::QueryDataByIccId get nothing");
322         return INVALID_VALUE;
323     }
324     int resultSetNum = result->GoToFirstRow();
325     while (resultSetNum == 0) {
326         SaveDataToBean(result, simBean);
327         resultSetNum = result->GoToNextRow();
328     }
329     result->Close();
330     return TELEPHONY_SUCCESS;
331 }
332 
QueryAllData(std::vector<SimRdbInfo> & vec)333 int32_t SimRdbHelper::QueryAllData(std::vector<SimRdbInfo> &vec)
334 {
335     TELEPHONY_LOGI("SimRdbHelper::QueryAllData");
336     std::vector<std::string> colume;
337     NativeRdb::DataAbilityPredicates predicates;
338     std::shared_ptr<NativeRdb::AbsSharedResultSet> result = Query(colume, predicates);
339     if (result == nullptr) {
340         TELEPHONY_LOGE("SimRdbHelper::QueryAllData get nothing");
341         return INVALID_VALUE;
342     }
343     int resultSetNum = result->GoToFirstRow();
344     while (resultSetNum == 0) {
345         SimRdbInfo simBean;
346         SaveDataToBean(result, simBean);
347         vec.push_back(simBean);
348         resultSetNum = result->GoToNextRow();
349     }
350     result->Close();
351     return TELEPHONY_SUCCESS;
352 }
353 
QueryAllValidData(std::vector<SimRdbInfo> & vec)354 int32_t SimRdbHelper::QueryAllValidData(std::vector<SimRdbInfo> &vec)
355 {
356     TELEPHONY_LOGI("SimRdbHelper::QueryAllValidData");
357     std::vector<std::string> colume;
358     std::string id = std::to_string(DEACTIVE);
359     NativeRdb::DataAbilityPredicates predicates;
360     predicates.GreaterThan(SimRdbInfo::IS_ACTIVE, id);
361     std::shared_ptr<NativeRdb::AbsSharedResultSet> result = Query(colume, predicates);
362     if (result == nullptr) {
363         TELEPHONY_LOGE("SimRdbHelper::QueryAllValidData get nothing");
364         return INVALID_VALUE;
365     }
366     int resultSetNum = result->GoToFirstRow();
367     while (resultSetNum == 0) {
368         SimRdbInfo simBean;
369         SaveDataToBean(result, simBean);
370         vec.push_back(simBean);
371         resultSetNum = result->GoToNextRow();
372     }
373     result->Close();
374     return TELEPHONY_SUCCESS;
375 }
376 
UpdateDataBySlotId(int32_t slotId,const NativeRdb::ValuesBucket & values)377 int32_t SimRdbHelper::UpdateDataBySlotId(int32_t slotId, const NativeRdb::ValuesBucket &values)
378 {
379     TELEPHONY_LOGI("SimRdbHelper::UpdateDataBySlotId = %{public}d", slotId);
380     std::string slot = std::to_string(slotId);
381     NativeRdb::DataAbilityPredicates predicates;
382     predicates.EqualTo(SimRdbInfo::SLOT_INDEX, slot);
383     return Update(values, predicates);
384 }
385 
UpdateDataByIccId(std::string iccId,const NativeRdb::ValuesBucket & values)386 int32_t SimRdbHelper::UpdateDataByIccId(std::string iccId, const NativeRdb::ValuesBucket &values)
387 {
388     TELEPHONY_LOGI("SimRdbHelper::UpdateDataByIccId");
389     NativeRdb::DataAbilityPredicates predicates;
390     predicates.EqualTo(SimRdbInfo::ICC_ID, iccId);
391     return Update(values, predicates);
392 }
393 
ForgetAllData()394 int32_t SimRdbHelper::ForgetAllData()
395 {
396     TELEPHONY_LOGI("SimRdbHelper::ForgetAllData");
397     NativeRdb::DataAbilityPredicates predicates;
398     NativeRdb::ValuesBucket values;
399     values.PutInt(SimRdbInfo::IS_ACTIVE, DEACTIVE);
400     return Update(values, predicates);
401 }
402 
ForgetAllData(int32_t slotId)403 int32_t SimRdbHelper::ForgetAllData(int32_t slotId)
404 {
405     TELEPHONY_LOGI("SimRdbHelper::ForgetAllData slotId = %{public}d", slotId);
406     NativeRdb::DataAbilityPredicates predicates;
407     predicates.EqualTo(SimRdbInfo::SLOT_INDEX, std::to_string(slotId));
408     NativeRdb::ValuesBucket values;
409     values.PutInt(SimRdbInfo::IS_ACTIVE, DEACTIVE);
410     return Update(values, predicates);
411 }
412 
ClearData()413 int32_t SimRdbHelper::ClearData()
414 {
415     std::string id = std::to_string(INVALID_VALUE);
416     NativeRdb::DataAbilityPredicates predicates;
417     predicates.GreaterThan(SimRdbInfo::SIM_ID, id);
418     return Delete(predicates);
419 }
420 } // namespace Telephony
421 } // namespace OHOS
422