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 UI_APPEARANCE_UTILS_SETTING_DATA_MANAGER_H 17 #define UI_APPEARANCE_UTILS_SETTING_DATA_MANAGER_H 18 19 #include "datashare_helper.h" 20 #include "errors.h" 21 #include "nocopyable.h" 22 #include "setting_data_observer.h" 23 24 namespace OHOS::ArkUi::UiAppearance { 25 class SettingDataManager final : public NoCopyable { 26 public: 27 static SettingDataManager &GetInstance(); 28 29 ErrCode Initialize(); 30 31 bool IsInitialized() const; 32 33 ErrCode RegisterObserver(const std::string& key, const SettingDataObserver::UpdateFunc& updateFunc, 34 int32_t userId = INVALID_USER_ID); 35 36 ErrCode UnregisterObserver(const std::string& key, int32_t userId = INVALID_USER_ID); 37 38 ErrCode GetStringValue(const std::string& key, std::string& value, int32_t userId = INVALID_USER_ID) const; 39 40 ErrCode GetInt32Value(const std::string& key, int32_t& value, int32_t userId = INVALID_USER_ID) const; 41 42 ErrCode GetInt32ValueStrictly(const std::string& key, int32_t& value, int32_t userId = INVALID_USER_ID) const; 43 44 ErrCode GetBoolValue(const std::string& key, bool& value, int32_t userId = INVALID_USER_ID) const; 45 46 ErrCode SetStringValue(const std::string& key, const std::string& value, int32_t userId = INVALID_USER_ID, 47 bool needNotify = true) const; 48 49 ErrCode SetInt32Value(const std::string& key, int32_t value, int32_t userId = INVALID_USER_ID, 50 bool needNotify = true) const; 51 52 ErrCode SetBoolValue(const std::string& key, bool value, int32_t userId = INVALID_USER_ID, 53 bool needNotify = true) const; 54 55 bool IsValidKey(const std::string& key, int32_t userId = INVALID_USER_ID) const; 56 57 private: 58 std::mutex initializeMutex_; 59 bool isInitialized_ = false; 60 sptr<IRemoteObject> remoteObject_; 61 62 std::mutex observersMutex_; 63 std::map<std::string, sptr<SettingDataObserver>> observers_; 64 65 ErrCode RegisterObserverInner(const sptr<SettingDataObserver>& observer) const; 66 67 ErrCode UnregisterObserverInner(const sptr<SettingDataObserver>& observer) const; 68 69 void CreateDataShareHelperAndUri(int32_t userId, const std::string& key, 70 std::string& uri, std::shared_ptr<DataShare::DataShareHelper>& helper) const; 71 72 std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper() const; 73 74 std::shared_ptr<DataShare::DataShareHelper> CreateUserDbDataShareHelper(int32_t userId) const; 75 76 static bool ReleaseDataShareHelper(const std::shared_ptr<DataShare::DataShareHelper>& helper); 77 78 static inline std::string GenerateObserverName(const std::string& key, int32_t userId); 79 80 static sptr<SettingDataObserver> CreateObserver(const std::string& key, 81 const SettingDataObserver::UpdateFunc& updateFunc, int32_t userId = INVALID_USER_ID); 82 83 static std::string AssembleUri(const std::string& key); 84 85 static std::string AssembleUserDbUri(int32_t userId, const std::string& key); 86 }; 87 } // namespace OHOS::ArkUi::UiAppearance 88 89 #endif // UI_APPEARANCE_UTILS_SETTING_DATA_MANAGER_H 90