1 /*
2  * Copyright (c) 2024 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 "switch_status_dependency.h"
17 
18 #include <fstream>
19 #include <iostream>
20 #include "datashare_manager.h"
21 #include "dtbschedmgr_log.h"
22 #include "if_system_ability_manager.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26 #include "uri.h"
27 
28 namespace OHOS {
29 namespace DistributedSchedule {
30 namespace {
31 const std::string TAG = "DMSSwitchStatusDep";
32 }
33 const std::string SwitchStatusDependency::SETTINGS_USER_SECURE_URI =
34     "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE";
35 const std::string SwitchStatusDependency::SETTINGS_DATA_FIELD_KEY = "KEYWORD";
36 const std::string SwitchStatusDependency::SETTINGS_DATA_FIELD_VAL = "VALUE";
37 const std::string SwitchStatusDependency::CONTINUE_SWITCH_STATUS_KEY = "Continue_Switch_Status";
38 const std::string SwitchStatusDependency::CONTINUE_SWITCH_OFF = "0";
39 const std::string SwitchStatusDependency::CONTINUE_SWITCH_ON = "1";
40 
GetInstance()41 SwitchStatusDependency &SwitchStatusDependency::GetInstance()
42 {
43     static SwitchStatusDependency instance;
44     return instance;
45 }
46 
IsContinueSwitchOn()47 bool SwitchStatusDependency::IsContinueSwitchOn()
48 {
49     HILOGD("IsContinueSwitchOn start");
50     std::lock_guard<std::mutex> lock(dataShareMutex_);
51     HILOGD("start query switch status from dataShare");
52     switchStatus_ = GetSwitchStatus(CONTINUE_SWITCH_STATUS_KEY, CONTINUE_SWITCH_ON);
53     return switchStatus_ != CONTINUE_SWITCH_OFF;
54 }
55 
GetSwitchStatus(const std::string &key, const std::string &defaultValue)56 std::string SwitchStatusDependency::GetSwitchStatus(const std::string &key, const std::string &defaultValue)
57 {
58     HILOGD("GetSwitchStatus start, key is %{public}s", key.c_str());
59     std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = GetDataShareHelper();
60     if (dataShareHelper == nullptr) {
61         HILOGE("dataShareHelper is null, key is %{public}s", key.c_str());
62         return defaultValue;
63     }
64     int32_t userId = DataShareManager::GetInstance().GetLocalAccountId();
65     Uri uri(DataShareManager::GetInstance().AssembleUserSecureUri(userId, key));
66     DataShare::DataSharePredicates dataSharePredicates;
67     std::vector<std::string> columns;
68     dataSharePredicates.EqualTo(SETTINGS_DATA_FIELD_KEY, key);
69     columns.emplace_back(SETTINGS_DATA_FIELD_VAL);
70     auto resultSet = dataShareHelper->Query(uri, dataSharePredicates, columns);
71     if (resultSet == nullptr) {
72         HILOGE("get switch status, resultSet is nullptr with key is %{public}s", key.c_str());
73         dataShareHelper->Release();
74         return defaultValue;
75     }
76     int32_t numRows = 0;
77     resultSet->GetRowCount(numRows);
78     if (numRows == 0) {
79         HILOGW("get switch status, numRows is zero with key is %{public}s", key.c_str());
80         resultSet->Close();
81         dataShareHelper->Release();
82         return defaultValue;
83     }
84     int32_t columnIndex = 0;
85     int32_t rowNumber = 0;
86     resultSet->GoToRow(rowNumber);
87     std::string valueResult;
88     int32_t ret = resultSet->GetString(columnIndex, valueResult);
89     if (ret != 0) {
90         HILOGE("get switch status, resultSet->GetString not ok with key is %{public}s", key.c_str());
91         resultSet->Close();
92         dataShareHelper->Release();
93         return defaultValue;
94     }
95     resultSet->Close();
96     dataShareHelper->Release();
97     HILOGI("GetStringValue, setting value is %{public}s with key is %{public}s", valueResult.c_str(), key.c_str());
98     return valueResult;
99 }
100 
GetDataShareHelper()101 std::shared_ptr<DataShare::DataShareHelper> SwitchStatusDependency::GetDataShareHelper()
102 {
103     HILOGD("create DataShareHelper instance");
104     DataShare::CreateOptions options;
105     options.isProxy_ = true;
106     return DataShare::DataShareHelper::Creator(SETTINGS_USER_SECURE_URI, options);
107 }
108 } // namespace DistributedSchedule
109 } // namespace OHOS