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 "application_info.h" 24 #include "block_queue.h" 25 #include "bundle_mgr_proxy.h" 26 #include "enable_ime_data_parser.h" 27 #include "settings_data_utils.h" 28 #include "security_mode_parser.h" 29 #include "element_name.h" 30 #include "event_handler.h" 31 #include "identity_checker_impl.h" 32 #include "ime_info_inquirer.h" 33 #include "input_method_status.h" 34 #include "input_method_system_ability_stub.h" 35 #include "inputmethod_dump.h" 36 #include "inputmethod_trace.h" 37 #include "peruser_session.h" 38 #include "system_ability.h" 39 #include "unRegistered_type.h" 40 41 namespace OHOS { 42 namespace MiscServices { 43 using AbilityType = AppExecFwk::ExtensionAbilityType; 44 using namespace AppExecFwk; 45 using namespace Security::AccessToken; 46 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 47 class InputMethodSystemAbility : public SystemAbility, public InputMethodSystemAbilityStub { 48 DECLARE_SYSTEM_ABILITY(InputMethodSystemAbility); 49 50 public: 51 DISALLOW_COPY_AND_MOVE(InputMethodSystemAbility); 52 InputMethodSystemAbility(int32_t systemAbilityId, bool runOnCreate); 53 InputMethodSystemAbility(); 54 ~InputMethodSystemAbility(); 55 56 int32_t StartInput(InputClientInfo &inputClientInfo, sptr<IRemoteObject> &agent) override; 57 int32_t ShowCurrentInput() override; 58 int32_t HideCurrentInput() override; 59 int32_t ShowInput(sptr<IInputClient> client) override; 60 int32_t HideInput(sptr<IInputClient> client) override; 61 int32_t StopInputSession() override; 62 int32_t ReleaseInput(sptr<IInputClient> client) override; 63 int32_t RequestShowInput() override; 64 int32_t RequestHideInput() override; 65 int32_t GetDefaultInputMethod(std::shared_ptr<Property> &prop) override; 66 int32_t GetInputMethodConfig(OHOS::AppExecFwk::ElementName &inputMethodConfig) override; 67 std::shared_ptr<Property> GetCurrentInputMethod() override; 68 std::shared_ptr<SubProperty> GetCurrentInputMethodSubtype() override; 69 int32_t ListInputMethod(InputMethodStatus status, std::vector<Property> &props) override; 70 int32_t ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps) override; 71 int32_t ListInputMethodSubtype(const std::string &bundleName, std::vector<SubProperty> &subProps) override; 72 int32_t SwitchInputMethod( 73 const std::string &bundleName, const std::string &subName, SwitchTrigger trigger) override; 74 int32_t DisplayOptionalInputMethod() override; 75 int32_t SetCoreAndAgent(const sptr<IInputMethodCore> &core, const sptr<IInputMethodAgent> &agent) override; 76 int32_t UnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core) override; 77 int32_t PanelStatusChange(const InputWindowStatus &status, const InputWindowInfo &windowInfo) override; 78 int32_t UpdateListenEventFlag(InputClientInfo &clientInfo, EventType eventType) override; 79 bool IsCurrentIme() override; 80 bool IsInputTypeSupported(InputType type) override; 81 int32_t StartInputType(InputType type) override; 82 int32_t ExitCurrentInputType() override; 83 int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown) override; 84 int32_t GetSecurityMode(int32_t &security) override; 85 86 // Deprecated because of no permission check, kept for compatibility 87 int32_t HideCurrentInputDeprecated() override; 88 int32_t ShowCurrentInputDeprecated() override; 89 int Dump(int fd, const std::vector<std::u16string> &args) override; 90 void DumpAllMethod(int fd); 91 92 protected: 93 void OnStart() override; 94 void OnStop() override; 95 96 private: 97 int32_t Init(); 98 void Initialize(); 99 100 std::thread workThreadHandler; /*!< thread handler of the WorkThread */ 101 std::shared_ptr<PerUserSession> userSession_ = nullptr; 102 std::shared_ptr<IdentityChecker> identityChecker_ = nullptr; 103 int32_t PrepareInput(InputClientInfo &clientInfo); 104 void WorkThread(); 105 bool StartInputService(const std::shared_ptr<ImeNativeCfg> &imeId); 106 void StopInputService(); 107 int32_t OnUserStarted(const Message *msg); 108 int32_t OnUserRemoved(const Message *msg); 109 int32_t OnPackageRemoved(const Message *msg); 110 int32_t OnDisplayOptionalInputMethod(); 111 void StartUserIdListener(); 112 bool IsNeedSwitch(const std::string &bundleName, const std::string &subName); 113 int32_t CheckSwitchPermission(const SwitchInfo &switchInfo, SwitchTrigger trigger); 114 bool IsStartInputTypePermitted(); 115 int32_t OnSwitchInputMethod(const SwitchInfo &switchInfo, SwitchTrigger trigger); 116 int32_t OnStartInputType(const SwitchInfo &switchInfo, bool isCheckPermission); 117 int32_t Switch(const std::string &bundleName, const std::shared_ptr<ImeInfo> &info); 118 int32_t SwitchExtension(const std::shared_ptr<ImeInfo> &info); 119 int32_t SwitchSubType(const std::shared_ptr<ImeInfo> &info); 120 int32_t SwitchInputType(const SwitchInfo &switchInfo); 121 ServiceRunningState state_; 122 void InitServiceHandler(); 123 void SetCurrentUserId(); 124 int32_t InitAccountMonitor(); 125 static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 126 int32_t userId_; 127 static constexpr const char *SELECT_DIALOG_ACTION = "action.system.inputmethodchoose"; 128 static constexpr const char *SELECT_DIALOG_HAP = "com.ohos.inputmethodchoosedialog"; 129 static constexpr const char *SELECT_DIALOG_ABILITY = "InputMethod"; 130 static constexpr int32_t MAX_WAIT_TIME = 5000; 131 BlockQueue<SwitchInfo> switchQueue_{ MAX_WAIT_TIME }; 132 bool stop_ = false; 133 void InitMonitors(); 134 int32_t InitKeyEventMonitor(); 135 bool InitWmsMonitor(); 136 void InitSystemLanguageMonitor(); 137 void InitWmsConnectionMonitor(); 138 int32_t SwitchByCombinationKey(uint32_t state); 139 int32_t SwitchMode(); 140 int32_t SwitchLanguage(); 141 int32_t SwitchType(); 142 int32_t GenerateClientInfo(InputClientInfo &clientInfo); 143 void RegisterEnableImeObserver(); 144 void RegisterSecurityModeObserver(); 145 void CheckSecurityMode(InputClientInfo &inputClientInfo); 146 147 std::mutex checkMutex_; 148 void DatashareCallback(const std::string &key); 149 bool enableImeOn_ = false; 150 bool enableSecurityMode_ = false; 151 }; 152 } // namespace MiscServices 153 } // namespace OHOS 154 #endif // SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H 155