• 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 "sms_persist_helper.h"
17 
18 #include "ability_manager_interface.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "system_ability.h"
22 #include "system_ability_definition.h"
23 #include "resource_manager.h"
24 #include "parameter.h"
25 
26 #include "telephony_log_wrapper.h"
27 #include "string_utils.h"
28 
29 namespace OHOS {
30 namespace Telephony {
31 using namespace OHOS::AppExecFwk;
32 using namespace OHOS::AAFwk;
33 using namespace NativeRdb;
34 class AbsSharedResultSet;
SmsPersistHelper()35 SmsPersistHelper::SmsPersistHelper() {}
36 
~SmsPersistHelper()37 SmsPersistHelper::~SmsPersistHelper() {}
38 
CreateDataAHelper()39 std::shared_ptr<AppExecFwk::DataAbilityHelper> SmsPersistHelper::CreateDataAHelper()
40 {
41     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42     if (saManager == nullptr) {
43         TELEPHONY_LOGE(" Get system ability mgr failed.");
44         return nullptr;
45     }
46     auto remoteObj = saManager->GetSystemAbility(TELEPHONY_SMS_MMS_SYS_ABILITY_ID);
47     if (remoteObj == nullptr) {
48         TELEPHONY_LOGE("GetSystemAbility Service Failed.");
49         return nullptr;
50     }
51     return DataAbilityHelper::Creator(remoteObj);
52 }
53 
Insert(NativeRdb::ValuesBucket & values)54 bool SmsPersistHelper::Insert(NativeRdb::ValuesBucket &values)
55 {
56     std::shared_ptr<DataAbilityHelper> helper = CreateDataAHelper();
57     if (helper == nullptr) {
58         TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
59         return false;
60     }
61     Uri uri(SMS_SUBSECTION);
62     int ret = helper->Insert(uri, values);
63     helper->Release();
64     return ret >= 0 ? true : false;
65 }
66 
Query(NativeRdb::DataAbilityPredicates & predicates,std::vector<SmsReceiveIndexer> & indexers)67 bool SmsPersistHelper::Query(NativeRdb::DataAbilityPredicates &predicates, std::vector<SmsReceiveIndexer> &indexers)
68 {
69     std::shared_ptr<DataAbilityHelper> helper = CreateDataAHelper();
70     if (helper == nullptr) {
71         TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
72         return false;
73     }
74     Uri uri(SMS_SUBSECTION);
75     std::vector<std::string> columns;
76     std::shared_ptr<NativeRdb::AbsSharedResultSet> resultSet = helper->Query(uri, columns, predicates);
77     helper->Release();
78     if (resultSet == nullptr) {
79         TELEPHONY_LOGE("Query Result Set nullptr Failed.");
80         return false;
81     }
82 
83     int resultSetNum = resultSet->GoToFirstRow();
84     while (resultSetNum == 0) {
85         SmsReceiveIndexer indexer;
86         ResultSetConvertToIndexer(indexer, resultSet);
87         indexers.push_back(indexer);
88         resultSetNum = resultSet->GoToNextRow();
89     }
90     resultSet->Close();
91     return true;
92 }
93 
Delete(NativeRdb::DataAbilityPredicates & predicates)94 bool SmsPersistHelper::Delete(NativeRdb::DataAbilityPredicates &predicates)
95 {
96     std::shared_ptr<DataAbilityHelper> helper = CreateDataAHelper();
97     if (helper == nullptr) {
98         TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
99         return false;
100     }
101     Uri uri(SMS_SUBSECTION);
102     int ret = helper->Delete(uri, predicates);
103     helper->Release();
104     return ret >= 0 ? true : false;
105 }
106 
QueryBlockPhoneNumber(const std::string & phoneNum)107 bool SmsPersistHelper::QueryBlockPhoneNumber(const std::string &phoneNum)
108 {
109     bool result = false;
110     int count = 0;
111     const string phoneNumber = "phone_number";
112     if (phoneNum.empty()) {
113         return result;
114     }
115     std::shared_ptr<DataAbilityHelper> helper = CreateDataAHelper();
116     if (helper == nullptr) {
117         TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
118         return false;
119     }
120     Uri uri(CONTACT_BLOCK);
121     std::vector<std::string> columns;
122     NativeRdb::DataAbilityPredicates predicates;
123     predicates.EqualTo(phoneNumber, phoneNum);
124     std::shared_ptr<NativeRdb::AbsSharedResultSet> resultSet = helper->Query(uri, columns, predicates);
125     helper->Release();
126     if (resultSet == nullptr) {
127         TELEPHONY_LOGE("Query Result Set nullptr Failed.");
128         return result;
129     }
130     if (resultSet->GetRowCount(count) == 0 && count != 0) {
131         result = true;
132     }
133     resultSet->Close();
134     return result;
135 }
136 
QueryParamBoolean(const std::string key,bool defValue)137 bool SmsPersistHelper::QueryParamBoolean(const std::string key, bool defValue)
138 {
139     const int PARAM_SIZE = 64;
140     char paramOutBuff[PARAM_SIZE] = {0};
141     std::string defStrValue = defValue ? "1" : "0";
142     std::string paramValue(defStrValue);
143 
144     int retLen = GetParameter(key.c_str(), defStrValue.c_str(), paramOutBuff, PARAM_SIZE);
145     if (retLen > 0) {
146         paramOutBuff[retLen] = '\0';
147         paramValue.assign(paramOutBuff, retLen);
148     }
149 
150     TELEPHONY_LOGI("QueryParamBoolean: %{public}zu : %{public}s", paramValue.size(), paramValue.c_str());
151     if ((paramValue == std::string("1")) || (paramValue == std::string("y")) || (paramValue == std::string("yes")) ||
152         (paramValue == std::string("on")) || (paramValue == std::string("true"))) {
153         return true;
154     } else if ((paramValue == std::string("0")) || (paramValue == std::string("n")) ||
155         (paramValue == std::string("no")) || (paramValue == std::string("off")) ||
156         (paramValue == std::string("false"))) {
157         return false;
158     }
159     return defValue;
160 }
161 
ConvertIntToIndexer(SmsReceiveIndexer & info,const std::shared_ptr<NativeRdb::AbsSharedResultSet> & resultSet)162 void SmsPersistHelper::ConvertIntToIndexer(
163     SmsReceiveIndexer &info, const std::shared_ptr<NativeRdb::AbsSharedResultSet> &resultSet)
164 {
165     int32_t columnInt;
166     int columnIndex;
167     resultSet->GetColumnIndex(SmsMmsData::FORMAT, columnIndex);
168     if (resultSet->GetInt(columnIndex, columnInt) == 0) {
169         info.SetIsCdma(columnInt != 0);
170     }
171 
172     resultSet->GetColumnIndex(SmsMmsData::SMS_SUBSECTION_ID, columnIndex);
173     if (resultSet->GetInt(columnIndex, columnInt) == 0) {
174         info.SetMsgRefId(columnInt);
175     }
176 
177     resultSet->GetColumnIndex(SmsMmsData::SIZE, columnIndex);
178     if (resultSet->GetInt(columnIndex, columnInt) == 0) {
179         info.SetMsgCount(columnInt);
180     }
181 
182     resultSet->GetColumnIndex(SmsMmsData::SUBSECTION_INDEX, columnIndex);
183     if (resultSet->GetInt(columnIndex, columnInt) == 0) {
184         info.SetMsgSeqId(columnInt);
185     }
186 
187     resultSet->GetColumnIndex(SmsMmsData::DEST_PORT, columnIndex);
188     if (resultSet->GetInt(columnIndex, columnInt) == 0) {
189         info.SetDestPort(columnInt);
190     }
191 }
192 
ConvertStringToIndexer(SmsReceiveIndexer & info,const std::shared_ptr<NativeRdb::AbsSharedResultSet> & resultSet)193 void SmsPersistHelper::ConvertStringToIndexer(
194     SmsReceiveIndexer &info, const std::shared_ptr<NativeRdb::AbsSharedResultSet> &resultSet)
195 {
196     int columnIndex;
197     std::string columnValue;
198     resultSet->GetColumnIndex(SmsMmsData::RECEIVER_NUMBER, columnIndex);
199     if (resultSet->GetString(columnIndex, columnValue) == 0) {
200         info.SetVisibleAddress(columnValue);
201     }
202     resultSet->GetColumnIndex(SmsMmsData::SENDER_NUMBER, columnIndex);
203     if (resultSet->GetString(columnIndex, columnValue) == 0) {
204         info.SetOriginatingAddress(columnValue);
205     }
206 
207     resultSet->GetColumnIndex(SmsMmsData::START_TIME, columnIndex);
208     if (resultSet->GetString(columnIndex, columnValue) == 0) {
209         info.SetTimestamp(std::stol(columnValue));
210     }
211     resultSet->GetColumnIndex(SmsMmsData::END_TIME, columnIndex);
212     if (resultSet->GetString(columnIndex, columnValue) == 0) {
213         info.SetTimestamp(std::stol(columnValue));
214     }
215 
216     resultSet->GetColumnIndex(SmsMmsData::RAW_PUD, columnIndex);
217     if (resultSet->GetString(columnIndex, columnValue) == 0) {
218         info.SetPdu(StringUtils::HexToByteVector(columnValue));
219     }
220 }
221 
ResultSetConvertToIndexer(SmsReceiveIndexer & info,const std::shared_ptr<NativeRdb::AbsSharedResultSet> & resultSet)222 void SmsPersistHelper::ResultSetConvertToIndexer(
223     SmsReceiveIndexer &info, const std::shared_ptr<NativeRdb::AbsSharedResultSet> &resultSet)
224 {
225     ConvertIntToIndexer(info, resultSet);
226     ConvertStringToIndexer(info, resultSet);
227 }
228 } // namespace Telephony
229 } // namespace OHOS
230