• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
57 
RegisterSettingsObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)58 void CellularDataSettingsRdbHelper::RegisterSettingsObserver(
59     const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
60 {
61     std::shared_ptr<DataShare::DataShareHelper> settingHelper = CreateDataShareHelper();
62     if (settingHelper == nullptr) {
63         TELEPHONY_LOGE("Register settings observer by nullptr");
64         return;
65     }
66     settingHelper->RegisterObserver(uri, dataObserver);
67     settingHelper->Release();
68 }
69 
NotifyChange(const Uri & uri)70 void CellularDataSettingsRdbHelper::NotifyChange(const Uri &uri)
71 {
72     std::shared_ptr<DataShare::DataShareHelper> settingHelper = CreateDataShareHelper();
73     if (settingHelper == nullptr) {
74         TELEPHONY_LOGE("notify settings changed fail by nullptr");
75         return;
76     }
77     settingHelper->NotifyChange(uri);
78     settingHelper->Release();
79 }
80 
GetValue(Uri & uri,const std::string & column,int32_t & value)81 int32_t CellularDataSettingsRdbHelper::GetValue(Uri &uri, const std::string &column, int32_t &value)
82 {
83     std::shared_ptr<DataShare::DataShareHelper> settingHelper = CreateDataShareHelper();
84     if (settingHelper == nullptr) {
85         TELEPHONY_LOGE("helper_ is null");
86         return TELEPHONY_ERR_LOCAL_PTR_NULL;
87     }
88     DataShare::DataSharePredicates predicates;
89     std::vector<std::string> columns;
90     predicates.EqualTo(CELLULAR_DATA_COLUMN_KEYWORD, column);
91     auto result = settingHelper->Query(uri, predicates, columns);
92     if (result == nullptr) {
93         TELEPHONY_LOGE("setting DB: query error");
94         return TELEPHONY_ERR_DATABASE_READ_FAIL;
95     }
96     settingHelper->Release();
97     result->GoToFirstRow();
98     int32_t columnIndex;
99     std::string resultValue;
100     result->GetColumnIndex(CELLULAR_DATA_COLUMN_VALUE, columnIndex);
101     result->GetString(columnIndex, resultValue);
102     result->Close();
103     TELEPHONY_LOGI("Query end resultValue is %{public}s", resultValue.c_str());
104     if (resultValue.empty()) {
105         TELEPHONY_LOGE("resultValue is empty");
106         return TELEPHONY_ERR_DATABASE_READ_FAIL;
107     }
108     value = atoi(resultValue.c_str());
109     return TELEPHONY_ERR_SUCCESS;
110 }
111 
PutValue(Uri & uri,const std::string & column,int value)112 int32_t CellularDataSettingsRdbHelper::PutValue(Uri &uri, const std::string &column, int value)
113 {
114     std::shared_ptr<DataShare::DataShareHelper> settingHelper = CreateDataShareHelper();
115     if (settingHelper == nullptr) {
116         TELEPHONY_LOGE("helper_ is null");
117         return TELEPHONY_ERR_LOCAL_PTR_NULL;
118     }
119     int32_t existValue = 0;
120     int32_t getValueRet = GetValue(uri, column, existValue);
121     DataShare::DataShareValueObject keyObj(column);
122     DataShare::DataShareValueObject valueObj(std::to_string(value));
123     DataShare::DataShareValuesBucket bucket;
124     bucket.Put(CELLULAR_DATA_COLUMN_VALUE, valueObj);
125     bucket.Put(CELLULAR_DATA_COLUMN_KEYWORD, keyObj);
126     int32_t result;
127     if (getValueRet != TELEPHONY_ERR_SUCCESS) {
128         result = settingHelper->Insert(uri, bucket);
129     } else {
130         DataShare::DataSharePredicates predicates;
131         predicates.EqualTo(CELLULAR_DATA_COLUMN_KEYWORD, column);
132         result = settingHelper->Update(uri, predicates, bucket);
133     }
134     TELEPHONY_LOGI("put value return %{public}d", result);
135     if (result == E_ERROR) {
136         Uri userDataEnableUri(CELLULAR_DATA_SETTING_DATA_ENABLE_URI);
137         Uri userDataRoamingUri(CELLULAR_DATA_SETTING_DATA_ROAMING_URI);
138         if (uri == userDataEnableUri) {
139             CellularDataHiSysEvent::WriteDataActivateFaultEvent(INVALID_PARAMETER, value,
140                 CellularDataErrorCode::DATA_ERROR_DATABASE_WRITE_ERROR,
141                 "SetCellularDataEnable " + std::to_string(value) + " fail");
142         } else if (uri == userDataRoamingUri) {
143             CellularDataHiSysEvent::WriteDataActivateFaultEvent(INVALID_PARAMETER, value,
144                 CellularDataErrorCode::DATA_ERROR_DATABASE_WRITE_ERROR,
145                 "SetUserDataRoamingOn " + std::to_string(value) + " fail");
146         } else {
147             TELEPHONY_LOGI("result is %{public}d, do not handle.", result);
148         }
149         settingHelper->Release();
150         return TELEPHONY_ERR_DATABASE_WRITE_FAIL;
151     }
152     settingHelper->NotifyChange(uri);
153     settingHelper->Release();
154     return TELEPHONY_ERR_SUCCESS;
155 }
156 } // namespace Telephony
157 } // namespace OHOS
158