• 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_ability.h"
17 
18 #include <cstdint>
19 
20 #include "ability_context.h"
21 #include "ability_loader.h"
22 #include "abs_rdb_predicates.h"
23 #include "abs_shared_result_set.h"
24 #include "data_ability_predicates.h"
25 #include "data_storage_errors.h"
26 #include "data_storage_log_wrapper.h"
27 #include "new"
28 #include "predicates_utils.h"
29 #include "rdb_errno.h"
30 #include "sim_data.h"
31 #include "uri.h"
32 #include "utility"
33 #include "values_bucket.h"
34 
35 namespace OHOS {
36 using AppExecFwk::AbilityLoader;
37 using AppExecFwk::Ability;
38 namespace Telephony {
OnStart(const AppExecFwk::Want & want)39 void SimAbility::OnStart(const AppExecFwk::Want &want)
40 {
41     DATA_STORAGE_LOGI("SimAbility::OnStart\n");
42     Ability::OnStart(want);
43     auto abilityContext = GetAbilityContext();
44     if (abilityContext == nullptr) {
45         DATA_STORAGE_LOGE("SimAbility::OnStart GetAbilityContext is null");
46         return;
47     }
48     // switch database dir to el1 for init before unlock
49     abilityContext->SwitchArea(0);
50     std::string path = abilityContext->GetDatabaseDir();
51     DATA_STORAGE_LOGI("GetDatabaseDir: %{public}s", path.c_str());
52     if (!path.empty()) {
53         initDatabaseDir = true;
54         path.append("/");
55         helper_.UpdateDbPath(path);
56         InitUriMap();
57         if (helper_.Init() == NativeRdb::E_OK) {
58             initRdbStore = true;
59         } else {
60             DATA_STORAGE_LOGE("SimAbility::OnStart rdb init fail!");
61             initRdbStore = false;
62         }
63     } else {
64         initDatabaseDir = false;
65         DATA_STORAGE_LOGE("SimAbility::OnStart##the databaseDir is empty\n");
66     }
67 }
68 
Insert(const Uri & uri,const NativeRdb::ValuesBucket & value)69 int SimAbility::Insert(const Uri &uri, const NativeRdb::ValuesBucket &value)
70 {
71     if (!IsInitOk()) {
72         return DATA_STORAGE_ERROR;
73     }
74     std::lock_guard<std::mutex> guard(lock_);
75     Uri tempUri = uri;
76     SimUriType simUriType = ParseUriType(tempUri);
77     int64_t id = DATA_STORAGE_ERROR;
78     if (simUriType == SimUriType::SIM_INFO) {
79         helper_.Insert(id, value, TABLE_SIM_INFO);
80     } else {
81         DATA_STORAGE_LOGI("SimAbility::Insert##uri = %{public}s\n", uri.ToString().c_str());
82     }
83     return id;
84 }
85 
Query(const Uri & uri,const std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)86 std::shared_ptr<NativeRdb::AbsSharedResultSet> SimAbility::Query(
87     const Uri &uri, const std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
88 {
89     std::shared_ptr<NativeRdb::AbsSharedResultSet> resultSet = nullptr;
90     if (!IsInitOk()) {
91         return resultSet;
92     }
93     Uri tempUri = uri;
94     SimUriType simUriType = ParseUriType(tempUri);
95     if (simUriType == SimUriType::SIM_INFO) {
96         NativeRdb::AbsRdbPredicates *absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_SIM_INFO);
97         if (absRdbPredicates != nullptr) {
98             ConvertPredicates(predicates, absRdbPredicates);
99             resultSet = helper_.Query(*absRdbPredicates, columns);
100             delete absRdbPredicates;
101             absRdbPredicates = nullptr;
102         } else {
103             DATA_STORAGE_LOGE("SimAbility::Update  NativeRdb::AbsRdbPredicates is null!");
104         }
105     } else {
106         DATA_STORAGE_LOGI("SimAbility::Query##uri = %{public}s\n", uri.ToString().c_str());
107     }
108     return resultSet;
109 }
110 
Update(const Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)111 int SimAbility::Update(
112     const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates)
113 {
114     int result = DATA_STORAGE_ERROR;
115     if (!IsInitOk()) {
116         return result;
117     }
118     std::lock_guard<std::mutex> guard(lock_);
119     Uri tempUri = uri;
120     SimUriType simUriType = ParseUriType(tempUri);
121     switch (simUriType) {
122         case SimUriType::SIM_INFO: {
123             NativeRdb::AbsRdbPredicates *absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_SIM_INFO);
124             if (absRdbPredicates != nullptr) {
125                 int changedRows = 0;
126                 ConvertPredicates(predicates, absRdbPredicates);
127                 result = helper_.Update(changedRows, value, *absRdbPredicates);
128                 delete absRdbPredicates;
129                 absRdbPredicates = nullptr;
130             } else {
131                 DATA_STORAGE_LOGE("SimAbility::Update  NativeRdb::AbsRdbPredicates is null!");
132             }
133             break;
134         }
135         case SimUriType::SET_CARD: {
136             result = SetCard(value);
137             if (result != NativeRdb::E_OK) {
138                 DATA_STORAGE_LOGE("SimAbility::Update  SetCard fail!");
139                 result = static_cast<int>(LoadProFileErrorType::SET_CARD_FAIL);
140             }
141             break;
142         }
143         default:
144             DATA_STORAGE_LOGI("SimAbility::Update##uri = %{public}s\n", uri.ToString().c_str());
145             break;
146     }
147     return result;
148 }
149 
SetCard(const NativeRdb::ValuesBucket & value)150 int SimAbility::SetCard(const NativeRdb::ValuesBucket &value)
151 {
152     int result = DATA_STORAGE_ERROR;
153     if (!value.HasColumn(SimData::SLOT_INDEX)) {
154         DATA_STORAGE_LOGE("SimAbility::Update##the slot_index in valuesBucket does not exist");
155         return result;
156     }
157     if (!value.HasColumn(SimData::CARD_TYPE)) {
158         DATA_STORAGE_LOGE("SimAbility::Update##the card_type in valuesBucket does not exist");
159         return result;
160     }
161     NativeRdb::ValueObject valueObject;
162     bool isExistSlotId = value.GetObject(SimData::SLOT_INDEX, valueObject);
163     if (!isExistSlotId) {
164         DATA_STORAGE_LOGE("SimAbility::Update##failed to get slot_index value in valuesBucket");
165         return result;
166     }
167     int slotId = 0;
168     valueObject.GetInt(slotId);
169 
170     bool isExistCardType = value.GetObject(SimData::CARD_TYPE, valueObject);
171     if (!isExistCardType) {
172         DATA_STORAGE_LOGE("SimAbility::Update##failed to get card_type value in valuesBucket");
173         return result;
174     }
175     int cardType = 0;
176     valueObject.GetInt(cardType);
177     result = helper_.SetDefaultCardByType(slotId, cardType);
178     return result;
179 }
180 
Delete(const Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)181 int SimAbility::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates)
182 {
183     int result = DATA_STORAGE_ERROR;
184     if (!IsInitOk()) {
185         return result;
186     }
187     std::lock_guard<std::mutex> guard(lock_);
188     Uri tempUri = uri;
189     SimUriType simUriType = ParseUriType(tempUri);
190     if (simUriType == SimUriType::SIM_INFO) {
191         NativeRdb::AbsRdbPredicates *absRdbPredicates = new NativeRdb::AbsRdbPredicates(TABLE_SIM_INFO);
192         if (absRdbPredicates != nullptr) {
193             ConvertPredicates(predicates, absRdbPredicates);
194             int deletedRows = 0;
195             result = helper_.Delete(deletedRows, *absRdbPredicates);
196             delete absRdbPredicates;
197             absRdbPredicates = nullptr;
198         } else {
199             DATA_STORAGE_LOGE("SimAbility::Update  NativeRdb::AbsRdbPredicates is null!");
200         }
201     } else {
202         DATA_STORAGE_LOGI("SimAbility::Delete##uri = %{public}s\n", uri.ToString().c_str());
203     }
204     return result;
205 }
206 
IsInitOk()207 bool SimAbility::IsInitOk()
208 {
209     if (!initDatabaseDir) {
210         DATA_STORAGE_LOGE("SimAbility::IsInitOk initDatabaseDir failed!");
211     } else if (!initRdbStore) {
212         DATA_STORAGE_LOGE("SimAbility::IsInitOk initRdbStore failed!");
213     }
214     return initDatabaseDir && initRdbStore;
215 }
216 
InitUriMap()217 void SimAbility::InitUriMap()
218 {
219     simUriMap = {
220         {"/sim/sim_info", SimUriType::SIM_INFO},
221         {"/sim/sim_info/set_card", SimUriType::SET_CARD}
222     };
223 }
224 
GetType(const Uri & uri)225 std::string SimAbility::GetType(const Uri &uri)
226 {
227     DATA_STORAGE_LOGI("SimAbility::GetType##uri = %{public}s\n", uri.ToString().c_str());
228     std::string retval(uri.ToString());
229     return retval;
230 }
231 
OpenFile(const Uri & uri,const std::string & mode)232 int SimAbility::OpenFile(const Uri &uri, const std::string &mode)
233 {
234     DATA_STORAGE_LOGI("SimAbility::OpenFile##uri = %{public}s\n", uri.ToString().c_str());
235     Uri tempUri = uri;
236     SimUriType simUriType = ParseUriType(tempUri);
237     return static_cast<int>(simUriType);
238 }
239 
ParseUriType(Uri & uri)240 SimUriType SimAbility::ParseUriType(Uri &uri)
241 {
242     DATA_STORAGE_LOGI("SimAbility::ParseUriType start\n");
243     SimUriType simUriType = SimUriType::UNKNOW;
244     std::string uriPath = uri.ToString();
245     if (!uriPath.empty()) {
246         helper_.ReplaceAllStr(uriPath, ":///", "://");
247         Uri tempUri(uriPath);
248         std::string path = tempUri.GetPath();
249         if (!path.empty()) {
250             DATA_STORAGE_LOGI("SimAbility::ParseUriType##path = %{public}s\n", path.c_str());
251             std::map<std::string, SimUriType>::iterator it = simUriMap.find(path);
252             if (it != simUriMap.end()) {
253                 simUriType = it->second;
254                 DATA_STORAGE_LOGI("SimAbility::ParseUriType##simUriType = %{public}d\n", simUriType);
255             }
256         }
257     }
258     return simUriType;
259 }
260 
ConvertPredicates(const NativeRdb::DataAbilityPredicates & dataPredicates,NativeRdb::AbsRdbPredicates * absRdbPredicates)261 void SimAbility::ConvertPredicates(
262     const NativeRdb::DataAbilityPredicates &dataPredicates, NativeRdb::AbsRdbPredicates *absRdbPredicates)
263 {
264     NativeRdb::PredicatesUtils::SetWhereClauseAndArgs(
265         absRdbPredicates, dataPredicates.GetWhereClause(), dataPredicates.GetWhereArgs());
266     NativeRdb::PredicatesUtils::SetAttributes(absRdbPredicates, dataPredicates.IsDistinct(),
267         dataPredicates.GetIndex(), dataPredicates.GetGroup(), dataPredicates.GetOrder(), dataPredicates.GetLimit(),
268         dataPredicates.GetOffset());
269 }
270 
271 REGISTER_AA(SimAbility);
272 }  // namespace Telephony
273 }  // namespace OHOS
274