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