• 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 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 
26 #include "bundle_mgr_proxy.h"
27 #include "element_name.h"
28 #include "enable_ime_data_parser.h"
29 #include "ime_cfg_manager.h"
30 #include "input_method_info.h"
31 #include "input_method_property.h"
32 #include "input_method_status.h"
33 #include "nlohmann/json.hpp"
34 #include "refbase.h"
35 namespace OHOS {
36 namespace MiscServices {
37 
38 struct ImeInfo {
39     std::string moduleName;
40     Property prop;
41     SubProperty subProp;
42     std::vector<SubProperty> subProps;
43     bool isNewIme{ false };
44 };
45 
46 enum class Condition {
47     UPPER = 0,
48     LOWER,
49     ENGLISH,
50     CHINESE,
51 };
52 
53 struct ImeConfig {
54     std::string systemInputMethodConfigAbility;
55     std::string defaultInputMethod;
56     bool enableInputMethodFeature = false;
57     bool enableFullExperienceFeature = false;
58 };
59 
60 class ImeInfoInquirer {
61 public:
62     using CompareHandler = std::function<bool(const SubProperty &)>;
63     static ImeInfoInquirer &GetInstance();
64     std::string GetDumpInfo(int32_t userId);
65     std::shared_ptr<ImeNativeCfg> GetImeToStart(int32_t userId);
66     std::shared_ptr<Property> GetImeByBundleName(int32_t userId, const std::string &bundleName);
67     std::shared_ptr<Property> GetCurrentInputMethod(int32_t userId);
68     std::shared_ptr<SubProperty> GetCurrentSubtype(int32_t userId);
69     std::shared_ptr<ImeInfo> GetImeInfo(int32_t userId, const std::string &bundleName, const std::string &subName);
70     std::shared_ptr<ImeInfo> GetDefaultImeInfo(int32_t userId);
71     std::shared_ptr<ImeInfo> GetCurrentImeInfo();
72     std::shared_ptr<Property> GetDefaultImeCfgProp();
73     void SetCurrentImeInfo(std::shared_ptr<ImeInfo> info);
74     void RefreshCurrentImeInfo(int32_t userId);
75     std::shared_ptr<SubProperty> FindTargetSubtypeByCondition(
76     const std::vector<SubProperty> &subProps, const Condition &condition);
77     int32_t GetDefaultInputMethod(const int32_t userId, std::shared_ptr<Property> &prop);
78     int32_t GetInputMethodConfig(const int32_t userId, AppExecFwk::ElementName &inputMethodConfig);
79     int32_t ListInputMethod(int32_t userId, InputMethodStatus status, std::vector<Property> &props, bool enableOn);
80     int32_t ListInputMethodSubtype(int32_t userId, const std::string &bundleName, std::vector<SubProperty> &subProps);
81     int32_t ListCurrentInputMethodSubtype(int32_t userId, std::vector<SubProperty> &subProps);
82     int32_t GetNextSwitchInfo(SwitchInfo &switchInfo, int32_t userId, bool enableOn);
83     bool IsEnableInputMethod();
84     bool IsEnableSecurityMode();
85     void InitConfig();
86 
87 private:
88     ImeInfoInquirer() = default;
89     ~ImeInfoInquirer() = default;
90     OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr();
91     void InitCache(int32_t userId);
92     SubProperty GetExtends(const std::vector<OHOS::AppExecFwk::Metadata> &metaData);
93     ImeNativeCfg GetDefaultIme();
94     std::string GetStringById(
95         const std::string &bundleName, const std::string &moduleName, const int32_t labelId, const int32_t userId);
96     std::shared_ptr<ImeInfo> GetImeInfoFromCache(
97         const int32_t userId, const std::string &bundleName, const std::string &subName);
98     std::shared_ptr<ImeInfo> GetImeInfoFromBundleMgr(
99         const int32_t userId, const std::string &bundleName, const std::string &subName);
100     int32_t GetExtInfosByBundleName(const int32_t userId, const std::string &bundleName,
101         std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &extInfos);
102     bool IsNewExtInfos(const std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &extInfos);
103     bool IsImeInstalled(const int32_t userId, const std::string &bundleName, const std::string &extName);
104     std::vector<InputMethodInfo> ListInputMethodInfo(const int32_t userId);
105     int32_t ListInputMethod(const int32_t userId, std::vector<Property> &props);
106     int32_t ListEnabledInputMethod(const int32_t userId, std::vector<Property> &props, bool enableOn);
107     int32_t ListDisabledInputMethod(const int32_t userId, std::vector<Property> &props, bool enableOn);
108     int32_t ListInputMethodSubtype(const int32_t userId,
109         const std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &extInfos, std::vector<SubProperty> &subProps);
110     int32_t ListInputMethodSubtype(const int32_t userId, const OHOS::AppExecFwk::ExtensionAbilityInfo &extInfo,
111         std::vector<SubProperty> &subProps);
112     bool ParseSubProp(const std::vector<std::string> &profiles, std::vector<SubProperty> &subProps);
113     bool ParseSubProp(const nlohmann::json &jsonSubProps, std::vector<SubProperty> &subProps);
114     void ParseSubProp(const nlohmann::json &jsonSubProp, SubProperty &subProp);
115     void ParseLanguage(const std::string &locale, std::string &language);
116     bool QueryImeExtInfos(const int32_t userId, std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &infos);
117 
118     ImeConfig imeConfig_;
119     std::mutex currentImeInfoLock_;
120     std::shared_ptr<ImeInfo> currentImeInfo_{ nullptr }; // current imeInfo of current user
121 };
122 } // namespace MiscServices
123 } // namespace OHOS
124 #endif // SERVICES_INCLUDE_IME_INFO_ENQUIRER_H
125