1 /* 2 * Copyright (C) 2021 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_INPUT_METHOD_SYSTEM_ABILITY_H 17 #define SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H 18 19 #include <atomic> 20 #include <map> 21 #include <thread> 22 23 #include "ability_manager_interface.h" 24 #include "application_info.h" 25 #include "bundle_mgr_proxy.h" 26 #include "event_handler.h" 27 #include "input_method_status.h" 28 #include "input_method_system_ability_stub.h" 29 #include "inputmethod_dump.h" 30 #include "inputmethod_trace.h" 31 #include "peruser_session.h" 32 #include "system_ability.h" 33 34 namespace OHOS { 35 namespace MiscServices { 36 using AbilityType = AppExecFwk::ExtensionAbilityType; 37 using namespace AppExecFwk; 38 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 39 40 class InputMethodSystemAbility : public SystemAbility, public InputMethodSystemAbilityStub { 41 DECLARE_SYSTEM_ABILITY(InputMethodSystemAbility); 42 43 public: 44 DISALLOW_COPY_AND_MOVE(InputMethodSystemAbility); 45 InputMethodSystemAbility(int32_t systemAbilityId, bool runOnCreate); 46 InputMethodSystemAbility(); 47 ~InputMethodSystemAbility(); 48 49 int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 50 51 int32_t PrepareInput(int32_t displayId, sptr<IInputClient> client, sptr<IInputDataChannel> channel, 52 InputAttribute &attribute) override; 53 int32_t StartInput(sptr<IInputClient> client, bool isShowKeyboard) override; 54 int32_t ShowCurrentInput() override; 55 int32_t HideCurrentInput() override; 56 int32_t StopInput(sptr<IInputClient> client) override; 57 int32_t StopInputSession() override; 58 int32_t ReleaseInput(sptr<IInputClient> client) override; 59 std::shared_ptr<Property> GetCurrentInputMethod() override; 60 std::shared_ptr<SubProperty> GetCurrentInputMethodSubtype() override; 61 int32_t ListInputMethod(InputMethodStatus status, std::vector<Property> &props) override; 62 int32_t ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps) override; 63 int32_t ListInputMethodSubtype(const std::string &name, std::vector<SubProperty> &subProps) override; 64 int32_t SwitchInputMethod(const std::string &name, const std::string &subName) override; 65 int32_t DisplayOptionalInputMethod() override; 66 int32_t SetCoreAndAgent(sptr<IInputMethodCore> core, sptr<IInputMethodAgent> agent) override; 67 68 // Deprecated because of no permission check, kept for compatibility 69 int32_t HideCurrentInputDeprecated() override; 70 int32_t ShowCurrentInputDeprecated() override; 71 int32_t DisplayOptionalInputMethodDeprecated() override; 72 73 int32_t ListInputMethodByUserId(int32_t userId, InputMethodStatus status, std::vector<Property> &props); 74 int Dump(int fd, const std::vector<std::u16string> &args) override; 75 void DumpAllMethod(int fd); 76 77 protected: 78 void OnStart() override; 79 void OnStop() override; 80 81 private: 82 int32_t Init(); 83 void Initialize(); 84 85 std::thread workThreadHandler; /*!< thread handler of the WorkThread */ 86 std::map<int32_t, std::shared_ptr<PerUserSession>> userSessions; 87 std::map<int32_t, MessageHandler *> msgHandlers; 88 89 void WorkThread(); 90 std::shared_ptr<PerUserSession> GetUserSession(int32_t userId); 91 bool StartInputService(std::string imeId); 92 void StopInputService(std::string imeId); 93 int32_t OnUserStarted(const Message *msg); 94 int32_t OnUserRemoved(const Message *msg); 95 int32_t OnPackageRemoved(const Message *msg); 96 int32_t OnDisplayOptionalInputMethod(int32_t userId); 97 static sptr<AAFwk::IAbilityManager> GetAbilityManagerService(); 98 OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr(); 99 std::vector<InputMethodInfo> ListInputMethodInfo(int32_t userId); 100 int32_t ListAllInputMethod(int32_t userId, std::vector<Property> &props); 101 int32_t ListEnabledInputMethod(std::vector<Property> &props); 102 int32_t ListDisabledInputMethod(int32_t userId, std::vector<Property> &props); 103 int32_t ListProperty(int32_t userId, std::vector<Property> &props); 104 int32_t ListSubtypeByBundleName(int32_t userId, const std::string &name, std::vector<SubProperty> &subProps); 105 void StartUserIdListener(); 106 int32_t SwitchInputMethodType(const std::string &name); 107 int32_t SwitchInputMethodSubtype(const std::string &name, const std::string &subName); 108 int32_t OnSwitchInputMethod(const std::string &bundleName, const std::string &name); 109 Property FindProperty(const std::string &name); 110 SubProperty FindSubProperty(const std::string &bundleName, const std::string &name); 111 std::string GetInputMethodParam(const std::vector<InputMethodInfo> &properties); 112 ServiceRunningState state_; 113 void InitServiceHandler(); 114 std::atomic_flag dialogLock_ = ATOMIC_FLAG_INIT; 115 static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 116 int32_t userId_; 117 static constexpr const char *SELECT_DIALOG_ACTION = "action.system.inputmethodchoose"; 118 static constexpr const char *SELECT_DIALOG_HAP = "cn.openharmony.inputmethodchoosedialog"; 119 static constexpr const char *SELECT_DIALOG_ABILITY = "InputMethod"; 120 121 int32_t InitKeyEventMonitor(); 122 using CompareHandler = std::function<bool(const SubProperty &)>; 123 SubProperty FindSubPropertyByCompare(const std::string &bundleName, CompareHandler compare); 124 SubProperty GetExtends(const std::vector<Metadata> &metaData); 125 int32_t SwitchByCombinationKey(uint32_t state); 126 127 int32_t QueryImeInfos(int32_t userId, std::vector<AppExecFwk::ExtensionAbilityInfo> &infos); 128 bool IsImeInstalled(int32_t userId, std::string &imeId); 129 std::string GetNewUserIme(int32_t userId); 130 }; 131 } // namespace MiscServices 132 } // namespace OHOS 133 #endif // SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H 134