• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ENABLE_IME_DATA_PARSER_H
17 #define ENABLE_IME_DATA_PARSER_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 "settings_data_utils.h"
28 
29 namespace OHOS {
30 namespace MiscServices {
31 struct SwitchInfo {
32     std::chrono::system_clock::time_point timestamp{};
33     std::string bundleName;
34     std::string subName;
35     bool operator==(const SwitchInfo &info) const
36     {
37         return (timestamp == info.timestamp && bundleName == info.bundleName && subName == info.subName);
38     }
39 };
40 
41 class EnableImeDataParser : public RefBase {
42 public:
43     static sptr<EnableImeDataParser> GetInstance();
44     int32_t Initialize(const int32_t userId);
45     int32_t GetEnableData(const std::string &key, std::vector<std::string> &enableVec, const int32_t userId);
46     // for enable list changed
47     bool CheckNeedSwitch(const std::string &key, SwitchInfo &switchInfo, const int32_t userId);
48     // for switch target ime
49     bool CheckNeedSwitch(const SwitchInfo &info, const int32_t userId);
50     void OnUserChanged(const int32_t userId);
51 
52     static constexpr const char *ENABLE_IME = "settings.inputmethod.enable_ime";
53     static constexpr const char *ENABLE_KEYBOARD = "settings.inputmethod.enable_keyboard";
54 
55 private:
56     EnableImeDataParser() = default;
57     ~EnableImeDataParser();
58 
59     bool ParseJsonData(const std::string &key, const std::string &valueStr, std::vector<std::string> &enableVec,
60         const int32_t userId);
61     const std::string GetJsonListName(const std::string &key);
62     bool CheckTargetEnableName(
63         const std::string &key, const std::string &targetName, std::string &nextIme, const int32_t userId);
64     std::shared_ptr<Property> GetDefaultIme();
65 
66 private:
67     static std::mutex instanceMutex_;
68     static sptr<EnableImeDataParser> instance_;
69     std::mutex listMutex_;
70     std::unordered_map<std::string, std::vector<std::string>> enableList_;
71     std::mutex defaultImeMutex_;
72     std::shared_ptr<Property> defaultImeInfo_{ nullptr };
73     int32_t currrentUserId_ = 0;
74 };
75 } // namespace MiscServices
76 } // namespace OHOS
77 
78 #endif // ENABLE_IME_DATA_PARSER_H
79