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 "block_queue.h" 26 #include "bundle_mgr_proxy.h" 27 #include "element_name.h" 28 #include "enable_ime_data_parser.h" 29 #include "event_handler.h" 30 #include "identity_checker_impl.h" 31 #include "ime_info_inquirer.h" 32 #include "input_method_status.h" 33 #include "input_method_system_ability_stub.h" 34 #include "inputmethod_dump.h" 35 #include "inputmethod_trace.h" 36 #include "message.h" 37 #include "security_mode_parser.h" 38 #include "settings_data_utils.h" 39 #include "system_ability.h" 40 #include "input_method_types.h" 41 42 namespace OHOS { 43 namespace MiscServices { 44 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 45 46 class InputMethodSystemAbility : public SystemAbility, public InputMethodSystemAbilityStub { 47 DECLARE_SYSTEM_ABILITY(InputMethodSystemAbility); 48 49 public: 50 DISALLOW_COPY_AND_MOVE(InputMethodSystemAbility); 51 InputMethodSystemAbility(int32_t systemAbilityId, bool runOnCreate); 52 InputMethodSystemAbility(); 53 ~InputMethodSystemAbility(); 54 55 int32_t StartInput(InputClientInfo &inputClientInfo, sptr<IRemoteObject> &agent) override; 56 int32_t ShowCurrentInput() override; 57 int32_t HideCurrentInput() override; 58 int32_t ShowInput(sptr<IInputClient> client) override; 59 int32_t HideInput(sptr<IInputClient> client) override; 60 int32_t StopInputSession() override; 61 int32_t ReleaseInput(sptr<IInputClient> client) override; 62 int32_t RequestShowInput() override; 63 int32_t RequestHideInput() override; 64 int32_t GetDefaultInputMethod(std::shared_ptr<Property> &prop, bool isBrief) override; 65 int32_t GetInputMethodConfig(OHOS::AppExecFwk::ElementName &inputMethodConfig) override; 66 std::shared_ptr<Property> GetCurrentInputMethod() override; 67 std::shared_ptr<SubProperty> GetCurrentInputMethodSubtype() override; 68 int32_t ListInputMethod(InputMethodStatus status, std::vector<Property> &props) override; 69 int32_t ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps) override; 70 int32_t ListInputMethodSubtype(const std::string &bundleName, std::vector<SubProperty> &subProps) override; 71 int32_t SwitchInputMethod( 72 const std::string &bundleName, const std::string &subName, SwitchTrigger trigger) override; 73 int32_t DisplayOptionalInputMethod() override; 74 int32_t SetCoreAndAgent(const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent) override; 75 int32_t InitConnect() override; 76 int32_t UnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core) override; 77 int32_t PanelStatusChange(const InputWindowStatus &status, const ImeWindowInfo &info) override; 78 int32_t UpdateListenEventFlag(InputClientInfo &clientInfo, uint32_t eventFlag) override; 79 bool IsCurrentIme() override; 80 bool IsInputTypeSupported(InputType type) override; 81 bool IsCurrentImeByPid(int32_t pid) override; 82 int32_t StartInputType(InputType type) override; 83 int32_t ExitCurrentInputType() override; 84 int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown) override; 85 int32_t GetSecurityMode(int32_t &security) override; 86 int32_t ConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent) override; 87 // Deprecated because of no permission check, kept for compatibility 88 int32_t HideCurrentInputDeprecated() override; 89 int32_t ShowCurrentInputDeprecated() override; 90 int Dump(int fd, const std::vector<std::u16string> &args) override; 91 void DumpAllMethod(int fd); 92 int32_t IsDefaultIme() override; 93 94 protected: 95 void OnStart() override; 96 void OnStop() override; 97 98 private: 99 int32_t Init(); 100 void Initialize(); 101 102 std::thread workThreadHandler; /*!< thread handler of the WorkThread */ 103 int32_t GetUserId(int32_t uid); 104 int32_t GetCallingUserId(); 105 std::shared_ptr<IdentityChecker> identityChecker_ = nullptr; 106 int32_t PrepareInput(int32_t userId, InputClientInfo &clientInfo); 107 void WorkThread(); 108 int32_t OnUserStarted(const Message *msg); 109 int32_t OnUserRemoved(const Message *msg); 110 int32_t OnUserStop(const Message *msg); 111 int32_t OnHideKeyboardSelf(const Message *msg); 112 bool IsNeedSwitch(int32_t userId, const std::string &bundleName, const std::string &subName); 113 int32_t CheckSwitchPermission(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger); 114 bool IsStartInputTypePermitted(int32_t userId); 115 int32_t OnSwitchInputMethod(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger); 116 int32_t OnStartInputType(int32_t userId, const SwitchInfo &switchInfo, bool isCheckPermission); 117 int32_t HandlePackageEvent(const Message *msg); 118 int32_t OnPackageRemoved(int32_t userId, const std::string &packageName); 119 int32_t OnDisplayOptionalInputMethod(); 120 static sptr<AAFwk::IAbilityManager> GetAbilityManagerService(); 121 void SubscribeCommonEvent(); 122 int32_t Switch(int32_t userId, const std::string &bundleName, const std::shared_ptr<ImeInfo> &info); 123 int32_t SwitchExtension(int32_t userId, const std::shared_ptr<ImeInfo> &info); 124 int32_t SwitchSubType(int32_t userId, const std::shared_ptr<ImeInfo> &info); 125 int32_t SwitchInputType(int32_t userId, const SwitchInfo &switchInfo); 126 void GetValidSubtype(const std::string &subName, const std::shared_ptr<ImeInfo> &info); 127 ServiceRunningState state_; 128 void InitServiceHandler(); 129 void UpdateUserInfo(int32_t userId); 130 void HandleWmsConnected(int32_t userId, int32_t screenId); 131 void HandleWmsDisconnected(int32_t userId, int32_t screenId); 132 void HandleScbStarted(int32_t userId, int32_t screenId); 133 void HandleUserSwitched(int32_t userId); 134 void HandleWmsStarted(); 135 void HandleMemStarted(); 136 void HandleDataShareReady(); 137 void HandleOsAccountStarted(); 138 void HandleFocusChanged(bool isFocused, int32_t pid, int32_t uid); 139 void StopImeInBackground(); 140 int32_t InitAccountMonitor(); 141 static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 142 int32_t userId_; 143 static constexpr const char *SELECT_DIALOG_ACTION = "action.system.inputmethodchoose"; 144 static constexpr const char *SELECT_DIALOG_HAP = "com.ohos.inputmethodchoosedialog"; 145 static constexpr const char *SELECT_DIALOG_ABILITY = "InputMethod"; 146 bool stop_ = false; 147 void InitMonitors(); 148 int32_t InitKeyEventMonitor(); 149 bool InitWmsMonitor(); 150 void InitSystemLanguageMonitor(); 151 bool InitMemMgrMonitor(); 152 void InitWmsConnectionMonitor(); 153 void InitFocusChangedMonitor(); 154 int32_t SwitchByCombinationKey(uint32_t state); 155 int32_t SwitchMode(); 156 int32_t SwitchLanguage(); 157 int32_t SwitchType(); 158 int32_t GenerateClientInfo(int32_t userId, InputClientInfo &clientInfo); 159 void RegisterEnableImeObserver(); 160 void RegisterSecurityModeObserver(); 161 int32_t CheckInputTypeOption(int32_t userId, InputClientInfo &inputClientInfo); 162 int32_t IsDefaultImeFromTokenId(int32_t userId, uint32_t tokenId); 163 void DealSwitchRequest(); 164 void DealSecurityChange(); 165 void OnSecurityModeChange(); 166 bool IsCurrentIme(int32_t userId); 167 int32_t StartInputType(int32_t userId, InputType type); 168 // if switch input type need to switch ime, then no need to hide panel first. 169 void NeedHideWhenSwitchInputType(int32_t userId, bool &needHide); 170 171 std::mutex checkMutex_; 172 void DatashareCallback(const std::string &key); 173 std::atomic<bool> enableImeOn_ = false; 174 std::atomic<bool> enableSecurityMode_ = false; 175 176 std::atomic<bool> isScbEnable_ = false; 177 std::mutex switchImeMutex_; 178 std::atomic<bool> switchTaskExecuting_ = false; 179 std::atomic<uint32_t> targetSwitchCount_ = 0; 180 181 std::mutex modeChangeMutex_; 182 bool isChangeHandling_ = false; 183 bool hasPendingChanges_ = false; 184 }; 185 } // namespace MiscServices 186 } // namespace OHOS 187 #endif // SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H 188