• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 IME_ENABLED_INFO_MANAGER_H
17 #define IME_ENABLED_INFO_MANAGER_H
18 #include "event_handler.h"
19 #include "input_method_property.h"
20 #include "input_method_status.h"
21 #include "input_method_utils.h"
22 #include "serializable.h"
23 #include "settings_data_utils.h"
24 
25 namespace OHOS {
26 namespace MiscServices {
27 struct ImeExtendInfo {
28     std::unordered_map<std::string, PrivateDataValue> privateCommand;
29 };
30 
31 struct ImeNativeCfg {
32     std::string imeId;
33     std::string bundleName;
34     std::string subName;
35     std::string extName;
36     ImeExtendInfo imeExtendInfo;
37 };
38 struct ExtraInfo : public Serializable {
39     bool isDefaultIme{ false };
40     bool isDefaultImeSet{ false };
41     bool isTmpIme{ false };
42     std::string currentSubName;
UnmarshalExtraInfo43     bool Unmarshal(cJSON *node) override
44     {
45         auto ret = GetValue(node, GET_NAME(isDefaultIme), isDefaultIme);
46         ret = GetValue(node, GET_NAME(isDefaultImeSet), isDefaultImeSet) && ret;
47         ret = GetValue(node, GET_NAME(isTmpIme), isTmpIme) && ret;
48         return GetValue(node, GET_NAME(currentSubName), currentSubName) && ret;
49     }
MarshalExtraInfo50     bool Marshal(cJSON *node) const override
51     {
52         auto ret = SetValue(node, GET_NAME(isDefaultIme), isDefaultIme);
53         ret = SetValue(node, GET_NAME(isDefaultImeSet), isDefaultImeSet) && ret;
54         ret = SetValue(node, GET_NAME(isTmpIme), isTmpIme) && ret;
55         return SetValue(node, GET_NAME(currentSubName), currentSubName) && ret;
56     }
57 
58     bool operator==(const ExtraInfo &extraInfo) const // for tdd
59     {
60         return isDefaultIme == extraInfo.isDefaultIme && isDefaultImeSet == extraInfo.isDefaultImeSet &&
61                isTmpIme == extraInfo.isTmpIme && currentSubName == extraInfo.currentSubName;
62     }
63 };
64 
65 struct ImeEnabledInfo : public Serializable {
66     ImeEnabledInfo() = default;
ImeEnabledInfoImeEnabledInfo67     ImeEnabledInfo(const std::string &bundleName, const std::string &extensionName, EnabledStatus enabledStatus)
68         : bundleName(bundleName), extensionName(extensionName), enabledStatus(enabledStatus){};
69     std::string bundleName;
70     std::string extensionName;
71     EnabledStatus enabledStatus{ EnabledStatus::DISABLED };
72     std::string stateUpdateTime; // user trigger
73     ExtraInfo extraInfo;
UnmarshalImeEnabledInfo74     bool Unmarshal(cJSON *node) override
75     {
76         auto ret = GetValue(node, GET_NAME(bundleName), bundleName);
77         ret = GetValue(node, GET_NAME(extensionName), extensionName) && ret;
78         int32_t enabledStatusTmp = 0;
79         ret = GetValue(node, GET_NAME(enabledStatus), enabledStatusTmp) && ret;
80         enabledStatus = static_cast<EnabledStatus>(enabledStatusTmp);
81         ret = GetValue(node, GET_NAME(stateUpdateTime), stateUpdateTime) && ret;
82         return GetValue(node, GET_NAME(extraInfo), extraInfo) && ret;
83     }
MarshalImeEnabledInfo84     bool Marshal(cJSON *node) const override
85     {
86         auto ret = SetValue(node, GET_NAME(bundleName), bundleName);
87         ret = SetValue(node, GET_NAME(extensionName), extensionName) && ret;
88         auto enabledStatusTmp = static_cast<int32_t>(enabledStatus);
89         ret = SetValue(node, GET_NAME(enabledStatus), enabledStatusTmp) && ret;
90         ret = SetValue(node, GET_NAME(stateUpdateTime), stateUpdateTime) && ret;
91         return SetValue(node, GET_NAME(extraInfo), extraInfo) && ret;
92     }
93     bool operator==(const ImeEnabledInfo &enabledInfo) const // for tdd
94     {
95         return bundleName == enabledInfo.bundleName && extensionName == enabledInfo.extensionName &&
96                enabledStatus == enabledInfo.enabledStatus && extraInfo == enabledInfo.extraInfo;
97     }
98 };
99 struct ImeEnabledCfg : public Serializable {
100     std::string version{ "empty" };
101     std::vector<ImeEnabledInfo> enabledInfos;
UnmarshalImeEnabledCfg102     bool Unmarshal(cJSON *node) override
103     {
104         auto ret = GetValue(node, GET_NAME(version), version);
105         return GetValue(node, GET_NAME(inputmethods), enabledInfos) && ret;
106     }
MarshalImeEnabledCfg107     bool Marshal(cJSON *node) const override
108     {
109         auto ret = SetValue(node, GET_NAME(version), version);
110         return SetValue(node, GET_NAME(inputmethods), enabledInfos) && ret;
111     }
112     bool operator==(const ImeEnabledCfg &enabledCfg) const
113     {
114         return version == enabledCfg.version && enabledInfos == enabledCfg.enabledInfos;
115     }
116 };
117 using CurrentImeStatusChangedHandler =
118     std::function<void(int32_t userId, const std::string &bundleName, EnabledStatus oldStatus)>;
119 class ImeEnabledInfoManager {
120 public:
121     static ImeEnabledInfoManager &GetInstance();
122     void SetCurrentImeStatusChangedHandler(CurrentImeStatusChangedHandler handler);
123     void SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &eventHandler);
124     int32_t Init(const std::map<int32_t, std::vector<FullImeInfo>> &fullImeInfos);
125     int32_t Switch(int32_t userId, const std::vector<FullImeInfo> &imeInfos);
126     int32_t Delete(int32_t userId);
127     int32_t Add(int32_t userId, const FullImeInfo &imeInfo);
128     int32_t Delete(int32_t userId, const std::string &bundleName);
129     int32_t Update(
130         int32_t userId, const std::string &bundleName, const std::string &extensionName, EnabledStatus status);
131     int32_t GetEnabledState(int32_t userId, const std::string &bundleName, EnabledStatus &status);
132     int32_t GetEnabledStates(int32_t userId, std::vector<Property> &props); // props not has sysSpecialIme
133     bool IsDefaultFullMode(int32_t userId, const std::string &bundleName);
134     int32_t SetCurrentIme(int32_t userId, const std::string &imeId, const std::string &subName, bool isSetByUser);
135     int32_t SetTmpIme(int32_t userId, const std::string &imeId);
136     std::shared_ptr<ImeNativeCfg> GetCurrentImeCfg(int32_t userId); // Return value is never nullptr.
137     bool IsDefaultImeSet(int32_t userId);
138     /* add for compatibility that sys ime mod full experience table in it's full experience switch changed */
139     void OnFullExperienceTableChanged(int32_t userId);
140 
141 private:
142     ImeEnabledInfoManager() = default;
143     ~ImeEnabledInfoManager();
144     int32_t UpdateEnabledCfgCache(int32_t userId, const std::vector<FullImeInfo> &imeInfos = {});
145     int32_t UpdateEnabledCfgCache(int32_t userId, const ImeEnabledCfg &cfg);
146     int32_t GetEnabledCfg(int32_t userId, const std::vector<FullImeInfo> &imeInfos, ImeEnabledCfg &cfg);
147     int32_t CorrectByBundleMgr(
148         int32_t userId, const std::vector<FullImeInfo> &imeInfos, std::vector<ImeEnabledInfo> &enabledInfos);
149     EnabledStatus ComputeEnabledStatus(const std::string &bundleName, EnabledStatus initStatus);
150     int32_t GetEnabledStateInner(int32_t userId, const std::string &bundleName, EnabledStatus &status);
151     int32_t GetEnabledStatesInner(int32_t userId, std::vector<Property> &props);
152     void SetEnabledCache(int32_t userId, const ImeEnabledCfg &cfg);
153     ImeEnabledCfg GetEnabledCache(int32_t userId);
154     void ClearEnabledCache(int32_t userId);
155     bool IsInEnabledCache(int32_t userId, const std::string &bundleName, const std::string &extensionName);
156     int32_t GetEnabledCacheWithCorrect(int32_t userId, ImeEnabledCfg &enabledCfg);
157     int32_t GetEnabledCacheWithCorrect(
158         int32_t userId, const std::string &bundleName, const std::string &extensionName, ImeEnabledCfg &enabledCfg);
159     int32_t CheckUpdate(
160         int32_t userId, const std::string &bundleName, const std::string &extensionName, EnabledStatus status);
161     void NotifyCurrentImeStatusChanged(int32_t userId, const std::string &bundleName, EnabledStatus newStatus);
162     bool HasEnabledSwitch();
163     bool IsExpired(const std::string &expirationTime);
164     std::pair<std::string, std::string> SplitImeId(const std::string &imeId);
165     void ModCurrentIme(std::vector<ImeEnabledInfo> &enabledInfos);
166     bool IsCurrentIme(const std::string &bundleName, const std::vector<ImeEnabledInfo> &enabledInfos);
167     /* add for compatibility that sys ime listen global table change for smart menu in tablet */
168     void UpdateGlobalEnabledTable(int32_t userId, const ImeEnabledCfg &newEnabledCfg);
169     std::mutex imeEnabledCfgLock_;
170     std::map<int32_t, ImeEnabledCfg> imeEnabledCfg_;
171     CurrentImeStatusChangedHandler currentImeStatusChangedHandler_;
172     std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_{ nullptr };
173     std::mutex operateLock_;
174 };
175 } // namespace MiscServices
176 } // namespace OHOS
177 
178 #endif // IME_ENABLED_INFO_MANAGER_H