• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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 "data_share_helper.h"
17 #include "common/sharing_log.h"
18 #include "iservice_registry.h"
19 #include "os_account_manager.h"
20 
21 namespace OHOS {
22 namespace Sharing {
23 const std::string SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
24 const std::string SETTINGS_DATA_BASE_URI =
25     "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
26 const std::string SETTINGS_DATA_SECURE_URI =
27     "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_";
28 const std::string SETTINGS_GENERAL_DEVICE_NAME = "settings.general.device_name";
29 const std::string USER_DEFINED_STRING = "settings.general.user_defined_device_name";
30 constexpr int32_t DEFAULT_OS_ACCOUNT_ID = 100;
31 
GetInstance()32 DataShareHelper &DataShareHelper::GetInstance()
33 {
34     static DataShareHelper instance{};
35     return instance;
36 }
37 
~DataShareHelper()38 DataShareHelper::~DataShareHelper()
39 {
40     helper_ = nullptr;
41 }
42 
RegisterObserver(const sptr<AAFwk::IDataAbilityObserver> & observer)43 int32_t DataShareHelper::RegisterObserver(const sptr<AAFwk::IDataAbilityObserver> &observer)
44 {
45     if (helper_ == nullptr && !Init()) {
46         SHARING_LOGE("fail to register observer, helper is null");
47         return DATA_SHARE_ERROR;
48     }
49     helper_->RegisterObserver(GetDefaultNamerUri(), observer);
50     helper_->RegisterObserver(GetUserDefinedNameUri(), observer);
51     return DATA_SHARE_SUCCESS;
52 }
53 
UnregisterObserver(const sptr<AAFwk::IDataAbilityObserver> & observer)54 int32_t DataShareHelper::UnregisterObserver(const sptr<AAFwk::IDataAbilityObserver> &observer)
55 {
56     if (helper_ == nullptr && !Init()) {
57         SHARING_LOGE("fail to unregister observer, helper is null");
58         return DATA_SHARE_ERROR;
59     }
60     helper_->UnregisterObserver(GetDefaultNamerUri(), observer);
61     helper_->UnregisterObserver(GetUserDefinedNameUri(), observer);
62     return DATA_SHARE_SUCCESS;
63 }
64 
Init()65 bool DataShareHelper::Init()
66 {
67     SHARING_LOGI("init enter");
68     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
69     if (samgr == nullptr) {
70         SHARING_LOGE("faile to init samgr is null");
71         return false;
72     }
73 
74     auto remoteObj = samgr->GetSystemAbility(SHARING_SERVICE_TEMP_SA_ID);
75     if (remoteObj == nullptr) {
76         SHARING_LOGE("faile to init remoteObj is null");
77         return false;
78     }
79     helper_ = DataShare::DataShareHelper::Creator(remoteObj, SETTINGS_DATA_BASE_URI, SETTINGS_DATA_EXT_URI);
80     if (helper_ == nullptr) {
81         SHARING_LOGE("faile to init helper_ is null");
82         return false;
83     }
84     SHARING_LOGI("init end");
85     return true;
86 }
87 
GetDefaultNamerUri()88 Uri DataShareHelper::GetDefaultNamerUri()
89 {
90     return Uri(SETTINGS_DATA_BASE_URI + "&key=" + SETTINGS_GENERAL_DEVICE_NAME);
91 }
92 
GetUserDefinedNameUri()93 Uri DataShareHelper::GetUserDefinedNameUri()
94 {
95     std::string osAccountId = std::to_string(GetCurrentActiveAccountUserId());
96     return Uri(SETTINGS_DATA_SECURE_URI + osAccountId + "?Proxy=true&key=" + USER_DEFINED_STRING);
97 }
98 
GetCurrentActiveAccountUserId()99 int32_t DataShareHelper::GetCurrentActiveAccountUserId()
100 {
101     std::vector<int32_t> activatedOsAccountIds;
102     ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activatedOsAccountIds);
103     if (ret != ERR_OK || activatedOsAccountIds.empty()) {
104         SHARING_LOGE("faile to get active userId, ret=%{public}d", ret);
105         return DEFAULT_OS_ACCOUNT_ID;
106     }
107     SHARING_LOGI("active user=%{public}d", activatedOsAccountIds[0]);
108     return activatedOsAccountIds[0];
109 }
110 } // namespace Sharing
111 } // namespace OHOS