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 SERVICES_INCLUDE_IME_INFO_ENQUIRER_H
17 #define SERVICES_INCLUDE_IME_INFO_ENQUIRER_H
18 
19 #include <application_info.h>
20 
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <vector>
25 #include "bundle_mgr_proxy.h"
26 #include "element_name.h"
27 #include "enable_ime_data_parser.h"
28 #include "ime_cfg_manager.h"
29 #include "input_method_info.h"
30 #include "input_method_property.h"
31 #include "input_method_status.h"
32 #include "refbase.h"
33 #include "resource_manager.h"
34 #include "sys_cfg_parser.h"
35 namespace OHOS {
36 namespace MiscServices {
37 enum class Condition {
38     UPPER = 0,
39     LOWER,
40     ENGLISH,
41     CHINESE,
42 };
43 
44 enum class ImeTargetString {
45     LABEL = 0,
46     DESCRIPTION,
47 };
48 
49 struct Subtype : public Serializable {
50     std::string label;
51     std::string id;
52     std::string icon;
53     std::string mode;
54     std::string locale;
UnmarshalSubtype55     bool Unmarshal(cJSON *node) override
56     {
57         GetValue(node, GET_NAME(label), label);
58         auto ret = GetValue(node, GET_NAME(id), id);
59         GetValue(node, GET_NAME(icon), icon);
60         GetValue(node, GET_NAME(mode), mode);
61         GetValue(node, GET_NAME(locale), locale);
62         return ret;
63     }
64 };
65 struct SubtypeCfg : public Serializable {
66     static constexpr uint32_t MAX_SUBTYPE_NUM = 256;
67     std::vector<Subtype> subtypes;
UnmarshalSubtypeCfg68     bool Unmarshal(cJSON *node) override
69     {
70         return GetValue(node, GET_NAME(subtypes), subtypes, MAX_SUBTYPE_NUM);
71     }
72 };
73 
74 class ImeInfoInquirer {
75 public:
76     using CompareHandler = std::function<bool(const SubProperty &)>;
77     static ImeInfoInquirer &GetInstance();
78     std::string GetDumpInfo(int32_t userId);
79     std::shared_ptr<ImeNativeCfg> GetImeToStart(int32_t userId);
80     std::shared_ptr<Property> GetImeProperty(
81         int32_t userId, const std::string &bundleName, const std::string &extName = "");
82     std::shared_ptr<Property> GetCurrentInputMethod(int32_t userId);
83     std::shared_ptr<SubProperty> GetCurrentSubtype(int32_t userId);
84     std::shared_ptr<ImeInfo> GetImeInfo(int32_t userId, const std::string &bundleName, const std::string &subName);
85     std::shared_ptr<ImeInfo> GetDefaultImeInfo(int32_t userId);
86     std::shared_ptr<Property> GetDefaultImeCfgProp();
87     std::shared_ptr<ImeNativeCfg> GetDefaultImeCfg();
88     std::shared_ptr<SubProperty> FindTargetSubtypeByCondition(const std::vector<SubProperty> &subProps,
89         const Condition &condition);
90     bool GetImeAppId(int32_t userId, const std::string &bundleName, std::string &appId);
91     bool GetImeVersionCode(int32_t userId, const std::string &bundleName, uint32_t &versionCode);
92     int32_t GetDefaultInputMethod(const int32_t userId, std::shared_ptr<Property> &prop, bool isBrief = false);
93     int32_t GetInputMethodConfig(const int32_t userId, AppExecFwk::ElementName &inputMethodConfig);
94     int32_t ListInputMethod(int32_t userId, InputMethodStatus status, std::vector<Property> &props, bool enableOn);
95     int32_t ListInputMethodSubtype(int32_t userId, const std::string &bundleName, std::vector<SubProperty> &subProps);
96     int32_t ListCurrentInputMethodSubtype(int32_t userId, std::vector<SubProperty> &subProps);
97     int32_t GetSwitchInfoBySwitchCount(SwitchInfo &switchInfo, int32_t userId, bool enableOn, uint32_t cacheCount);
98     bool IsEnableInputMethod();
99     bool IsEnableSecurityMode();
100     void InitSystemConfig();
101     ImeNativeCfg GetDefaultIme();
102     int32_t QueryFullImeInfo(std::vector<std::pair<int32_t, std::vector<FullImeInfo>>> &imeInfos);
103     int32_t QueryFullImeInfo(int32_t userId, std::vector<FullImeInfo> &imeInfos);
104     int32_t GetFullImeInfo(int32_t userId, const std::string &bundleName, FullImeInfo &imeInfo);
105     bool IsInputMethod(int32_t userId, const std::string &bundleName);
106     bool IsRunningIme(int32_t userId, const std::string &bundleName);
107     std::vector<std::string> GetRunningIme(int32_t userId);
108     bool IsDefaultImeSet(int32_t userId);
109 
110 private:
111     ImeInfoInquirer() = default;
112     ~ImeInfoInquirer() = default;
113     OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr();
114     SubProperty GetExtends(const std::vector<OHOS::AppExecFwk::Metadata> &metaData);
115     std::string GetTargetString(
116         const AppExecFwk::ExtensionAbilityInfo &extension, ImeTargetString target, int32_t userId);
117     std::string GetStringById(const std::string &bundleName, const std::string &moduleName, const uint32_t labelId,
118         const int32_t userId);
119     bool GetBundleInfoByBundleName(int32_t userId, const std::string &bundleName, AppExecFwk::BundleInfo &bundleInfo);
120     std::shared_ptr<ImeInfo> GetImeInfoFromCache(const int32_t userId, const std::string &bundleName,
121         const std::string &subName);
122     std::shared_ptr<ImeInfo> GetImeInfoFromBundleMgr(
123         const int32_t userId, const std::string &bundleName, const std::string &subName);
124     int32_t GetExtInfosByBundleName(const int32_t userId, const std::string &bundleName,
125         std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &extInfos);
126     bool IsNewExtInfos(const std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &extInfos);
127     bool IsImeInstalled(const int32_t userId, const std::string &bundleName, const std::string &extName);
128     std::vector<InputMethodInfo> ListInputMethodInfo(const int32_t userId);
129     int32_t ListInputMethod(const int32_t userId, std::vector<Property> &props);
130     int32_t ListEnabledInputMethod(const int32_t userId, std::vector<Property> &props, bool enableOn);
131     int32_t ListDisabledInputMethod(const int32_t userId, std::vector<Property> &props, bool enableOn);
132     int32_t ListInputMethodSubtype(const int32_t userId,
133         const std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &extInfos, std::vector<SubProperty> &subProps);
134     int32_t ListInputMethodSubtype(const int32_t userId, const OHOS::AppExecFwk::ExtensionAbilityInfo &extInfo,
135         std::vector<SubProperty> &subProps);
136     int32_t GetSubProperty(int32_t userId, const std::string &subName,
137         const std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &extInfos, SubProperty &subProp);
138     int32_t GetSubProperty(int32_t userId, const std::string &subName,
139         const OHOS::AppExecFwk::ExtensionAbilityInfo &extInfo, SubProperty &subProp);
140     int32_t ParseSubtype(const OHOS::AppExecFwk::ExtensionAbilityInfo &extInfo, std::vector<Subtype> &subtypes);
141     bool ParseSubtypeProfile(const std::vector<std::string> &profiles, SubtypeCfg &subtypeCfg);
142     void CovertToLanguage(const std::string &locale, std::string &language);
143     bool QueryImeExtInfos(const int32_t userId, std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &infos);
144     std::shared_ptr<Global::Resource::ResourceManager> GetResMgr(const std::string &resourcePath);
145     int32_t GetFullImeInfo(
146         int32_t userId, const std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &extInfos, FullImeInfo &imeInfo);
147 
148     SystemConfig systemConfig_;
149     bool IsTempInputMethod(const OHOS::AppExecFwk::ExtensionAbilityInfo &extInfo);
150 };
151 } // namespace MiscServices
152 } // namespace OHOS
153 #endif // SERVICES_INCLUDE_IME_INFO_ENQUIRER_H
154