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 "cellular_data_rdb_helper.h"
17
18 #include "telephony_log_wrapper.h"
19
20 #include "cellular_data_constant.h"
21
22 namespace OHOS {
23 namespace Telephony {
CellularDataRdbHelper()24 CellularDataRdbHelper::CellularDataRdbHelper() : cellularDataUri_(CELLULAR_DATA_RDB_SELECTION) {}
25
26 CellularDataRdbHelper::~CellularDataRdbHelper() = default;
27
CreateDataAbilityHelper()28 std::shared_ptr<DataShare::DataShareHelper> CellularDataRdbHelper::CreateDataAbilityHelper()
29 {
30 TELEPHONY_LOGI("Create data ability helper");
31 sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
32 if (saManager == nullptr) {
33 TELEPHONY_LOGE("CellularDataRdbHelper GetSystemAbilityManager failed.");
34 return nullptr;
35 }
36 sptr<IRemoteObject> remoteObj = saManager->GetSystemAbility(TELEPHONY_CELLULAR_DATA_SYS_ABILITY_ID);
37 if (remoteObj == nullptr) {
38 TELEPHONY_LOGE("CellularDataRdbHelper GetSystemAbility Service Failed.");
39 return nullptr;
40 }
41 return DataShare::DataShareHelper::Creator(remoteObj, CELLULAR_DATA_RDB_URI);
42 }
43
Update(const DataShare::DataShareValuesBucket & value,const DataShare::DataSharePredicates & predicates)44 int CellularDataRdbHelper::Update(
45 const DataShare::DataShareValuesBucket &value, const DataShare::DataSharePredicates &predicates)
46 {
47 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataAbilityHelper();
48 if (dataShareHelper == nullptr) {
49 TELEPHONY_LOGE("dataShareHelper is null");
50 return NULL_POINTER_EXCEPTION;
51 }
52 TELEPHONY_LOGI("Cellular data RDB helper update");
53 int32_t result = dataShareHelper->Update(cellularDataUri_, predicates, value);
54 dataShareHelper->NotifyChange(cellularDataUri_);
55 dataShareHelper->Release();
56 dataShareHelper = nullptr;
57 return result;
58 }
59
Insert(const DataShare::DataShareValuesBucket & values)60 int CellularDataRdbHelper::Insert(const DataShare::DataShareValuesBucket &values)
61 {
62 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataAbilityHelper();
63 if (dataShareHelper == nullptr) {
64 TELEPHONY_LOGE("dataShareHelper is null");
65 return NULL_POINTER_EXCEPTION;
66 }
67 TELEPHONY_LOGI("Cellular data RDB helper insert");
68 int32_t result = dataShareHelper->Insert(cellularDataUri_, values);
69 dataShareHelper->NotifyChange(cellularDataUri_);
70 dataShareHelper->Release();
71 dataShareHelper = nullptr;
72 return result;
73 }
74
QueryApns(const std::string & mcc,const std::string & mnc,std::vector<PdpProfile> & apnVec)75 bool CellularDataRdbHelper::QueryApns(const std::string &mcc, const std::string &mnc, std::vector<PdpProfile> &apnVec)
76 {
77 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataAbilityHelper();
78 if (dataShareHelper == nullptr) {
79 TELEPHONY_LOGE("dataShareHelper is null");
80 return false;
81 }
82 std::vector<std::string> columns;
83 DataShare::DataSharePredicates predicates;
84 predicates.EqualTo(PdpProfileData::MCC, mcc)->And()->EqualTo(PdpProfileData::MNC, mnc);
85 std::shared_ptr<DataShare::DataShareResultSet> result =
86 dataShareHelper->Query(cellularDataUri_, predicates, columns);
87 if (result == nullptr) {
88 TELEPHONY_LOGE("CellularDataRdbHelper: query apns error");
89 dataShareHelper->Release();
90 return false;
91 }
92 ReadApnResult(result, apnVec);
93 result->Close();
94 dataShareHelper->Release();
95 dataShareHelper = nullptr;
96 return true;
97 }
98
ReadApnResult(const std::shared_ptr<DataShare::DataShareResultSet> & result,std::vector<PdpProfile> & apnVec)99 void CellularDataRdbHelper::ReadApnResult(
100 const std::shared_ptr<DataShare::DataShareResultSet> &result, std::vector<PdpProfile> &apnVec)
101 {
102 if (result == nullptr) {
103 TELEPHONY_LOGI("ReadApnResult result is nullptr");
104 return;
105 }
106
107 int rowCnt = 0;
108 int index = 0;
109 result->GetRowCount(rowCnt);
110 TELEPHONY_LOGI("CellularDataRdbHelper::query apns rowCnt = %{public}d", rowCnt);
111 for (int i = 0; i < rowCnt; ++i) {
112 PdpProfile apnBean;
113 result->GoToRow(i);
114 result->GetColumnIndex(PdpProfileData::PROFILE_ID, index);
115 result->GetInt(index, apnBean.profileId);
116 result->GetColumnIndex(PdpProfileData::PROFILE_NAME, index);
117 result->GetString(index, apnBean.profileName);
118 result->GetColumnIndex(PdpProfileData::MCC, index);
119 result->GetString(index, apnBean.mcc);
120 result->GetColumnIndex(PdpProfileData::MNC, index);
121 result->GetString(index, apnBean.mnc);
122 result->GetColumnIndex(PdpProfileData::APN, index);
123 result->GetString(index, apnBean.apn);
124 result->GetColumnIndex(PdpProfileData::AUTH_TYPE, index);
125 result->GetInt(index, apnBean.authType);
126 result->GetColumnIndex(PdpProfileData::AUTH_USER, index);
127 result->GetString(index, apnBean.authUser);
128 result->GetColumnIndex(PdpProfileData::AUTH_PWD, index);
129 result->GetString(index, apnBean.authPwd);
130 result->GetColumnIndex(PdpProfileData::APN_TYPES, index);
131 result->GetString(index, apnBean.apnTypes);
132 result->GetColumnIndex(PdpProfileData::APN_PROTOCOL, index);
133 result->GetString(index, apnBean.pdpProtocol);
134 result->GetColumnIndex(PdpProfileData::APN_ROAM_PROTOCOL, index);
135 result->GetString(index, apnBean.roamPdpProtocol);
136 if (apnBean.pdpProtocol.empty()) {
137 apnBean.pdpProtocol = "IPV4V6";
138 }
139 if (apnBean.roamPdpProtocol.empty()) {
140 apnBean.roamPdpProtocol = "IPV4V6";
141 }
142 apnVec.push_back(apnBean);
143 }
144 }
145
RegisterObserver(const sptr<AAFwk::IDataAbilityObserver> & dataObserver)146 void CellularDataRdbHelper::RegisterObserver(const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
147 {
148 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataAbilityHelper();
149 if (dataShareHelper == nullptr) {
150 TELEPHONY_LOGE("dataShareHelper is null");
151 return;
152 }
153 dataShareHelper->RegisterObserver(cellularDataUri_, dataObserver);
154 dataShareHelper->Release();
155 dataShareHelper = nullptr;
156 TELEPHONY_LOGI("CellularDataRdbHelper::RegisterObserver Success");
157 }
158
UnRegisterObserver(const sptr<AAFwk::IDataAbilityObserver> & dataObserver)159 void CellularDataRdbHelper::UnRegisterObserver(const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
160 {
161 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataAbilityHelper();
162 if (dataShareHelper == nullptr) {
163 TELEPHONY_LOGE("dataShareHelper is null");
164 return;
165 }
166 dataShareHelper->UnregisterObserver(cellularDataUri_, dataObserver);
167 dataShareHelper->Release();
168 dataShareHelper = nullptr;
169 TELEPHONY_LOGI("CellularDataRdbHelper::UnRegisterObserver Success");
170 }
171 } // namespace Telephony
172 } // namespace OHOS
173