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 "iservice_registry.h"
19
20 #include "ability_context.h"
21
22 #include "telephony_log_wrapper.h"
23
24 namespace OHOS {
25 namespace Telephony {
26 class AbsSharedResultSet;
CallDataRdbObserver(std::vector<std::string> * phones)27 CallDataRdbObserver::CallDataRdbObserver(std::vector<std::string> *phones)
28 {
29 this->phones = phones;
30 }
31
~CallDataRdbObserver()32 CallDataRdbObserver::~CallDataRdbObserver() {}
33
OnChange()34 void CallDataRdbObserver::OnChange()
35 {
36 std::shared_ptr<CallDataBaseHelper> callDataPtr = DelayedSingleton<CallDataBaseHelper>::GetInstance();
37 if (callDataPtr == nullptr) {
38 TELEPHONY_LOGE("callDataPtr is nullptr!");
39 return;
40 }
41
42 NativeRdb::DataAbilityPredicates predicates;
43 predicates.NotEqualTo("phone_number", std::string(""));
44 this->phones->clear();
45 callDataPtr->Query(this->phones, predicates);
46 }
47
CallDataBaseHelper()48 CallDataBaseHelper::CallDataBaseHelper() {}
49
~CallDataBaseHelper()50 CallDataBaseHelper::~CallDataBaseHelper() {}
51
CreateDataAHelper()52 std::shared_ptr<AppExecFwk::DataAbilityHelper> CallDataBaseHelper::CreateDataAHelper()
53 {
54 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
55 if (saManager == nullptr) {
56 TELEPHONY_LOGE("Get system ability mgr failed.");
57 return nullptr;
58 }
59 auto remoteObj = saManager->GetSystemAbility(TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID);
60 if (remoteObj == nullptr) {
61 TELEPHONY_LOGE("GetSystemAbility Service Failed.");
62 return nullptr;
63 }
64 return AppExecFwk::DataAbilityHelper::Creator(remoteObj);
65 }
66
RegisterObserver(std::vector<std::string> * phones)67 void CallDataBaseHelper::RegisterObserver(std::vector<std::string> *phones)
68 {
69 std::shared_ptr<AppExecFwk::DataAbilityHelper> helper = CreateDataAHelper();
70 if (helper == nullptr) {
71 TELEPHONY_LOGE("helper_ is null");
72 return;
73 }
74 Uri uri(CALL_BLOCK);
75 callDataRdbObserverPtr_ = (std::make_unique<CallDataRdbObserver>(phones)).release();
76 if (callDataRdbObserverPtr_ == nullptr) {
77 TELEPHONY_LOGE("callDataRdbObserverPtr_ is null");
78 return;
79 }
80 helper->RegisterObserver(uri, callDataRdbObserverPtr_);
81 }
82
UnRegisterObserver()83 void CallDataBaseHelper::UnRegisterObserver()
84 {
85 std::shared_ptr<AppExecFwk::DataAbilityHelper> helper = CreateDataAHelper();
86 if (helper == nullptr) {
87 TELEPHONY_LOGE("helper_ is null");
88 return;
89 }
90 Uri uri(CALL_BLOCK);
91 if (callDataRdbObserverPtr_ == nullptr) {
92 TELEPHONY_LOGE("callDataRdbObserverPtr_ is null");
93 return;
94 }
95 helper->UnregisterObserver(uri, callDataRdbObserverPtr_);
96 }
97
Insert(NativeRdb::ValuesBucket & values)98 bool CallDataBaseHelper::Insert(NativeRdb::ValuesBucket &values)
99 {
100 std::shared_ptr<AppExecFwk::DataAbilityHelper> helper = CreateDataAHelper();
101 if (helper == nullptr) {
102 TELEPHONY_LOGE("helper is nullptr!");
103 return false;
104 }
105 Uri uri(CALL_SUBSECTION);
106 return helper->Insert(uri, values);
107 }
108
Query(std::vector<std::string> * phones,NativeRdb::DataAbilityPredicates & predicates)109 bool CallDataBaseHelper::Query(std::vector<std::string> *phones, NativeRdb::DataAbilityPredicates &predicates)
110 {
111 std::shared_ptr<AppExecFwk::DataAbilityHelper> helper = CreateDataAHelper();
112 if (helper == nullptr) {
113 TELEPHONY_LOGE("helper is nullptr");
114 return false;
115 }
116 Uri uri(CALL_BLOCK);
117 std::vector<std::string> columns;
118 columns.push_back("phone_number");
119 std::shared_ptr<NativeRdb::AbsSharedResultSet> resultSet = helper->Query(uri, columns, predicates);
120 helper->Release();
121 if (resultSet == nullptr) {
122 return false;
123 }
124 int32_t resultSetNum = resultSet->GoToFirstRow();
125 while (resultSetNum == 0) {
126 std::string phone;
127 int32_t columnIndex;
128 int32_t ret = resultSet->GetColumnIndex("phone_number", columnIndex);
129 ret = resultSet->GetString(columnIndex, phone);
130 if (ret == 0 && (!phone.empty())) {
131 phones->push_back(phone);
132 }
133 resultSetNum = resultSet->GoToNextRow();
134 }
135 resultSet->Close();
136 TELEPHONY_LOGI("Query end");
137 return true;
138 }
139
Delete(NativeRdb::DataAbilityPredicates & predicates)140 bool CallDataBaseHelper::Delete(NativeRdb::DataAbilityPredicates &predicates)
141 {
142 std::shared_ptr<AppExecFwk::DataAbilityHelper> helper = CreateDataAHelper();
143 if (helper == nullptr) {
144 TELEPHONY_LOGE("helper is nullptr!");
145 return false;
146 }
147 Uri uri(CALL_SUBSECTION);
148 return helper->Delete(uri, predicates);
149 }
150
ResultSetConvertToIndexer(const std::shared_ptr<NativeRdb::AbsSharedResultSet> & resultSet)151 void CallDataBaseHelper::ResultSetConvertToIndexer(const std::shared_ptr<NativeRdb::AbsSharedResultSet> &resultSet)
152 {}
153 } // namespace Telephony
154 } // namespace OHOS