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 const std::string SWITCH_KEY = "intell_voice_trigger_enabled";
28
29 namespace {
AssembleUri(const std::string & key)30 Uri AssembleUri(const std::string& key)
31 {
32 Uri uri(SWITCH_URI_PROXY + "&key=" + key);
33 return uri;
34 }
35 }
36
SwitchProvider()37 SwitchProvider::SwitchProvider()
38 {
39 }
40
~SwitchProvider()41 SwitchProvider::~SwitchProvider()
42 {
43 helper_ = nullptr;
44 }
45
Init()46 bool SwitchProvider::Init()
47 {
48 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
49 if (saManager == nullptr) {
50 INTELL_VOICE_LOG_ERROR("saManager is nullptr");
51 return false;
52 }
53 auto remoteObj = saManager->GetSystemAbility(INTELL_VOICE_SERVICE_ID);
54 if (remoteObj == nullptr) {
55 INTELL_VOICE_LOG_ERROR("remoteObj is nullptr");
56 return false;
57 }
58
59 helper_ = DataShare::DataShareHelper::Creator(remoteObj, SWITCH_URI_PROXY);
60 if (helper_ == nullptr) {
61 INTELL_VOICE_LOG_ERROR("helper_ is nullptr");
62 return false;
63 }
64
65 return true;
66 }
67
RegisterObserver(const sptr<SwitchObserver> & observer)68 void SwitchProvider::RegisterObserver(const sptr<SwitchObserver> &observer)
69 {
70 auto uri = AssembleUri(SWITCH_KEY);
71 helper_->RegisterObserver(uri, observer);
72 }
73
UnregisterObserver(const sptr<SwitchObserver> & observer)74 void SwitchProvider::UnregisterObserver(const sptr<SwitchObserver> &observer)
75 {
76 auto uri = AssembleUri(SWITCH_KEY);
77 helper_->UnregisterObserver(uri, observer);
78 }
79
QuerySwitchStatus()80 bool SwitchProvider::QuerySwitchStatus()
81 {
82 std::vector<std::string> columns = {"VALUE"};
83 DataShare::DataSharePredicates predicates;
84 predicates.EqualTo("KEYWORD", SWITCH_KEY);
85 auto uri = AssembleUri(SWITCH_KEY);
86 auto resultSet = helper_->Query(uri, predicates, columns);
87 if (resultSet == nullptr) {
88 INTELL_VOICE_LOG_ERROR("helper->Query return nullptr");
89 return false;
90 }
91
92 int32_t count;
93 resultSet->GetRowCount(count);
94 if (count == 0) {
95 INTELL_VOICE_LOG_ERROR("not found value");
96 return false;
97 }
98 const int32_t INDEX = 0;
99 resultSet->GoToRow(INDEX);
100 std::string value;
101 resultSet->GetString(INDEX, value);
102 resultSet->Close();
103
104 if (value == "0") {
105 return false;
106 } else if (value == "1") {
107 return true;
108 } else {
109 return false;
110 }
111 }
112 } // namespace IntellVoice
113 } // namespace OHOS