• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "setting_data_helper.h"
16 
17 #include "cloud_daemon_stub.h"
18 #include "data_sync_const.h"
19 #include "datashare_helper.h"
20 #include "settings_data_manager.h"
21 #include "utils_log.h"
22 
23 namespace OHOS {
24 namespace FileManagement {
25 namespace CloudFile {
26 using namespace std;
27 using namespace OHOS::FileManagement::CloudSync;
28 namespace {
29 static const string SETTINGS_DATA_COMMON_URI =
30     "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
31 static const string SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
32 static const string SYNC_SWITCH_KEY = "photos_sync_options";
33 } // namespace
34 
GetInstance()35 SettingDataHelper &SettingDataHelper::GetInstance()
36 {
37     static SettingDataHelper instance_;
38     return instance_;
39 }
40 
SetUserData(void * data)41 void SettingDataHelper::SetUserData(void *data)
42 {
43     lock_guard<mutex> lck(dataMtx_);
44     data_ = data;
45 }
46 
IsDataShareReady()47 bool SettingDataHelper::IsDataShareReady()
48 {
49     if (isDataShareReady_) {
50         return true;
51     }
52     // try get DataShareHelper
53     auto remote = sptr<ICloudDaemonBroker>(new (std::nothrow) IRemoteStub<ICloudDaemonBroker>());
54     if (remote == nullptr) {
55         LOGE("remote is nullptr");
56         return false;
57     }
58     auto remoteObj = remote->AsObject();
59     if (remoteObj == nullptr) {
60         LOGE("remoteObj is nullptr");
61         return false;
62     }
63 
64     pair<int, shared_ptr<DataShare::DataShareHelper>> ret =
65         DataShare::DataShareHelper::Create(remoteObj, SETTINGS_DATA_COMMON_URI, SETTINGS_DATA_EXT_URI);
66     LOGI("create datashare helper, ret=%{public}d", ret.first);
67     if (ret.first == DataShare::E_DATA_SHARE_NOT_READY) {
68         LOGE("create datashare helper faild");
69         isDataShareReady_ = false;
70         return false;
71     }
72     if (ret.first == DataShare::E_OK) {
73         auto helper = ret.second;
74         if (helper != nullptr) {
75             bool releaseRet = helper->Release();
76             LOGI("release datashare helper, ret=%{public}d", releaseRet);
77         }
78         isDataShareReady_ = true;
79         return true;
80     }
81     return true;
82 }
83 
GetActiveBundle()84 string SettingDataHelper::GetActiveBundle()
85 {
86     // get from datashare
87     SwitchStatus status = SettingsDataManager::GetSwitchStatus();
88     if (status == AI_FAMILY) {
89         return HDC_BUNDLE_NAME;
90     }
91     return GALLERY_BUNDLE_NAME;
92 }
93 
InitActiveBundle()94 bool SettingDataHelper::InitActiveBundle()
95 {
96     lock_guard<mutex> lck(initMtx_);
97     if (isBundleInited_) {
98         return true;
99     }
100     if (!IsDataShareReady()) {
101         return false;
102     }
103     UpdateActiveBundle();
104     RegisterObserver();
105     isBundleInited_ = true;
106     return true;
107 }
108 
UpdateActiveBundle()109 void SettingDataHelper::UpdateActiveBundle()
110 {
111     // get latest data
112     string bundle = GetActiveBundle();
113     // update FuseData
114     SetActiveBundle(bundle);
115 }
116 
RegisterObserver()117 void SettingDataHelper::RegisterObserver()
118 {
119     sptr<SyncSwitchObserver> observer(new (std::nothrow) SyncSwitchObserver());
120     SettingsDataManager::RegisterObserver(SYNC_SWITCH_KEY, observer);
121 }
122 
OnChange()123 void SyncSwitchObserver::OnChange()
124 {
125     SettingDataHelper::GetInstance().UpdateActiveBundle();
126 }
127 } // namespace CloudFile
128 } // namespace FileManagement
129 } // namespace OHOS