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_roaming_observer.h"
17
18 #include "cellular_data_constant.h"
19 #include "cellular_data_event_code.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 {
CellularDataRoamingObserver(std::shared_ptr<AppExecFwk::EventHandler> && cellularDataHandler,int32_t slotId)26 CellularDataRoamingObserver::CellularDataRoamingObserver(
27 std::shared_ptr<AppExecFwk::EventHandler> &&cellularDataHandler, int32_t slotId)
28 : cellularDataHandler_(std::move(cellularDataHandler)), slotId_(slotId)
29 {}
30
31 CellularDataRoamingObserver::~CellularDataRoamingObserver() = default;
32
OnChange()33 void CellularDataRoamingObserver::OnChange()
34 {
35 std::shared_ptr<CellularDataSettingsRdbHelper> settingHelper = CellularDataSettingsRdbHelper::GetInstance();
36 if (settingHelper == nullptr) {
37 return;
38 }
39 int32_t simId = CoreManagerInner::GetInstance().GetSimId(slotId_);
40 if (simId <= INVALID_SIM_ID) {
41 TELEPHONY_LOGE("Slot%{public}d: failed due to invalid sim id %{public}d", slotId_, simId);
42 return;
43 }
44 Uri uri(std::string(CELLULAR_DATA_SETTING_DATA_ROAMING_URI) + std::to_string(simId));
45 int value = static_cast<int32_t>(RoamingSwitchCode::CELLULAR_DATA_ROAMING_DISABLED);
46 if (settingHelper->GetValue(uri, std::string(CELLULAR_DATA_COLUMN_ROAMING) + std::to_string(simId), value) !=
47 TELEPHONY_ERR_SUCCESS) {
48 TELEPHONY_LOGE("OnChange GetValue failed!");
49 return;
50 }
51
52 TELEPHONY_LOGI("cellular data roaming switch is %{public}d", value);
53 if (cellularDataHandler_ != nullptr) {
54 cellularDataHandler_->SendEvent(CellularDataEventCode::MSG_DB_SETTING_ROAMING_CHANGED, value, 0);
55 }
56 }
57 } // namespace Telephony
58 } // namespace OHOS