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