• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
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     return true;
65 }
66 
RegisterObserver(const sptr<SwitchObserver> & observer,const std::string & key)67 void SwitchProvider::RegisterObserver(const sptr<SwitchObserver> &observer, const std::string &key)
68 {
69     auto uri = AssembleUri(key);
70     helper_->RegisterObserver(uri, observer);
71 }
72 
UnregisterObserver(const sptr<SwitchObserver> & observer,const std::string & key)73 void SwitchProvider::UnregisterObserver(const sptr<SwitchObserver> &observer, const std::string &key)
74 {
75     auto uri = AssembleUri(key);
76     helper_->UnregisterObserver(uri, observer);
77 }
78 
QuerySwitchStatus(const std::string & key)79 bool SwitchProvider::QuerySwitchStatus(const std::string &key)
80 {
81     std::vector<std::string> columns = {"VALUE"};
82     DataShare::DataSharePredicates predicates;
83     predicates.EqualTo("KEYWORD", key);
84     auto uri = AssembleUri(key);
85     auto resultSet = helper_->Query(uri, predicates, columns);
86     if (resultSet == nullptr) {
87         INTELL_VOICE_LOG_ERROR("helper->Query return nullptr");
88         return false;
89     }
90 
91     int32_t count;
92     resultSet->GetRowCount(count);
93     if (count == 0) {
94         INTELL_VOICE_LOG_WARN("not found value, key is %{public}s", key.c_str());
95         resultSet->Close();
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 
IsSwitchError(const std::string & key)113 bool SwitchProvider::IsSwitchError(const std::string &key)
114 {
115     std::vector<std::string> columns = {"VALUE"};
116     DataShare::DataSharePredicates predicates;
117     predicates.EqualTo("KEYWORD", key);
118     auto uri = AssembleUri(key);
119     auto resultSet = helper_->Query(uri, predicates, columns);
120     if (resultSet == nullptr) {
121         INTELL_VOICE_LOG_ERROR("resultSet is nullptr");
122         return true;
123     }
124 
125     int32_t count;
126     resultSet->GetRowCount(count);
127     if (count == 0) {
128         INTELL_VOICE_LOG_ERROR("not found value, key is %{public}s", key.c_str());
129         resultSet->Close();
130         return true;
131     }
132 
133     return false;
134 }
135 
CheckIfDataShareReady()136 bool SwitchProvider::CheckIfDataShareReady()
137 {
138     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
139     if (saManager == nullptr) {
140         INTELL_VOICE_LOG_ERROR("saManager is nullptr");
141         return false;
142     }
143     auto remoteObj = saManager->GetSystemAbility(INTELL_VOICE_SERVICE_ID);
144     if (remoteObj == nullptr) {
145         INTELL_VOICE_LOG_ERROR("remoteObj is nullptr");
146         return false;
147     }
148 
149     auto ret = DataShare::DataShareHelper::Create(remoteObj, SWITCH_URI_PROXY, SWITCH_DATA_EXT_URI);
150     if (ret.first == OHOS::DataShare::E_OK) {
151         INTELL_VOICE_LOG_INFO("create data share helper success");
152         if (ret.second != nullptr) {
153             ret.second->Release();
154         }
155         return true;
156     }
157     if (ret.first == OHOS::DataShare::E_DATA_SHARE_NOT_READY) {
158         INTELL_VOICE_LOG_INFO("create data share helper failed");
159         return false;
160     }
161     INTELL_VOICE_LOG_INFO("data share ret:%{public}d is unknown", ret.first);
162     return true;
163 }
164 }  // namespace IntellVoice
165 }  // namespace OHOS