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 #ifndef DMS_DATASHARE_MANAGER_H
17 #define DMS_DATASHARE_MANAGER_H
18 
19 #include <map>
20 
21 #include "datashare_helper.h"
22 #include "data_ability_observer_stub.h"
23 #include "single_instance.h"
24 
25 namespace OHOS {
26 namespace DistributedSchedule {
27 
28 class SettingObserver : public AAFwk::DataAbilityObserverStub {
29 public:
30     SettingObserver();
31     ~SettingObserver() override;
32     void OnChange() override;
33 
34     using ObserverCallback = std::function<void()>;
35     void SetObserverCallback(ObserverCallback &observerCallback);
36 private:
37     ObserverCallback observerCallback_  = nullptr;
38 };
39 
40 class DataShareManager {
41     DECLARE_SINGLE_INSTANCE(DataShareManager);
42 public:
43     void RegisterObserver(const std::string &key, SettingObserver::ObserverCallback &observerCallback);
44     void UnregisterObserver(const std::string &key);
45 
46     using ObserverCallback = std::function<void()>;
47 
48     int32_t GetLocalAccountId();
49     Uri AssembleUserSecureUri(int userId, const std::string& key);
50     void UpdateSwitchStatus(const std::string &key, const std::string &value);
51     std::atomic<bool> isCurrentContinueSwitchOn_ = true;
52     bool IsCurrentContinueSwitchOn();
53     void SetCurrentContinueSwitch(bool status);
54 
55 private:
56     std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper();
57     sptr<SettingObserver> GetSettingObserver(const std::string &key);
58 
59 private:
60     std::map<std::string, sptr<SettingObserver>> settingObserverMap_;
61     std::mutex observerMapMutex_;
62     std::mutex datashareMutex_;
63 };
64 } // namespace DistributedSchedule
65 } // namespace OHOS
66 
67 #endif // DMS_DATASHARE_MANAGER_H