• 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 "input_method_info.h"
28 #include "input_method_property.h"
29 #include "input_method_status.h"
30 #include "nlohmann/json.hpp"
31 #include "refbase.h"
32 namespace OHOS {
33 namespace MiscServices {
34 struct ImeInfo {
35     Property prop;
36     SubProperty subProp;
37     std::vector<SubProperty> subProps;
38     bool isNewIme{ false };
39 };
40 
41 enum class Condition {
42     UPPER = 0,
43     LOWER,
44     ENGLISH,
45     CHINESE,
46 };
47 
48 class ImeInfoInquirer {
49 public:
50     using CompareHandler = std::function<bool(const SubProperty &)>;
51     static ImeInfoInquirer &GetInstance();
52     std::shared_ptr<SubProperty> GetImeSubProp(const std::vector<SubProperty> &subProps, const Condition &condition);
53     std::string GetInputMethodParam(const int32_t userId);
54     std::shared_ptr<Property> GetCurrentInputMethod(const int32_t userId);
55     std::shared_ptr<SubProperty> GetCurrentInputMethodSubtype(const int32_t userId);
56     std::string GetStartedIme(const int32_t userId);
57     std::shared_ptr<ImeInfo> GetDefaultImeInfo(const int32_t userId);
58     int32_t GetImeInfo(
59         const int32_t userId, const std::string &bundleName, const std::string &subName, ImeInfo &info);
60     void SetCurrentImeInfo(const ImeInfo &info);
61     void SetCurrentImeInfo(const int32_t userId);
62     void ResetCurrentImeInfo();
63     int32_t ListInputMethod(const int32_t userId, const InputMethodStatus status, std::vector<Property> &props);
64     int32_t ListInputMethodSubtype(
65         const int32_t userId, const std::string &bundleName, std::vector<SubProperty> &subProps);
66     int32_t ListCurrentInputMethodSubtype(const int32_t userId, std::vector<SubProperty> &subProps);
67 
68 private:
69     ImeInfoInquirer() = default;
70     ~ImeInfoInquirer() = default;
71     OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr();
72     SubProperty GetExtends(const std::vector<OHOS::AppExecFwk::Metadata> &metaData);
73     std::string GetDefaultIme();
74     std::string GetStringById(
75         const std::string &bundleName, const std::string &moduleName, const int32_t labelId, const int32_t userId);
76     int32_t GetImeInfoFromNative(const int32_t userId, const std::string &subName, ImeInfo &info);
77     int32_t GetImeInfoFromBundleMgr(
78         const int32_t userId, const std::string &bundleName, const std::string &subName, ImeInfo &info);
79     int32_t GetExtInfosByBundleName(const int32_t userId, const std::string &bundleName,
80         std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &extInfos);
81     bool IsNewExtInfos(const std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &extInfos);
82     bool IsImeInstalled(const int32_t userId, const std::string &bundleName, const std::string &extName);
83     std::vector<InputMethodInfo> ListInputMethodInfo(const int32_t userId);
84     int32_t ListInputMethod(const int32_t userId, std::vector<Property> &props);
85     int32_t ListEnabledInputMethod(const int32_t userId, std::vector<Property> &props);
86     int32_t ListDisabledInputMethod(const int32_t userId, std::vector<Property> &props);
87     int32_t ListInputMethodSubtype(const int32_t userId,
88         const std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &extInfos, std::vector<SubProperty> &subProps);
89     int32_t ListInputMethodSubtype(const int32_t userId, const OHOS::AppExecFwk::ExtensionAbilityInfo &extInfo,
90         std::vector<SubProperty> &subProps);
91     bool ParseSubProp(const std::vector<std::string> &profiles, std::vector<SubProperty> &subProps);
92     void ParseSubProp(const nlohmann::json &jsonSubProps, std::vector<SubProperty> &subProps);
93     void ParseSubProp(const nlohmann::json &jsonSubProp, SubProperty &subProp);
94     void ParseLanguage(const std::string &locale, std::string &language);
95     bool QueryImeExtInfos(const int32_t userId, std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> &infos);
96 
97     std::recursive_mutex currentImeInfoLock_;
98     std::shared_ptr<ImeInfo> currentImeInfo_{ nullptr };
99     std::mutex defaultImeInfoLock_;
100     std::shared_ptr<ImeInfo> defaultImeInfo_{ nullptr };
101 };
102 } // namespace MiscServices
103 } // namespace OHOS
104 #endif // SERVICES_INCLUDE_IME_INFO_ENQUIRER_H
105