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_settings_rdb_helper.h"
17
18 #include "cellular_data_constant.h"
19 #include "cellular_data_error.h"
20 #include "cellular_data_hisysevent.h"
21 #include "telephony_log_wrapper.h"
22
23 namespace OHOS {
24 namespace Telephony {
25 static constexpr const int32_t E_ERROR = -1;
26
CellularDataSettingsRdbHelper()27 CellularDataSettingsRdbHelper::CellularDataSettingsRdbHelper() {}
28
~CellularDataSettingsRdbHelper()29 CellularDataSettingsRdbHelper::~CellularDataSettingsRdbHelper() {}
30
CreateDataShareHelper()31 std::shared_ptr<DataShare::DataShareHelper> CellularDataSettingsRdbHelper::CreateDataShareHelper()
32 {
33 sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
34 if (saManager == nullptr) {
35 TELEPHONY_LOGE("CellularDataRdbHelper GetSystemAbilityManager failed.");
36 return nullptr;
37 }
38 sptr<IRemoteObject> remoteObj = saManager->GetSystemAbility(TELEPHONY_CELLULAR_DATA_SYS_ABILITY_ID);
39 if (remoteObj == nullptr) {
40 TELEPHONY_LOGE("CellularDataRdbHelper GetSystemAbility Service Failed.");
41 return nullptr;
42 }
43 return DataShare::DataShareHelper::Creator(remoteObj, CELLULAR_DATA_SETTING_URI);
44 }
45
UnRegisterSettingsObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)46 void CellularDataSettingsRdbHelper::UnRegisterSettingsObserver(
47 const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
48 {
49 std::shared_ptr<DataShare::DataShareHelper> settingHelper = CreateDataShareHelper();
50 if (settingHelper == nullptr) {
51 TELEPHONY_LOGE("UnRegister settings observer failed by nullptr");
52 return;
53 }
54 settingHelper->UnregisterObserver(uri, dataObserver);
55 settingHelper->Release();
56 settingHelper = nullptr;
57 }
58
RegisterSettingsObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)59 void CellularDataSettingsRdbHelper::RegisterSettingsObserver(
60 const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
61 {
62 std::shared_ptr<DataShare::DataShareHelper> settingHelper = CreateDataShareHelper();
63 if (settingHelper == nullptr) {
64 TELEPHONY_LOGE("Register settings observer by nullptr");
65 return;
66 }
67 settingHelper->RegisterObserver(uri, dataObserver);
68 settingHelper->Release();
69 settingHelper = nullptr;
70 }
71
NotifyChange(const Uri & uri)72 void CellularDataSettingsRdbHelper::NotifyChange(const Uri &uri)
73 {
74 std::shared_ptr<DataShare::DataShareHelper> settingHelper = CreateDataShareHelper();
75 if (settingHelper == nullptr) {
76 TELEPHONY_LOGE("notify settings changed fail by nullptr");
77 return;
78 }
79 settingHelper->NotifyChange(uri);
80 settingHelper->Release();
81 settingHelper = nullptr;
82 }
83
GetValue(Uri & uri,const std::string & column,int32_t & value)84 int32_t CellularDataSettingsRdbHelper::GetValue(Uri &uri, const std::string &column, int32_t &value)
85 {
86 std::shared_ptr<DataShare::DataShareHelper> settingHelper = CreateDataShareHelper();
87 if (settingHelper == nullptr) {
88 TELEPHONY_LOGE("helper_ is null");
89 return TELEPHONY_ERR_LOCAL_PTR_NULL;
90 }
91 DataShare::DataSharePredicates predicates;
92 std::vector<std::string> columns;
93 predicates.EqualTo(CELLULAR_DATA_COLUMN_KEYWORD, column);
94 auto result = settingHelper->Query(uri, predicates, columns);
95 if (result == nullptr) {
96 TELEPHONY_LOGE("setting DB: query error");
97 settingHelper->Release();
98 return TELEPHONY_ERR_DATABASE_READ_FAIL;
99 }
100 result->GoToFirstRow();
101 int32_t columnIndex;
102 std::string resultValue;
103 result->GetColumnIndex(CELLULAR_DATA_COLUMN_VALUE, columnIndex);
104 result->GetString(columnIndex, resultValue);
105 result->Close();
106 settingHelper->Release();
107 settingHelper = nullptr;
108 TELEPHONY_LOGI("Query end resultValue is %{public}s", resultValue.c_str());
109 if (resultValue.empty()) {
110 TELEPHONY_LOGE("resultValue is empty");
111 return TELEPHONY_ERR_DATABASE_READ_FAIL;
112 }
113 value = atoi(resultValue.c_str());
114 return TELEPHONY_ERR_SUCCESS;
115 }
116
PutValue(Uri & uri,const std::string & column,int value)117 int32_t CellularDataSettingsRdbHelper::PutValue(Uri &uri, const std::string &column, int value)
118 {
119 std::shared_ptr<DataShare::DataShareHelper> settingHelper = CreateDataShareHelper();
120 if (settingHelper == nullptr) {
121 TELEPHONY_LOGE("helper_ is null");
122 return TELEPHONY_ERR_LOCAL_PTR_NULL;
123 }
124 int32_t existValue = 0;
125 int32_t getValueRet = GetValue(uri, column, existValue);
126 DataShare::DataShareValueObject keyObj(column);
127 DataShare::DataShareValueObject valueObj(std::to_string(value));
128 DataShare::DataShareValuesBucket bucket;
129 bucket.Put(CELLULAR_DATA_COLUMN_VALUE, valueObj);
130 bucket.Put(CELLULAR_DATA_COLUMN_KEYWORD, keyObj);
131 int32_t result;
132 if (getValueRet != TELEPHONY_ERR_SUCCESS) {
133 result = settingHelper->Insert(uri, bucket);
134 } else {
135 DataShare::DataSharePredicates predicates;
136 predicates.EqualTo(CELLULAR_DATA_COLUMN_KEYWORD, column);
137 result = settingHelper->Update(uri, predicates, bucket);
138 }
139 TELEPHONY_LOGI("put value return %{public}d", result);
140 if (result == E_ERROR) {
141 Uri userDataEnableUri(CELLULAR_DATA_SETTING_DATA_ENABLE_URI);
142 Uri userDataRoamingUri(CELLULAR_DATA_SETTING_DATA_ROAMING_URI);
143 if (uri == userDataEnableUri) {
144 CellularDataHiSysEvent::WriteDataActivateFaultEvent(INVALID_PARAMETER, value,
145 CellularDataErrorCode::DATA_ERROR_DATABASE_WRITE_ERROR,
146 "SetCellularDataEnable " + std::to_string(value) + " fail");
147 } else if (uri == userDataRoamingUri) {
148 CellularDataHiSysEvent::WriteDataActivateFaultEvent(INVALID_PARAMETER, value,
149 CellularDataErrorCode::DATA_ERROR_DATABASE_WRITE_ERROR,
150 "SetUserDataRoamingOn " + std::to_string(value) + " fail");
151 } else {
152 TELEPHONY_LOGI("result is %{public}d, do not handle.", result);
153 }
154 settingHelper->Release();
155 settingHelper = nullptr;
156 return TELEPHONY_ERR_DATABASE_WRITE_FAIL;
157 }
158 settingHelper->NotifyChange(uri);
159 settingHelper->Release();
160 settingHelper = nullptr;
161 return TELEPHONY_ERR_SUCCESS;
162 }
163 } // namespace Telephony
164 } // namespace OHOS
165