1 /*
2  * Copyright (c) 2023 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 #include "switch_provider.h"
16 #include "intell_voice_log.h"
17 #include "iservice_registry.h"
18 #include "system_ability_definition.h"
19 
20 #define LOG_TAG "SwitchProvider"
21 
22 using namespace OHOS::IntellVoiceUtils;
23 
24 namespace OHOS {
25 namespace IntellVoiceEngine {
26 const std::string SWITCH_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
27 
28 namespace {
AssembleUri(const std::string & key)29 Uri AssembleUri(const std::string& key)
30 {
31     Uri uri(SWITCH_URI_PROXY + "&key=" + key);
32     return uri;
33 }
34 }
35 
SwitchProvider()36 SwitchProvider::SwitchProvider()
37 {
38 }
39 
~SwitchProvider()40 SwitchProvider::~SwitchProvider()
41 {
42     helper_ = nullptr;
43 }
44 
Init()45 bool SwitchProvider::Init()
46 {
47     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48     if (saManager == nullptr) {
49         INTELL_VOICE_LOG_ERROR("saManager is nullptr");
50         return false;
51     }
52     auto remoteObj = saManager->GetSystemAbility(INTELL_VOICE_SERVICE_ID);
53     if (remoteObj == nullptr) {
54         INTELL_VOICE_LOG_ERROR("remoteObj is nullptr");
55         return false;
56     }
57 
58     helper_ = DataShare::DataShareHelper::Creator(remoteObj, SWITCH_URI_PROXY);
59     if (helper_ == nullptr) {
60         INTELL_VOICE_LOG_ERROR("helper_ is nullptr");
61         return false;
62     }
63     return true;
64 }
65 
RegisterObserver(const sptr<SwitchObserver> & observer,const std::string & key)66 void SwitchProvider::RegisterObserver(const sptr<SwitchObserver> &observer, const std::string &key)
67 {
68     auto uri = AssembleUri(key);
69     helper_->RegisterObserver(uri, observer);
70 }
71 
UnregisterObserver(const sptr<SwitchObserver> & observer,const std::string & key)72 void SwitchProvider::UnregisterObserver(const sptr<SwitchObserver> &observer, const std::string &key)
73 {
74     auto uri = AssembleUri(key);
75     helper_->UnregisterObserver(uri, observer);
76 }
77 
QuerySwitchStatus(const std::string & key)78 bool SwitchProvider::QuerySwitchStatus(const std::string &key)
79 {
80     std::vector<std::string> columns = {"VALUE"};
81     DataShare::DataSharePredicates predicates;
82     predicates.EqualTo("KEYWORD", key);
83     auto uri = AssembleUri(key);
84     auto resultSet = helper_->Query(uri, predicates, columns);
85     if (resultSet == nullptr) {
86         INTELL_VOICE_LOG_ERROR("helper->Query return nullptr");
87         return false;
88     }
89 
90     int32_t count;
91     resultSet->GetRowCount(count);
92     if (count == 0) {
93         INTELL_VOICE_LOG_WARN("not found value, key is %{public}s", key.c_str());
94         resultSet->Close();
95         return false;
96     }
97     const int32_t INDEX = 0;
98     resultSet->GoToRow(INDEX);
99     std::string value;
100     resultSet->GetString(INDEX, value);
101     resultSet->Close();
102 
103     if (value == "0") {
104         return false;
105     } else if (value == "1") {
106         return true;
107     } else {
108         return false;
109     }
110 }
111 }  // namespace IntellVoice
112 }  // namespace OHOS