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 SetCoreAndAgentDeprecated(sptr<IInputMethodCore> core, sptr<IInputMethodAgent> agent) override; 70 int32_t HideCurrentInputDeprecated() override; 71 int32_t ShowCurrentInputDeprecated() override; 72 int32_t DisplayOptionalInputMethodDeprecated() override; 73 74 int32_t ListInputMethodByUserId(int32_t userId, InputMethodStatus status, std::vector<Property> &props); 75 int Dump(int fd, const std::vector<std::u16string> &args) override; 76 void DumpAllMethod(int fd); 77 78 protected: 79 void OnStart() override; 80 void OnStop() override; 81 82 private: 83 int32_t Init(); 84 void Initialize(); 85 86 std::thread workThreadHandler; /*!< thread handler of the WorkThread */ 87 std::map<int32_t, std::shared_ptr<PerUserSession>> userSessions; 88 std::map<int32_t, MessageHandler *> msgHandlers; 89 90 void WorkThread(); 91 std::shared_ptr<PerUserSession> GetUserSession(int32_t userId); 92 bool StartInputService(std::string imeId); 93 void StopInputService(std::string imeId); 94 int32_t OnUserStarted(const Message *msg); 95 int32_t OnUserRemoved(const Message *msg); 96 int32_t OnHandleMessage(Message *msg); 97 int32_t OnPackageRemoved(const Message *msg); 98 int32_t OnDisplayOptionalInputMethod(int32_t userId); 99 static sptr<AAFwk::IAbilityManager> GetAbilityManagerService(); 100 OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr(); 101 std::vector<InputMethodInfo> ListInputMethodInfo(int32_t userId); 102 int32_t ListAllInputMethod(int32_t userId, std::vector<Property> &props); 103 int32_t ListEnabledInputMethod(std::vector<Property> &props); 104 int32_t ListDisabledInputMethod(int32_t userId, std::vector<Property> &props); 105 int32_t ListProperty(int32_t userId, std::vector<Property> &props); 106 int32_t ListSubtypeByBundleName(int32_t userId, const std::string &name, std::vector<SubProperty> &subProps); 107 void StartUserIdListener(); 108 int32_t SwitchInputMethodType(const std::string &name); 109 int32_t SwitchInputMethodSubtype(const std::string &name, const std::string &subName); 110 int32_t OnSwitchInputMethod(const std::string &bundleName, const std::string &name); 111 Property FindProperty(const std::string &name); 112 SubProperty FindSubProperty(const std::string &bundleName, const std::string &name); 113 std::string GetInputMethodParam(const std::vector<InputMethodInfo> &properties); 114 ServiceRunningState state_; 115 void InitServiceHandler(); 116 std::atomic_flag dialogLock_ = ATOMIC_FLAG_INIT; 117 static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 118 int32_t userId_; 119 static constexpr const char *SELECT_DIALOG_ACTION = "action.system.inputmethodchoose"; 120 static constexpr const char *SELECT_DIALOG_HAP = "cn.openharmony.inputmethodchoosedialog"; 121 static constexpr const char *SELECT_DIALOG_ABILITY = "InputMethod"; 122 123 int32_t InitKeyEventMonitor(); 124 using CompareHandler = std::function<bool(const SubProperty &)>; 125 SubProperty FindSubPropertyByCompare(const std::string &bundleName, CompareHandler compare); 126 SubProperty GetExtends(const std::vector<Metadata> &metaData); 127 int32_t SwitchByCombinationKey(uint32_t state); 128 129 int32_t QueryImeInfos(int32_t userId, std::vector<AppExecFwk::ExtensionAbilityInfo> &infos); 130 bool IsImeInstalled(int32_t userId, std::string &imeId); 131 std::string GetNewUserIme(int32_t userId); 132 }; 133 } // namespace MiscServices 134 } // namespace OHOS 135 #endif // SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H 136