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 "advanced_datashare_helper_ext.h"
17
18 #include "ans_log_wrapper.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "message_parcel.h"
22 #include "os_account_manager.h"
23 #include "singleton.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace Notification {
28 namespace {
29 constexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
30 constexpr const char *USER_SETTINGS_DATA_SECURE_URI =
31 "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_";
32 constexpr const char *SETTINGS_DATASHARE_URI =
33 "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_100?Proxy=true";
34 constexpr const char *UNIFIED_GROUP_ENABLE_URI = "?Proxy=true&key=unified_group_enable";
35 constexpr const char *ADVANCED_DATA_COLUMN_KEYWORD = "KEYWORD";
36 constexpr const char *ADVANCED_DATA_COLUMN_VALUE = "VALUE";
37 } // namespace
AdvancedDatashareHelperExt()38 AdvancedDatashareHelperExt::AdvancedDatashareHelperExt()
39 {
40 CreateDataShareHelper();
41 }
42
CreateDataShareHelper()43 std::shared_ptr<DataShare::DataShareHelper> AdvancedDatashareHelperExt::CreateDataShareHelper()
44 {
45 sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
46 if (saManager == nullptr) {
47 ANS_LOGE("The sa manager is nullptr.");
48 return nullptr;
49 }
50 sptr<IRemoteObject> remoteObj = saManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
51 if (remoteObj == nullptr) {
52 ANS_LOGE("The remoteObj is nullptr.");
53 return nullptr;
54 }
55 return DataShare::DataShareHelper::Creator(remoteObj, SETTINGS_DATASHARE_URI, SETTINGS_DATA_EXT_URI);
56 }
57
Query(Uri & uri,const std::string & key,std::string & value)58 bool AdvancedDatashareHelperExt::Query(Uri &uri, const std::string &key, std::string &value)
59 {
60 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataShareHelper();
61 if (dataShareHelper == nullptr) {
62 ANS_LOGE("The data share helper is nullptr.");
63 return false;
64 }
65 DataShare::DataSharePredicates predicates;
66 std::vector<std::string> columns;
67 predicates.EqualTo(ADVANCED_DATA_COLUMN_KEYWORD, key);
68 auto result = dataShareHelper->Query(uri, predicates, columns);
69 if (result == nullptr) {
70 ANS_LOGE("Query error, result is null.");
71 dataShareHelper->Release();
72 return false;
73 }
74 if (result->GoToFirstRow() != DataShare::E_OK) {
75 ANS_LOGE("Query failed, go to first row error.");
76 result->Close();
77 dataShareHelper->Release();
78 return false;
79 }
80 int32_t columnIndex;
81 result->GetColumnIndex(ADVANCED_DATA_COLUMN_VALUE, columnIndex);
82 result->GetString(columnIndex, value);
83 result->Close();
84 ANS_LOGD("Query success, value[%{public}s]", value.c_str());
85 dataShareHelper->Release();
86 return true;
87 }
88
GetUnifiedGroupEnableUri() const89 std::string AdvancedDatashareHelperExt::GetUnifiedGroupEnableUri() const
90 {
91 std::vector<int32_t> accountIds;
92 OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(accountIds);
93 std::string userId = "100";
94 if (!accountIds.empty()) {
95 userId = std::to_string(accountIds[0]);
96 }
97 return USER_SETTINGS_DATA_SECURE_URI + userId + UNIFIED_GROUP_ENABLE_URI;
98 }
99 } // namespace Notification
100 } // namespace OHOS
101