• 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 "call_data_base_helper.h"
17 
18 #include "ability_context.h"
19 #include "call_manager_errors.h"
20 #include "call_number_utils.h"
21 #include "iservice_registry.h"
22 #include "phonenumbers/phonenumber.pb.h"
23 #include "phonenumberutil.h"
24 #include "telephony_log_wrapper.h"
25 
26 namespace OHOS {
27 namespace Telephony {
28 class AbsSharedResultSet;
29 static constexpr const char *CALLLOG_URI = "datashare:///com.ohos.calllogability";
30 static constexpr const char *CALL_SUBSECTION = "datashare:///com.ohos.calllogability/calls/calllog";
31 static constexpr const char *CONTACT_URI = "datashare:///com.ohos.contactsdataability";
32 static constexpr const char *CALL_BLOCK = "datashare:///com.ohos.contactsdataability/contacts/contact_blocklist";
33 static constexpr const char *CONTACT_DATA = "datashare:///com.ohos.contactsdataability/contacts/contact_data";
34 static constexpr const char *ISO_COUNTRY_CODE = "CN";
35 constexpr int32_t E_OK = 0;
36 
CallDataRdbObserver(std::vector<std::string> * phones)37 CallDataRdbObserver::CallDataRdbObserver(std::vector<std::string> *phones)
38 {
39     this->phones = phones;
40 }
41 
~CallDataRdbObserver()42 CallDataRdbObserver::~CallDataRdbObserver() {}
43 
OnChange()44 void CallDataRdbObserver::OnChange()
45 {
46     std::shared_ptr<CallDataBaseHelper> callDataPtr = DelayedSingleton<CallDataBaseHelper>::GetInstance();
47     if (callDataPtr == nullptr) {
48         TELEPHONY_LOGE("callDataPtr is nullptr!");
49         return;
50     }
51 
52     DataShare::DataSharePredicates predicates;
53     predicates.NotEqualTo("phone_number", std::string(""));
54     this->phones->clear();
55     callDataPtr->Query(this->phones, predicates);
56 }
57 
CallDataBaseHelper()58 CallDataBaseHelper::CallDataBaseHelper() {}
59 
~CallDataBaseHelper()60 CallDataBaseHelper::~CallDataBaseHelper() {}
61 
CreateDataShareHelper(std::string uri)62 std::shared_ptr<DataShare::DataShareHelper> CallDataBaseHelper::CreateDataShareHelper(std::string uri)
63 {
64     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
65     if (saManager == nullptr) {
66         TELEPHONY_LOGE("Get system ability mgr failed.");
67         return nullptr;
68     }
69     auto remoteObj = saManager->GetSystemAbility(TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID);
70     if (remoteObj == nullptr) {
71         TELEPHONY_LOGE("GetSystemAbility Service Failed.");
72         return nullptr;
73     }
74     return DataShare::DataShareHelper::Creator(remoteObj, uri);
75 }
76 
RegisterObserver(std::vector<std::string> * phones)77 void CallDataBaseHelper::RegisterObserver(std::vector<std::string> *phones)
78 {
79     callDataRdbObserverPtr_ = new (std::nothrow) CallDataRdbObserver(phones);
80     if (callDataRdbObserverPtr_ == nullptr) {
81         TELEPHONY_LOGE("callDataRdbObserverPtr_ is null");
82         return;
83     }
84     std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(CONTACT_URI);
85     if (helper == nullptr) {
86         TELEPHONY_LOGE("helper is null");
87         return;
88     }
89     Uri uri(CALL_BLOCK);
90     helper->RegisterObserver(uri, callDataRdbObserverPtr_);
91     helper->Release();
92 }
93 
UnRegisterObserver()94 void CallDataBaseHelper::UnRegisterObserver()
95 {
96     if (callDataRdbObserverPtr_ == nullptr) {
97         TELEPHONY_LOGE("callDataRdbObserverPtr_ is null");
98         return;
99     }
100     std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(CONTACT_URI);
101     if (helper == nullptr) {
102         TELEPHONY_LOGE("helper_ is null");
103         return;
104     }
105     Uri uri(CALL_BLOCK);
106     helper->UnregisterObserver(uri, callDataRdbObserverPtr_);
107     helper->Release();
108 }
109 
Insert(DataShare::DataShareValuesBucket & values)110 bool CallDataBaseHelper::Insert(DataShare::DataShareValuesBucket &values)
111 {
112     std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(CALLLOG_URI);
113     if (helper == nullptr) {
114         TELEPHONY_LOGE("helper is nullptr!");
115         return false;
116     }
117     Uri uri(CALL_SUBSECTION);
118     bool result = (helper->Insert(uri, values) > 0);
119     helper->Release();
120     return result;
121 }
122 
Query(std::vector<std::string> * phones,DataShare::DataSharePredicates & predicates)123 bool CallDataBaseHelper::Query(std::vector<std::string> *phones, DataShare::DataSharePredicates &predicates)
124 {
125     std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(CONTACT_URI);
126     if (helper == nullptr) {
127         TELEPHONY_LOGE("helper is nullptr");
128         return false;
129     }
130     Uri uri(CALL_BLOCK);
131     std::vector<std::string> columns;
132     columns.push_back("phone_number");
133     auto resultSet = helper->Query(uri, predicates, columns);
134     if (resultSet == nullptr) {
135         helper->Release();
136         return false;
137     }
138     int32_t resultSetNum = resultSet->GoToFirstRow();
139     while (resultSetNum == 0) {
140         std::string phone;
141         int32_t columnIndex;
142         resultSet->GetColumnIndex("phone_number", columnIndex);
143         int32_t ret = resultSet->GetString(columnIndex, phone);
144         if (ret == 0 && (!phone.empty())) {
145             phones->push_back(phone);
146         }
147         resultSetNum = resultSet->GoToNextRow();
148     }
149     resultSet->Close();
150     helper->Release();
151     TELEPHONY_LOGI("Query end");
152     return true;
153 }
154 
Query(ContactInfo & contactInfo,DataShare::DataSharePredicates & predicates)155 bool CallDataBaseHelper::Query(ContactInfo &contactInfo, DataShare::DataSharePredicates &predicates)
156 {
157     std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(CONTACT_URI);
158     if (helper == nullptr) {
159         TELEPHONY_LOGE("helper is nullptr");
160         return false;
161     }
162     Uri uri(CONTACT_DATA);
163     std::vector<std::string> columns;
164     auto resultSet = helper->Query(uri, predicates, columns);
165     if (resultSet == nullptr) {
166         TELEPHONY_LOGE("resultSet is nullptr");
167         helper->Release();
168         return false;
169     }
170     int32_t resultSetNum = resultSet->GoToFirstRow();
171     while (resultSetNum == 0) {
172         std::string displayName;
173         int32_t columnIndex;
174         resultSet->GetColumnIndex(CALL_DISPLAY_NAME, columnIndex);
175         int32_t ret = resultSet->GetString(columnIndex, displayName);
176         if (ret == 0 && (!displayName.empty())) {
177             size_t cpyLen = displayName.length() + 1;
178             if (displayName.length() > static_cast<size_t>(CONTACT_NAME_LEN)) {
179                 resultSet->Close();
180                 helper->Release();
181                 return false;
182             }
183             if (strcpy_s(contactInfo.name, cpyLen, displayName.c_str()) != EOK) {
184                 TELEPHONY_LOGE("strcpy_s fail.");
185                 resultSet->Close();
186                 helper->Release();
187                 return false;
188             }
189         }
190         resultSetNum = resultSet->GoToNextRow();
191     }
192     resultSet->Close();
193     helper->Release();
194     TELEPHONY_LOGI("Query end");
195     return true;
196 }
197 
QueryCallLog(std::map<std::string,int32_t> & phoneNumAndUnreadCountMap,DataShare::DataSharePredicates & predicates)198 bool CallDataBaseHelper::QueryCallLog(
199     std::map<std::string, int32_t> &phoneNumAndUnreadCountMap, DataShare::DataSharePredicates &predicates)
200 {
201     std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(CALLLOG_URI);
202     if (helper == nullptr) {
203         TELEPHONY_LOGE("helper is nullptr!");
204         return false;
205     }
206     Uri uri(CALL_SUBSECTION);
207     std::vector<std::string> columns;
208     columns.push_back(CALL_PHONE_NUMBER);
209     auto resultSet = helper->Query(uri, predicates, columns);
210     if (resultSet == nullptr) {
211         helper->Release();
212         return false;
213     }
214     int32_t operationResult = resultSet->GoToFirstRow();
215     while (operationResult == TELEPHONY_SUCCESS) {
216         std::string phoneNumber = "";
217         int32_t columnIndex = 0;
218         resultSet->GetColumnIndex(CALL_PHONE_NUMBER, columnIndex);
219         operationResult = resultSet->GetString(columnIndex, phoneNumber);
220         if (operationResult == TELEPHONY_SUCCESS && (!phoneNumber.empty())) {
221             auto iter = phoneNumAndUnreadCountMap.find(phoneNumber);
222             if (iter != phoneNumAndUnreadCountMap.end()) {
223                 iter->second++;
224             } else {
225                 phoneNumAndUnreadCountMap.insert(
226                     std::map<std::string, int32_t>::value_type(phoneNumber, CALL_LOG_DEFAULT_COUNT));
227             }
228         }
229         operationResult = resultSet->GoToNextRow();
230     }
231     resultSet->Close();
232     helper->Release();
233     TELEPHONY_LOGI("QueryCallLog end");
234     return true;
235 }
236 
Update(DataShare::DataSharePredicates & predicates,DataShare::DataShareValuesBucket & values)237 bool CallDataBaseHelper::Update(DataShare::DataSharePredicates &predicates, DataShare::DataShareValuesBucket &values)
238 {
239     std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(CALLLOG_URI);
240     if (helper == nullptr) {
241         TELEPHONY_LOGE("helper is nullptr");
242         return true;
243     }
244     Uri uri(CALL_SUBSECTION);
245     bool result = (helper->Update(uri, predicates, values) > 0);
246     helper->Release();
247     return result;
248 }
249 
Delete(DataShare::DataSharePredicates & predicates)250 bool CallDataBaseHelper::Delete(DataShare::DataSharePredicates &predicates)
251 {
252     std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(CALLLOG_URI);
253     if (helper == nullptr) {
254         TELEPHONY_LOGE("helper is nullptr!");
255         return false;
256     }
257     Uri uri(CALL_SUBSECTION);
258     bool result = (helper->Delete(uri, predicates) > 0);
259     helper->Release();
260     return result;
261 }
262 
QueryIsBlockPhoneNumber(const std::string & phoneNum,bool & result)263 int32_t CallDataBaseHelper::QueryIsBlockPhoneNumber(const std::string &phoneNum, bool &result)
264 {
265     result = false;
266     std::shared_ptr<DataShare::DataShareHelper> callDataHelper = CreateDataShareHelper(CALLLOG_URI);
267     if (callDataHelper == nullptr) {
268         TELEPHONY_LOGE("callDataHelper is nullptr!");
269         return TELEPHONY_ERR_LOCAL_PTR_NULL;
270     }
271     Uri uri(CALL_BLOCK);
272     DataShare::DataSharePredicates predicates;
273     std::vector<std::string> columns;
274     std::string internationalNumber;
275     std::string nationalNumber;
276     int32_t ret = DelayedSingleton<CallNumberUtils>::GetInstance()->FormatPhoneNumberToNational(
277         phoneNum, ISO_COUNTRY_CODE, nationalNumber);
278     if (ret != TELEPHONY_SUCCESS) {
279         TELEPHONY_LOGE("Format phone number failed.");
280         nationalNumber = phoneNum;
281     }
282     ret = DelayedSingleton<CallNumberUtils>::GetInstance()->FormatPhoneNumberToInternational(
283         phoneNum, ISO_COUNTRY_CODE, internationalNumber);
284     if (ret != TELEPHONY_SUCCESS) {
285         TELEPHONY_LOGE("Format phone number failed.");
286         internationalNumber = phoneNum;
287     }
288     predicates.EqualTo(CALL_PHONE_NUMBER, nationalNumber)->Or()->EqualTo(CALL_PHONE_NUMBER, internationalNumber);
289     auto resultSet = callDataHelper->Query(uri, predicates, columns);
290     if (resultSet == nullptr) {
291         TELEPHONY_LOGE("Query Result Set nullptr Failed.");
292         callDataHelper->Release();
293         return TELEPHONY_ERR_LOCAL_PTR_NULL;
294     }
295     int32_t count = 0;
296     if (resultSet->GetRowCount(count) == E_OK && count != 0) {
297         result = true;
298     }
299     TELEPHONY_LOGI("count: %{public}d", count);
300     resultSet->Close();
301     callDataHelper->Release();
302     return TELEPHONY_SUCCESS;
303 }
304 } // namespace Telephony
305 } // namespace OHOS
306