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