1686862fbSopenharmony_ci/* 2686862fbSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4686862fbSopenharmony_ci * you may not use this file except in compliance with the License. 5686862fbSopenharmony_ci * You may obtain a copy of the License at 6686862fbSopenharmony_ci * 7686862fbSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8686862fbSopenharmony_ci * 9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12686862fbSopenharmony_ci * See the License for the specific language governing permissions and 13686862fbSopenharmony_ci * limitations under the License. 14686862fbSopenharmony_ci */ 15686862fbSopenharmony_ci 16686862fbSopenharmony_ci#include "switch_status_dependency.h" 17686862fbSopenharmony_ci 18686862fbSopenharmony_ci#include <fstream> 19686862fbSopenharmony_ci#include <iostream> 20686862fbSopenharmony_ci#include "datashare_manager.h" 21686862fbSopenharmony_ci#include "dtbschedmgr_log.h" 22686862fbSopenharmony_ci#include "if_system_ability_manager.h" 23686862fbSopenharmony_ci#include "ipc_skeleton.h" 24686862fbSopenharmony_ci#include "iservice_registry.h" 25686862fbSopenharmony_ci#include "system_ability_definition.h" 26686862fbSopenharmony_ci#include "uri.h" 27686862fbSopenharmony_ci 28686862fbSopenharmony_cinamespace OHOS { 29686862fbSopenharmony_cinamespace DistributedSchedule { 30686862fbSopenharmony_cinamespace { 31686862fbSopenharmony_ciconst std::string TAG = "DMSSwitchStatusDep"; 32686862fbSopenharmony_ci} 33686862fbSopenharmony_ciconst std::string SwitchStatusDependency::SETTINGS_USER_SECURE_URI = 34686862fbSopenharmony_ci "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE"; 35686862fbSopenharmony_ciconst std::string SwitchStatusDependency::SETTINGS_DATA_FIELD_KEY = "KEYWORD"; 36686862fbSopenharmony_ciconst std::string SwitchStatusDependency::SETTINGS_DATA_FIELD_VAL = "VALUE"; 37686862fbSopenharmony_ciconst std::string SwitchStatusDependency::CONTINUE_SWITCH_STATUS_KEY = "Continue_Switch_Status"; 38686862fbSopenharmony_ciconst std::string SwitchStatusDependency::CONTINUE_SWITCH_OFF = "0"; 39686862fbSopenharmony_ciconst std::string SwitchStatusDependency::CONTINUE_SWITCH_ON = "1"; 40686862fbSopenharmony_ci 41686862fbSopenharmony_ciSwitchStatusDependency &SwitchStatusDependency::GetInstance() 42686862fbSopenharmony_ci{ 43686862fbSopenharmony_ci static SwitchStatusDependency instance; 44686862fbSopenharmony_ci return instance; 45686862fbSopenharmony_ci} 46686862fbSopenharmony_ci 47686862fbSopenharmony_cibool SwitchStatusDependency::IsContinueSwitchOn() 48686862fbSopenharmony_ci{ 49686862fbSopenharmony_ci HILOGD("IsContinueSwitchOn start"); 50686862fbSopenharmony_ci std::lock_guard<std::mutex> lock(dataShareMutex_); 51686862fbSopenharmony_ci HILOGD("start query switch status from dataShare"); 52686862fbSopenharmony_ci switchStatus_ = GetSwitchStatus(CONTINUE_SWITCH_STATUS_KEY, CONTINUE_SWITCH_ON); 53686862fbSopenharmony_ci return switchStatus_ != CONTINUE_SWITCH_OFF; 54686862fbSopenharmony_ci} 55686862fbSopenharmony_ci 56686862fbSopenharmony_cistd::string SwitchStatusDependency::GetSwitchStatus(const std::string &key, const std::string &defaultValue) 57686862fbSopenharmony_ci{ 58686862fbSopenharmony_ci HILOGD("GetSwitchStatus start, key is %{public}s", key.c_str()); 59686862fbSopenharmony_ci std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = GetDataShareHelper(); 60686862fbSopenharmony_ci if (dataShareHelper == nullptr) { 61686862fbSopenharmony_ci HILOGE("dataShareHelper is null, key is %{public}s", key.c_str()); 62686862fbSopenharmony_ci return defaultValue; 63686862fbSopenharmony_ci } 64686862fbSopenharmony_ci int32_t userId = DataShareManager::GetInstance().GetLocalAccountId(); 65686862fbSopenharmony_ci Uri uri(DataShareManager::GetInstance().AssembleUserSecureUri(userId, key)); 66686862fbSopenharmony_ci DataShare::DataSharePredicates dataSharePredicates; 67686862fbSopenharmony_ci std::vector<std::string> columns; 68686862fbSopenharmony_ci dataSharePredicates.EqualTo(SETTINGS_DATA_FIELD_KEY, key); 69686862fbSopenharmony_ci columns.emplace_back(SETTINGS_DATA_FIELD_VAL); 70686862fbSopenharmony_ci auto resultSet = dataShareHelper->Query(uri, dataSharePredicates, columns); 71686862fbSopenharmony_ci if (resultSet == nullptr) { 72686862fbSopenharmony_ci HILOGE("get switch status, resultSet is nullptr with key is %{public}s", key.c_str()); 73686862fbSopenharmony_ci dataShareHelper->Release(); 74686862fbSopenharmony_ci return defaultValue; 75686862fbSopenharmony_ci } 76686862fbSopenharmony_ci int32_t numRows = 0; 77686862fbSopenharmony_ci resultSet->GetRowCount(numRows); 78686862fbSopenharmony_ci if (numRows == 0) { 79686862fbSopenharmony_ci HILOGW("get switch status, numRows is zero with key is %{public}s", key.c_str()); 80686862fbSopenharmony_ci resultSet->Close(); 81686862fbSopenharmony_ci dataShareHelper->Release(); 82686862fbSopenharmony_ci return defaultValue; 83686862fbSopenharmony_ci } 84686862fbSopenharmony_ci int32_t columnIndex = 0; 85686862fbSopenharmony_ci int32_t rowNumber = 0; 86686862fbSopenharmony_ci resultSet->GoToRow(rowNumber); 87686862fbSopenharmony_ci std::string valueResult; 88686862fbSopenharmony_ci int32_t ret = resultSet->GetString(columnIndex, valueResult); 89686862fbSopenharmony_ci if (ret != 0) { 90686862fbSopenharmony_ci HILOGE("get switch status, resultSet->GetString not ok with key is %{public}s", key.c_str()); 91686862fbSopenharmony_ci resultSet->Close(); 92686862fbSopenharmony_ci dataShareHelper->Release(); 93686862fbSopenharmony_ci return defaultValue; 94686862fbSopenharmony_ci } 95686862fbSopenharmony_ci resultSet->Close(); 96686862fbSopenharmony_ci dataShareHelper->Release(); 97686862fbSopenharmony_ci HILOGI("GetStringValue, setting value is %{public}s with key is %{public}s", valueResult.c_str(), key.c_str()); 98686862fbSopenharmony_ci return valueResult; 99686862fbSopenharmony_ci} 100686862fbSopenharmony_ci 101686862fbSopenharmony_cistd::shared_ptr<DataShare::DataShareHelper> SwitchStatusDependency::GetDataShareHelper() 102686862fbSopenharmony_ci{ 103686862fbSopenharmony_ci HILOGD("create DataShareHelper instance"); 104686862fbSopenharmony_ci DataShare::CreateOptions options; 105686862fbSopenharmony_ci options.isProxy_ = true; 106686862fbSopenharmony_ci return DataShare::DataShareHelper::Creator(SETTINGS_USER_SECURE_URI, options); 107686862fbSopenharmony_ci} 108686862fbSopenharmony_ci} // namespace DistributedSchedule 109686862fbSopenharmony_ci} // namespace OHOS