• 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 "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     IsUserDataOn(dataEnabled);
33     IsUserDataRoamingOn(dataRoamingEnabled);
34     TELEPHONY_LOGI("LoadSwitchValue 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("SetUserDataOn 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("SetUserDataOn 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 
IsUserDataOn(bool & dataEnabled)66 int32_t DataSwitchSettings::IsUserDataOn(bool &dataEnabled)
67 {
68     std::shared_ptr<CellularDataSettingsRdbHelper> settingsRdbHelper = CellularDataSettingsRdbHelper::GetInstance();
69     if (settingsRdbHelper == nullptr) {
70         TELEPHONY_LOGE("IsUserDataOn settingsRdbHelper == nullptr!");
71         return TELEPHONY_ERR_LOCAL_PTR_NULL;
72     }
73     Uri userDataEnableUri(CELLULAR_DATA_SETTING_DATA_ENABLE_URI);
74     int32_t userDataEnable = static_cast<int32_t>(DataSwitchCode::CELLULAR_DATA_DISABLED);
75     int32_t ret = settingsRdbHelper->GetValue(userDataEnableUri, CELLULAR_DATA_COLUMN_ENABLE, userDataEnable);
76     if (ret != TELEPHONY_ERR_SUCCESS) {
77         TELEPHONY_LOGE("GetValue failed!");
78         return ret;
79     }
80     userDataOn_ = (userDataEnable == static_cast<int32_t>(DataSwitchCode::CELLULAR_DATA_ENABLED));
81     dataEnabled = userDataOn_;
82     return TELEPHONY_ERR_SUCCESS;
83 }
84 
SetUserDataRoamingOn(bool dataRoamingEnabled)85 int32_t DataSwitchSettings::SetUserDataRoamingOn(bool dataRoamingEnabled)
86 {
87     std::shared_ptr<CellularDataSettingsRdbHelper> settingsRdbHelper = CellularDataSettingsRdbHelper::GetInstance();
88     if (settingsRdbHelper == nullptr) {
89         TELEPHONY_LOGE("SetUserDataRoamingOn settingsRdbHelper is nullptr!");
90         return TELEPHONY_ERR_LOCAL_PTR_NULL;
91     }
92     int32_t simId = CoreManagerInner::GetInstance().GetSimId(slotId_);
93     if (simId <= INVALID_SIM_ID) {
94         TELEPHONY_LOGE("Slot%{public}d: failed due to invalid sim id %{public}d", slotId_, simId);
95         return TELEPHONY_ERR_SLOTID_INVALID;
96     }
97     int value = (dataRoamingEnabled ? static_cast<int>(RoamingSwitchCode::CELLULAR_DATA_ROAMING_ENABLED)
98                                     : static_cast<int>(RoamingSwitchCode::CELLULAR_DATA_ROAMING_DISABLED));
99     TELEPHONY_LOGI("SetUserDataRoamingOn value:%{public}d", value);
100     Uri userDataRoamingUri(std::string(CELLULAR_DATA_SETTING_DATA_ROAMING_URI) + std::to_string(simId));
101     int32_t result = settingsRdbHelper->PutValue(
102         userDataRoamingUri, std::string(CELLULAR_DATA_COLUMN_ROAMING) + std::to_string(simId), value);
103     if (result == TELEPHONY_ERR_SUCCESS) {
104         userDataRoaming_ = dataRoamingEnabled;
105     }
106     return result;
107 }
108 
IsUserDataRoamingOn(bool & dataRoamingEnabled)109 int32_t DataSwitchSettings::IsUserDataRoamingOn(bool &dataRoamingEnabled)
110 {
111     std::shared_ptr<CellularDataSettingsRdbHelper> settingsRdbHelper = CellularDataSettingsRdbHelper::GetInstance();
112     if (settingsRdbHelper == nullptr) {
113         TELEPHONY_LOGE("IsUserDataRoamingOn settingsRdbHelper == nullptr!");
114         return TELEPHONY_ERR_LOCAL_PTR_NULL;
115     }
116 
117     int32_t simId = CoreManagerInner::GetInstance().GetSimId(slotId_);
118     if (simId <= INVALID_SIM_ID) {
119         TELEPHONY_LOGE("Slot%{public}d: IsUserDataRoamingOn invalid sim id %{public}d", slotId_, simId);
120         return TELEPHONY_ERR_LOCAL_PTR_NULL;
121     }
122     Uri userDataRoamingUri(std::string(CELLULAR_DATA_SETTING_DATA_ROAMING_URI) + std::to_string(simId));
123     int32_t userDataRoamingValue = static_cast<int32_t>(RoamingSwitchCode::CELLULAR_DATA_ROAMING_DISABLED);
124     int32_t ret = settingsRdbHelper->GetValue(
125         userDataRoamingUri, std::string(CELLULAR_DATA_COLUMN_ROAMING) + std::to_string(simId), userDataRoamingValue);
126     if (ret != TELEPHONY_ERR_SUCCESS) {
127         TELEPHONY_LOGE("GetValue failed!");
128         return ret;
129     }
130     userDataRoaming_ = (userDataRoamingValue == static_cast<int32_t>(RoamingSwitchCode::CELLULAR_DATA_ROAMING_ENABLED));
131     dataRoamingEnabled = userDataRoaming_;
132     return TELEPHONY_ERR_SUCCESS;
133 }
134 
IsPolicyDataOn() const135 bool DataSwitchSettings::IsPolicyDataOn() const
136 {
137     return policyDataOn_;
138 }
139 
SetPolicyDataOn(bool policyDataOn)140 void DataSwitchSettings::SetPolicyDataOn(bool policyDataOn)
141 {
142     policyDataOn_ = policyDataOn;
143 }
144 
IsCarrierDataOn() const145 bool DataSwitchSettings::IsCarrierDataOn() const
146 {
147     return carrierDataOn_;
148 }
149 
SetCarrierDataOn(bool carrierDataOn)150 void DataSwitchSettings::SetCarrierDataOn(bool carrierDataOn)
151 {
152     carrierDataOn_ = carrierDataOn;
153 }
154 
IsAllowActiveData() const155 bool DataSwitchSettings::IsAllowActiveData() const
156 {
157     if (userDataOn_ && policyDataOn_) {
158         return true;
159     } else {
160         TELEPHONY_LOGE("Activation not allowed[user:%{public}d policy:%{public}d]", userDataOn_, policyDataOn_);
161         return false;
162     }
163 }
164 } // namespace Telephony
165 } // namespace OHOS