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