• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #include "edm_data_ability_utils.h"
17 #include "datashare_helper.h"
18 #include "datashare_predicates.h"
19 #include "edm_constants.h"
20 #include "edm_log.h"
21 #include "edm_sys_manager.h"
22 #include "system_ability_definition.h"
23 #include "uri.h"
24 
25 namespace OHOS {
26 namespace EDM {
27 const std::string SETTINGS_DATA_FIELD_KEYWORD = "KEYWORD";
28 const std::string SETTINGS_DATA_FIELD_VALUE = "VALUE";
29 const std::string EdmDataAbilityUtils::SETTINGS_DATA_BASE_URI =
30     "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
31 
GetStringFromDataShare(const std::string & dataBaseUri,const std::string & key,std::string & value)32 ErrCode EdmDataAbilityUtils::GetStringFromDataShare(const std::string &dataBaseUri,
33     const std::string &key, std::string &value)
34 {
35     EDMLOGI("EdmDataAbilityUtils::GetStringFromDataShareHelper enter.");
36     sptr<IRemoteObject> remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
37     auto dataShareHelper = DataShare::DataShareHelper::Creator(remoteObject, dataBaseUri);
38     if (dataShareHelper == nullptr) {
39         EDMLOGE("EdmDataAbilityUtils::Acquire dataShareHelper failed.");
40         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
41     }
42     Uri uri(dataBaseUri);
43     std::vector<std::string> columns{SETTINGS_DATA_FIELD_VALUE};
44     DataShare::DataSharePredicates predicates;
45     predicates.EqualTo(SETTINGS_DATA_FIELD_KEYWORD, key);
46     auto resultset = dataShareHelper->Query(uri, predicates, columns);
47     if (resultset == nullptr) {
48         EDMLOGE("EdmDataAbilityUtils::GetStringFromDataAbility query fail.");
49         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
50     }
51     int32_t numRows = 0;
52     resultset->GetRowCount(numRows);
53     if (numRows <= 0) {
54         EDMLOGD("EdmDataAbilityUtils::GetStringFromDataAbility row zero.");
55         return ERR_OK;
56     }
57     int columnIndex = 0;
58     resultset->GoToFirstRow();
59     resultset->GetColumnIndex(SETTINGS_DATA_FIELD_VALUE, columnIndex);
60     resultset->GetString(columnIndex, value);
61     return ERR_OK;
62 }
63 
GetIntFromDataShare(const std::string & dataBaseUri,const std::string & key,int32_t & result)64 ErrCode EdmDataAbilityUtils::GetIntFromDataShare(const std::string &dataBaseUri,
65     const std::string &key, int32_t &result)
66 {
67     std::string valueStr;
68     if (FAILED(GetStringFromDataShare(dataBaseUri, key, valueStr))) {
69         EDMLOGE("EdmDataAbilityUtils::GetIntFromDataShare fail");
70         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
71     }
72     if (valueStr.empty()) {
73         EDMLOGE("EdmDataAbilityUtils::GetIntFromDataShare empty.");
74         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
75     }
76     result = strtol(valueStr.c_str(), nullptr, EdmConstants::DECIMAL);
77     return ERR_OK;
78 }
79 } // namespace EDM
80 } // namespace OHOS
81