• 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 "rdb_sim_helper.h"
17 
18 #include "data_storage_errors.h"
19 #include "data_storage_log_wrapper.h"
20 #include "rdb_errno.h"
21 #include "rdb_sim_callback.h"
22 #include "rdb_store_config.h"
23 #include "sim_data.h"
24 #include "values_bucket.h"
25 #include "vector"
26 
27 namespace OHOS {
28 namespace Telephony {
RdbSimHelper()29 RdbSimHelper::RdbSimHelper() {}
30 
Init()31 int RdbSimHelper::Init()
32 {
33     int errCode = NativeRdb::E_OK;
34     NativeRdb::RdbStoreConfig config(dbPath_);
35     config.SetJournalMode(NativeRdb::JournalMode::MODE_TRUNCATE);
36     std::string simInfoStr;
37     CreateSimInfoTableStr(simInfoStr);
38     std::vector<std::string> createTableVec;
39     createTableVec.push_back(simInfoStr);
40     RdbSimCallback callback(createTableVec);
41     CreateRdbStore(config, VERSION, callback, errCode);
42     return errCode;
43 }
44 
UpdateDbPath(const std::string & path)45 void RdbSimHelper::UpdateDbPath(const std::string &path)
46 {
47     dbPath_ = path + DB_NAME;
48 }
49 
CreateSimInfoTableStr(std::string & createTableStr)50 void RdbSimHelper::CreateSimInfoTableStr(std::string &createTableStr)
51 {
52     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_SIM_INFO);
53     createTableStr.append("(").append(SimData::SIM_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
54     createTableStr.append(SimData::ICC_ID).append(" TEXT NOT NULL, ");
55     createTableStr.append(SimData::CARD_ID).append(" TEXT NOT NULL, ");
56     createTableStr.append(SimData::SLOT_INDEX).append(" INTEGER DEFAULT -1, ");
57     createTableStr.append(SimData::SHOW_NAME).append(" TEXT DEFAULT '', ");
58     createTableStr.append(SimData::PHONE_NUMBER).append(" TEXT DEFAULT '', ");
59     createTableStr.append(SimData::COUNTRY_CODE).append(" TEXT DEFAULT '', ");
60     createTableStr.append(SimData::IMSI).append(" TEXT DEFAULT '', ");
61     createTableStr.append(SimData::IS_MAIN_CARD).append(" INTEGER DEFAULT 0, ");
62     createTableStr.append(SimData::IS_VOICE_CARD).append(" INTEGER DEFAULT 0, ");
63     createTableStr.append(SimData::IS_MESSAGE_CARD).append(" INTEGER DEFAULT 0, ");
64     createTableStr.append(SimData::IS_CELLULAR_DATA_CARD).append(" INTEGER DEFAULT 0, ");
65     createTableStr.append(SimData::IS_ACTIVE).append(" INTEGER DEFAULT 1, ");
66     createTableStr.append(SimData::CARD_TYPE).append(" INTEGER , ");
67     createTableStr.append(SimData::IMS_SWITCH).append(" INTEGER DEFAULT -1, ");
68     createTableStr.append(SimData::LANGUAGE).append(" TEXT DEFAULT '', ");
69     createTableStr.append(SimData::OPKEY).append(" TEXT DEFAULT '', ");
70     createTableStr.append(SimData::MCC).append(" TEXT DEFAULT '', ");
71     createTableStr.append(SimData::MNC).append(" TEXT DEFAULT '', ");
72     createTableStr.append(SimData::IS_ESIM).append(" INTEGER DEFAULT 0, ");
73     createTableStr.append(SimData::SIM_LABEL_INDEX).append(" INTEGER DEFAULT 1, ");
74     createTableStr.append(SimData::OPERATOR_NAME).append(" TEXT DEFAULT '', ");
75     createTableStr.append("UNIQUE (");
76     createTableStr.append(SimData::ICC_ID).append(", ");
77     createTableStr.append(SimData::CARD_ID).append("))");
78 }
79 
SetDefaultCardByType(int32_t simId,int32_t type)80 int32_t RdbSimHelper::SetDefaultCardByType(int32_t simId, int32_t type)
81 {
82     int result = BeginTransaction();
83     if (result != NativeRdb::E_OK) {
84         DATA_STORAGE_LOGE("RdbSimHelper::SetDefaultCardByType BeginTransaction is error!");
85         return result;
86     }
87     int updateState = 0;
88     int whereState = 1;
89     result = UpdateCardStateByType(type, updateState, whereState);
90     if (result != NativeRdb::E_OK) {
91         DATA_STORAGE_LOGE("RdbSimHelper::SetDefaultCardByType UpdateCardStateByType is error!");
92         return result;
93     }
94     int changedRows = 0;
95     NativeRdb::ValuesBucket values;
96     switch (type) {
97         case static_cast<int32_t>(SimCardType::MAIN): {
98             values.PutInt(SimData::IS_MAIN_CARD, 1);
99             break;
100         }
101         case static_cast<int32_t>(SimCardType::VOICE): {
102             values.PutInt(SimData::IS_VOICE_CARD, 1);
103             break;
104         }
105         case static_cast<int32_t>(SimCardType::MESSAGE): {
106             values.PutInt(SimData::IS_MESSAGE_CARD, 1);
107             break;
108         }
109         case static_cast<int32_t>(SimCardType::CELLULAR_DATA): {
110             values.PutInt(SimData::IS_CELLULAR_DATA_CARD, 1);
111             break;
112         }
113         default:
114             DATA_STORAGE_LOGE("RdbSimHelper::SetDefaultDataByType is error!");
115             return DATA_STORAGE_ERROR;
116     }
117     std::string whereClause;
118     whereClause.append(SimData::SIM_ID).append("=").append(std::to_string(simId));
119     result = Update(changedRows, TABLE_SIM_INFO, values, whereClause);
120     if (result != NativeRdb::E_OK) {
121         DATA_STORAGE_LOGE("RdbSimHelper::SetDefaultCardByType Update is error!");
122     }
123     result = CommitTransactionAction();
124     return result;
125 }
126 
UpdateCardStateByType(int32_t type,int32_t updateState,int32_t whereSate)127 int32_t RdbSimHelper::UpdateCardStateByType(int32_t type, int32_t updateState, int32_t whereSate)
128 {
129     int32_t result = DATA_STORAGE_ERROR;
130     bool isExist = false;
131     NativeRdb::ValuesBucket values;
132     std::string whereClause;
133     switch (type) {
134         case static_cast<int32_t>(SimCardType::MAIN): {
135             isExist = true;
136             values.PutInt(SimData::IS_MAIN_CARD, updateState);
137             whereClause.append(SimData::IS_MAIN_CARD).append("=").append(std::to_string(whereSate));
138             break;
139         }
140         case static_cast<int32_t>(SimCardType::VOICE): {
141             isExist = true;
142             values.PutInt(SimData::IS_VOICE_CARD, updateState);
143             whereClause.append(SimData::IS_VOICE_CARD).append("=").append(std::to_string(whereSate));
144             break;
145         }
146         case static_cast<int32_t>(SimCardType::MESSAGE): {
147             isExist = true;
148             values.PutInt(SimData::IS_MESSAGE_CARD, updateState);
149             whereClause.append(SimData::IS_MESSAGE_CARD).append("=").append(std::to_string(whereSate));
150             break;
151         }
152         case static_cast<int32_t>(SimCardType::CELLULAR_DATA): {
153             isExist = true;
154             values.PutInt(SimData::IS_CELLULAR_DATA_CARD, updateState);
155             whereClause.append(SimData::IS_CELLULAR_DATA_CARD).append("=").append(std::to_string(whereSate));
156             break;
157         }
158         default:
159             break;
160     }
161     if (isExist) {
162         int changedRows = 0;
163         return Update(changedRows, TABLE_SIM_INFO, values, whereClause);
164     }
165     return result;
166 }
167 
CommitTransactionAction()168 int RdbSimHelper::CommitTransactionAction()
169 {
170     int result = Commit();
171     if (result != NativeRdb::E_OK) {
172         RollBack();
173     }
174     return result;
175 }
176 
ClearData()177 int32_t RdbSimHelper::ClearData()
178 {
179     std::string sql;
180     sql.append("delete from ").append(TABLE_SIM_INFO);
181     return ExecuteSql(sql);
182 }
183 } // namespace Telephony
184 } // namespace OHOS