1 /*
2  * Copyright (C) 2024 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 "number_identity_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 static constexpr const char *NUMBER_MARK_INFO_URI = "datashare:///numbermarkability/number_mark_info";
29 const char *NUMBER_LOCATION = "number_location";
30 const char *MARK_TYPE = "markType";
31 const char *MARK_CONTENT = "markContent";
32 const char *MARK_COUNT = "markCount";
33 const char *MARK_SOURCE = "markSource";
34 const char *IS_CLOUD = "isCloud";
35 
NumberIdentityDataBaseHelper()36 NumberIdentityDataBaseHelper::NumberIdentityDataBaseHelper() {}
37 
~NumberIdentityDataBaseHelper()38 NumberIdentityDataBaseHelper::~NumberIdentityDataBaseHelper() {}
39 
CreateDataShareHelper(std::string uri)40 std::shared_ptr<DataShare::DataShareHelper> NumberIdentityDataBaseHelper::CreateDataShareHelper(std::string uri)
41 {
42     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
43     if (saManager == nullptr) {
44         TELEPHONY_LOGE("Get system ability mgr failed.");
45         return nullptr;
46     }
47     auto remoteObj = saManager->GetSystemAbility(TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID);
48     if (remoteObj == nullptr) {
49         TELEPHONY_LOGE("GetSystemAbility Service Failed.");
50         return nullptr;
51     }
52     return DataShare::DataShareHelper::Creator(remoteObj, uri);
53 }
54 
Query(std::string & numberLocation,DataShare::DataSharePredicates & predicates)55 bool NumberIdentityDataBaseHelper::Query(std::string &numberLocation, DataShare::DataSharePredicates &predicates)
56 {
57     std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(NUMBER_IDENTITY_URI);
58     if (helper == nullptr) {
59         TELEPHONY_LOGE("helper is nullptr");
60         return false;
61     }
62     Uri uri(NUMBER_IDENTITY_URI);
63     std::string isExactMatchStr = "true";
64     std::vector<std::string> columns;
65     columns.push_back(isExactMatchStr);
66     auto resultSet = helper->Query(uri, predicates, columns);
67     if (resultSet == nullptr) {
68         TELEPHONY_LOGE("resultSet is nullptr");
69         helper->Release();
70         helper = nullptr;
71         return false;
72     }
73     int rowCount = 0;
74     resultSet->GetRowCount(rowCount);
75     if (rowCount == 0) {
76         TELEPHONY_LOGE("query success, but rowCount is 0");
77         helper->Release();
78         return TELEPHONY_SUCCESS;
79     }
80     resultSet->GoToFirstRow();
81     int columnIndex = 0;
82     resultSet->GetColumnIndex(NUMBER_LOCATION, columnIndex);
83     resultSet->GetString(columnIndex, numberLocation);
84     resultSet->Close();
85     helper->Release();
86     helper = nullptr;
87     TELEPHONY_LOGI("Query end");
88     return true;
89 }
90 
QueryYellowPageAndMark(NumberMarkInfo & numberMarkInfo,DataShare::DataSharePredicates & predicates)91 bool NumberIdentityDataBaseHelper::QueryYellowPageAndMark(NumberMarkInfo &numberMarkInfo,
92     DataShare::DataSharePredicates &predicates)
93 {
94     TELEPHONY_LOGE("query yellow page and mark.");
95     std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(NUMBER_MARK_INFO_URI);
96     if (helper == nullptr) {
97         TELEPHONY_LOGE("helper is nullptr");
98         return false;
99     }
100     Uri uri(NUMBER_MARK_INFO_URI);
101     std::vector<std::string> columns;
102     auto resultSet = helper->Query(uri, predicates, columns);
103     if (resultSet == nullptr) {
104         TELEPHONY_LOGE("resultSet is nullptr");
105         helper->Release();
106         helper = nullptr;
107         return false;
108     }
109     int rowCount = 0;
110     resultSet->GetRowCount(rowCount);
111     if (rowCount == 0) {
112         TELEPHONY_LOGE("query success, but rowCount is 0");
113         helper->Release();
114         return TELEPHONY_SUCCESS;
115     }
116     SetMarkInfoValues(resultSet, numberMarkInfo);
117 
118     resultSet->Close();
119     helper->Release();
120     helper = nullptr;
121     TELEPHONY_LOGI("QueryYellowPageAndMark success.");
122     return true;
123 }
124 
SetMarkInfoValues(std::shared_ptr<DataShare::DataShareResultSet> & resultSet,NumberMarkInfo & numberMarkInfo)125 bool NumberIdentityDataBaseHelper::SetMarkInfoValues(std::shared_ptr<DataShare::DataShareResultSet> &resultSet,
126     NumberMarkInfo &numberMarkInfo)
127 {
128     resultSet->GoToFirstRow();
129     int columnIndex = 0;
130     int64_t longValue;
131     std::string stringValue;
132 
133     resultSet->GetColumnIndex(MARK_TYPE, columnIndex);
134     resultSet->GetLong(columnIndex, longValue);
135     numberMarkInfo.markType = static_cast<MarkType>(longValue);
136 
137     resultSet->GetColumnIndex(MARK_CONTENT, columnIndex);
138     resultSet->GetString(columnIndex, stringValue);
139     errno_t result = memcpy_s(numberMarkInfo.markContent, kMaxNumberLen, stringValue.c_str(), stringValue.length());
140     if (result != EOK) {
141         TELEPHONY_LOGE("memcpy_s failed!");
142         return false;
143     }
144 
145     resultSet->GetColumnIndex(MARK_COUNT, columnIndex);
146     resultSet->GetLong(columnIndex, longValue);
147     numberMarkInfo.markCount = longValue;
148 
149     resultSet->GetColumnIndex(MARK_SOURCE, columnIndex);
150     resultSet->GetString(columnIndex, stringValue);
151     result = memcpy_s(numberMarkInfo.markSource, kMaxNumberLen, stringValue.c_str(), stringValue.length());
152     if (result != EOK) {
153         TELEPHONY_LOGE("memcpy_s failed!");
154         return false;
155     }
156 
157     resultSet->GetColumnIndex(IS_CLOUD, columnIndex);
158     resultSet->GetLong(columnIndex, longValue);
159     if (longValue == 1L) {
160         numberMarkInfo.isCloud = true;
161     } else {
162         numberMarkInfo.isCloud = false;
163     }
164     return true;
165 }
166 } // namespace Telephony
167 } // namespace OHOS