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 "data_switch_settings.h"
17
18 #include "cellular_data_constant.h"
19 #include "cellular_data_error.h"
20 #include "cellular_data_settings_rdb_helper.h"
21 #include "core_manager_inner.h"
22 #include "telephony_log_wrapper.h"
23
24 namespace OHOS {
25 namespace Telephony {
DataSwitchSettings(int32_t slotId)26 DataSwitchSettings::DataSwitchSettings(int32_t slotId) : slotId_(slotId) {}
27
LoadSwitchValue()28 void DataSwitchSettings::LoadSwitchValue()
29 {
30 bool dataEnabled = false;
31 bool dataRoamingEnabled = false;
32 QueryUserDataStatus(dataEnabled);
33 QueryUserDataRoamingStatus(dataRoamingEnabled);
34 TELEPHONY_LOGI("userDataOn_:%{public}d userDataRoaming_:%{public}d policyDataOn_:%{public}d",
35 userDataOn_, userDataRoaming_, policyDataOn_);
36 }
37
IsInternalDataOn() const38 bool DataSwitchSettings::IsInternalDataOn() const
39 {
40 return internalDataOn_;
41 }
42
SetInternalDataOn(bool internalDataOn)43 void DataSwitchSettings::SetInternalDataOn(bool internalDataOn)
44 {
45 internalDataOn_ = internalDataOn;
46 }
47
SetUserDataOn(bool userDataOn)48 int32_t DataSwitchSettings::SetUserDataOn(bool userDataOn)
49 {
50 std::shared_ptr<CellularDataSettingsRdbHelper> settingsRdbHelper = CellularDataSettingsRdbHelper::GetInstance();
51 if (settingsRdbHelper == nullptr) {
52 TELEPHONY_LOGE("settingsRdbHelper == nullptr!");
53 return TELEPHONY_ERR_LOCAL_PTR_NULL;
54 }
55 int value = (userDataOn ? static_cast<int>(DataSwitchCode::CELLULAR_DATA_ENABLED)
56 : static_cast<int>(DataSwitchCode::CELLULAR_DATA_DISABLED));
57 TELEPHONY_LOGI("value:%{public}d", value);
58 Uri userDataEnableUri(CELLULAR_DATA_SETTING_DATA_ENABLE_URI);
59 int32_t result = settingsRdbHelper->PutValue(userDataEnableUri, CELLULAR_DATA_COLUMN_ENABLE, value);
60 if (result == TELEPHONY_ERR_SUCCESS) {
61 userDataOn_ = userDataOn;
62 }
63 return result;
64 }
65
SetIntelliSwitchOn(bool userSwitchOn)66 int32_t DataSwitchSettings::SetIntelliSwitchOn(bool userSwitchOn)
67 {
68 std::shared_ptr<CellularDataSettingsRdbHelper> settingsRdbHelper = CellularDataSettingsRdbHelper::GetInstance();
69 if (settingsRdbHelper == nullptr) {
70 TELEPHONY_LOGE("settingsRdbHelper == nullptr!");
71 return TELEPHONY_ERR_LOCAL_PTR_NULL;
72 }
73 int value = (userSwitchOn ? static_cast<int>(IntelligenceSwitchCode::INTELLIGENCE_SWITCH_ENABLED)
74 : static_cast<int>(IntelligenceSwitchCode::INTELLIGENCE_SWITCH_DISABLED));
75 TELEPHONY_LOGI("value:%{public}d", value);
76 Uri intelliSwitchEnableUri(CELLULAR_DATA_SETTING_INTELLIGENCE_SWITCH_URI);
77 int32_t result = settingsRdbHelper->PutValue(intelliSwitchEnableUri, INTELLIGENCE_SWITCH_COLUMN_ENABLE, value);
78 if (result == TELEPHONY_ERR_SUCCESS) {
79 intelliSwitchOn_ = userSwitchOn;
80 }
81 return result;
82 }
83
QueryUserDataStatus(bool & dataEnabled)84 int32_t DataSwitchSettings::QueryUserDataStatus(bool &dataEnabled)
85 {
86 std::shared_ptr<CellularDataSettingsRdbHelper> settingsRdbHelper = CellularDataSettingsRdbHelper::GetInstance();
87 if (settingsRdbHelper == nullptr) {
88 TELEPHONY_LOGE("settingsRdbHelper is nullptr!");
89 return TELEPHONY_ERR_LOCAL_PTR_NULL;
90 }
91 Uri userDataEnableUri(CELLULAR_DATA_SETTING_DATA_ENABLE_URI);
92 int32_t userDataEnable = static_cast<int32_t>(DataSwitchCode::CELLULAR_DATA_ENABLED);
93 int32_t ret = settingsRdbHelper->GetValue(userDataEnableUri, CELLULAR_DATA_COLUMN_ENABLE, userDataEnable);
94 if (ret != TELEPHONY_ERR_SUCCESS) {
95 TELEPHONY_LOGE("GetValue failed!");
96 }
97 userDataOn_ = (userDataEnable == static_cast<int32_t>(DataSwitchCode::CELLULAR_DATA_ENABLED));
98 dataEnabled = userDataOn_;
99 return TELEPHONY_ERR_SUCCESS;
100 }
101
QueryIntelligenceSwitchStatus(bool & switchEnabled)102 int32_t DataSwitchSettings::QueryIntelligenceSwitchStatus(bool &switchEnabled)
103 {
104 std::shared_ptr<CellularDataSettingsRdbHelper> settingsRdbHelper = CellularDataSettingsRdbHelper::GetInstance();
105 if (settingsRdbHelper == nullptr) {
106 TELEPHONY_LOGE("settingsRdbHelper is nullptr!");
107 return TELEPHONY_ERR_LOCAL_PTR_NULL;
108 }
109 Uri intelliSwitchEnableUri(CELLULAR_DATA_SETTING_INTELLIGENCE_SWITCH_URI);
110 int32_t intelliSwitchEnable = static_cast<int32_t>(IntelligenceSwitchCode::INTELLIGENCE_SWITCH_DISABLED);
111 int32_t ret = settingsRdbHelper->GetValue(intelliSwitchEnableUri,
112 INTELLIGENCE_SWITCH_COLUMN_ENABLE, intelliSwitchEnable);
113 if (ret != TELEPHONY_ERR_SUCCESS) {
114 TELEPHONY_LOGE("GetValue failed!");
115 }
116 intelliSwitchOn_ =
117 (intelliSwitchEnable == static_cast<int32_t>(IntelligenceSwitchCode::INTELLIGENCE_SWITCH_ENABLED));
118 switchEnabled = intelliSwitchOn_;
119 return TELEPHONY_ERR_SUCCESS;
120 }
121
SetUserDataRoamingOn(bool dataRoamingEnabled)122 int32_t DataSwitchSettings::SetUserDataRoamingOn(bool dataRoamingEnabled)
123 {
124 std::shared_ptr<CellularDataSettingsRdbHelper> settingsRdbHelper = CellularDataSettingsRdbHelper::GetInstance();
125 if (settingsRdbHelper == nullptr) {
126 TELEPHONY_LOGE("settingsRdbHelper is nullptr!");
127 return TELEPHONY_ERR_LOCAL_PTR_NULL;
128 }
129 int32_t simId = CoreManagerInner::GetInstance().GetSimId(slotId_);
130 if (simId <= INVALID_SIM_ID) {
131 TELEPHONY_LOGE("Slot%{public}d: failed due to invalid sim id %{public}d", slotId_, simId);
132 return TELEPHONY_ERR_SLOTID_INVALID;
133 }
134 int value = (dataRoamingEnabled ? static_cast<int>(RoamingSwitchCode::CELLULAR_DATA_ROAMING_ENABLED)
135 : static_cast<int>(RoamingSwitchCode::CELLULAR_DATA_ROAMING_DISABLED));
136 TELEPHONY_LOGI("value:%{public}d", value);
137 Uri userDataRoamingUri(std::string(CELLULAR_DATA_SETTING_DATA_ROAMING_URI) + std::to_string(simId));
138 int32_t result = settingsRdbHelper->PutValue(
139 userDataRoamingUri, std::string(CELLULAR_DATA_COLUMN_ROAMING) + std::to_string(simId), value);
140 if (result == TELEPHONY_ERR_SUCCESS) {
141 userDataRoaming_ = dataRoamingEnabled;
142 }
143 return result;
144 }
145
QueryUserDataRoamingStatus(bool & dataRoamingEnabled)146 int32_t DataSwitchSettings::QueryUserDataRoamingStatus(bool &dataRoamingEnabled)
147 {
148 std::shared_ptr<CellularDataSettingsRdbHelper> settingsRdbHelper = CellularDataSettingsRdbHelper::GetInstance();
149 if (settingsRdbHelper == nullptr) {
150 TELEPHONY_LOGE("settingsRdbHelper is nullptr!");
151 return TELEPHONY_ERR_LOCAL_PTR_NULL;
152 }
153
154 int32_t simId = CoreManagerInner::GetInstance().GetSimId(slotId_);
155 if (simId <= INVALID_SIM_ID) {
156 TELEPHONY_LOGE("Slot%{public}d: invalid sim id %{public}d", slotId_, simId);
157 return TELEPHONY_ERR_LOCAL_PTR_NULL;
158 }
159 Uri userDataRoamingUri(std::string(CELLULAR_DATA_SETTING_DATA_ROAMING_URI) + std::to_string(simId));
160 int32_t userDataRoamingValue = static_cast<int32_t>(RoamingSwitchCode::CELLULAR_DATA_ROAMING_DISABLED);
161 int32_t ret = settingsRdbHelper->GetValue(
162 userDataRoamingUri, std::string(CELLULAR_DATA_COLUMN_ROAMING) + std::to_string(simId), userDataRoamingValue);
163 if (ret != TELEPHONY_ERR_SUCCESS) {
164 TELEPHONY_LOGE("GetValue failed!");
165 return ret;
166 }
167 userDataRoaming_ = (userDataRoamingValue == static_cast<int32_t>(RoamingSwitchCode::CELLULAR_DATA_ROAMING_ENABLED));
168 dataRoamingEnabled = userDataRoaming_;
169 return TELEPHONY_ERR_SUCCESS;
170 }
171
IsPolicyDataOn() const172 bool DataSwitchSettings::IsPolicyDataOn() const
173 {
174 return policyDataOn_;
175 }
176
SetPolicyDataOn(bool policyDataOn)177 void DataSwitchSettings::SetPolicyDataOn(bool policyDataOn)
178 {
179 policyDataOn_ = policyDataOn;
180 }
181
IsCarrierDataOn() const182 bool DataSwitchSettings::IsCarrierDataOn() const
183 {
184 return carrierDataOn_;
185 }
186
SetCarrierDataOn(bool carrierDataOn)187 void DataSwitchSettings::SetCarrierDataOn(bool carrierDataOn)
188 {
189 carrierDataOn_ = carrierDataOn;
190 }
191
IsAllowActiveData() const192 bool DataSwitchSettings::IsAllowActiveData() const
193 {
194 if (userDataOn_ && policyDataOn_ && internalDataOn_) {
195 return true;
196 } else {
197 TELEPHONY_LOGD("Activation not allowed[user:%{public}d policy:%{public}d internal:%{public}d]", userDataOn_,
198 policyDataOn_, internalDataOn_);
199 return false;
200 }
201 }
202
IsUserDataOn()203 bool DataSwitchSettings::IsUserDataOn()
204 {
205 return userDataOn_;
206 }
207
IsUserDataRoamingOn()208 bool DataSwitchSettings::IsUserDataRoamingOn()
209 {
210 return userDataRoaming_;
211 }
212 } // namespace Telephony
213 } // namespace OHOS