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     bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> &helper);
57     Uri GenerateTargetUri(const std::string &uriProxy, const std::string &key);
58     bool EnableIme(int32_t userId, const std::string &bundleName);
59 
60 private:
61     SettingsDataUtils() = default;
62     ~SettingsDataUtils();
63     int32_t RegisterObserver(const sptr<SettingsDataObserver> &observer);
64     int32_t UnregisterObserver(const sptr<SettingsDataObserver> &observer);
65     sptr<IRemoteObject> GetToken();
66     std::vector<std::string> split(const std::string &text, char separator);
67     std::string SetSettingValues(const std::string &settingValue, const std::string &bundleName);
68 
69 private:
70     static std::mutex instanceMutex_;
71     static sptr<SettingsDataUtils> instance_;
72     std::mutex tokenMutex_;
73     sptr<IRemoteObject> remoteObj_ = nullptr;
74     std::mutex observerListMutex_;
75     std::vector<sptr<SettingsDataObserver>> observerList_;
76 };
77 } // namespace MiscServices
78 } // namespace OHOS
79 
80 #endif // SETTINGS_DATA_UTILS_H
81