1 /* 2 * Copyright (c) 2023 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 SETTINGS_DATA_UTILS_H 17 #define SETTINGS_DATA_UTILS_H 18 19 #include <mutex> 20 #include <string> 21 #include <unordered_map> 22 #include <vector> 23 24 #include "datashare_helper.h" 25 #include "global.h" 26 #include "input_method_property.h" 27 #include "serializable.h" 28 #include "settings_data_observer.h" 29 #include "uri.h" 30 31 namespace OHOS { 32 namespace MiscServices { 33 constexpr const char *SETTING_COLUMN_KEYWORD = "KEYWORD"; 34 constexpr const char *SETTING_COLUMN_VALUE = "VALUE"; 35 const std::string SETTING_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/" 36 "SETTINGSDATA?Proxy=true"; 37 const std::string SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility"; 38 const std::string SETTINGS_USER_DATA_URI = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_"; 39 struct UserImeConfig : public Serializable { 40 std::string userId; 41 std::vector<std::string> identities; UnmarshalUserImeConfig42 bool Unmarshal(cJSON *node) override 43 { 44 GetValue(node, userId, identities); 45 return true; 46 } 47 }; 48 49 class SettingsDataUtils : public RefBase { 50 public: 51 static sptr<SettingsDataUtils> GetInstance(); 52 std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(const std::string &uriProxy); 53 int32_t CreateAndRegisterObserver(const std::string &key, SettingsDataObserver::CallbackFunc func); 54 int32_t GetStringValue(const std::string &uriProxy, const std::string &key, std::string &value); 55 bool SetStringValue(const std::string &uriProxy, const std::string &key, const std::string &value); 56 57 private: 58 SettingsDataUtils() = default; 59 ~SettingsDataUtils(); 60 bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> &helper); 61 int32_t RegisterObserver(const sptr<SettingsDataObserver> &observer); 62 int32_t UnregisterObserver(const sptr<SettingsDataObserver> &observer); 63 Uri GenerateTargetUri(const std::string &uriProxy, const std::string &key); 64 sptr<IRemoteObject> GetToken(); 65 66 private: 67 static std::mutex instanceMutex_; 68 static sptr<SettingsDataUtils> instance_; 69 std::mutex tokenMutex_; 70 sptr<IRemoteObject> remoteObj_ = nullptr; 71 std::mutex observerListMutex_; 72 std::vector<sptr<SettingsDataObserver>> observerList_; 73 }; 74 } // namespace MiscServices 75 } // namespace OHOS 76 77 #endif // SETTINGS_DATA_UTILS_H 78