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