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